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
|
|
|
)
|
|
|
|
|
|
|
|
// GetCommitGraph return a list of commit (GraphItems) from all branches
|
2021-12-20 12:41:31 +08:00
|
|
|
func GetCommitGraph(r *git.Repository, page, maxAllowedColors int, hidePRRefs bool, branches, files []string) (*Graph, error) {
|
2020-11-09 01:21:54 +08:00
|
|
|
format := "DATA:%D|%H|%ad|%h|%s"
|
2016-12-29 07:44:32 +08:00
|
|
|
|
2020-07-17 03:24:36 +08:00
|
|
|
if page == 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
2020-11-09 01:21:54 +08:00
|
|
|
args := make([]string, 0, 12+len(branches)+len(files))
|
|
|
|
|
|
|
|
args = append(args, "--graph", "--date-order", "--decorate=full")
|
|
|
|
|
|
|
|
if hidePRRefs {
|
2021-12-02 15:28:08 +08:00
|
|
|
args = append(args, "--exclude="+git.PullPrefix+"*")
|
2020-11-09 01:21:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(branches) == 0 {
|
|
|
|
args = append(args, "--all")
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args,
|
2016-12-29 07:44:32 +08:00
|
|
|
"-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",
|
2020-11-09 01:21:54 +08:00
|
|
|
fmt.Sprintf("--pretty=format:%s", format))
|
|
|
|
|
|
|
|
if len(branches) > 0 {
|
|
|
|
args = append(args, branches...)
|
|
|
|
}
|
|
|
|
args = append(args, "--")
|
|
|
|
if len(files) > 0 {
|
|
|
|
args = append(args, files...)
|
|
|
|
}
|
|
|
|
|
2022-02-07 03:01:47 +08:00
|
|
|
graphCmd := git.NewCommand(r.Ctx, "log")
|
2020-11-09 01:21:54 +08:00
|
|
|
graphCmd.AddArguments(args...)
|
2020-08-06 16:04:08 +08:00
|
|
|
graph := NewGraph()
|
|
|
|
|
2020-07-17 03:24:36 +08:00
|
|
|
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)
|
|
|
|
|
2022-04-01 10:55:30 +08:00
|
|
|
if err := graphCmd.Run(&git.RunOpts{
|
|
|
|
Dir: r.Path,
|
|
|
|
Stdout: stdoutWriter,
|
|
|
|
Stderr: stderr,
|
2022-02-11 20:47:22 +08:00
|
|
|
PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
|
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
defer stdoutReader.Close()
|
|
|
|
parser := &Parser{}
|
|
|
|
parser.firstInUse = -1
|
|
|
|
parser.maxAllowedColors = maxAllowedColors
|
|
|
|
if maxAllowedColors > 0 {
|
|
|
|
parser.availableColors = make([]int, maxAllowedColors)
|
|
|
|
for i := range parser.availableColors {
|
|
|
|
parser.availableColors[i] = i + 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
parser.availableColors = []int{1, 2}
|
2020-08-06 16:04:08 +08:00
|
|
|
}
|
2022-02-11 20:47:22 +08:00
|
|
|
for commitsToSkip > 0 && scanner.Scan() {
|
|
|
|
line := scanner.Bytes()
|
|
|
|
dataIdx := bytes.Index(line, []byte("DATA:"))
|
|
|
|
if dataIdx < 0 {
|
|
|
|
dataIdx = len(line)
|
|
|
|
}
|
|
|
|
starIdx := bytes.IndexByte(line, '*')
|
|
|
|
if starIdx >= 0 && starIdx < dataIdx {
|
|
|
|
commitsToSkip--
|
|
|
|
}
|
|
|
|
parser.ParseGlyphs(line[:dataIdx])
|
2020-07-17 03:24:36 +08:00
|
|
|
}
|
2020-08-06 16:04:08 +08:00
|
|
|
|
2022-02-11 20:47:22 +08:00
|
|
|
row := 0
|
|
|
|
|
|
|
|
// Skip initial non-commit lines
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Bytes()
|
|
|
|
if bytes.IndexByte(line, '*') >= 0 {
|
|
|
|
if err := parser.AddLineToGraph(graph, row, line); err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
parser.ParseGlyphs(line)
|
|
|
|
}
|
2020-08-06 16:04:08 +08:00
|
|
|
|
2022-02-11 20:47:22 +08:00
|
|
|
for scanner.Scan() {
|
|
|
|
row++
|
|
|
|
line := scanner.Bytes()
|
2020-08-06 16:04:08 +08:00
|
|
|
if err := parser.AddLineToGraph(graph, row, line); err != nil {
|
2020-07-17 03:24:36 +08:00
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 20:47:22 +08:00
|
|
|
return scanner.Err()
|
|
|
|
},
|
2020-07-17 03:24:36 +08:00
|
|
|
}); err != nil {
|
2020-08-06 16:04:08 +08:00
|
|
|
return graph, err
|
2016-12-29 07:44:32 +08:00
|
|
|
}
|
2020-08-06 16:04:08 +08:00
|
|
|
return graph, nil
|
2016-12-29 07:44:32 +08:00
|
|
|
}
|