Browse files

Add packed-refs annotated tag support.

  • Loading branch information...
1 parent 2c17813 commit a683a584c0056635d15955d4ff2a9566115974ce @defunkt defunkt committed Feb 13, 2009
Showing with 50 additions and 16 deletions.
  1. +26 −12 lib/grit/tag.rb
  2. +2 −1 test/dot_git/packed-refs
  3. +22 −3 test/test_tag.rb
View
38 lib/grit/tag.rb
@@ -4,7 +4,6 @@ class Tag < Ref
def self.find_all(repo, options = {})
refs = []
already = {}
- git_ruby_repo = GitRuby::Repository.new(repo.path)
Dir.chdir(repo.path) do
files = Dir.glob(prefix + '/**/*')
@@ -14,15 +13,7 @@ def self.find_all(repo, options = {})
id = File.read(ref).chomp
name = ref.sub("#{prefix}/", '')
- object = git_ruby_repo.get_object_by_sha1(id)
-
- if object.type == :commit
- commit = Commit.create(repo, :id => id)
- elsif object.type == :tag
- commit = Commit.create(repo, :id => object.object)
- else
- raise "Unknown object type."
- end
+ commit = commit_from_sha(repo, id)
if !already[name]
refs << self.new(name, commit)
@@ -31,11 +22,21 @@ def self.find_all(repo, options = {})
end
if File.file?('packed-refs')
- File.readlines('packed-refs').each do |line|
+ lines = File.readlines('packed-refs')
+ lines.each_with_index do |line, i|
if m = /^(\w{40}) (.*?)$/.match(line)
next if !Regexp.new('^' + prefix).match(m[2])
name = m[2].sub("#{prefix}/", '')
- commit = Commit.create(repo, :id => m[1])
+
+ # Annotated tags in packed-refs include a reference
+ # to the commit object on the following line.
+ next_line = lines[i+1]
+ if next_line && next_line[0] == ?^
+ commit = Commit.create(repo, :id => lines[i+1][1..-1].chomp)
+ else
+ commit = commit_from_sha(repo, m[1])
+ end
+
if !already[name]
refs << self.new(name, commit)
already[name] = true
@@ -47,6 +48,19 @@ def self.find_all(repo, options = {})
refs
end
+
+ def self.commit_from_sha(repo, id)
+ git_ruby_repo = GitRuby::Repository.new(repo.path)
+ object = git_ruby_repo.get_object_by_sha1(id)
+
+ if object.type == :commit
+ Commit.create(repo, :id => id)
+ elsif object.type == :tag
+ Commit.create(repo, :id => object.object)
+ else
+ raise "Unknown object type."
+ end
+ end
end
end
View
3 test/dot_git/packed-refs
@@ -5,5 +5,6 @@ ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a refs/heads/test/chacon
2d3acf90f35989df8f262dc50beadc4ee3ae1560 refs/heads/testing
ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a refs/remotes/origin/master
2d3acf90f35989df8f262dc50beadc4ee3ae1560 refs/remotes/tom/master
-f0055fda16c18fd8b27986dbf038c735b82198d7 refs/tags/v0.7.0
+f0055fda16c18fd8b27986dbf038c735b82198d7 refs/tags/packed_annotated
^7bcc0ee821cdd133d8a53e8e7173a334fef448aa
+ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a refs/tags/packed
View
25 test/test_tag.rb
@@ -10,7 +10,7 @@ def setup
def test_list_from_string
tags = @r.tags
- assert_equal 3, tags.size
+ assert_equal 5, tags.size
assert_equal 'not_annotated', tags[1].name
assert_equal 'ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a', tags[1].commit.id
end
@@ -20,8 +20,8 @@ def test_list_from_string
def test_list_from_string_for_signed_tag
tags = @r.tags
- assert_equal 'v0.7.0', tags.last.name
- assert_equal '7bcc0ee821cdd133d8a53e8e7173a334fef448aa', tags.last.commit.id
+ assert_equal 'v0.7.0', tags[2].name
+ assert_equal '7bcc0ee821cdd133d8a53e8e7173a334fef448aa', tags[2].commit.id
end
# list_from_string_for_annotated_tag
@@ -33,6 +33,25 @@ def test_list_from_string_for_annotated_tag
assert_equal 'ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a', tags.first.commit.id
end
+ # list_from_string_for_packed_tag
+
+ def test_list_from_string_for_packed_tag
+ tags = @r.tags
+
+ assert_equal 'packed', tags[4].name
+ assert_equal 'ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a', tags[4].commit.id
+ end
+
+ # list_from_string_for_packed_annotated_tag
+
+ def test_list_from_string_for_packed_annotated_tag
+ tags = @r.tags
+
+ assert_equal 'packed_annotated', tags[3].name
+ assert_equal '7bcc0ee821cdd133d8a53e8e7173a334fef448aa', tags[3].commit.id
+ end
+
+
# inspect
def test_inspect

0 comments on commit a683a58

Please sign in to comment.