Skip to content

Commit 464bbd7

Browse files
GiteaBotsilverwind
andauthored
Fix commit retrieval by tag (#21804) (#23266)
Backport #21804 It is not correct to return tag data when commit data is requested, so remove the hacky code that overwrote parts of a commit with parts of a tag. This fixes commit retrieval by tag for both the latest commit in the UI and the commit info on tag webhook events. Fixes: #21687 Replaces: #21693 <img width="324" alt="Screenshot 2022-11-13 at 15 26 37" src="https://user-images.githubusercontent.com/115237/201526975-736c6ea7-ad6a-467a-a823-9a63d6ecb718.png"> <img width="789" alt="image" src="https://user-images.githubusercontent.com/115237/201526876-90a13ffc-1e5c-4d76-911b-f1ae51e8eaab.png"> Co-authored-by: silverwind <[email protected]>
1 parent 574182e commit 464bbd7

19 files changed

+26
-58
lines changed

modules/git/repo_commit_gogit.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package git
88

99
import (
10-
"fmt"
1110
"strings"
1211

1312
"github.com/go-git/go-git/v5/plumbing"
@@ -67,38 +66,6 @@ func (repo *Repository) IsCommitExist(name string) bool {
6766
return err == nil
6867
}
6968

70-
func convertPGPSignatureForTag(t *object.Tag) *CommitGPGSignature {
71-
if t.PGPSignature == "" {
72-
return nil
73-
}
74-
75-
var w strings.Builder
76-
var err error
77-
78-
if _, err = fmt.Fprintf(&w,
79-
"object %s\ntype %s\ntag %s\ntagger ",
80-
t.Target.String(), t.TargetType.Bytes(), t.Name); err != nil {
81-
return nil
82-
}
83-
84-
if err = t.Tagger.Encode(&w); err != nil {
85-
return nil
86-
}
87-
88-
if _, err = fmt.Fprintf(&w, "\n\n"); err != nil {
89-
return nil
90-
}
91-
92-
if _, err = fmt.Fprintf(&w, t.Message); err != nil {
93-
return nil
94-
}
95-
96-
return &CommitGPGSignature{
97-
Signature: t.PGPSignature,
98-
Payload: strings.TrimSpace(w.String()) + "\n",
99-
}
100-
}
101-
10269
func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
10370
var tagObject *object.Tag
10471

@@ -122,12 +89,6 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
12289
commit := convertCommit(gogitCommit)
12390
commit.repo = repo
12491

125-
if tagObject != nil {
126-
commit.CommitMessage = strings.TrimSpace(tagObject.Message)
127-
commit.Author = &tagObject.Tagger
128-
commit.Signature = convertPGPSignatureForTag(tagObject)
129-
}
130-
13192
tree, err := gogitCommit.Tree()
13293
if err != nil {
13394
return nil, err

modules/git/repo_commit_nogogit.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Co
107107
return nil, err
108108
}
109109

110-
commit.CommitMessage = strings.TrimSpace(tag.Message)
111-
commit.Author = tag.Tagger
112-
commit.Signature = tag.Signature
113-
114110
return commit, nil
115111
case "commit":
116112
commit, err := CommitFromReader(repo, id, io.LimitReader(rd, size))

modules/git/repo_commit_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ func TestGetTagCommitWithSignature(t *testing.T) {
4343
assert.NoError(t, err)
4444
defer bareRepo1.Close()
4545

46-
commit, err := bareRepo1.GetCommit("3ad28a9149a2864384548f3d17ed7f38014c9e8a")
46+
// both the tag and the commit are signed here, this validates only the commit signature
47+
commit, err := bareRepo1.GetCommit("28b55526e7100924d864dd89e35c1ea62e7a5a32")
4748
assert.NoError(t, err)
4849
assert.NotNil(t, commit)
4950
assert.NotNil(t, commit.Signature)
5051
// test that signature is not in message
51-
assert.Equal(t, "tag", commit.CommitMessage)
52+
assert.Equal(t, "signed-commit\n", commit.CommitMessage)
5253
}
5354

5455
func TestGetCommitWithBadCommitID(t *testing.T) {

modules/git/repo_ref_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ func TestRepository_GetRefs(t *testing.T) {
1919
refs, err := bareRepo1.GetRefs()
2020

2121
assert.NoError(t, err)
22-
assert.Len(t, refs, 5)
22+
assert.Len(t, refs, 6)
2323

2424
expectedRefs := []string{
2525
BranchPrefix + "branch1",
2626
BranchPrefix + "branch2",
2727
BranchPrefix + "master",
2828
TagPrefix + "test",
29+
TagPrefix + "signed-tag",
2930
NotesRef,
3031
}
3132

@@ -43,9 +44,12 @@ func TestRepository_GetRefsFiltered(t *testing.T) {
4344
refs, err := bareRepo1.GetRefsFiltered(TagPrefix)
4445

4546
assert.NoError(t, err)
46-
if assert.Len(t, refs, 1) {
47-
assert.Equal(t, TagPrefix+"test", refs[0].Name)
47+
if assert.Len(t, refs, 2) {
48+
assert.Equal(t, TagPrefix+"signed-tag", refs[0].Name)
4849
assert.Equal(t, "tag", refs[0].Type)
49-
assert.Equal(t, "3ad28a9149a2864384548f3d17ed7f38014c9e8a", refs[0].Object.String())
50+
assert.Equal(t, "36f97d9a96457e2bab511db30fe2db03893ebc64", refs[0].Object.String())
51+
assert.Equal(t, TagPrefix+"test", refs[1].Name)
52+
assert.Equal(t, "tag", refs[1].Type)
53+
assert.Equal(t, "3ad28a9149a2864384548f3d17ed7f38014c9e8a", refs[1].Object.String())
5054
}
5155
}

modules/git/repo_stats_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ func TestRepository_GetCodeActivityStats(t *testing.T) {
2424
assert.NoError(t, err)
2525
assert.NotNil(t, code)
2626

27-
assert.EqualValues(t, 9, code.CommitCount)
27+
assert.EqualValues(t, 10, code.CommitCount)
2828
assert.EqualValues(t, 3, code.AuthorCount)
29-
assert.EqualValues(t, 9, code.CommitCountInAllBranches)
29+
assert.EqualValues(t, 10, code.CommitCountInAllBranches)
3030
assert.EqualValues(t, 10, code.Additions)
3131
assert.EqualValues(t, 1, code.Deletions)
3232
assert.Len(t, code.Authors, 3)

modules/git/repo_tag_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ func TestRepository_GetTags(t *testing.T) {
2525
assert.NoError(t, err)
2626
return
2727
}
28-
assert.Len(t, tags, 1)
28+
assert.Len(t, tags, 2)
2929
assert.Equal(t, len(tags), total)
30-
assert.EqualValues(t, "test", tags[0].Name)
31-
assert.EqualValues(t, "3ad28a9149a2864384548f3d17ed7f38014c9e8a", tags[0].ID.String())
30+
assert.EqualValues(t, "signed-tag", tags[0].Name)
31+
assert.EqualValues(t, "36f97d9a96457e2bab511db30fe2db03893ebc64", tags[0].ID.String())
3232
assert.EqualValues(t, "tag", tags[0].Type)
33+
assert.EqualValues(t, "test", tags[1].Name)
34+
assert.EqualValues(t, "3ad28a9149a2864384548f3d17ed7f38014c9e8a", tags[1].ID.String())
35+
assert.EqualValues(t, "tag", tags[1].Type)
3336
}
3437

3538
func TestRepository_GetTag(t *testing.T) {

modules/git/repo_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ func TestGetLatestCommitTime(t *testing.T) {
1414
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
1515
lct, err := GetLatestCommitTime(DefaultContext, bareRepo1Path)
1616
assert.NoError(t, err)
17-
// Time is Sun Jul 21 22:43:13 2019 +0200
17+
// Time is Sun Nov 13 16:40:14 2022 +0100
1818
// which is the time of commit
19-
// feaf4ba6bc635fec442f46ddd4512416ec43c2c2 (refs/heads/master)
20-
assert.EqualValues(t, 1563741793, lct.Unix())
19+
// ce064814f4a0d337b333e646ece456cd39fab612 (refs/heads/master)
20+
assert.EqualValues(t, 1668354014, lct.Unix())
2121
}
2222

2323
func TestRepoIsEmpty(t *testing.T) {
65 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
37991dec2c8e592043f47155ce4808d4580f9123 feaf4ba6bc635fec442f46ddd4512416ec43c2c2 silverwind <[email protected]> 1563741799 +0200 push
2+
feaf4ba6bc635fec442f46ddd4512416ec43c2c2 ce064814f4a0d337b333e646ece456cd39fab612 silverwind <[email protected]> 1668354026 +0100 push
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
37991dec2c8e592043f47155ce4808d4580f9123 feaf4ba6bc635fec442f46ddd4512416ec43c2c2 silverwind <[email protected]> 1563741799 +0200 push
2+
feaf4ba6bc635fec442f46ddd4512416ec43c2c2 ce064814f4a0d337b333e646ece456cd39fab612 silverwind <[email protected]> 1668354026 +0100 push
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
feaf4ba6bc635fec442f46ddd4512416ec43c2c2
1+
ce064814f4a0d337b333e646ece456cd39fab612
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
36f97d9a96457e2bab511db30fe2db03893ebc64

0 commit comments

Comments
 (0)