diff --git a/lib/grit/commit.rb b/lib/grit/commit.rb index 79da6b31..3246e59d 100644 --- a/lib/grit/commit.rb +++ b/lib/grit/commit.rb @@ -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{#} diff --git a/test/test_commit.rb b/test/test_commit.rb index fdb39b59..17c220cf 100644 --- a/test/test_commit.rb +++ b/test/test_commit.rb @@ -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