Skip to content

Commit a8a9cf5

Browse files
committed
Add model and tests for graph
1 parent 7c46667 commit a8a9cf5

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

models/graph.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2016 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
11+
"code.gitea.io/git"
12+
)
13+
14+
// GraphItem represent one commit, or one relation in timeline
15+
type GraphItem struct {
16+
GraphAcii string
17+
Relation string
18+
Branch string
19+
Rev string
20+
Date string
21+
Author string
22+
AuthorEmail string
23+
ShortRev string
24+
Subject string
25+
OnlyRelation bool
26+
}
27+
28+
// GraphItems is a list of commits from all branches
29+
type GraphItems []GraphItem
30+
31+
// GetCommitGraph return a list of commit (GraphItems) from all branches
32+
func GetCommitGraph(r *git.Repository) (GraphItems, error) {
33+
34+
var Commitgraph []GraphItem
35+
36+
format := "DATA:|%d|%H|%ad|%an|%ae|%h|%s"
37+
38+
graphCmd := git.NewCommand("log")
39+
graphCmd.AddArguments("--graph",
40+
"--date-order",
41+
"--all",
42+
"-C",
43+
"-M",
44+
"-n 100",
45+
"--date=iso",
46+
fmt.Sprintf("--pretty=format:%s", format),
47+
)
48+
graph, err := graphCmd.RunInDir(r.Path)
49+
if err != nil {
50+
return Commitgraph, err
51+
}
52+
53+
Commitgraph = make([]GraphItem, 0, 100)
54+
for _, s := range strings.Split(graph, "\n") {
55+
GraphItem, err := graphItemFromString(s, r)
56+
if err != nil {
57+
return Commitgraph, err
58+
}
59+
Commitgraph = append(Commitgraph, GraphItem)
60+
}
61+
62+
return Commitgraph, nil
63+
}
64+
65+
func graphItemFromString(s string, r *git.Repository) (GraphItem, error) {
66+
67+
var ascii string
68+
var data = "|||||||"
69+
lines := strings.Split(s, "DATA:")
70+
71+
switch len(lines) {
72+
case 1:
73+
ascii = lines[0]
74+
case 2:
75+
ascii = lines[0]
76+
data = lines[1]
77+
default:
78+
return GraphItem{}, fmt.Errorf("Failed parsing grap line:%s. Expect 1 or two fields", s)
79+
}
80+
81+
rows := strings.Split(data, "|")
82+
if len(rows) != 8 {
83+
return GraphItem{}, fmt.Errorf("Failed parsing grap line:%s - Should containt 8 datafields", s)
84+
}
85+
86+
/* // see format in getCommitGraph()
87+
0 Relation string
88+
1 Branch string
89+
2 Rev string
90+
3 Date string
91+
4 Author string
92+
5 AuthorEmail string
93+
6 ShortRev string
94+
7 Subject string
95+
*/
96+
gi := GraphItem{ascii,
97+
rows[0],
98+
rows[1],
99+
rows[2],
100+
rows[3],
101+
rows[4],
102+
rows[5],
103+
rows[6],
104+
rows[7],
105+
len(rows[2]) == 0, // no commits refered to, only relation in current line.
106+
}
107+
return gi, nil
108+
}

models/graph_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2016 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
import (
8+
"testing"
9+
10+
"code.gitea.io/git"
11+
)
12+
13+
func BenchmarkGetCommitGraph(b *testing.B) {
14+
15+
currentRepo, err := git.OpenRepository(".")
16+
if err != nil {
17+
b.Error("Could not open repository")
18+
}
19+
20+
graph, err := GetCommitGraph(currentRepo)
21+
if err != nil {
22+
b.Error("Could get commit graph")
23+
}
24+
25+
if len(graph) < 100 {
26+
b.Error("Should get 100 log lines.")
27+
}
28+
}
29+
30+
func BenchmarkParseCommitString(b *testing.B) {
31+
testString := "* DATA:||4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|Kjell Kvinge|[email protected]|4e61bac|Add route for graph"
32+
33+
graphItem, err := graphItemFromString(testString, nil)
34+
if err != nil {
35+
b.Error("could not parse teststring")
36+
}
37+
38+
if graphItem.Author != "Kjell Kvinge" {
39+
b.Error("Did not get expected data")
40+
}
41+
}

0 commit comments

Comments
 (0)