mojombo-grit 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -27,6 +27,7 @@ lib/grit/lazy.rb
27
27
  lib/grit/ref.rb
28
28
  lib/grit/repo.rb
29
29
  lib/grit/status.rb
30
+ lib/grit/submodule.rb
30
31
  lib/grit/tag.rb
31
32
  lib/grit/tree.rb
32
33
  lib/grit.rb
data/README.txt CHANGED
@@ -1,5 +1,5 @@
1
1
  grit
2
- by Tom Preston-Werner, Chris Wanstrath
2
+ by Tom Preston-Werner, Scott Chacon
3
3
  http://github.com/mojombo/grit
4
4
 
5
5
  == DESCRIPTION:
@@ -9,7 +9,7 @@ object oriented manner.
9
9
 
10
10
  == REQUIREMENTS:
11
11
 
12
- * git (http://git.or.cz) tested with 1.5.3.4
12
+ * git (http://git-scm.com) tested with 1.6.0.2
13
13
 
14
14
  == INSTALL:
15
15
 
data/grit.gemspec CHANGED
@@ -1,15 +1,14 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "grit"
3
- s.version = "0.9.3"
4
- s.date = "2008-04-24"
3
+ s.version = "0.9.4"
4
+ s.date = "2008-11-07"
5
5
  s.summary = "Object model interface to a git repo"
6
6
  s.email = "tom@rubyisawesome.com"
7
- s.homepage = "http://github.com/schacon/grit"
7
+ s.homepage = "http://github.com/mojombo/grit"
8
8
  s.description = "Grit is a Ruby library for extracting information from a git repository in and object oriented manner."
9
9
  s.has_rdoc = true
10
10
  s.authors = ["Tom Preston-Werner", "Scott Chacon"]
11
11
  s.files = ["History.txt",
12
- "Manifest.txt",
13
12
  "README.txt",
14
13
  "Rakefile",
15
14
  "grit.gemspec",
@@ -23,7 +22,6 @@ Gem::Specification.new do |s|
23
22
  "lib/grit/git-ruby/commit_db.rb",
24
23
  "lib/grit/git-ruby/file_index.rb",
25
24
  "lib/grit/git-ruby/git_object.rb",
26
- "lib/grit/git-ruby/internal",
27
25
  "lib/grit/git-ruby/internal/loose.rb",
28
26
  "lib/grit/git-ruby/internal/mmap.rb",
29
27
  "lib/grit/git-ruby/internal/pack.rb",
@@ -38,9 +36,11 @@ Gem::Specification.new do |s|
38
36
  "lib/grit/ref.rb",
39
37
  "lib/grit/repo.rb",
40
38
  "lib/grit/status.rb",
39
+ "lib/grit/submodule.rb",
41
40
  "lib/grit/tag.rb",
42
41
  "lib/grit/tree.rb",
43
- "lib/grit.rb"]
42
+ "lib/grit.rb",
43
+ "lib/open3_detach.rb"]
44
44
  s.test_files = ["test/test_actor.rb",
45
45
  "test/test_blob.rb", "test/test_commit.rb",
46
46
  "test/test_config.rb",
data/lib/grit.rb CHANGED
@@ -7,12 +7,17 @@ require 'time'
7
7
  # stdlib
8
8
  require 'timeout'
9
9
  require 'logger'
10
+ require 'digest/sha1'
11
+
12
+ if defined? RUBY_ENGINE && RUBY_ENGINE == 'jruby'
13
+ require 'open3'
14
+ else
15
+ require 'open3_detach'
16
+ end
10
17
 
11
18
  # third party
12
19
  require 'rubygems'
13
20
  require 'mime/types'
14
- require 'open4'
15
- require 'digest/sha1'
16
21
 
17
22
  # internal requires
18
23
  require 'grit/lazy'
@@ -30,6 +35,7 @@ require 'grit/config'
30
35
  require 'grit/repo'
31
36
  require 'grit/index'
32
37
  require 'grit/status'
38
+ require 'grit/submodule'
33
39
 
34
40
 
35
41
  module Grit
@@ -49,5 +55,5 @@ module Grit
49
55
 
50
56
  @logger ||= ::Logger.new(STDOUT)
51
57
 
52
- VERSION = '0.9.1'
58
+ VERSION = '0.9.4'
53
59
  end
data/lib/grit/commit.rb CHANGED
@@ -71,7 +71,7 @@ module Grit
71
71
  #
72
72
  # Returns Integer
73
73
  def self.count(repo, ref)
74
- repo.git.rev_list({}, ref).strip.split("\n").size
74
+ repo.git.rev_list({}, ref).size / 41
75
75
  end
76
76
 
77
77
  # Find all commits matching the given criteria.
data/lib/grit/git-ruby.rb CHANGED
@@ -51,7 +51,7 @@ module Grit
51
51
  elsif (options.size == 0)
52
52
  # pure rev-list
53
53
  begin
54
- return file_index.commits_from(rev_parse({}, ref)).join("\n")
54
+ return file_index.commits_from(rev_parse({}, ref)).join("\n") + "\n"
55
55
  rescue
56
56
  return method_missing('rev-list', options, ref)
57
57
  end
@@ -65,7 +65,9 @@ module Grit
65
65
  end
66
66
  end
67
67
 
68
- def rev_parse(options, string)
68
+ def rev_parse(options, string)
69
+ raise RuntimeError, "invalid string: #{string}" unless string.is_a?(String)
70
+
69
71
  if string =~ /\.\./
70
72
  (sha1, sha2) = string.split('..')
71
73
  return [rev_parse({}, sha1), rev_parse({}, sha2)]
@@ -89,7 +91,7 @@ module Grit
89
91
  if File.file?(packref)
90
92
  File.readlines(packref).each do |line|
91
93
  if m = /^(\w{40}) refs\/.+?\/(.*?)$/.match(line)
92
- next if !Regexp.new(string + '$').match(m[3])
94
+ next if !Regexp.new(Regexp.escape(string) + '$').match(m[3])
93
95
  return m[1].chomp
94
96
  end
95
97
  end
@@ -27,7 +27,8 @@ module Grit
27
27
  def [](sha1)
28
28
  sha1 = sha1.unpack("H*")[0]
29
29
  begin
30
- path = @directory+'/'+sha1[0...2]+'/'+sha1[2..39]
30
+ return nil unless sha1[0...2] && sha1[2..39]
31
+ path = @directory + '/' + sha1[0...2] + '/' + sha1[2..39]
31
32
  get_raw_object(File.read(path))
32
33
  rescue Errno::ENOENT
33
34
  nil
@@ -70,6 +70,8 @@ module Grit
70
70
 
71
71
  # returns a raw object given a SHA1
72
72
  def get_raw_object_by_sha1(sha1o)
73
+ raise NoSuchShaFound if sha1o.nil? || sha1o.empty? || !sha1o.is_a?(String)
74
+
73
75
  sha1 = [sha1o.chomp].pack("H*")
74
76
  # try packs
75
77
  packs.each do |pack|
data/lib/grit/git.rb CHANGED
@@ -1,10 +1,3 @@
1
- trap("CHLD") do
2
- begin
3
- Process.wait(-1, Process::WNOHANG)
4
- rescue Object
5
- end
6
- end
7
-
8
1
  module Grit
9
2
 
10
3
  class Git
@@ -26,7 +19,7 @@ module Grit
26
19
  end
27
20
 
28
21
  self.git_binary = "/usr/bin/env git"
29
- self.git_timeout = 5
22
+ self.git_timeout = 10
30
23
 
31
24
  attr_accessor :git_dir, :bytes_read
32
25
 
@@ -54,7 +47,7 @@ module Grit
54
47
  timeout = true if timeout.nil?
55
48
 
56
49
  opt_args = transform_options(options)
57
- ext_args = args.map { |a| a == '--' ? a : "'#{a}'" }
50
+ ext_args = args.reject { |a| a.empty? }.map { |a| (a == '--' || a[0].chr == '|') ? a : "'#{a}'" }
58
51
 
59
52
  call = "#{prefix}#{Git.git_binary} --git-dir='#{self.git_dir}' #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{postfix}"
60
53
  Grit.log(call) if Grit.debug
@@ -65,37 +58,41 @@ module Grit
65
58
  end
66
59
 
67
60
  def sh(command)
68
- ret, pid, err = nil, nil, nil
69
- Open4.popen4(command) do |id, _, stdout, stderr|
70
- pid = id
71
- ret = Timeout.timeout(self.class.git_timeout) { stdout.read }
72
- err = stderr.read
73
- @bytes_read += ret.size
61
+ ret, err = '', ''
62
+ Open3.popen3(command) do |_, stdout, stderr|
63
+ Timeout.timeout(self.class.git_timeout) do
64
+ while tmp = stdout.read(1024)
65
+ ret += tmp
66
+ if (@bytes_read += tmp.size) > 5242880 # 5.megabytes
67
+ bytes = @bytes_read
68
+ @bytes_read = 0
69
+ raise GitTimeout.new(command, bytes)
70
+ end
71
+ end
72
+ end
74
73
 
75
- if @bytes_read > 5242880 # 5.megabytes
76
- bytes = @bytes_read
77
- @bytes_read = 0
78
- raise GitTimeout.new(command, bytes)
74
+ while tmp = stderr.read(1024)
75
+ err += tmp
79
76
  end
80
77
  end
81
78
  [ret, err]
82
- rescue Errno::ECHILD
83
- [ret, err]
84
- rescue Object => e
85
- Process.kill('KILL', pid) rescue nil
79
+ rescue Timeout::Error, Grit::Git::GitTimeout
86
80
  bytes = @bytes_read
87
81
  @bytes_read = 0
88
82
  raise GitTimeout.new(command, bytes)
89
83
  end
90
84
 
91
85
  def wild_sh(command)
92
- ret, err = nil, nil
93
- Open4.popen4(command) {|pid, _, stdout, stderr|
94
- ret = stdout.read
95
- err = stderr.read
96
- }
97
- [ret, err]
98
- rescue Errno::ECHILD
86
+ ret, err = '', ''
87
+ Open3.popen3(command) do |_, stdout, stderr|
88
+ while tmp = stdout.read(1024)
89
+ ret += tmp
90
+ end
91
+
92
+ while tmp = stderr.read(1024)
93
+ err += tmp
94
+ end
95
+ end
99
96
  [ret, err]
100
97
  end
101
98
 
data/lib/grit/lazy.rb CHANGED
@@ -20,8 +20,10 @@ module Lazy
20
20
  args.each do |arg|
21
21
  ivar = "@#{arg}"
22
22
  define_method(arg) do
23
- val = instance_variable_get(ivar)
24
- return val if val
23
+ if instance_variable_defined?(ivar)
24
+ val = instance_variable_get(ivar)
25
+ return val if val
26
+ end
25
27
  instance_variable_set(ivar, (@lazy_source ||= lazy_source).send(arg))
26
28
  end
27
29
  end
@@ -0,0 +1,84 @@
1
+ module Grit
2
+
3
+ class Submodule
4
+ attr_reader :id
5
+ attr_reader :mode
6
+ attr_reader :name
7
+
8
+ # Create a Submodule containing just the specified attributes
9
+ # +repo+ is the Repo
10
+ # +atts+ is a Hash of instance variable data
11
+ #
12
+ # Returns Grit::Submodule (unbaked)
13
+ def self.create(repo, atts)
14
+ self.allocate.create_initialize(repo, atts)
15
+ end
16
+
17
+ # Initializer for Submodule.create
18
+ # +repo+ is the Repo
19
+ # +atts+ is a Hash of instance variable data
20
+ #
21
+ # Returns Grit::Submodule
22
+ def create_initialize(repo, atts)
23
+ @repo = repo
24
+ atts.each do |k, v|
25
+ instance_variable_set("@#{k}".to_sym, v)
26
+ end
27
+ self
28
+ end
29
+
30
+ # The url of this submodule
31
+ # +ref+ is the committish that should be used to look up the url
32
+ #
33
+ # Returns String
34
+ def url(ref)
35
+ config = self.class.config(@repo, ref)
36
+
37
+ lookup = config.keys.inject({}) do |acc, key|
38
+ id = config[key]['id']
39
+ acc[id] = config[key]['url']
40
+ acc
41
+ end
42
+
43
+ lookup[@id]
44
+ end
45
+
46
+ # The configuration information for the given +repo+
47
+ # +repo+ is the Repo
48
+ # +ref+ is the committish (defaults to 'master')
49
+ #
50
+ # Returns a Hash of { <path:String> => { 'url' => <url:String>, 'id' => <id:String> } }
51
+ # Returns {} if no .gitmodules file was found
52
+ def self.config(repo, ref = "master")
53
+ commit = repo.commit(ref)
54
+ blob = commit.tree/'.gitmodules'
55
+ return {} unless blob
56
+
57
+ lines = blob.data.split("\n")
58
+
59
+ config = {}
60
+ current = nil
61
+
62
+ lines.each do |line|
63
+ if line =~ /^\[submodule "(.+)"\]$/
64
+ current = $1
65
+ config[current] = {}
66
+ config[current]['id'] = (commit.tree/current).id
67
+ elsif line =~ /^\t(\w+) = (.+)$/
68
+ config[current][$1] = $2
69
+ config[current]['id'] = (commit.tree/$2).id if $1 == 'path'
70
+ else
71
+ # ignore
72
+ end
73
+ end
74
+
75
+ config
76
+ end
77
+
78
+ # Pretty object inspection
79
+ def inspect
80
+ %Q{#<Grit::Submodule "#{@id}">}
81
+ end
82
+ end # Submodule
83
+
84
+ end # Grit
data/lib/grit/tree.rb CHANGED
@@ -72,7 +72,7 @@ module Grit
72
72
  when "link"
73
73
  Blob.create(repo, :id => id, :mode => mode, :name => name)
74
74
  when "commit"
75
- nil
75
+ Submodule.create(repo, :id => id, :mode => mode, :name => name)
76
76
  else
77
77
  raise "Invalid type: #{type}"
78
78
  end
@@ -88,7 +88,11 @@ module Grit
88
88
  #
89
89
  # Returns Grit::Blob or Grit::Tree or nil if not found
90
90
  def /(file)
91
- self.contents.find { |c| c.name == file }
91
+ if file =~ /\//
92
+ file.split("/").inject(self) { |acc, x| acc/x } rescue nil
93
+ else
94
+ self.contents.find { |c| c.name == file }
95
+ end
92
96
  end
93
97
 
94
98
  # Pretty object inspection
@@ -0,0 +1,46 @@
1
+ module Open3
2
+ extend self
3
+
4
+ def popen3(*cmd)
5
+ pw = IO::pipe # pipe[0] for read, pipe[1] for write
6
+ pr = IO::pipe
7
+ pe = IO::pipe
8
+
9
+ pid = fork{
10
+ # child
11
+ fork{
12
+ # grandchild
13
+ pw[1].close
14
+ STDIN.reopen(pw[0])
15
+ pw[0].close
16
+
17
+ pr[0].close
18
+ STDOUT.reopen(pr[1])
19
+ pr[1].close
20
+
21
+ pe[0].close
22
+ STDERR.reopen(pe[1])
23
+ pe[1].close
24
+
25
+ exec(*cmd)
26
+ }
27
+ exit!(0)
28
+ }
29
+
30
+ pw[0].close
31
+ pr[1].close
32
+ pe[1].close
33
+ Process.waitpid(pid)
34
+ pi = [pw[1], pr[0], pe[0]]
35
+ pw[1].sync = true
36
+ if defined? yield
37
+ begin
38
+ return yield(*pi)
39
+ ensure
40
+ Process.detach(pid) if pid
41
+ pi.each { |p| p.close unless p.closed? }
42
+ end
43
+ end
44
+ pi
45
+ end
46
+ end
data/test/test_commit.rb CHANGED
@@ -22,6 +22,12 @@ class TestCommit < Test::Unit::TestCase
22
22
  assert_equal '80f136f', @r.commit('80f136f500dfdb8c3e8abf4ae716f875f0a1b57f').id_abbrev
23
23
  end
24
24
 
25
+ # count
26
+
27
+ def test_count
28
+ assert_equal 107, Commit.count(@r, 'master')
29
+ end
30
+
25
31
  # diff
26
32
 
27
33
  def test_diff
data/test/test_git.rb CHANGED
@@ -8,7 +8,7 @@ class TestGit < Test::Unit::TestCase
8
8
  def teardown
9
9
  Grit.debug = false
10
10
  end
11
-
11
+
12
12
  def test_method_missing
13
13
  assert_match(/^git version [\w\.]*$/, @git.version)
14
14
  end
@@ -50,21 +50,15 @@ class TestGit < Test::Unit::TestCase
50
50
  def test_raises_if_too_many_bytes
51
51
  @git.instance_variable_set(:@bytes_read, 6000000)
52
52
  assert_raises Grit::Git::GitTimeout do
53
- @git.something
53
+ @git.version
54
54
  end
55
55
  end
56
56
 
57
57
  def test_raises_on_slow_shell
58
- Grit::Git.git_timeout = 0.5
59
- Open4.expects(:popen4).yields( nil, nil, mock(:read => proc { sleep 1 }), nil )
58
+ Grit::Git.git_timeout = 0.001
60
59
  assert_raises Grit::Git::GitTimeout do
61
- @git.something
60
+ @git.version
62
61
  end
63
- end
64
-
65
- def test_works_fine_if_quick
66
- output = 'output'
67
- Open4.expects(:popen4).yields( nil, nil, mock(:read => output), stub(:read => nil) )
68
- assert_equal output, @git.something
62
+ Grit::Git.git_timeout = 5.0
69
63
  end
70
64
  end
data/test/test_tree.rb CHANGED
@@ -50,12 +50,12 @@ class TestTree < Test::Unit::TestCase
50
50
  assert_equal "grit.rb", tree.name
51
51
  end
52
52
 
53
- def test_content_from_string_tree_should_return_commit
54
- text = fixture('ls_tree_commit').split("\n")[1]
53
+ def test_content_from_string_tree_should_return_submodule
54
+ text = fixture('ls_tree_submodule').split("\n").first
55
55
 
56
- tree = @t.content_from_string(nil, text)
56
+ sm = @t.content_from_string(nil, text)
57
57
 
58
- assert_nil tree
58
+ assert_kind_of Submodule, sm
59
59
  end
60
60
 
61
61
  def test_content_from_string_invalid_type_should_raise
@@ -82,7 +82,7 @@ class TestTree < Test::Unit::TestCase
82
82
  )
83
83
  tree = @r.tree('master')
84
84
 
85
- assert_nil tree/'bar'
85
+ assert_equal 'd35b34c6e931b9da8f6941007a92c9c9a9b0141a', (tree/'bar').id
86
86
  assert_equal '2afb47bcedf21663580d5e6d2f406f08f3f65f19', (tree/'foo').id
87
87
  assert_equal 'f623ee576a09ca491c4a27e48c0dfe04be5f4a2e', (tree/'baz').id
88
88
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mojombo-grit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Preston-Werner
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2008-04-24 00:00:00 -07:00
13
+ date: 2008-11-07 00:00:00 -08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -52,7 +52,6 @@ extra_rdoc_files:
52
52
  - README.txt
53
53
  files:
54
54
  - History.txt
55
- - Manifest.txt
56
55
  - README.txt
57
56
  - Rakefile
58
57
  - grit.gemspec
@@ -66,7 +65,6 @@ files:
66
65
  - lib/grit/git-ruby/commit_db.rb
67
66
  - lib/grit/git-ruby/file_index.rb
68
67
  - lib/grit/git-ruby/git_object.rb
69
- - lib/grit/git-ruby/internal
70
68
  - lib/grit/git-ruby/internal/loose.rb
71
69
  - lib/grit/git-ruby/internal/mmap.rb
72
70
  - lib/grit/git-ruby/internal/pack.rb
@@ -81,11 +79,14 @@ files:
81
79
  - lib/grit/ref.rb
82
80
  - lib/grit/repo.rb
83
81
  - lib/grit/status.rb
82
+ - lib/grit/submodule.rb
84
83
  - lib/grit/tag.rb
85
84
  - lib/grit/tree.rb
86
85
  - lib/grit.rb
86
+ - lib/open3_detach.rb
87
+ - Manifest.txt
87
88
  has_rdoc: true
88
- homepage: http://github.com/schacon/grit
89
+ homepage: http://github.com/mojombo/grit
89
90
  post_install_message:
90
91
  rdoc_options:
91
92
  - --main