Skip to content

Commit 22e1bd3

Browse files
kjellkvingelunny
authored andcommitted
commithgraph / timeline (#428)
* Add model and tests for graph * Add route and router for graph * Add assets for graph * Add template for graph
1 parent 35d9378 commit 22e1bd3

File tree

10 files changed

+673
-2
lines changed

10 files changed

+673
-2
lines changed

cmd/web.go

+1
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ func runWeb(ctx *cli.Context) error {
547547
m.Get("/src/*", repo.SetEditorconfigIfExists, repo.Home)
548548
m.Get("/raw/*", repo.SingleDownload)
549549
m.Get("/commits/*", repo.RefCommits)
550+
m.Get("/graph", repo.Graph)
550551
m.Get("/commit/:sha([a-f0-9]{7,40})$", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff)
551552
m.Get("/forks", repo.Forks)
552553
}, context.RepoRef())

models/graph.go

+108
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

+41
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+
}

public/css/gitgraph.css

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
body {font:13.34px/1.4 helvetica,arial,freesans,clean,sans-serif;}
2+
em {font-style:normal;}
3+
4+
#git-graph-container, #rel-container {float:left;}
5+
#git-graph-container {}
6+
#git-graph-container li {list-style-type:none;height:20px;line-height:20px;overflow:hidden;}
7+
#git-graph-container li .node-relation {font-family:'Bitstream Vera Sans Mono', 'Courier', monospace;}
8+
#git-graph-container li .author {color:#666666;}
9+
#git-graph-container li .time {color:#999999;font-size:80%}
10+
#git-graph-container li a {color:#000000;}
11+
#git-graph-container li a:hover {text-decoration:underline;}
12+
#git-graph-container li a em {color:#BB0000;border-bottom:1px dotted #BBBBBB;text-decoration:none;font-style:normal;}
13+
#rev-container {width:80%}
14+
#rev-list {margin:0;padding:0 5px 0 0;width:80%}
15+
#graph-raw-list {margin:0px;}

public/js/draw.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
$(document).ready(function () {
2+
var graphList = [];
3+
4+
if (!document.getElementById('graph-canvas')) {
5+
return;
6+
}
7+
8+
$("#graph-raw-list li span.node-relation").each(function () {
9+
graphList.push($(this).text());
10+
})
11+
12+
gitGraph(document.getElementById('graph-canvas'), graphList);
13+
14+
if ($("#rev-container")) {
15+
$("#rev-container").css("width", document.body.clientWidth - document.getElementById('graph-canvas').width);
16+
}
17+
})

0 commit comments

Comments
 (0)