From a1762a6f9b711f2a098028f2f6882c179c5a0518 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sun, 10 Nov 2024 03:59:40 +0100 Subject: [PATCH] fix: Proper paring of date for git commits - Properly parse the date of the git commit and pass that around. --- modules/gitgraph/graph.go | 2 +- modules/gitgraph/graph_models.go | 12 +++++++++--- modules/gitgraph/graph_test.go | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/modules/gitgraph/graph.go b/modules/gitgraph/graph.go index 331ad6b218..4db5598015 100644 --- a/modules/gitgraph/graph.go +++ b/modules/gitgraph/graph.go @@ -16,7 +16,7 @@ import ( // GetCommitGraph return a list of commit (GraphItems) from all branches func GetCommitGraph(r *git.Repository, page, maxAllowedColors int, hidePRRefs bool, branches, files []string) (*Graph, error) { - format := "DATA:%D|%H|%ad|%h|%s" + format := "DATA:%D|%H|%aD|%h|%s" if page == 0 { page = 1 diff --git a/modules/gitgraph/graph_models.go b/modules/gitgraph/graph_models.go index 82f460ecf0..8ff1a6e916 100644 --- a/modules/gitgraph/graph_models.go +++ b/modules/gitgraph/graph_models.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "strings" + "time" asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/db" @@ -198,6 +199,11 @@ func NewCommit(row, column int, line []byte) (*Commit, error) { if len(data) < 5 { return nil, fmt.Errorf("malformed data section on line %d with commit: %s", row, string(line)) } + // Format is a slight modifcation from RFC1123Z + t, err := time.Parse("Mon, _2 Jan 2006 15:04:05 -0700", string(data[2])) + if err != nil { + return nil, fmt.Errorf("could not parse date of commit: %w", err) + } return &Commit{ Row: row, Column: column, @@ -205,8 +211,8 @@ func NewCommit(row, column int, line []byte) (*Commit, error) { Refs: newRefsFromRefNames(data[0]), // 1 matches git log --pretty=format:%H => commit hash Rev: string(data[1]), - // 2 matches git log --pretty=format:%ad => author date (format respects --date= option) - Date: string(data[2]), + // 2 matches git log --pretty=format:%aD => author date, RFC2822 style + Date: t, // 3 matches git log --pretty=format:%h => abbreviated commit hash ShortRev: string(data[3]), // 4 matches git log --pretty=format:%s => subject @@ -245,7 +251,7 @@ type Commit struct { Column int Refs []git.Reference Rev string - Date string + Date time.Time ShortRev string Subject string } diff --git a/modules/gitgraph/graph_test.go b/modules/gitgraph/graph_test.go index 18d427acd9..e7e437e42d 100644 --- a/modules/gitgraph/graph_test.go +++ b/modules/gitgraph/graph_test.go @@ -241,7 +241,7 @@ func TestParseGlyphs(t *testing.T) { } func TestCommitStringParsing(t *testing.T) { - dataFirstPart := "* DATA:|4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|4e61bac|" + dataFirstPart := "* DATA:|4e61bacab44e9b4730e44a6615d04098dd3a8eaf|Tue, 20 Dec 2016 21:10:41 +0100|4e61bac|" tests := []struct { shouldPass bool testName string