Skip to content

Fix no empty input check in newSignatureFromCommitline for gogit version #28849

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

Closed
wants to merge 5 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
35 changes: 28 additions & 7 deletions modules/git/signature_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,59 @@ type Signature = object.Signature
// Helper to get a signature from the commit line, which looks like these:
//
// author Patrick Gundlach <[email protected]> 1378823654 +0200
// author Patrick Gundlach <[email protected]> Thu, 07 Apr 2005 22:13:13 +0200
// author Patrick Gundlach <[email protected]> Thu Apr 07 22:13:13 2005 +0200
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the code is different from nogogit? And it seems that the nogogit function does pretty well on parsing the date/time:

image

//
// but without the "author " at the beginning (this method should)
// be used for author and committer.
//
// FIXME: include timezone for timestamp!
func newSignatureFromCommitline(line []byte) (_ *Signature, err error) {
sig := new(Signature)
emailStart := bytes.IndexByte(line, '<')
emailEnd := bytes.IndexByte(line, '>')
if emailStart == -1 || emailEnd == -1 || emailEnd < emailStart {
return sig, err
}

if emailStart > 0 { // Empty name has already occurred, even if it shouldn't
sig.Name = strings.TrimSpace(string(line[:emailStart-1]))
}
emailEnd := bytes.IndexByte(line, '>')
sig.Email = string(line[emailStart+1 : emailEnd])

// Check date format.
if len(line) > emailEnd+2 {
firstChar := line[emailEnd+2]
if firstChar >= 48 && firstChar <= 57 {
timestop := bytes.IndexByte(line[emailEnd+2:], ' ')
if timestop < 0 {
return sig, nil
}

timestring := string(line[emailEnd+2 : emailEnd+2+timestop])
seconds, _ := strconv.ParseInt(timestring, 10, 64)
sig.When = time.Unix(seconds, 0)

timestop += emailEnd + 3
if timestop >= len(line) || timestop+5 > len(line) {
return sig, nil
}

timezone := string(line[timestop : timestop+5])
tzhours, err1 := strconv.ParseInt(timezone[0:3], 10, 64)
tzmins, err2 := strconv.ParseInt(timezone[3:], 10, 64)
if err1 != nil || err2 != nil {
return sig, err
}
if tzhours < 0 {
tzmins *= -1
}
tz := time.FixedZone("", int(tzhours*60*60+tzmins*60))
sig.When = sig.When.In(tz)

} else {
sig.When, err = time.Parse(GitTimeLayout, string(line[emailEnd+2:]))
if err != nil {
return nil, err
}
}
} else {
// Fall back to unix 0 time
sig.When = time.Unix(0, 0)
}
return sig, nil
}
41 changes: 41 additions & 0 deletions modules/git/signature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package git

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestNewNewSignatureFromCommitline(t *testing.T) {
tz := time.FixedZone("", 2*60*60)

kases := map[string]Signature{
"": {},
"author gitea test <[email protected]>": {
Name: "author gitea test",
Email: "[email protected]",
},
"author gitea test <[email protected]> 1705912028 +0200": {
Name: "author gitea test",
Email: "[email protected]",
When: time.Unix(1705912028, 0).In(tz),
},
"author gitea test <[email protected]> Mon Jan 22 10:27:08 2024 +0200": {
Name: "author gitea test",
Email: "[email protected]",
When: time.Unix(1705912028, 0).In(tz),
},
}

for text, sign := range kases {
newSign, err := newSignatureFromCommitline([]byte(text))
assert.NoError(t, err)
assert.Equal(t, sign.Name, newSign.Name)
assert.Equal(t, sign.Email, newSign.Email)
assert.Equal(t, sign.When, newSign.When)
}
}