2016-12-29 02:44:32 +03: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 03:47:57 +03:00
|
|
|
package gitgraph
|
2016-12-29 02:44:32 +03:00
|
|
|
|
|
|
|
import (
|
2020-07-16 22:24:36 +03:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2016-12-29 02:44:32 +03:00
|
|
|
"fmt"
|
2020-07-16 22:24:36 +03:00
|
|
|
"os"
|
2016-12-29 02:44:32 +03:00
|
|
|
"strings"
|
|
|
|
|
2019-03-27 12:33:00 +03:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2018-07-23 17:12:06 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2016-12-29 02:44:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetCommitGraph return a list of commit (GraphItems) from all branches
|
2020-08-06 11:04:08 +03:00
|
|
|
func GetCommitGraph(r *git.Repository, page int, maxAllowedColors int) (*Graph, error) {
|
|
|
|
format := "DATA:%d|%H|%ad|%an|%ae|%h|%s"
|
2016-12-29 02:44:32 +03:00
|
|
|
|
2020-07-16 22:24:36 +03:00
|
|
|
if page == 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
2016-12-29 02:44:32 +03:00
|
|
|
graphCmd := git.NewCommand("log")
|
|
|
|
graphCmd.AddArguments("--graph",
|
|
|
|
"--date-order",
|
|
|
|
"--all",
|
|
|
|
"-C",
|
|
|
|
"-M",
|
2020-07-16 22:24:36 +03:00
|
|
|
fmt.Sprintf("-n %d", setting.UI.GraphMaxCommitNum*page),
|
2016-12-29 02:44:32 +03:00
|
|
|
"--date=iso",
|
|
|
|
fmt.Sprintf("--pretty=format:%s", format),
|
|
|
|
)
|
2020-08-06 11:04:08 +03:00
|
|
|
graph := NewGraph()
|
|
|
|
|
2020-07-16 22:24:36 +03:00
|
|
|
stderr := new(strings.Builder)
|
|
|
|
stdoutReader, stdoutWriter, err := os.Pipe()
|
2016-12-29 02:44:32 +03:00
|
|
|
if err != nil {
|
2020-07-16 22:24:36 +03:00
|
|
|
return nil, err
|
2016-12-29 02:44:32 +03:00
|
|
|
}
|
2020-07-16 22:24:36 +03: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()
|
2020-08-06 11:04:08 +03:00
|
|
|
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-07-16 22:24:36 +03:00
|
|
|
for commitsToSkip > 0 && scanner.Scan() {
|
|
|
|
line := scanner.Bytes()
|
|
|
|
dataIdx := bytes.Index(line, []byte("DATA:"))
|
2020-08-06 11:04:08 +03:00
|
|
|
if dataIdx < 0 {
|
|
|
|
dataIdx = len(line)
|
|
|
|
}
|
2020-07-16 22:24:36 +03:00
|
|
|
starIdx := bytes.IndexByte(line, '*')
|
|
|
|
if starIdx >= 0 && starIdx < dataIdx {
|
|
|
|
commitsToSkip--
|
|
|
|
}
|
2020-08-06 11:04:08 +03:00
|
|
|
parser.ParseGlyphs(line[:dataIdx])
|
2020-07-16 22:24:36 +03:00
|
|
|
}
|
2020-08-06 11:04:08 +03:00
|
|
|
|
|
|
|
row := 0
|
|
|
|
|
2020-07-16 22:24:36 +03:00
|
|
|
// Skip initial non-commit lines
|
|
|
|
for scanner.Scan() {
|
2020-08-06 11:04:08 +03:00
|
|
|
line := scanner.Bytes()
|
|
|
|
if bytes.IndexByte(line, '*') >= 0 {
|
|
|
|
if err := parser.AddLineToGraph(graph, row, line); err != nil {
|
2020-07-16 22:24:36 +03:00
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-08-06 11:04:08 +03:00
|
|
|
parser.ParseGlyphs(line)
|
2020-07-16 22:24:36 +03:00
|
|
|
}
|
2016-12-29 02:44:32 +03:00
|
|
|
|
2020-07-16 22:24:36 +03:00
|
|
|
for scanner.Scan() {
|
2020-08-06 11:04:08 +03:00
|
|
|
row++
|
|
|
|
line := scanner.Bytes()
|
|
|
|
if err := parser.AddLineToGraph(graph, row, line); err != nil {
|
2020-07-16 22:24:36 +03:00
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
2016-12-29 02:44:32 +03:00
|
|
|
}
|
2020-07-16 22:24:36 +03:00
|
|
|
return scanner.Err()
|
|
|
|
}); err != nil {
|
2020-08-06 11:04:08 +03:00
|
|
|
return graph, err
|
2016-12-29 02:44:32 +03:00
|
|
|
}
|
2020-08-06 11:04:08 +03:00
|
|
|
return graph, nil
|
2016-12-29 02:44:32 +03:00
|
|
|
}
|