2016-12-29 05:14:32 +05:30
|
|
|
// 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"
|
2018-07-23 19:42:06 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2016-12-29 05:14:32 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
2017-01-05 06:20:34 +05:30
|
|
|
var CommitGraph []GraphItem
|
2016-12-29 05:14:32 +05:30
|
|
|
|
|
|
|
format := "DATA:|%d|%H|%ad|%an|%ae|%h|%s"
|
|
|
|
|
|
|
|
graphCmd := git.NewCommand("log")
|
|
|
|
graphCmd.AddArguments("--graph",
|
|
|
|
"--date-order",
|
|
|
|
"--all",
|
|
|
|
"-C",
|
|
|
|
"-M",
|
2018-07-23 19:42:06 +05:30
|
|
|
fmt.Sprintf("-n %d", setting.UI.GraphMaxCommitNum),
|
2016-12-29 05:14:32 +05:30
|
|
|
"--date=iso",
|
|
|
|
fmt.Sprintf("--pretty=format:%s", format),
|
|
|
|
)
|
|
|
|
graph, err := graphCmd.RunInDir(r.Path)
|
|
|
|
if err != nil {
|
2017-01-05 06:20:34 +05:30
|
|
|
return CommitGraph, err
|
2016-12-29 05:14:32 +05:30
|
|
|
}
|
|
|
|
|
2017-01-05 06:20:34 +05:30
|
|
|
CommitGraph = make([]GraphItem, 0, 100)
|
2016-12-29 05:14:32 +05:30
|
|
|
for _, s := range strings.Split(graph, "\n") {
|
|
|
|
GraphItem, err := graphItemFromString(s, r)
|
|
|
|
if err != nil {
|
2017-01-05 06:20:34 +05:30
|
|
|
return CommitGraph, err
|
2016-12-29 05:14:32 +05:30
|
|
|
}
|
2017-01-05 06:20:34 +05:30
|
|
|
CommitGraph = append(CommitGraph, GraphItem)
|
2016-12-29 05:14:32 +05:30
|
|
|
}
|
|
|
|
|
2017-01-05 06:20:34 +05:30
|
|
|
return CommitGraph, nil
|
2016-12-29 05:14:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func graphItemFromString(s string, r *git.Repository) (GraphItem, error) {
|
|
|
|
|
|
|
|
var ascii string
|
|
|
|
var data = "|||||||"
|
2018-07-21 23:47:10 +05:30
|
|
|
lines := strings.SplitN(s, "DATA:", 2)
|
2016-12-29 05:14:32 +05:30
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-03-11 09:31:38 +05:30
|
|
|
rows := strings.SplitN(data, "|", 8)
|
|
|
|
if len(rows) < 8 {
|
2016-12-29 05:14:32 +05:30
|
|
|
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],
|
2017-01-05 06:20:34 +05:30
|
|
|
len(rows[2]) == 0, // no commits referred to, only relation in current line.
|
2016-12-29 05:14:32 +05:30
|
|
|
}
|
|
|
|
return gi, nil
|
|
|
|
}
|