From e39bb2d05aba9eecfa7e93ca42245be2166a741a Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 6 Dec 2022 13:16:47 +0000 Subject: [PATCH] Handle empty author names (#21902) (#22027) Backport #21902 Although git does expect that author names should be of the form: `NAME ` some users have been able to create commits with: `` Fix #21900 Signed-off-by: Andrew Thornton Co-authored-by: Lauris BH Co-authored-by: Lunny Xiao --- modules/git/signature_gogit.go | 5 ++++- modules/git/signature_nogogit.go | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/git/signature_gogit.go b/modules/git/signature_gogit.go index 6f1c98420..5ab38cd85 100644 --- a/modules/git/signature_gogit.go +++ b/modules/git/signature_gogit.go @@ -10,6 +10,7 @@ package git import ( "bytes" "strconv" + "strings" "time" "github.com/go-git/go-git/v5/plumbing/object" @@ -30,7 +31,9 @@ type Signature = object.Signature func newSignatureFromCommitline(line []byte) (_ *Signature, err error) { sig := new(Signature) emailStart := bytes.IndexByte(line, '<') - sig.Name = string(line[:emailStart-1]) + 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]) diff --git a/modules/git/signature_nogogit.go b/modules/git/signature_nogogit.go index 07a3b79f1..3fa5c8da3 100644 --- a/modules/git/signature_nogogit.go +++ b/modules/git/signature_nogogit.go @@ -11,6 +11,7 @@ import ( "bytes" "fmt" "strconv" + "strings" "time" ) @@ -51,7 +52,9 @@ func newSignatureFromCommitline(line []byte) (sig *Signature, err error) { return } - sig.Name = string(line[:emailStart-1]) + if emailStart > 0 { // Empty name has already occurred, even if it shouldn't + sig.Name = strings.TrimSpace(string(line[:emailStart-1])) + } sig.Email = string(line[emailStart+1 : emailEnd]) hasTime := emailEnd+2 < len(line)