Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ type CommitGPGSignature struct {
}

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

Expand Down
17 changes: 15 additions & 2 deletions repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,28 @@ l:
}
commit.Committer = sig
case "gpgsig":
sig, err := newGPGSignatureFromCommitline(data, nextline+spacepos+1)
sig, err := newGPGSignatureFromCommitline(data, nextline+spacepos+1, false)
if err != nil {
return nil, err
}
commit.Signature = sig
}
nextline += eol + 1
case eol == 0:
commit.CommitMessage = string(data[nextline+1:])
cm := string(data[nextline+1:])

// Tag GPG signatures are stored below the commit message
sigindex := strings.Index(cm, "-----BEGIN PGP SIGNATURE-----")
if sigindex != -1 {
sig, err := newGPGSignatureFromCommitline(data, (nextline+1)+sigindex, true)
if err == nil && sig != nil {
// remove signature from commit message
cm = cm[:sigindex-1]
commit.Signature = sig
}
}

commit.CommitMessage = cm
break l
default:
break l
Expand Down
12 changes: 12 additions & 0 deletions repo_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ func TestRepository_GetBranches(t *testing.T) {
assert.Equal(t, testCase.ExpectedBranches, branches)
}
}

func TestGetTagCommitWithSignature(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
commit, err := bareRepo1.GetCommit("3ad28a9149a2864384548f3d17ed7f38014c9e8a")

assert.NoError(t, err)
assert.NotNil(t, commit)
assert.NotNil(t, commit.Signature)
// test that signature is not in message
assert.Equal(t, "tag", commit.CommitMessage)
}
Binary file not shown.
1 change: 1 addition & 0 deletions tests/repos/repo1_bare/refs/tags/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3ad28a9149a2864384548f3d17ed7f38014c9e8a