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.

134 lines
2.9 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "bufio"
  7. "context"
  8. "fmt"
  9. "io"
  10. "os"
  11. "os/exec"
  12. "regexp"
  13. "code.gitea.io/gitea/modules/process"
  14. )
  15. // BlamePart represents block of blame - continuous lines with one sha
  16. type BlamePart struct {
  17. Sha string
  18. Lines []string
  19. }
  20. // BlameReader returns part of file blame one by one
  21. type BlameReader struct {
  22. cmd *exec.Cmd
  23. pid int64
  24. output io.ReadCloser
  25. scanner *bufio.Scanner
  26. lastSha *string
  27. cancel context.CancelFunc
  28. }
  29. var shaLineRegex = regexp.MustCompile("^([a-z0-9]{40})")
  30. // NextPart returns next part of blame (sequencial code lines with the same commit)
  31. func (r *BlameReader) NextPart() (*BlamePart, error) {
  32. var blamePart *BlamePart
  33. scanner := r.scanner
  34. if r.lastSha != nil {
  35. blamePart = &BlamePart{*r.lastSha, make([]string, 0)}
  36. }
  37. for scanner.Scan() {
  38. line := scanner.Text()
  39. // Skip empty lines
  40. if len(line) == 0 {
  41. continue
  42. }
  43. lines := shaLineRegex.FindStringSubmatch(line)
  44. if lines != nil {
  45. sha1 := lines[1]
  46. if blamePart == nil {
  47. blamePart = &BlamePart{sha1, make([]string, 0)}
  48. }
  49. if blamePart.Sha != sha1 {
  50. r.lastSha = &sha1
  51. return blamePart, nil
  52. }
  53. } else if line[0] == '\t' {
  54. code := line[1:]
  55. blamePart.Lines = append(blamePart.Lines, code)
  56. }
  57. }
  58. r.lastSha = nil
  59. return blamePart, nil
  60. }
  61. // Close BlameReader - don't run NextPart after invoking that
  62. func (r *BlameReader) Close() error {
  63. defer process.GetManager().Remove(r.pid)
  64. r.cancel()
  65. _ = r.output.Close()
  66. if err := r.cmd.Wait(); err != nil {
  67. return fmt.Errorf("Wait: %v", err)
  68. }
  69. return nil
  70. }
  71. // CreateBlameReader creates reader for given repository, commit and file
  72. func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*BlameReader, error) {
  73. gitRepo, err := OpenRepository(repoPath)
  74. if err != nil {
  75. return nil, err
  76. }
  77. gitRepo.Close()
  78. return createBlameReader(ctx, repoPath, GitExecutable, "blame", commitID, "--porcelain", "--", file)
  79. }
  80. func createBlameReader(ctx context.Context, dir string, command ...string) (*BlameReader, error) {
  81. // Here we use the provided context - this should be tied to the request performing the blame so that it does not hang around.
  82. ctx, cancel := context.WithCancel(ctx)
  83. cmd := exec.CommandContext(ctx, command[0], command[1:]...)
  84. cmd.Dir = dir
  85. cmd.Stderr = os.Stderr
  86. stdout, err := cmd.StdoutPipe()
  87. if err != nil {
  88. defer cancel()
  89. return nil, fmt.Errorf("StdoutPipe: %v", err)
  90. }
  91. if err = cmd.Start(); err != nil {
  92. defer cancel()
  93. return nil, fmt.Errorf("Start: %v", err)
  94. }
  95. pid := process.GetManager().Add(fmt.Sprintf("GetBlame [repo_path: %s]", dir), cancel)
  96. scanner := bufio.NewScanner(stdout)
  97. return &BlameReader{
  98. cmd,
  99. pid,
  100. stdout,
  101. scanner,
  102. nil,
  103. cancel,
  104. }, nil
  105. }