aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorKjell Kvinge <kjell@kvinge.biz>2016-12-29 00:44:32 +0100
committerLunny Xiao <xiaolunwen@gmail.com>2016-12-29 07:44:32 +0800
commit22e1bd31c68586e963262db964d6a83f6115e56f (patch)
tree39d669cd4b982063512320e91ed359357a518f1f /models
parent35d9378e4e1b3b1c15db3a7e7237a55fa96919a1 (diff)
downloadgitea-22e1bd31c68586e963262db964d6a83f6115e56f.tar.gz
gitea-22e1bd31c68586e963262db964d6a83f6115e56f.zip
commithgraph / timeline (#428)
* Add model and tests for graph * Add route and router for graph * Add assets for graph * Add template for graph
Diffstat (limited to 'models')
-rw-r--r--models/graph.go108
-rw-r--r--models/graph_test.go41
2 files changed, 149 insertions, 0 deletions
diff --git a/models/graph.go b/models/graph.go
new file mode 100644
index 0000000000..973476a746
--- /dev/null
+++ b/models/graph.go
@@ -0,0 +1,108 @@
+// 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/git"
+)
+
+// 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) (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",
+ "-n 100",
+ "--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.Split(s, "DATA:")
+
+ 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.Split(data, "|")
+ 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 refered to, only relation in current line.
+ }
+ return gi, nil
+}
diff --git a/models/graph_test.go b/models/graph_test.go
new file mode 100644
index 0000000000..23d8aa8492
--- /dev/null
+++ b/models/graph_test.go
@@ -0,0 +1,41 @@
+// 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 (
+ "testing"
+
+ "code.gitea.io/git"
+)
+
+func BenchmarkGetCommitGraph(b *testing.B) {
+
+ currentRepo, err := git.OpenRepository(".")
+ if err != nil {
+ b.Error("Could not open repository")
+ }
+
+ graph, err := GetCommitGraph(currentRepo)
+ 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"
+
+ 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")
+ }
+}