From 981116dd548500ca21cda389218445501f146f18 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Fri, 19 Jan 2024 00:40:27 +0000 Subject: [PATCH 1/4] fix --- modules/git/signature_gogit.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/git/signature_gogit.go b/modules/git/signature_gogit.go index c984ad6e20ddc..6d5de0543db6b 100644 --- a/modules/git/signature_gogit.go +++ b/modules/git/signature_gogit.go @@ -30,10 +30,14 @@ type Signature = object.Signature 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. From 88760aacac4a9595bd43e66ee6816b46e0b26915 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 22 Jan 2024 08:47:26 +0000 Subject: [PATCH 2/4] add test --- modules/git/signature_gogit.go | 29 +++++++++++++++---- modules/git/signature_gogit_test.go | 43 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 modules/git/signature_gogit_test.go diff --git a/modules/git/signature_gogit.go b/modules/git/signature_gogit.go index 6d5de0543db6b..1f3c18e517d8c 100644 --- a/modules/git/signature_gogit.go +++ b/modules/git/signature_gogit.go @@ -21,12 +21,10 @@ type Signature = object.Signature // Helper to get a signature from the commit line, which looks like these: // // author Patrick Gundlach 1378823654 +0200 -// author Patrick Gundlach Thu, 07 Apr 2005 22:13:13 +0200 +// author Patrick Gundlach 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, '<') @@ -45,18 +43,37 @@ func newSignatureFromCommitline(line []byte) (_ *Signature, err error) { 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 } diff --git a/modules/git/signature_gogit_test.go b/modules/git/signature_gogit_test.go new file mode 100644 index 0000000000000..1aba913da4a23 --- /dev/null +++ b/modules/git/signature_gogit_test.go @@ -0,0 +1,43 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +//go:build gogit + +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{ + "": Signature{}, + "author gitea test ": Signature{ + Name: "author gitea test", + Email: "test@gitea.com", + }, + "author gitea test 1705912028 +0200": Signature{ + Name: "author gitea test", + Email: "test@gitea.com", + When: time.Unix(1705912028, 0).In(tz), + }, + "author gitea test Mon Jan 22 10:27:08 2024 +0200": Signature{ + Name: "author gitea test", + Email: "test@gitea.com", + 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) + } +} From 14cf5c9b23f104a2366a701424ba033847a82e8d Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 22 Jan 2024 09:02:51 +0000 Subject: [PATCH 3/4] fix lint --- modules/git/signature_gogit_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/git/signature_gogit_test.go b/modules/git/signature_gogit_test.go index 1aba913da4a23..db65129896c54 100644 --- a/modules/git/signature_gogit_test.go +++ b/modules/git/signature_gogit_test.go @@ -16,17 +16,17 @@ func TestNewNewSignatureFromCommitline(t *testing.T) { tz := time.FixedZone("", 2*60*60) kases := map[string]Signature{ - "": Signature{}, - "author gitea test ": Signature{ + "": {}, + "author gitea test ": { Name: "author gitea test", Email: "test@gitea.com", }, - "author gitea test 1705912028 +0200": Signature{ + "author gitea test 1705912028 +0200": { Name: "author gitea test", Email: "test@gitea.com", When: time.Unix(1705912028, 0).In(tz), }, - "author gitea test Mon Jan 22 10:27:08 2024 +0200": Signature{ + "author gitea test Mon Jan 22 10:27:08 2024 +0200": { Name: "author gitea test", Email: "test@gitea.com", When: time.Unix(1705912028, 0).In(tz), From 18d755f97f70942036687c8ab0f57cbec129321a Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 29 Jan 2024 06:52:06 +0000 Subject: [PATCH 4/4] improve --- modules/git/{signature_gogit_test.go => signature_test.go} | 2 -- 1 file changed, 2 deletions(-) rename modules/git/{signature_gogit_test.go => signature_test.go} (98%) diff --git a/modules/git/signature_gogit_test.go b/modules/git/signature_test.go similarity index 98% rename from modules/git/signature_gogit_test.go rename to modules/git/signature_test.go index db65129896c54..45451049cc4c8 100644 --- a/modules/git/signature_gogit_test.go +++ b/modules/git/signature_test.go @@ -1,8 +1,6 @@ // Copyright 2024 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -//go:build gogit - package git import (