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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "fmt"
  8. "io"
  9. "os"
  10. "os/exec"
  11. "regexp"
  12. "code.gitea.io/gitea/modules/process"
  13. )
  14. // BlamePart represents block of blame - continuous lines with one sha
  15. type BlamePart struct {
  16. Sha string
  17. Lines []string
  18. }
  19. // BlameReader returns part of file blame one by one
  20. type BlameReader struct {
  21. cmd *exec.Cmd
  22. pid int64
  23. output io.ReadCloser
  24. scanner *bufio.Scanner
  25. lastSha *string
  26. }
  27. var shaLineRegex = regexp.MustCompile("^([a-z0-9]{40})")
  28. // NextPart returns next part of blame (sequencial code lines with the same commit)
  29. func (r *BlameReader) NextPart() (*BlamePart, error) {
  30. var blamePart *BlamePart
  31. scanner := r.scanner
  32. if r.lastSha != nil {
  33. blamePart = &BlamePart{*r.lastSha, make([]string, 0)}
  34. }
  35. for scanner.Scan() {
  36. line := scanner.Text()
  37. // Skip empty lines
  38. if len(line) == 0 {
  39. continue
  40. }
  41. lines := shaLineRegex.FindStringSubmatch(line)
  42. if lines != nil {
  43. sha1 := lines[1]
  44. if blamePart == nil {
  45. blamePart = &BlamePart{sha1, make([]string, 0)}
  46. }
  47. if blamePart.Sha != sha1 {
  48. r.lastSha = &sha1
  49. return blamePart, nil
  50. }
  51. } else if line[0] == '\t' {
  52. code := line[1:]
  53. blamePart.Lines = append(blamePart.Lines, code)
  54. }
  55. }
  56. r.lastSha = nil
  57. return blamePart, nil
  58. }
  59. // Close BlameReader - don't run NextPart after invoking that
  60. func (r *BlameReader) Close() error {
  61. process.GetManager().Remove(r.pid)
  62. if err := r.cmd.Wait(); err != nil {
  63. return fmt.Errorf("Wait: %v", err)
  64. }
  65. return nil
  66. }
  67. // CreateBlameReader creates reader for given repository, commit and file
  68. func CreateBlameReader(repoPath, commitID, file string) (*BlameReader, error) {
  69. gitRepo, err := OpenRepository(repoPath)
  70. if err != nil {
  71. return nil, err
  72. }
  73. gitRepo.Close()
  74. return createBlameReader(repoPath, GitExecutable, "blame", commitID, "--porcelain", "--", file)
  75. }
  76. func createBlameReader(dir string, command ...string) (*BlameReader, error) {
  77. cmd := exec.Command(command[0], command[1:]...)
  78. cmd.Dir = dir
  79. cmd.Stderr = os.Stderr
  80. stdout, err := cmd.StdoutPipe()
  81. if err != nil {
  82. return nil, fmt.Errorf("StdoutPipe: %v", err)
  83. }
  84. if err = cmd.Start(); err != nil {
  85. return nil, fmt.Errorf("Start: %v", err)
  86. }
  87. pid := process.GetManager().Add(fmt.Sprintf("GetBlame [repo_path: %s]", dir), cmd)
  88. scanner := bufio.NewScanner(stdout)
  89. return &BlameReader{
  90. cmd,
  91. pid,
  92. stdout,
  93. scanner,
  94. nil,
  95. }, nil
  96. }