2016-12-29 07:44:32 +08:00
|
|
|
// 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.
|
|
|
|
|
2019-11-16 08:47:57 +08:00
|
|
|
package gitgraph
|
2016-12-29 07:44:32 +08:00
|
|
|
|
|
|
|
import (
|
2020-07-17 03:24:36 +08:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2016-12-29 07:44:32 +08:00
|
|
|
"fmt"
|
2020-07-17 03:24:36 +08:00
|
|
|
"os"
|
2016-12-29 07:44:32 +08:00
|
|
|
"strings"
|
|
|
|
|
2019-03-27 17:33:00 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2018-07-23 22:12:06 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2016-12-29 07:44:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2019-10-15 05:38:35 +08:00
|
|
|
func GetCommitGraph(r *git.Repository, page int) (GraphItems, error) {
|
2016-12-29 07:44:32 +08:00
|
|
|
format := "DATA:|%d|%H|%ad|%an|%ae|%h|%s"
|
|
|
|
|
2020-07-17 03:24:36 +08:00
|
|
|
if page == 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
2016-12-29 07:44:32 +08:00
|
|
|
graphCmd := git.NewCommand("log")
|
|
|
|
graphCmd.AddArguments("--graph",
|
|
|
|
"--date-order",
|
|
|
|
"--all",
|
|
|
|
"-C",
|
|
|
|
"-M",
|
2020-07-17 03:24:36 +08:00
|
|
|
fmt.Sprintf("-n %d", setting.UI.GraphMaxCommitNum*page),
|
2016-12-29 07:44:32 +08:00
|
|
|
"--date=iso",
|
|
|
|
fmt.Sprintf("--pretty=format:%s", format),
|
|
|
|
)
|
2020-07-17 03:24:36 +08:00
|
|
|
commitGraph := make([]GraphItem, 0, 100)
|
|
|
|
stderr := new(strings.Builder)
|
|
|
|
stdoutReader, stdoutWriter, err := os.Pipe()
|
2016-12-29 07:44:32 +08:00
|
|
|
if err != nil {
|
2020-07-17 03:24:36 +08:00
|
|
|
return nil, err
|
2016-12-29 07:44:32 +08:00
|
|
|
}
|
2020-07-17 03:24:36 +08:00
|
|
|
commitsToSkip := setting.UI.GraphMaxCommitNum * (page - 1)
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(stdoutReader)
|
|
|
|
|
|
|
|
if err := graphCmd.RunInDirTimeoutEnvFullPipelineFunc(nil, -1, r.Path, stdoutWriter, stderr, nil, func(ctx context.Context, cancel context.CancelFunc) error {
|
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
defer stdoutReader.Close()
|
|
|
|
for commitsToSkip > 0 && scanner.Scan() {
|
|
|
|
line := scanner.Bytes()
|
|
|
|
dataIdx := bytes.Index(line, []byte("DATA:"))
|
|
|
|
starIdx := bytes.IndexByte(line, '*')
|
|
|
|
if starIdx >= 0 && starIdx < dataIdx {
|
|
|
|
commitsToSkip--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Skip initial non-commit lines
|
|
|
|
for scanner.Scan() {
|
|
|
|
if bytes.IndexByte(scanner.Bytes(), '*') >= 0 {
|
|
|
|
line := scanner.Text()
|
|
|
|
graphItem, err := graphItemFromString(line, r)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
commitGraph = append(commitGraph, graphItem)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-12-29 07:44:32 +08:00
|
|
|
|
2020-07-17 03:24:36 +08:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
graphItem, err := graphItemFromString(line, r)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
commitGraph = append(commitGraph, graphItem)
|
2016-12-29 07:44:32 +08:00
|
|
|
}
|
2020-07-17 03:24:36 +08:00
|
|
|
return scanner.Err()
|
|
|
|
}); err != nil {
|
|
|
|
return commitGraph, err
|
2016-12-29 07:44:32 +08:00
|
|
|
}
|
|
|
|
|
2020-07-17 03:24:36 +08:00
|
|
|
return commitGraph, nil
|
2016-12-29 07:44:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func graphItemFromString(s string, r *git.Repository) (GraphItem, error) {
|
|
|
|
|
|
|
|
var ascii string
|
|
|
|
var data = "|||||||"
|
2018-07-22 02:17:10 +08:00
|
|
|
lines := strings.SplitN(s, "DATA:", 2)
|
2016-12-29 07:44:32 +08:00
|
|
|
|
|
|
|
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 12:01:38 +08:00
|
|
|
rows := strings.SplitN(data, "|", 8)
|
|
|
|
if len(rows) < 8 {
|
2016-12-29 07:44:32 +08:00
|
|
|
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 08:50:34 +08:00
|
|
|
len(rows[2]) == 0, // no commits referred to, only relation in current line.
|
2016-12-29 07:44:32 +08:00
|
|
|
}
|
|
|
|
return gi, nil
|
|
|
|
}
|