-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
// | ||
// 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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: