Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tag commit message #21693

Closed
wants to merge 7 commits into from
Closed
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
42 changes: 8 additions & 34 deletions modules/git/repo_commit_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package git

import (
"fmt"
"strings"

"github.com/go-git/go-git/v5/plumbing"
Expand Down Expand Up @@ -68,38 +67,6 @@ func (repo *Repository) IsCommitExist(name string) bool {
return err == nil
}

func convertPGPSignatureForTag(t *object.Tag) *CommitGPGSignature {
if t.PGPSignature == "" {
return nil
}

var w strings.Builder
var err error

if _, err = fmt.Fprintf(&w,
"object %s\ntype %s\ntag %s\ntagger ",
t.Target.String(), t.TargetType.Bytes(), t.Name); err != nil {
return nil
}

if err = t.Tagger.Encode(&w); err != nil {
return nil
}

if _, err = fmt.Fprintf(&w, "\n\n"); err != nil {
return nil
}

if _, err = fmt.Fprintf(&w, t.Message); err != nil {
return nil
}

return &CommitGPGSignature{
Signature: t.PGPSignature,
Payload: strings.TrimSpace(w.String()) + "\n",
}
}

func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
var tagObject *object.Tag

Expand All @@ -112,6 +79,13 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
}
}
if err == nil {
tagObject.Message = strings.TrimSpace(tagObject.Message)
if tagObject.Message == "" {
tagObject.Message = tagObject.Name
} else if tagObject.Name != "" {
tagObject.Message = tagObject.Name + "\n\n" + tagObject.Message
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I think we should not alter the tag message.


gogitCommit, err = repo.gogitRepo.CommitObject(tagObject.Target)
}
// if we get a plumbing.ErrObjectNotFound here then the repository is broken and it should be 500
Expand All @@ -124,7 +98,7 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
commit.repo = repo

if tagObject != nil {
commit.CommitMessage = strings.TrimSpace(tagObject.Message)
commit.CommitMessage = tagObject.Message
commit.Author = &tagObject.Tagger
commit.Signature = convertPGPSignatureForTag(tagObject)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/git/repo_commit_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Co
return nil, err
}

commit.CommitMessage = strings.TrimSpace(tag.Message)
commit.CommitMessage = tag.Message
commit.Author = tag.Tagger
commit.Signature = tag.Signature
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we overwriting the commit's data here with incorrect data from the tag? I think this is the root of the problem and I guess if we just remove these three lines, the issue may be fixed.


Expand Down
2 changes: 1 addition & 1 deletion modules/git/repo_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestGetTagCommitWithSignature(t *testing.T) {
assert.NotNil(t, commit)
assert.NotNil(t, commit.Signature)
// test that signature is not in message
assert.Equal(t, "tag", commit.CommitMessage)
assert.Equal(t, "test\n\ntag", commit.CommitMessage)
}

func TestGetCommitWithBadCommitID(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions modules/git/repo_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ func parseTagRef(ref map[string]string) (tag *Tag, err error) {
}
}

tag.Message = strings.TrimSpace(tag.Message)
if tag.Message == "" {
tag.Message = tag.Name
} else if tag.Name != "" {
tag.Message = tag.Name + "\n\n" + tag.Message
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this joining is incorrect, we should not alter the tag message. What was wrong was just the retrieval of the tagged commit message.


return tag, nil
}

Expand Down
1 change: 0 additions & 1 deletion modules/git/repo_tag_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ func (repo *Repository) getTag(tagID SHA1, name string) (*Tag, error) {
return nil, err
}

tag.Name = name
tag.ID = tagID
tag.Type = tp

Expand Down
10 changes: 10 additions & 0 deletions modules/git/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ l:
case "type":
// A commit can have one or more parents
tag.Type = string(line[spacepos+1:])
case "tag":
tag.Name = string(line[spacepos+1:])
case "tagger":
sig, err := newSignatureFromCommitline(line[spacepos+1:])
if err != nil {
Expand All @@ -83,6 +85,14 @@ l:
tag.Message = tag.Message[:idx+1]
}
}

tag.Message = strings.TrimSpace(tag.Message)
if tag.Message == "" {
tag.Message = tag.Name
} else if tag.Name != "" {
tag.Message = tag.Name + "\n\n" + tag.Message
}

return tag, nil
}

Expand Down
8 changes: 4 additions & 4 deletions modules/git/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ tag 1.22.0
tagger Lucas Michot <[email protected]> 1484491741 +0100

`), tag: Tag{
Name: "",
Name: "1.22.0",
ID: SHA1{},
Object: SHA1{0x3b, 0x11, 0x4a, 0xb8, 0x0, 0xc6, 0x43, 0x2a, 0xd4, 0x23, 0x87, 0xcc, 0xf6, 0xbc, 0x8d, 0x43, 0x88, 0xa2, 0x88, 0x5a},
Type: "commit",
Tagger: &Signature{Name: "Lucas Michot", Email: "[email protected]", When: time.Unix(1484491741, 0)},
Message: "",
Message: "1.22.0",
Signature: nil,
}},
{data: []byte(`object 7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc
Expand All @@ -39,12 +39,12 @@ test message
o

ono`), tag: Tag{
Name: "",
Name: "1.22.1",
ID: SHA1{},
Object: SHA1{0x7c, 0xdf, 0x42, 0xc0, 0xb1, 0xcc, 0x76, 0x3a, 0xb7, 0xe4, 0xc3, 0x3c, 0x47, 0xa2, 0x4e, 0x27, 0xc6, 0x6b, 0xfc, 0xcc},
Type: "commit",
Tagger: &Signature{Name: "Lucas Michot", Email: "[email protected]", When: time.Unix(1484553735, 0)},
Message: "test message\no\n\nono",
Message: "1.22.1\n\ntest message\no\n\nono",
Signature: nil,
}},
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/api_repo_git_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestAPIGitTags(t *testing.T) {
assert.Equal(t, aTagName, tag.Tag)
assert.Equal(t, aTag.ID.String(), tag.SHA)
assert.Equal(t, commit.ID.String(), tag.Object.SHA)
assert.Equal(t, aTagMessage+"\n", tag.Message)
assert.Equal(t, aTagName+"\n\n"+aTagMessage, tag.Message)
assert.Equal(t, user.Name, tag.Tagger.Name)
assert.Equal(t, user.Email, tag.Tagger.Email)
assert.Equal(t, util.URLJoin(repo.APIURL(), "git/tags", aTag.ID.String()), tag.URL)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/api_repo_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAPIRepoTags(t *testing.T) {

assert.Len(t, tags, 1)
assert.Equal(t, "v1.1", tags[0].Name)
assert.Equal(t, "Initial commit", tags[0].Message)
assert.Equal(t, "v1.1\n\nInitial commit", tags[0].Message)
assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", tags[0].Commit.SHA)
assert.Equal(t, setting.AppURL+"api/v1/repos/user2/repo1/git/commits/65f1bf27bc3bf70f64657658635e66094edbcb4d", tags[0].Commit.URL)
assert.Equal(t, setting.AppURL+"user2/repo1/archive/v1.1.zip", tags[0].ZipballURL)
Expand All @@ -49,7 +49,7 @@ func TestAPIRepoTags(t *testing.T) {
if tag.Name != "v1.1" {
assert.EqualValues(t, newTag.Name, tag.Name)
assert.EqualValues(t, newTag.Message, tag.Message)
assert.EqualValues(t, "nice!\nand some text", tag.Message)
assert.EqualValues(t, "gitea/22\n\nnice!\nand some text", tag.Message)
assert.EqualValues(t, newTag.Commit.SHA, tag.Commit.SHA)
}
}
Expand Down