Skip to content

Commit

Permalink
Merge branch 'patch-id' into github
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Jun 8, 2011
2 parents f6dd621 + bc628b0 commit 087676c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/grit/commit.rb
Expand Up @@ -252,6 +252,21 @@ def notes
ret
end

# Calculates the commit's Patch ID. The Patch ID is essentially the SHA1
# of the diff that the commit is introducing.
#
# Returns the 40 character hex String if a patch-id could be calculated
# or nil otherwise.
def patch_id
show = @repo.git.show({}, @id)
patch_line = @repo.git.native(:patch_id, :input => show)
if patch_line =~ /^([0-9a-f]{40}) [0-9a-f]{40}\n$/
$1
else
nil
end
end

# Pretty object inspection
def inspect
%Q{#<Grit::Commit "#{@id}">}
Expand Down
7 changes: 7 additions & 0 deletions test/test_commit.rb
Expand Up @@ -193,6 +193,13 @@ def test_to_patch
assert patch.include?("1.7.")
end

# patch_id

def test_patch_id
@c = Commit.create(@r, :id => '80f136f500dfdb8c3e8abf4ae716f875f0a1b57f')
assert_equal '9450b04e4f83ad0067199c9e9e338197d1835cbb', @c.patch_id
end

# inspect

def test_inspect
Expand Down

0 comments on commit 087676c

Please sign in to comment.