gitrb 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/gitrb.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'gitrb'
3
- s.version = '0.2.1'
3
+ s.version = '0.2.2'
4
4
  s.summary = 'Pure ruby git implementation'
5
5
  s.author = 'Daniel Mendler'
6
6
  s.email = 'mail@daniel-mendler.de'
@@ -33,6 +33,7 @@ test/commit_test.rb
33
33
  test/helper.rb
34
34
  test/profile.rb
35
35
  test/repository_test.rb
36
+ test/repository_init_test.rb
36
37
  test/trie_test.rb
37
38
  test/tree_test.rb
38
39
  }
@@ -7,7 +7,7 @@ module Gitrb
7
7
  @id = options[:id]
8
8
  end
9
9
 
10
- def object
10
+ def git_object
11
11
  self
12
12
  end
13
13
 
@@ -9,9 +9,7 @@ module Gitrb
9
9
 
10
10
  def method_missing(name, *args, &block)
11
11
  if @object
12
- unless name == :to_ary || name == :to_str
13
- # Ruby 1.9 uses the presence of the to_ary and to_str methods to determine if an object is coercable.
14
- # If we create these methods, Ruby will incorrectly think that the object can be converted to an array.
12
+ if @object.respond_to? name
15
13
  instance_eval %{def self.#{name}(*args, &block); @object.send("#{name}", *args, &block); end}
16
14
  end
17
15
  @object.send(name, *args, &block)
@@ -21,17 +19,12 @@ module Gitrb
21
19
  @properties[name]
22
20
  elsif @properties.include?(name.to_s)
23
21
  @properties[name.to_s]
24
- elsif object
25
- method_missing(name, *args, &block)
26
22
  else
27
- super
23
+ @object = repository.get(id)
24
+ method_missing(name, *args, &block)
28
25
  end
29
26
  end
30
27
 
31
- def object
32
- @object ||= repository.get(id)
33
- end
34
-
35
28
  def resolved?
36
29
  @object != nil
37
30
  end
@@ -17,6 +17,14 @@ module Gitrb
17
17
  class Repository
18
18
  attr_reader :path, :root, :branch, :head, :encoding
19
19
 
20
+ def self.git_path
21
+ @git_path ||= begin
22
+ path = `which git`.chomp
23
+ raise 'git not found' if $?.exitstatus != 0
24
+ path
25
+ end
26
+ end
27
+
20
28
  SHA_PATTERN = /^[A-Fa-f0-9]{5,40}$/
21
29
  REVISION_PATTERN = /^[\w\-\.]+([\^~](\d+)?)*$/
22
30
  DEFAULT_ENCODING = 'utf-8'
@@ -176,8 +184,8 @@ module Gitrb
176
184
  :message => message.strip)
177
185
  end
178
186
  commits
179
- rescue => ex
180
- return [] if ex.message =~ /bad default revision 'HEAD'/i
187
+ rescue CommandError => ex
188
+ return [] if ex.output =~ /bad default revision 'HEAD'/i
181
189
  raise
182
190
  end
183
191
 
@@ -233,7 +241,7 @@ module Gitrb
233
241
  content, type = pack.get_object(offset)
234
242
  end
235
243
 
236
- @logger.debug "gitrb: Loaded #{id}"
244
+ @logger.debug "gitrb: Loaded #{type} #{id}"
237
245
 
238
246
  set_encoding(id)
239
247
  object = GitObject.factory(type, :repository => self, :id => id, :data => content)
@@ -249,6 +257,8 @@ module Gitrb
249
257
  #
250
258
  # Returns the object.
251
259
  def put(object)
260
+ raise ArgumentError unless object && GitObject === object
261
+
252
262
  content = object.dump
253
263
  data = "#{object.type} #{content.bytesize rescue content.length}\0#{content}"
254
264
  id = sha(data)
@@ -273,22 +283,25 @@ module Gitrb
273
283
  object
274
284
  end
275
285
 
276
- def method_missing(name, *args, &block)
286
+ def method_missing(name, *args)
277
287
  cmd = name.to_s
278
288
  if cmd[0..3] == 'git_'
279
- ENV['GIT_DIR'] = path
280
289
  cmd = cmd[4..-1].tr('_', '-')
281
- args = args.flatten.compact.map {|s| "'" + s.to_s.gsub("'", "'\\\\''") + "'" }.join(' ')
282
- cmdline = "git #{cmd} #{args} 2>&1"
283
-
284
- @logger.debug "gitrb: #{cmdline}"
285
-
286
- # Read in binary mode (ascii-8bit) and convert afterwards
287
- out = if block_given?
288
- IO.popen(cmdline, 'rb', &block)
289
- else
290
- set_encoding IO.popen(cmdline, 'rb') {|io| io.read }
291
- end
290
+ args = args.flatten.compact.map {|a| a.to_s }
291
+
292
+ @logger.debug "gitrb: #{self.class.git_path} #{cmd} #{args.inspect}"
293
+
294
+ out = IO.popen('-', 'rb') do |io|
295
+ if io
296
+ # Read in binary mode (ascii-8bit) and convert afterwards
297
+ block_given? ? yield(io) : set_encoding(io.read)
298
+ else
299
+ # child's stderr goes to stdout
300
+ STDERR.reopen(STDOUT)
301
+ ENV['GIT_DIR'] = path
302
+ exec(self.class.git_path, cmd, *args)
303
+ end
304
+ end
292
305
 
293
306
  if $?.exitstatus > 0
294
307
  return '' if $?.exitstatus == 1 && out == ''
@@ -302,18 +315,13 @@ module Gitrb
302
315
  end
303
316
 
304
317
  def default_user
305
- name = git_config('user.name').chomp
306
- email = git_config('user.email').chomp
307
- if name.empty?
308
- require 'etc'
309
- user = Etc.getpwnam(Etc.getlogin)
310
- name = user.gecos
318
+ @default_user ||= begin
319
+ name = git_config('user.name').chomp
320
+ email = git_config('user.email').chomp
321
+ name = ENV['USER'] if name.empty?
322
+ email = ENV['USER'] + '@' + `hostname -f`.chomp if email.empty?
323
+ User.new(name, email)
311
324
  end
312
- if email.empty?
313
- require 'etc'
314
- email = Etc.getlogin + '@' + `hostname -f`.chomp
315
- end
316
- User.new(name, email)
317
325
  end
318
326
 
319
327
  private
@@ -334,11 +342,7 @@ module Gitrb
334
342
  if create && !File.exists?("#{@path}/objects")
335
343
  FileUtils.mkpath(@path) if !File.exists?(@path)
336
344
  raise ArgumentError, "Not a valid Git repository: '#{@path}'" if !File.directory?(@path)
337
- if @bare
338
- Dir.chdir(@path) { git_init '--bare' }
339
- else
340
- Dir.chdir(@path[0..-6]) { git_init }
341
- end
345
+ git_init(@bare ? '--bare' : nil)
342
346
  else
343
347
  raise ArgumentError, "Not a valid Git repository: '#{@path}'" if !File.directory?("#{@path}/objects")
344
348
  end
data/lib/gitrb/tree.rb CHANGED
@@ -73,7 +73,7 @@ module Gitrb
73
73
 
74
74
  # Write an entry on specified path.
75
75
  def []=(path, entry)
76
- raise ArgumentError if !entry
76
+ raise ArgumentError unless entry && GitObject === entry
77
77
  path = normalize_path(path)
78
78
  if path.empty?
79
79
  raise 'Empty path'
data/lib/gitrb/util.rb CHANGED
@@ -40,4 +40,3 @@ if !1.respond_to?(:ord)
40
40
  end
41
41
  end
42
42
  end
43
-
@@ -1,20 +1,11 @@
1
1
  require 'helper'
2
2
 
3
3
  describe Gitrb do
4
-
5
- REPO = '/tmp/gitrb_test.git'
6
-
7
4
  before do
8
- FileUtils.rm_rf REPO
9
- Dir.mkdir REPO
10
-
11
- @repo = Gitrb::Repository.new(:path => REPO, :create => true)
12
- end
5
+ FileUtils.rm_rf REPO_PATH
6
+ Dir.mkdir REPO_PATH
13
7
 
14
- it 'should fail to initialize without a valid git repository' do
15
- lambda {
16
- Gitrb::Repository.new('/foo', 'master', true)
17
- }.should.raise(ArgumentError)
8
+ @repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true, :bare => true)
18
9
  end
19
10
 
20
11
  it 'should save and load entries' do
data/test/benchmark.rb CHANGED
@@ -3,13 +3,13 @@ require 'grit'
3
3
  require 'benchmark'
4
4
  require 'fileutils'
5
5
 
6
- REPO = '/tmp/gitrb'
6
+ REPO_PATH = '/tmp/gitrb_test'
7
7
 
8
- FileUtils.rm_rf REPO
9
- FileUtils.mkpath REPO
10
- Dir.chdir REPO
8
+ FileUtils.rm_rf REPO_PATH
9
+ FileUtils.mkpath REPO_PATH
10
+ Dir.chdir REPO_PATH
11
11
 
12
- repo = Gitrb::Repository.new(:path => REPO, :create => true)
12
+ repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true)
13
13
 
14
14
  grit = nil
15
15
  gitrb = nil
data/test/commit_test.rb CHANGED
@@ -1,14 +1,11 @@
1
1
  require 'helper'
2
2
 
3
3
  describe Gitrb::Commit do
4
-
5
- REPO = '/tmp/gitrb_test'
6
-
7
4
  before do
8
- FileUtils.rm_rf REPO
9
- Dir.mkdir REPO
5
+ FileUtils.rm_rf REPO_PATH
6
+ Dir.mkdir REPO_PATH
10
7
 
11
- @repo = Gitrb::Repository.new(:path => REPO, :create => true)
8
+ @repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true)
12
9
  end
13
10
 
14
11
  it "should dump in right format" do
@@ -36,12 +33,14 @@ This is a message"
36
33
  repo.root['a'] = Gitrb::Blob.new(:data => "Yay")
37
34
  commit = repo.commit("Commit Message", author, author)
38
35
 
39
- IO.popen("git log") do |io|
40
- io.gets.should.equal "commit #{commit.id}\n"
41
- io.gets.should.equal "Author: hans <hans@email.de>\n"
42
- io.gets.should.equal "Date: Mon Apr 20 00:00:00 2009 #{Time.now.strftime('%z')}\n"
43
- io.gets.should.equal "\n"
44
- io.gets.should.equal " Commit Message\n"
36
+ with_git_dir do
37
+ IO.popen("git log") do |io|
38
+ io.gets.should.equal "commit #{commit.id}\n"
39
+ io.gets.should.equal "Author: hans <hans@email.de>\n"
40
+ io.gets.should.equal "Date: Mon Apr 20 00:00:00 2009 #{Time.now.strftime('%z')}\n"
41
+ io.gets.should.equal "\n"
42
+ io.gets.should.equal " Commit Message\n"
43
+ end
45
44
  end
46
45
  end
47
46
 
data/test/helper.rb CHANGED
@@ -11,13 +11,23 @@ module TestHelper
11
11
  open(file, 'w') { |io| io << data }
12
12
 
13
13
  repo.git_add(file)
14
- repo.git_commit('-m', 'added #{file}')
14
+ repo.git_commit('-m', "added #{file}")
15
15
  File.unlink(file)
16
16
  end
17
17
  end
18
+
19
+ def with_git_dir
20
+ old_path = ENV['GIT_DIR']
21
+ ENV['GIT_DIR'] = repo.path
22
+ yield
23
+ ensure
24
+ ENV['GIT_DIR'] = old_path
25
+ end
18
26
  end
19
27
 
20
28
  class Bacon::Context
21
29
  include TestHelper
22
30
  attr_reader :repo
23
31
  end
32
+
33
+ REPO_PATH = '/tmp/gitrb_test'
data/test/profile.rb CHANGED
@@ -3,12 +3,12 @@ require 'fileutils'
3
3
  require 'grit'
4
4
  require 'ruby-prof'
5
5
 
6
- REPO = '/tmp/gitrb'
6
+ REPO_PATH = '/tmp/gitrb_test'
7
7
 
8
- FileUtils.rm_rf REPO
9
- FileUtils.mkpath REPO
8
+ FileUtils.rm_rf REPO_PATH
9
+ FileUtils.mkpath REPO_PATH
10
10
 
11
- repo = Gitrb::Repository.new(:path => REPO, :create => true)
11
+ repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true)
12
12
  repo.transaction { 'aaa'.upto('jjj') { |key| repo.root[key] = Gitrb::Blob.new(:data => rand.to_s) } }
13
13
 
14
14
  result = RubyProf.profile do
@@ -0,0 +1,34 @@
1
+ require 'helper'
2
+
3
+ describe Gitrb do
4
+ it 'should fail to initialize without a valid git repository' do
5
+ File.exists?('/foo').should.be.false
6
+ lambda do
7
+ Gitrb::Repository.new(:path => '/foo', :branch => 'master', :bare => true)
8
+ end.should.raise(ArgumentError)
9
+ File.exists?('/foo').should.be.false
10
+ lambda do
11
+ Gitrb::Repository.new(:path => '/foo', :branch => 'master')
12
+ end.should.raise(ArgumentError)
13
+ File.exists?('/foo').should.be.false
14
+ end
15
+
16
+ it 'should create repository on relative path' do
17
+ FileUtils.rm_rf('/tmp/gitrb_test')
18
+ Dir.chdir('/tmp') do
19
+ Gitrb::Repository.new(:path => "gitrb_test/repo",
20
+ :bare => true, :create => true)
21
+ end
22
+ File.directory?('/tmp/gitrb_test/repo').should.be.true
23
+ File.directory?('/tmp/gitrb_test/repo/objects').should.be.true
24
+ File.exists?('/tmp/gitrb_test/repo/.git').should.be.false
25
+
26
+ FileUtils.rm_rf('/tmp/gitrb_test')
27
+ Dir.chdir('/tmp') do
28
+ Gitrb::Repository.new(:path => "gitrb_test/repo", :create => true)
29
+ end
30
+ File.directory?('/tmp/gitrb_test/repo').should.be.true
31
+ File.directory?('/tmp/gitrb_test/repo/.git').should.be.true
32
+ File.directory?('/tmp/gitrb_test/repo/.git/objects').should.be.true
33
+ end
34
+ end
@@ -1,19 +1,10 @@
1
1
  require 'helper'
2
2
 
3
3
  describe Gitrb do
4
-
5
- REPO = '/tmp/gitrb_test'
6
-
7
4
  before do
8
- FileUtils.rm_rf REPO
9
- Dir.mkdir REPO
10
- @repo = Gitrb::Repository.new(:path => REPO, :create => true)
11
- end
12
-
13
- it 'should fail to initialize without a valid git repository' do
14
- lambda do
15
- Gitrb::Repository.new(:path => '/')
16
- end.should.raise(ArgumentError)
5
+ FileUtils.rm_rf REPO_PATH
6
+ Dir.mkdir REPO_PATH
7
+ @repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true)
17
8
  end
18
9
 
19
10
  it 'should put and get objects by sha' do
@@ -77,8 +68,8 @@ describe Gitrb do
77
68
 
78
69
  repo.refresh
79
70
 
80
- repo.root['x'].object.should.be.kind_of(Gitrb::Tree)
81
- repo.root['y'].object.should.be.kind_of(Gitrb::Tree)
71
+ repo.root['x'].git_object.should.be.kind_of(Gitrb::Tree)
72
+ repo.root['y'].git_object.should.be.kind_of(Gitrb::Tree)
82
73
 
83
74
  repo.root['x']['a'].data.should.equal 'Hello'
84
75
  repo.root['y']['b'].data.should.equal 'World'
@@ -151,11 +142,12 @@ describe Gitrb do
151
142
 
152
143
  begin
153
144
  repo.transaction do
154
- repo.root['a/b'] = 'Changed'
155
- repo.root['x/a'] = 'Added'
156
- raise
145
+ repo.root['a/b'] = Gitrb::Blob.new(:data => 'Changed')
146
+ repo.root['x/a'] = Gitrb::Blob.new(:data => 'Added')
147
+ raise 'boo'
157
148
  end
158
- rescue
149
+ rescue RuntimeError => ex
150
+ ex.message.should.equal 'boo'
159
151
  end
160
152
 
161
153
  repo.root['a/b'].data.should.equal 'Hello'
@@ -172,8 +164,8 @@ describe Gitrb do
172
164
  repo.root['x/a'] = Gitrb::Blob.new(:data => 'Added')
173
165
  end
174
166
 
175
- a = ls_tree(repo.root['a'].object.id)
176
- x = ls_tree(repo.root['x'].object.id)
167
+ a = ls_tree(repo.root['a'].id)
168
+ x = ls_tree(repo.root['x'].id)
177
169
 
178
170
  a.should.equal [["100644", "blob", "b653cf27cef08de46da49a11fa5016421e9e3b32", "b"]]
179
171
  x.should.equal [["100644", "blob", "87d2b203800386b1cc8735a7d540a33e246357fa", "a"]]
@@ -203,9 +195,12 @@ describe Gitrb do
203
195
 
204
196
  ready = false
205
197
 
198
+ # This test case produces a deadlock in old ruby versions
199
+ # (Works in 1.8.7_p302 and 1.9)
206
200
  repo.transaction do
207
201
  Thread.start do
208
202
  repo.transaction do
203
+ sleep 0.1
209
204
  repo.root['a/b'] = Gitrb::Blob.new(:data => 'Changed by second thread')
210
205
  end
211
206
  ready = true
@@ -213,15 +208,14 @@ describe Gitrb do
213
208
  repo.root['a/b'] = Gitrb::Blob.new(:data => 'Changed')
214
209
  end
215
210
 
216
- sleep 0.01 until ready
211
+ repo.root['a/b'].data.should.equal 'Changed'
217
212
 
218
- repo.refresh
213
+ sleep 0.01 until ready
219
214
 
220
215
  repo.root['a/b'].data.should.equal 'Changed by second thread'
221
216
  end
222
217
 
223
218
  it 'should find all objects' do
224
- repo.refresh
225
219
  repo.root['c'] = Gitrb::Blob.new(:data => 'Hello')
226
220
  repo.root['d'] = Gitrb::Blob.new(:data => 'World')
227
221
  repo.commit
@@ -231,6 +225,8 @@ describe Gitrb do
231
225
  end
232
226
 
233
227
  it "should load log" do
228
+ repo.log.should.be.empty
229
+
234
230
  repo.root['a'] = Gitrb::Blob.new(:data => 'a')
235
231
  repo.commit 'added a'
236
232
 
@@ -253,7 +249,7 @@ describe Gitrb do
253
249
  tag = repo.get(id)
254
250
 
255
251
  tag.tagtype.should.equal 'commit'
256
- tag.object.object.should.equal repo.head
252
+ tag.object.git_object.should.equal repo.head
257
253
  tag.tagger.name.should.equal user.name
258
254
  tag.tagger.email.should.equal user.email
259
255
  tag.message.should =~ /message/
data/test/tree_test.rb CHANGED
@@ -1,13 +1,11 @@
1
1
  require 'helper'
2
2
 
3
3
  describe Gitrb::Tree do
4
- REPO = '/tmp/gitrb_test'
5
-
6
4
  before do
7
- FileUtils.rm_rf REPO
8
- Dir.mkdir REPO
5
+ FileUtils.rm_rf REPO_PATH
6
+ Dir.mkdir REPO_PATH
9
7
 
10
- @repo = Gitrb::Repository.new(:path => REPO, :create => true)
8
+ @repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true)
11
9
  end
12
10
 
13
11
  it "should write a table" do
data/test/trie_test.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'gitrb'
2
2
 
3
3
  describe Gitrb::Trie do
4
-
5
4
  before do
6
5
  @trie = Gitrb::Trie.new
7
6
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 1
9
- version: 0.2.1
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Daniel Mendler
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-22 00:00:00 +02:00
17
+ date: 2010-09-03 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -60,6 +60,7 @@ files:
60
60
  - test/helper.rb
61
61
  - test/profile.rb
62
62
  - test/repository_test.rb
63
+ - test/repository_init_test.rb
63
64
  - test/trie_test.rb
64
65
  - test/tree_test.rb
65
66
  has_rdoc: true