You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.4 KiB
111 lines
2.4 KiB
8 years ago
|
// 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.
|
||
|
|
||
5 years ago
|
package gitgraph
|
||
8 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
6 years ago
|
"code.gitea.io/gitea/modules/git"
|
||
7 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
8 years ago
|
)
|
||
|
|
||
|
// 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
|
||
5 years ago
|
func GetCommitGraph(r *git.Repository, page int) (GraphItems, error) {
|
||
8 years ago
|
|
||
8 years ago
|
var CommitGraph []GraphItem
|
||
8 years ago
|
|
||
|
format := "DATA:|%d|%H|%ad|%an|%ae|%h|%s"
|
||
|
|
||
|
graphCmd := git.NewCommand("log")
|
||
|
graphCmd.AddArguments("--graph",
|
||
|
"--date-order",
|
||
|
"--all",
|
||
|
"-C",
|
||
|
"-M",
|
||
7 years ago
|
fmt.Sprintf("-n %d", setting.UI.GraphMaxCommitNum),
|
||
5 years ago
|
fmt.Sprintf("--skip=%d", setting.UI.GraphMaxCommitNum*(page-1)),
|
||
8 years ago
|
"--date=iso",
|
||
|
fmt.Sprintf("--pretty=format:%s", format),
|
||
|
)
|
||
|
graph, err := graphCmd.RunInDir(r.Path)
|
||
|
if err != nil {
|
||
8 years ago
|
return CommitGraph, err
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
CommitGraph = make([]GraphItem, 0, 100)
|
||
8 years ago
|
for _, s := range strings.Split(graph, "\n") {
|
||
|
GraphItem, err := graphItemFromString(s, r)
|
||
|
if err != nil {
|
||
8 years ago
|
return CommitGraph, err
|
||
8 years ago
|
}
|
||
8 years ago
|
CommitGraph = append(CommitGraph, GraphItem)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
return CommitGraph, nil
|
||
8 years ago
|
}
|
||
|
|
||
|
func graphItemFromString(s string, r *git.Repository) (GraphItem, error) {
|
||
|
|
||
|
var ascii string
|
||
|
var data = "|||||||"
|
||
7 years ago
|
lines := strings.SplitN(s, "DATA:", 2)
|
||
8 years ago
|
|
||
|
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)
|
||
|
}
|
||
|
|
||
8 years ago
|
rows := strings.SplitN(data, "|", 8)
|
||
|
if len(rows) < 8 {
|
||
8 years ago
|
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],
|
||
8 years ago
|
len(rows[2]) == 0, // no commits referred to, only relation in current line.
|
||
8 years ago
|
}
|
||
|
return gi, nil
|
||
|
}
|