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.

blame.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. defer r.cancel()
  65. if err := r.cmd.Wait(); err != nil {
  66. return fmt.Errorf("Wait: %v", err)
  67. }
  68. return nil
  69. }
  70. // CreateBlameReader creates reader for given repository, commit and file
  71. func CreateBlameReader(repoPath, commitID, file string) (*BlameReader, error) {
  72. gitRepo, err := OpenRepository(repoPath)
  73. if err != nil {
  74. return nil, err
  75. }
  76. gitRepo.Close()
  77. return createBlameReader(repoPath, GitExecutable, "blame", commitID, "--porcelain", "--", file)
  78. }
  79. func createBlameReader(dir string, command ...string) (*BlameReader, error) {
  80. // FIXME: graceful: This should have a timeout
  81. ctx, cancel := context.WithCancel(DefaultContext)
  82. cmd := exec.CommandContext(ctx, command[0], command[1:]...)
  83. cmd.Dir = dir
  84. cmd.Stderr = os.Stderr
  85. stdout, err := cmd.StdoutPipe()
  86. if err != nil {
  87. defer cancel()
  88. return nil, fmt.Errorf("StdoutPipe: %v", err)
  89. }
  90. if err = cmd.Start(); err != nil {
  91. defer cancel()
  92. return nil, fmt.Errorf("Start: %v", err)
  93. }
  94. pid := process.GetManager().Add(fmt.Sprintf("GetBlame [repo_path: %s]", dir), cancel)
  95. scanner := bufio.NewScanner(stdout)
  96. return &BlameReader{
  97. cmd,
  98. pid,
  99. stdout,
  100. scanner,
  101. nil,
  102. cancel,
  103. }, nil
  104. }