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.

graph.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package gitgraph
  4. import (
  5. "bufio"
  6. "bytes"
  7. "context"
  8. "os"
  9. "strings"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. // GetCommitGraph return a list of commit (GraphItems) from all branches
  14. func GetCommitGraph(r *git.Repository, page, maxAllowedColors int, hidePRRefs bool, branches, files []string) (*Graph, error) {
  15. format := "DATA:%D|%H|%ad|%h|%s"
  16. if page == 0 {
  17. page = 1
  18. }
  19. graphCmd := git.NewCommand(r.Ctx, "log", "--graph", "--date-order", "--decorate=full")
  20. if hidePRRefs {
  21. graphCmd.AddArguments("--exclude=" + git.PullPrefix + "*")
  22. }
  23. if len(branches) == 0 {
  24. graphCmd.AddArguments("--all")
  25. }
  26. graphCmd.AddArguments("-C", "-M", "--date=iso").
  27. AddOptionFormat("-n %d", setting.UI.GraphMaxCommitNum*page).
  28. AddOptionFormat("--pretty=format:%s", format)
  29. if len(branches) > 0 {
  30. graphCmd.AddDynamicArguments(branches...)
  31. }
  32. if len(files) > 0 {
  33. graphCmd.AddDashesAndList(files...)
  34. }
  35. graph := NewGraph()
  36. stderr := new(strings.Builder)
  37. stdoutReader, stdoutWriter, err := os.Pipe()
  38. if err != nil {
  39. return nil, err
  40. }
  41. commitsToSkip := setting.UI.GraphMaxCommitNum * (page - 1)
  42. scanner := bufio.NewScanner(stdoutReader)
  43. if err := graphCmd.Run(&git.RunOpts{
  44. Dir: r.Path,
  45. Stdout: stdoutWriter,
  46. Stderr: stderr,
  47. PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
  48. _ = stdoutWriter.Close()
  49. defer stdoutReader.Close()
  50. parser := &Parser{}
  51. parser.firstInUse = -1
  52. parser.maxAllowedColors = maxAllowedColors
  53. if maxAllowedColors > 0 {
  54. parser.availableColors = make([]int, maxAllowedColors)
  55. for i := range parser.availableColors {
  56. parser.availableColors[i] = i + 1
  57. }
  58. } else {
  59. parser.availableColors = []int{1, 2}
  60. }
  61. for commitsToSkip > 0 && scanner.Scan() {
  62. line := scanner.Bytes()
  63. dataIdx := bytes.Index(line, []byte("DATA:"))
  64. if dataIdx < 0 {
  65. dataIdx = len(line)
  66. }
  67. starIdx := bytes.IndexByte(line, '*')
  68. if starIdx >= 0 && starIdx < dataIdx {
  69. commitsToSkip--
  70. }
  71. parser.ParseGlyphs(line[:dataIdx])
  72. }
  73. row := 0
  74. // Skip initial non-commit lines
  75. for scanner.Scan() {
  76. line := scanner.Bytes()
  77. if bytes.IndexByte(line, '*') >= 0 {
  78. if err := parser.AddLineToGraph(graph, row, line); err != nil {
  79. cancel()
  80. return err
  81. }
  82. break
  83. }
  84. parser.ParseGlyphs(line)
  85. }
  86. for scanner.Scan() {
  87. row++
  88. line := scanner.Bytes()
  89. if err := parser.AddLineToGraph(graph, row, line); err != nil {
  90. cancel()
  91. return err
  92. }
  93. }
  94. return scanner.Err()
  95. },
  96. }); err != nil {
  97. return graph, err
  98. }
  99. return graph, nil
  100. }