gitrb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/benchmark.rb ADDED
@@ -0,0 +1,39 @@
1
+ Gem.activate 'diff-lcs'
2
+
3
+ require 'gitrb'
4
+ require 'grit'
5
+ require 'benchmark'
6
+ require 'fileutils'
7
+
8
+ REPO = '/tmp/gitrb'
9
+
10
+ FileUtils.rm_rf REPO
11
+ FileUtils.mkpath REPO
12
+ Dir.chdir REPO
13
+
14
+ repo = Gitrb::Repository.new(:path => REPO, :create => true)
15
+
16
+ grit = nil
17
+ gitrb = nil
18
+
19
+ Benchmark.bm 20 do |x|
20
+ x.report 'store 1000 objects' do
21
+ repo.transaction { 'aaa'.upto('jjj') { |key| repo.root[key] = Gitrb::Blob.new(:data => rand.to_s) } }
22
+ end
23
+ x.report 'commit one object' do
24
+ repo.transaction { repo.root['aa'] = Gitrb::Blob.new(:data => rand.to_s) }
25
+ end
26
+ x.report 'init gitrb' do
27
+ gitrb = Gitrb::Repository.new(:path => '.')
28
+ end
29
+ x.report 'init grit' do
30
+ grit = Grit::Repo.new('.')
31
+ end
32
+ x.report 'load 1000 with gitrb' do
33
+ gitrb.root.values.each { |v| v.data }
34
+ end
35
+ x.report 'load 1000 with grit' do
36
+ grit.tree.contents.each { |e| e.data }
37
+ end
38
+ end
39
+
@@ -0,0 +1,73 @@
1
+ require "#{File.dirname(__FILE__)}/../lib/gitrb"
2
+ require 'pp'
3
+
4
+ describe Gitrb::Commit do
5
+
6
+ REPO = '/tmp/gitrb_test'
7
+
8
+ attr_reader :repo
9
+
10
+ before(:each) do
11
+ FileUtils.rm_rf REPO
12
+ Dir.mkdir REPO
13
+
14
+ @repo = Gitrb::Repository.new(:path => REPO, :create => true)
15
+ end
16
+
17
+ it "should dump in right format" do
18
+ user = Gitrb::User.new("hanni", "hanni@email.de", Time.now)
19
+
20
+ commit = Gitrb::Commit.new
21
+ commit.tree = @repo.root
22
+ commit.author = user
23
+ commit.committer = user
24
+ commit.message = "This is a message"
25
+
26
+ content = commit.dump
27
+
28
+ content.should == "tree #{@repo.root.id}
29
+ author #{user.dump}
30
+ committer #{user.dump}
31
+
32
+ This is a message"
33
+ end
34
+
35
+ it "should be readable by git binary" do
36
+ time = Time.local(2009, 4, 20)
37
+ author = Gitrb::User.new("hans", "hans@email.de", time)
38
+
39
+ repo.root['a'] = Gitrb::Blob.new(:data => "Yay")
40
+ commit = repo.commit("Commit Message", author, author)
41
+
42
+ IO.popen("git log") do |io|
43
+ io.gets.should == "commit #{commit.id}\n"
44
+ io.gets.should == "Author: hans <hans@email.de>\n"
45
+ io.gets.should == "Date: Mon Apr 20 00:00:00 2009 #{Time.now.strftime('%z')}\n"
46
+ io.gets.should == "\n"
47
+ io.gets.should == " Commit Message\n"
48
+ end
49
+ end
50
+
51
+ it "should diff 2 commits" do
52
+ repo.root['x'] = Gitrb::Blob.new(:data => 'a')
53
+ repo.root['y'] = Gitrb::Blob.new(:data => "
54
+ First Line.
55
+ Second Line.
56
+ Last Line.
57
+ ")
58
+ a = repo.commit
59
+
60
+ repo.root.delete('x')
61
+ repo.root['y'] = Gitrb::Blob.new(:data => "
62
+ First Line.
63
+ Last Line.
64
+ Another Line.
65
+ ")
66
+ repo.root['z'] = Gitrb::Blob.new(:data => 'c')
67
+
68
+ b = repo.commit
69
+
70
+ diff = repo.diff(a, b)
71
+ end
72
+
73
+ end
@@ -0,0 +1,235 @@
1
+ require "#{File.dirname(__FILE__)}/../lib/gitrb"
2
+ require "#{File.dirname(__FILE__)}/helper"
3
+ require 'pp'
4
+
5
+ describe Gitrb do
6
+
7
+ include Helper
8
+
9
+ REPO = '/tmp/gitrb_test'
10
+
11
+ attr_reader :repo
12
+
13
+ before(:each) do
14
+ FileUtils.rm_rf REPO
15
+ Dir.mkdir REPO
16
+ @repo = Gitrb::Repository.new(:path => REPO, :create => true)
17
+ end
18
+
19
+ it 'should fail to initialize without a valid git repository' do
20
+ lambda {
21
+ Gitrb::Repository.new(:path => '/')
22
+ }.should raise_error(ArgumentError)
23
+ end
24
+
25
+ it 'should find modified entries' do
26
+ repo.root['a'] = Gitrb::Blob.new(:data => 'Hello')
27
+
28
+ repo.root.should be_modified
29
+
30
+ repo.commit
31
+
32
+ repo.root.should_not be_modified
33
+
34
+ repo.root['a'] = Gitrb::Blob.new(:data => 'Bello')
35
+
36
+ repo.root.should be_modified
37
+ end
38
+
39
+ it 'should load a repo' do
40
+ file 'a', 'Hello'
41
+ file 'b', 'World'
42
+
43
+ repo.refresh
44
+
45
+ repo.root['a'].data.should == 'Hello'
46
+ repo.root['b'].data.should == 'World'
47
+ end
48
+
49
+ it 'should load folders' do
50
+ file 'x/a', 'Hello'
51
+ file 'y/b', 'World'
52
+
53
+ repo.refresh
54
+
55
+ repo.root['x'].object.should be_kind_of(Gitrb::Tree)
56
+ repo.root['y'].object.should be_kind_of(Gitrb::Tree)
57
+
58
+ repo.root['x']['a'].data.should == 'Hello'
59
+ repo.root['y']['b'].data.should == 'World'
60
+ end
61
+
62
+ it 'should detect modification' do
63
+ repo.transaction do
64
+ repo.root['x/a'] = Gitrb::Blob.new(:data => 'a')
65
+ end
66
+
67
+ repo.refresh
68
+
69
+ repo.root['x/a'].data.should == 'a'
70
+
71
+ repo.transaction do
72
+ repo.root['x/a'] = Gitrb::Blob.new(:data => 'b')
73
+ repo.root['x'].should be_modified
74
+ repo.root.should be_modified
75
+ end
76
+
77
+ repo.refresh
78
+
79
+ repo.root['x/a'].data.should == 'b'
80
+ end
81
+
82
+ it 'should resolve paths' do
83
+ file 'x/a', 'Hello'
84
+ file 'y/b', 'World'
85
+
86
+ repo.refresh
87
+
88
+ repo.root['x/a'].data.should == 'Hello'
89
+ repo.root['y/b'].data.should == 'World'
90
+
91
+ repo.root['y/b'] = Gitrb::Blob.new(:data => 'Now this')
92
+
93
+ repo.root['y']['b'].data.should == 'Now this'
94
+ end
95
+
96
+ it 'should create new trees' do
97
+ repo.root['new/tree'] = Gitrb::Blob.new(:data => 'This tree')
98
+ repo.root['new/tree'].data.should == 'This tree'
99
+ end
100
+
101
+ it 'should delete entries' do
102
+ repo.root['a'] = Gitrb::Blob.new(:data => 'Hello')
103
+ repo.root.delete('a')
104
+
105
+ repo.root['a'].should be_nil
106
+ end
107
+
108
+ it 'should have a head commit' do
109
+ file 'a', 'Hello'
110
+
111
+ repo.refresh
112
+ repo.head.should_not be_nil
113
+ end
114
+
115
+ it 'should detect changes' do
116
+ file 'a', 'Hello'
117
+
118
+ repo.should be_changed
119
+ end
120
+
121
+ it 'should rollback a transaction' do
122
+ file 'a/b', 'Hello'
123
+ file 'c/d', 'World'
124
+
125
+ begin
126
+ repo.transaction do
127
+ repo.root['a/b'] = 'Changed'
128
+ repo.root['x/a'] = 'Added'
129
+ raise
130
+ end
131
+ rescue
132
+ end
133
+
134
+ repo.root['a/b'].data.should == 'Hello'
135
+ repo.root['c/d'].data.should == 'World'
136
+ repo.root['x/a'].should be_nil
137
+ end
138
+
139
+ it 'should commit a transaction' do
140
+ file 'a/b', 'Hello'
141
+ file 'c/d', 'World'
142
+
143
+ repo.transaction do
144
+ repo.root['a/b'] = Gitrb::Blob.new(:data => 'Changed')
145
+ repo.root['x/a'] = Gitrb::Blob.new(:data => 'Added')
146
+ end
147
+
148
+ a = ls_tree(repo.root['a'].object.id)
149
+ x = ls_tree(repo.root['x'].object.id)
150
+
151
+ a.should == [["100644", "blob", "b653cf27cef08de46da49a11fa5016421e9e3b32", "b"]]
152
+ x.should == [["100644", "blob", "87d2b203800386b1cc8735a7d540a33e246357fa", "a"]]
153
+
154
+ repo.git_show(a[0][2]).should == 'Changed'
155
+ repo.git_show(x[0][2]).should == 'Added'
156
+ end
157
+
158
+ it "should save blobs" do
159
+ repo.root['a'] = Gitrb::Blob.new(:data => 'a')
160
+ repo.root['b'] = Gitrb::Blob.new(:data => 'b')
161
+ repo.root['c'] = Gitrb::Blob.new(:data => 'c')
162
+
163
+ repo.commit
164
+
165
+ a = repo.root['a'].id
166
+ b = repo.root['b'].id
167
+ c = repo.root['c'].id
168
+
169
+ repo.git_show(a).should == 'a'
170
+ repo.git_show(b).should == 'b'
171
+ repo.git_show(c).should == 'c'
172
+ end
173
+
174
+ it 'should allow only one transaction' do
175
+ file 'a/b', 'Hello'
176
+
177
+ ready = false
178
+
179
+ repo.transaction do
180
+ Thread.start do
181
+ repo.transaction do
182
+ repo.root['a/b'] = Gitrb::Blob.new(:data => 'Changed by second thread')
183
+ end
184
+ ready = true
185
+ end
186
+ repo.root['a/b'] = Gitrb::Blob.new(:data => 'Changed')
187
+ end
188
+
189
+ sleep 0.01 until ready
190
+
191
+ repo.refresh
192
+
193
+ repo.root['a/b'].data.should == 'Changed by second thread'
194
+ end
195
+
196
+ it 'should find all objects' do
197
+ repo.refresh
198
+ repo.root['c'] = Gitrb::Blob.new(:data => 'Hello')
199
+ repo.root['d'] = Gitrb::Blob.new(:data => 'World')
200
+ repo.commit
201
+
202
+ repo.root.to_a[0][1].data.should == 'Hello'
203
+ repo.root.to_a[1][1].data.should == 'World'
204
+ end
205
+
206
+ it "should load log" do
207
+ repo.root['a'] = Gitrb::Blob.new(:data => 'a')
208
+ repo.commit 'added a'
209
+
210
+ repo.root['b'] = Gitrb::Blob.new(:data => 'b')
211
+ repo.commit 'added b'
212
+
213
+ repo.log[0].message.should == 'added b'
214
+ repo.log[1].message.should == 'added a'
215
+ end
216
+
217
+ it "should load tags" do
218
+ file 'a', 'init'
219
+
220
+ repo.git_tag('-m', 'message', '0.1')
221
+
222
+ repo.refresh
223
+
224
+ user = repo.default_user
225
+ id = File.read(repo.path + '/refs/tags/0.1')
226
+ tag = repo.get(id)
227
+
228
+ tag.tagtype.should == 'commit'
229
+ tag.object.object.should == repo.head
230
+ tag.tagger.name.should == user.name
231
+ tag.tagger.email.should == user.email
232
+ tag.message.should =~ /message/
233
+ end
234
+
235
+ end
data/test/tree_spec.rb ADDED
@@ -0,0 +1,75 @@
1
+ require "#{File.dirname(__FILE__)}/../lib/gitrb"
2
+ require "#{File.dirname(__FILE__)}/helper"
3
+ require 'pp'
4
+
5
+ describe Gitrb::Tree do
6
+ REPO = '/tmp/gitrb_test'
7
+
8
+ include Helper
9
+
10
+ attr_reader :repo, :tree
11
+
12
+ before(:each) do
13
+ FileUtils.rm_rf REPO
14
+ Dir.mkdir REPO
15
+
16
+ @repo = Gitrb::Repository.new(:path => REPO, :create => true)
17
+ end
18
+
19
+ it "should write a table" do
20
+ tree = Gitrb::Tree.new(:repository => repo)
21
+
22
+ tree['a'] = Gitrb::Blob.new(:data => 'a')
23
+ tree['b'] = Gitrb::Blob.new(:data => 'b')
24
+ tree['c'] = Gitrb::Blob.new(:data => 'c')
25
+
26
+ id = tree.save
27
+
28
+ a = ["2e65efe2a145dda7ee51d1741299f848e5bf752e"].pack('H*')
29
+ b = ["63d8dbd40c23542e740659a7168a0ce3138ea748"].pack('H*')
30
+ c = ["3410062ba67c5ed59b854387a8bc0ec012479368"].pack('H*')
31
+
32
+ data =
33
+ "100644 a\0#{a}" +
34
+ "100644 b\0#{b}" +
35
+ "100644 c\0#{c}"
36
+
37
+ repo.get(id).should be_a(Gitrb::Tree)
38
+ repo.get(id).names.should include('a')
39
+ repo.get(id).names.should include('b')
40
+ repo.get(id).names.should include('c')
41
+ end
42
+
43
+ it "should save trees" do
44
+ tree = Gitrb::Tree.new(:repository => repo)
45
+
46
+ tree['a'] = Gitrb::Blob.new(:data => 'a')
47
+ tree['b'] = Gitrb::Blob.new(:data => 'b')
48
+ tree['c'] = Gitrb::Blob.new(:data => 'c')
49
+
50
+ tree.save
51
+
52
+ ls_tree(tree.id).should ==
53
+ [["100644", "blob", "2e65efe2a145dda7ee51d1741299f848e5bf752e", "a"],
54
+ ["100644", "blob", "63d8dbd40c23542e740659a7168a0ce3138ea748", "b"],
55
+ ["100644", "blob", "3410062ba67c5ed59b854387a8bc0ec012479368", "c"]]
56
+ end
57
+
58
+ it "should save nested trees" do
59
+ tree = Gitrb::Tree.new(:repository => repo)
60
+
61
+ tree['x/a'] = Gitrb::Blob.new(:data => 'a')
62
+ tree['x/b'] = Gitrb::Blob.new(:data => 'b')
63
+ tree['x/c'] = Gitrb::Blob.new(:data => 'c')
64
+
65
+ tree.save
66
+
67
+ ls_tree(tree.id).should ==
68
+ [["040000", "tree", "24e88cb96c396400000ef706d1ca1ed9a88251aa", "x"]]
69
+
70
+ ls_tree("24e88cb96c396400000ef706d1ca1ed9a88251aa").should ==
71
+ [["100644", "blob", "2e65efe2a145dda7ee51d1741299f848e5bf752e", "a"],
72
+ ["100644", "blob", "63d8dbd40c23542e740659a7168a0ce3138ea748", "b"],
73
+ ["100644", "blob", "3410062ba67c5ed59b854387a8bc0ec012479368", "c"]]
74
+ end
75
+ end
data/test/trie_spec.rb ADDED
@@ -0,0 +1,26 @@
1
+ require "#{File.dirname(__FILE__)}/../lib/gitrb"
2
+ require 'pp'
3
+
4
+ describe Gitrb::Trie do
5
+
6
+ it "should add children" do
7
+ trie = Gitrb::Trie.new
8
+ 0.upto(100) do |i|
9
+ trie.insert('a' * i, i)
10
+ end
11
+ trie.find('').key.should == ''
12
+ trie.find('').value.should == 0
13
+ 1.upto(100) do |i|
14
+ trie.find('a' * i).key.should == 'a'
15
+ trie.find('a' * i).value.should == i
16
+ end
17
+ end
18
+
19
+ it "should split node" do
20
+ trie = Gitrb::Trie.new
21
+ trie.insert("abc", 1)
22
+ trie.insert("ab", 2)
23
+ trie.insert("ac", 3)
24
+ end
25
+
26
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Mendler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-04 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Pure ruby git implementation similar to grit.
17
+ email: mail@daniel-mendler.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.md
24
+ files:
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - gitrb.gemspec
29
+ - lib/gitrb/blob.rb
30
+ - lib/gitrb/commit.rb
31
+ - lib/gitrb/diff.rb
32
+ - lib/gitrb/object.rb
33
+ - lib/gitrb/pack.rb
34
+ - lib/gitrb/repository.rb
35
+ - lib/gitrb/tag.rb
36
+ - lib/gitrb/tree.rb
37
+ - lib/gitrb/trie.rb
38
+ - lib/gitrb/user.rb
39
+ - test/bare_repository_spec.rb
40
+ - test/benchmark.rb
41
+ - test/commit_spec.rb
42
+ - test/repository_spec.rb
43
+ - test/trie_spec.rb
44
+ - test/tree_spec.rb
45
+ has_rdoc: true
46
+ homepage: https://github.com/minad/gitrb
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project: gitrb
67
+ rubygems_version: 1.3.1
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: Pure ruby git implementation
71
+ test_files: []
72
+