Commit 6aa0206c03505aef8cdcf3231b525bdddae9ce1f

initial fleshout of the "Gitto" sha strainer class

Commit diff

lib/gitorious/gitto/gitto.rb

 
1module Gitorious
2 module Gitto
3
4 class BadShaError < StandardError; end # :nodoc:
5
6 # The point of this class is primary to prevent dangerous/malicious arguments
7 # to be passed down to the underlying Git library, since it shells out to
8 # the git binary.
9 class Gitto
10 def initialize(repository_path)
11 @repository_path = repository_path
12 @git = Git.bare(@repository_path)
13 end
14 attr_reader :repository_path, :git
15
16 def log(num, skip=nil)
17 @git.log(num, skip)
18 end
19
20 def tree(sha)
21 check_sha(sha)
22 @git.gtree(sha)
23 end
24
25 def commit(sha)
26 check_sha(sha)
27 @git.gcommit(sha)
28 end
29
30 def diff(old_sha, new_sha)
31 check_sha(old_sha)
32 check_sha(new_sha)
33 @git.diff(old_sha, new_sha)
34 end
35
36 def blob(sha)
37 check_sha(sha)
38 @git.gblob(sha)
39 end
40
41 def branches
42 @git.branches
43 end
44
45 def tags
46 @git.tags
47 end
48
49 def remotes
50 @git.remotes
51 end
52
53 def check_sha(objectish)
54 if /^[a-z0-9~\{\}\^\.]*$/i !~ objectish.to_s
55 raise BadShaError
56 end
57 end
58
59 end
60
61 end
62end
toggle raw diff

spec/lib/gitorious/gitto/gitto_spec.rb

 
1require File.dirname(__FILE__) + '/../../../spec_helper'
2
3describe Gitorious::Gitto::Gitto do
4 Gitto = Gitorious::Gitto::Gitto
5
6 before(:each) do
7 @path = "/home/repositories/foo"
8 @git_mock = mock("Git mock")
9 Git.should_receive(:bare).with(@path).and_return(@git_mock)
10 @gitto = Gitto.new(@path)
11 end
12
13 def some_sha(char = "a")
14 char * 40
15 end
16
17 it "should return Git#log(10)" do
18 @git_mock.should_receive(:log).with(10, nil).and_return([])
19 @gitto.log(10)
20 end
21
22 it "returns Git#log(10,20)" do
23 @git_mock.should_receive(:log).with(10, 20).and_return([])
24 @gitto.log(10, 20)
25 end
26
27 it "returns Git#gtre" do
28 @git_mock.should_receive(:gtree).with(some_sha).and_return(mock("tree"))
29 @gitto.should_receive(:check_sha).with(some_sha)
30 @gitto.tree(some_sha)
31 end
32
33 it "return Git#gcommit" do
34 @git_mock.should_receive(:gcommit).with(some_sha).and_return(mock("commit"))
35 @gitto.should_receive(:check_sha).with(some_sha)
36 @gitto.commit(some_sha)
37 end
38
39 it "return Git#diff" do
40 @git_mock.should_receive(:diff).with(some_sha("a"), some_sha("b")).and_return(mock("diff"))
41 @gitto.should_receive(:check_sha).with(some_sha("a"))
42 @gitto.should_receive(:check_sha).with(some_sha("b"))
43 @gitto.diff(some_sha("a"), some_sha("b"))
44 end
45
46 it "return Git#gblob" do
47 @git_mock.should_receive(:gblob).with(some_sha).and_return(mock("blob"))
48 @gitto.should_receive(:check_sha).with(some_sha)
49 @gitto.blob(some_sha)
50 end
51
52 it "return Git#gblob" do
53 @git_mock.should_receive(:gblob).with(some_sha).and_return(mock("blob"))
54 @gitto.should_receive(:check_sha).with(some_sha)
55 @gitto.blob(some_sha)
56 end
57
58 it "returns Git#tags" do
59 @git_mock.should_receive(:tags).and_return([])
60 @gitto.tags
61 end
62
63 it "returns Git#branches" do
64 @git_mock.should_receive(:branches).and_return([])
65 @gitto.branches
66 end
67
68 it "returns Git#remotes" do
69 @git_mock.should_receive(:remotes).and_return([])
70 @gitto.remotes
71 end
72
73 describe "objectish validation" do
74 it "accepts good objectish" do
75 proc{
76 @gitto.check_sha(some_sha)
77 }.should_not raise_error(Gitorious::Gitto::BadShaError)
78 proc{
79 @gitto.check_sha( ("a"*20) + ("2"*20) )
80 }.should_not raise_error(Gitorious::Gitto::BadShaError)
81 proc{
82 @gitto.check_sha("HEAD")
83 }.should_not raise_error(Gitorious::Gitto::BadShaError)
84 proc{
85 @gitto.check_sha("HEAD~1")
86 }.should_not raise_error(Gitorious::Gitto::BadShaError)
87 proc{
88 @gitto.check_sha("HEAD^{1}") # valid chars at least
89 }.should_not raise_error(Gitorious::Gitto::BadShaError)
90 proc{
91 @gitto.check_sha("v0.1^{tree}")
92 }.should_not raise_error(Gitorious::Gitto::BadShaError)
93 end
94
95 it "raises on bad objectish" do
96 proc{
97 @gitto.check_sha("asd;rm -rf")
98 }.should raise_error(Gitorious::Gitto::BadShaError)
99 proc{
100 @gitto.check_sha("asd;cat /etc/foo")
101 }.should raise_error(Gitorious::Gitto::BadShaError)
102
103 proc{
104 @gitto.check_sha("%3B++%2Ffoo")
105 }.should raise_error(Gitorious::Gitto::BadShaError)
106 end
107 end
108
109end
toggle raw diff