Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit 6ef79e8

Browse files
christopherjmedlinjonasfranz
authored andcommitted
Add parsing support for tag GPG signatures (#124)
* Add parsing support for tag GPG signatures * Test that signature is not left in commit message * Fix object not found error in test
1 parent 344971f commit 6ef79e8

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

commit.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@ type CommitGPGSignature struct {
3434
}
3535

3636
// similar to https://github.com/git/git/blob/3bc53220cb2dcf709f7a027a3f526befd021d858/commit.c#L1128
37-
func newGPGSignatureFromCommitline(data []byte, signatureStart int) (*CommitGPGSignature, error) {
37+
func newGPGSignatureFromCommitline(data []byte, signatureStart int, tag bool) (*CommitGPGSignature, error) {
3838
sig := new(CommitGPGSignature)
3939
signatureEnd := bytes.LastIndex(data, []byte("-----END PGP SIGNATURE-----"))
4040
if signatureEnd == -1 {
4141
return nil, fmt.Errorf("end of commit signature not found")
4242
}
4343
sig.Signature = strings.Replace(string(data[signatureStart:signatureEnd+27]), "\n ", "\n", -1)
44-
sig.Payload = string(data[:signatureStart-8]) + string(data[signatureEnd+27:])
44+
if tag {
45+
sig.Payload = string(data[:signatureStart-1])
46+
} else {
47+
sig.Payload = string(data[:signatureStart-8]) + string(data[signatureEnd+27:])
48+
}
4549
return sig, nil
4650
}
4751

repo_commit.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,28 @@ l:
7878
}
7979
commit.Committer = sig
8080
case "gpgsig":
81-
sig, err := newGPGSignatureFromCommitline(data, nextline+spacepos+1)
81+
sig, err := newGPGSignatureFromCommitline(data, nextline+spacepos+1, false)
8282
if err != nil {
8383
return nil, err
8484
}
8585
commit.Signature = sig
8686
}
8787
nextline += eol + 1
8888
case eol == 0:
89-
commit.CommitMessage = string(data[nextline+1:])
89+
cm := string(data[nextline+1:])
90+
91+
// Tag GPG signatures are stored below the commit message
92+
sigindex := strings.Index(cm, "-----BEGIN PGP SIGNATURE-----")
93+
if sigindex != -1 {
94+
sig, err := newGPGSignatureFromCommitline(data, (nextline+1)+sigindex, true)
95+
if err == nil && sig != nil {
96+
// remove signature from commit message
97+
cm = cm[:sigindex-1]
98+
commit.Signature = sig
99+
}
100+
}
101+
102+
commit.CommitMessage = cm
90103
break l
91104
default:
92105
break l

repo_commit_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,15 @@ func TestRepository_GetBranches(t *testing.T) {
3535
assert.Equal(t, testCase.ExpectedBranches, branches)
3636
}
3737
}
38+
39+
func TestGetTagCommitWithSignature(t *testing.T) {
40+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
41+
bareRepo1, err := OpenRepository(bareRepo1Path)
42+
commit, err := bareRepo1.GetCommit("3ad28a9149a2864384548f3d17ed7f38014c9e8a")
43+
44+
assert.NoError(t, err)
45+
assert.NotNil(t, commit)
46+
assert.NotNil(t, commit.Signature)
47+
// test that signature is not in message
48+
assert.Equal(t, "tag", commit.CommitMessage)
49+
}
Binary file not shown.

tests/repos/repo1_bare/refs/tags/test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3ad28a9149a2864384548f3d17ed7f38014c9e8a

0 commit comments

Comments
 (0)