From 11208f33818ae3fe2f32581285c358b5cfcf5992 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 16 Nov 2019 08:47:57 +0800 Subject: Move git graph from models to modules/graph (#9027) --- models/graph.go | 110 ----------------------------------------- models/graph_test.go | 76 ---------------------------- modules/gitgraph/graph.go | 110 +++++++++++++++++++++++++++++++++++++++++ modules/gitgraph/graph_test.go | 76 ++++++++++++++++++++++++++++ routers/repo/commit.go | 3 +- 5 files changed, 188 insertions(+), 187 deletions(-) delete mode 100644 models/graph.go delete mode 100644 models/graph_test.go create mode 100644 modules/gitgraph/graph.go create mode 100644 modules/gitgraph/graph_test.go diff --git a/models/graph.go b/models/graph.go deleted file mode 100644 index 0efb51b3fc..0000000000 --- a/models/graph.go +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2016 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package models - -import ( - "fmt" - "strings" - - "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/setting" -) - -// GraphItem represent one commit, or one relation in timeline -type GraphItem struct { - GraphAcii string - Relation string - Branch string - Rev string - Date string - Author string - AuthorEmail string - ShortRev string - Subject string - OnlyRelation bool -} - -// GraphItems is a list of commits from all branches -type GraphItems []GraphItem - -// GetCommitGraph return a list of commit (GraphItems) from all branches -func GetCommitGraph(r *git.Repository, page int) (GraphItems, error) { - - var CommitGraph []GraphItem - - format := "DATA:|%d|%H|%ad|%an|%ae|%h|%s" - - graphCmd := git.NewCommand("log") - graphCmd.AddArguments("--graph", - "--date-order", - "--all", - "-C", - "-M", - fmt.Sprintf("-n %d", setting.UI.GraphMaxCommitNum), - fmt.Sprintf("--skip=%d", setting.UI.GraphMaxCommitNum*(page-1)), - "--date=iso", - fmt.Sprintf("--pretty=format:%s", format), - ) - graph, err := graphCmd.RunInDir(r.Path) - if err != nil { - return CommitGraph, err - } - - CommitGraph = make([]GraphItem, 0, 100) - for _, s := range strings.Split(graph, "\n") { - GraphItem, err := graphItemFromString(s, r) - if err != nil { - return CommitGraph, err - } - CommitGraph = append(CommitGraph, GraphItem) - } - - return CommitGraph, nil -} - -func graphItemFromString(s string, r *git.Repository) (GraphItem, error) { - - var ascii string - var data = "|||||||" - lines := strings.SplitN(s, "DATA:", 2) - - switch len(lines) { - case 1: - ascii = lines[0] - case 2: - ascii = lines[0] - data = lines[1] - default: - return GraphItem{}, fmt.Errorf("Failed parsing grap line:%s. Expect 1 or two fields", s) - } - - rows := strings.SplitN(data, "|", 8) - if len(rows) < 8 { - return GraphItem{}, fmt.Errorf("Failed parsing grap line:%s - Should containt 8 datafields", s) - } - - /* // see format in getCommitGraph() - 0 Relation string - 1 Branch string - 2 Rev string - 3 Date string - 4 Author string - 5 AuthorEmail string - 6 ShortRev string - 7 Subject string - */ - gi := GraphItem{ascii, - rows[0], - rows[1], - rows[2], - rows[3], - rows[4], - rows[5], - rows[6], - rows[7], - len(rows[2]) == 0, // no commits referred to, only relation in current line. - } - return gi, nil -} diff --git a/models/graph_test.go b/models/graph_test.go deleted file mode 100644 index 78bfcb27ec..0000000000 --- a/models/graph_test.go +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2016 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package models - -import ( - "fmt" - "testing" - - "code.gitea.io/gitea/modules/git" -) - -func BenchmarkGetCommitGraph(b *testing.B) { - - currentRepo, err := git.OpenRepository(".") - if err != nil { - b.Error("Could not open repository") - } - defer currentRepo.Close() - - for i := 0; i < b.N; i++ { - graph, err := GetCommitGraph(currentRepo, 1) - if err != nil { - b.Error("Could get commit graph") - } - - if len(graph) < 100 { - b.Error("Should get 100 log lines.") - } - } -} - -func BenchmarkParseCommitString(b *testing.B) { - testString := "* DATA:||4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|Kjell Kvinge|kjell@kvinge.biz|4e61bac|Add route for graph" - - for i := 0; i < b.N; i++ { - graphItem, err := graphItemFromString(testString, nil) - if err != nil { - b.Error("could not parse teststring") - } - - if graphItem.Author != "Kjell Kvinge" { - b.Error("Did not get expected data") - } - } -} - -func TestCommitStringParsing(t *testing.T) { - dataFirstPart := "* DATA:||4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|Author|user@mail.something|4e61bac|" - tests := []struct { - shouldPass bool - testName string - commitMessage string - }{ - {true, "normal", "not a fancy message"}, - {true, "extra pipe", "An extra pipe: |"}, - {true, "extra 'Data:'", "DATA: might be trouble"}, - } - - for _, test := range tests { - - t.Run(test.testName, func(t *testing.T) { - testString := fmt.Sprintf("%s%s", dataFirstPart, test.commitMessage) - graphItem, err := graphItemFromString(testString, nil) - if err != nil && test.shouldPass { - t.Errorf("Could not parse %s", testString) - return - } - - if test.commitMessage != graphItem.Subject { - t.Errorf("%s does not match %s", test.commitMessage, graphItem.Subject) - } - }) - } -} diff --git a/modules/gitgraph/graph.go b/modules/gitgraph/graph.go new file mode 100644 index 0000000000..0dd68ad5c5 --- /dev/null +++ b/modules/gitgraph/graph.go @@ -0,0 +1,110 @@ +// Copyright 2016 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gitgraph + +import ( + "fmt" + "strings" + + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/setting" +) + +// GraphItem represent one commit, or one relation in timeline +type GraphItem struct { + GraphAcii string + Relation string + Branch string + Rev string + Date string + Author string + AuthorEmail string + ShortRev string + Subject string + OnlyRelation bool +} + +// GraphItems is a list of commits from all branches +type GraphItems []GraphItem + +// GetCommitGraph return a list of commit (GraphItems) from all branches +func GetCommitGraph(r *git.Repository, page int) (GraphItems, error) { + + var CommitGraph []GraphItem + + format := "DATA:|%d|%H|%ad|%an|%ae|%h|%s" + + graphCmd := git.NewCommand("log") + graphCmd.AddArguments("--graph", + "--date-order", + "--all", + "-C", + "-M", + fmt.Sprintf("-n %d", setting.UI.GraphMaxCommitNum), + fmt.Sprintf("--skip=%d", setting.UI.GraphMaxCommitNum*(page-1)), + "--date=iso", + fmt.Sprintf("--pretty=format:%s", format), + ) + graph, err := graphCmd.RunInDir(r.Path) + if err != nil { + return CommitGraph, err + } + + CommitGraph = make([]GraphItem, 0, 100) + for _, s := range strings.Split(graph, "\n") { + GraphItem, err := graphItemFromString(s, r) + if err != nil { + return CommitGraph, err + } + CommitGraph = append(CommitGraph, GraphItem) + } + + return CommitGraph, nil +} + +func graphItemFromString(s string, r *git.Repository) (GraphItem, error) { + + var ascii string + var data = "|||||||" + lines := strings.SplitN(s, "DATA:", 2) + + switch len(lines) { + case 1: + ascii = lines[0] + case 2: + ascii = lines[0] + data = lines[1] + default: + return GraphItem{}, fmt.Errorf("Failed parsing grap line:%s. Expect 1 or two fields", s) + } + + rows := strings.SplitN(data, "|", 8) + if len(rows) < 8 { + return GraphItem{}, fmt.Errorf("Failed parsing grap line:%s - Should containt 8 datafields", s) + } + + /* // see format in getCommitGraph() + 0 Relation string + 1 Branch string + 2 Rev string + 3 Date string + 4 Author string + 5 AuthorEmail string + 6 ShortRev string + 7 Subject string + */ + gi := GraphItem{ascii, + rows[0], + rows[1], + rows[2], + rows[3], + rows[4], + rows[5], + rows[6], + rows[7], + len(rows[2]) == 0, // no commits referred to, only relation in current line. + } + return gi, nil +} diff --git a/modules/gitgraph/graph_test.go b/modules/gitgraph/graph_test.go new file mode 100644 index 0000000000..a2c7f447b6 --- /dev/null +++ b/modules/gitgraph/graph_test.go @@ -0,0 +1,76 @@ +// Copyright 2016 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gitgraph + +import ( + "fmt" + "testing" + + "code.gitea.io/gitea/modules/git" +) + +func BenchmarkGetCommitGraph(b *testing.B) { + + currentRepo, err := git.OpenRepository(".") + if err != nil { + b.Error("Could not open repository") + } + defer currentRepo.Close() + + for i := 0; i < b.N; i++ { + graph, err := GetCommitGraph(currentRepo, 1) + if err != nil { + b.Error("Could get commit graph") + } + + if len(graph) < 100 { + b.Error("Should get 100 log lines.") + } + } +} + +func BenchmarkParseCommitString(b *testing.B) { + testString := "* DATA:||4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|Kjell Kvinge|kjell@kvinge.biz|4e61bac|Add route for graph" + + for i := 0; i < b.N; i++ { + graphItem, err := graphItemFromString(testString, nil) + if err != nil { + b.Error("could not parse teststring") + } + + if graphItem.Author != "Kjell Kvinge" { + b.Error("Did not get expected data") + } + } +} + +func TestCommitStringParsing(t *testing.T) { + dataFirstPart := "* DATA:||4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|Author|user@mail.something|4e61bac|" + tests := []struct { + shouldPass bool + testName string + commitMessage string + }{ + {true, "normal", "not a fancy message"}, + {true, "extra pipe", "An extra pipe: |"}, + {true, "extra 'Data:'", "DATA: might be trouble"}, + } + + for _, test := range tests { + + t.Run(test.testName, func(t *testing.T) { + testString := fmt.Sprintf("%s%s", dataFirstPart, test.commitMessage) + graphItem, err := graphItemFromString(testString, nil) + if err != nil && test.shouldPass { + t.Errorf("Could not parse %s", testString) + return + } + + if test.commitMessage != graphItem.Subject { + t.Errorf("%s does not match %s", test.commitMessage, graphItem.Subject) + } + }) + } +} diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 2a123a2eb9..084c495174 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitgraph" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/services/gitdiff" @@ -99,7 +100,7 @@ func Graph(ctx *context.Context) { page := ctx.QueryInt("page") - graph, err := models.GetCommitGraph(ctx.Repo.GitRepo, page) + graph, err := gitgraph.GetCommitGraph(ctx.Repo.GitRepo, page) if err != nil { ctx.ServerError("GetCommitGraph", err) return -- cgit v1.2.3