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.
|
|
|
|
|
2019-11-16 06:17:57 +05:30
|
|
|
package gitgraph
|
2016-12-29 05:14:32 +05:30
|
|
|
|
|
|
|
import (
|
2020-07-17 00:54:36 +05:30
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2016-12-29 05:14:32 +05:30
|
|
|
"fmt"
|
2020-07-17 00:54:36 +05:30
|
|
|
"os"
|
2016-12-29 05:14:32 +05:30
|
|
|
"strings"
|
|
|
|
|
2019-03-27 15:03:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2018-07-23 19:42:06 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2016-12-29 05:14:32 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// GetCommitGraph return a list of commit (GraphItems) from all branches
|
2021-12-20 10:11:31 +05:30
|
|
|
func GetCommitGraph(r *git.Repository, page, maxAllowedColors int, hidePRRefs bool, branches, files []string) (*Graph, error) {
|
2020-11-08 22:51:54 +05:30
|
|
|
format := "DATA:%D|%H|%ad|%h|%s"
|
2016-12-29 05:14:32 +05:30
|
|
|
|
2020-07-17 00:54:36 +05:30
|
|
|
if page == 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
2022-10-15 16:19:26 +05:30
|
|
|
graphCmd := git.NewCommand(r.Ctx, "log", "--graph", "--date-order", "--decorate=full")
|
2020-11-08 22:51:54 +05:30
|
|
|
|
|
|
|
if hidePRRefs {
|
2022-10-15 16:19:26 +05:30
|
|
|
graphCmd.AddArguments("--exclude=" + git.PullPrefix + "*")
|
2020-11-08 22:51:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if len(branches) == 0 {
|
2022-10-15 16:19:26 +05:30
|
|
|
graphCmd.AddArguments("--all")
|
2020-11-08 22:51:54 +05:30
|
|
|
}
|
|
|
|
|
2022-10-15 16:19:26 +05:30
|
|
|
graphCmd.AddArguments(
|
2016-12-29 05:14:32 +05:30
|
|
|
"-C",
|
|
|
|
"-M",
|
2022-10-23 20:14:45 +05:30
|
|
|
git.CmdArg(fmt.Sprintf("-n %d", setting.UI.GraphMaxCommitNum*page)),
|
2016-12-29 05:14:32 +05:30
|
|
|
"--date=iso",
|
2022-10-23 20:14:45 +05:30
|
|
|
git.CmdArg(fmt.Sprintf("--pretty=format:%s", format)))
|
2020-11-08 22:51:54 +05:30
|
|
|
|
|
|
|
if len(branches) > 0 {
|
2022-10-15 16:19:26 +05:30
|
|
|
graphCmd.AddDynamicArguments(branches...)
|
2020-11-08 22:51:54 +05:30
|
|
|
}
|
|
|
|
if len(files) > 0 {
|
2022-10-23 20:14:45 +05:30
|
|
|
graphCmd.AddDashesAndList(files...)
|
2020-11-08 22:51:54 +05:30
|
|
|
}
|
2020-08-06 13:34:08 +05:30
|
|
|
graph := NewGraph()
|
|
|
|
|
2020-07-17 00:54:36 +05:30
|
|
|
stderr := new(strings.Builder)
|
|
|
|
stdoutReader, stdoutWriter, err := os.Pipe()
|
2016-12-29 05:14:32 +05:30
|
|
|
if err != nil {
|
2020-07-17 00:54:36 +05:30
|
|
|
return nil, err
|
2016-12-29 05:14:32 +05:30
|
|
|
}
|
2020-07-17 00:54:36 +05:30
|
|
|
commitsToSkip := setting.UI.GraphMaxCommitNum * (page - 1)
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(stdoutReader)
|
|
|
|
|
2022-04-01 08:25:30 +05:30
|
|
|
if err := graphCmd.Run(&git.RunOpts{
|
|
|
|
Dir: r.Path,
|
|
|
|
Stdout: stdoutWriter,
|
|
|
|
Stderr: stderr,
|
2022-02-11 18:17:22 +05:30
|
|
|
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 13:34:08 +05:30
|
|
|
}
|
2022-02-11 18:17:22 +05:30
|
|
|
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 00:54:36 +05:30
|
|
|
}
|
2020-08-06 13:34:08 +05:30
|
|
|
|
2022-02-11 18:17:22 +05:30
|
|
|
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 13:34:08 +05:30
|
|
|
|
2022-02-11 18:17:22 +05:30
|
|
|
for scanner.Scan() {
|
|
|
|
row++
|
|
|
|
line := scanner.Bytes()
|
2020-08-06 13:34:08 +05:30
|
|
|
if err := parser.AddLineToGraph(graph, row, line); err != nil {
|
2020-07-17 00:54:36 +05:30
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 18:17:22 +05:30
|
|
|
return scanner.Err()
|
|
|
|
},
|
2020-07-17 00:54:36 +05:30
|
|
|
}); err != nil {
|
2020-08-06 13:34:08 +05:30
|
|
|
return graph, err
|
2016-12-29 05:14:32 +05:30
|
|
|
}
|
2020-08-06 13:34:08 +05:30
|
|
|
return graph, nil
|
2016-12-29 05:14:32 +05:30
|
|
|
}
|