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.

git_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 models
  5. import (
  6. "bufio"
  7. "fmt"
  8. "io"
  9. "os"
  10. "os/exec"
  11. "regexp"
  12. "code.gitea.io/gitea/modules/git"
  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. }
  28. var shaLineRegex = regexp.MustCompile("^([a-z0-9]{40})")
  29. // NextPart returns next part of blame (sequencial code lines with the same commit)
  30. func (r *BlameReader) NextPart() (*BlamePart, error) {
  31. var blamePart *BlamePart
  32. scanner := r.scanner
  33. if r.lastSha != nil {
  34. blamePart = &BlamePart{*r.lastSha, make([]string, 0)}
  35. }
  36. for scanner.Scan() {
  37. line := scanner.Text()
  38. // Skip empty lines
  39. if len(line) == 0 {
  40. continue
  41. }
  42. lines := shaLineRegex.FindStringSubmatch(line)
  43. if lines != nil {
  44. sha1 := lines[1]
  45. if blamePart == nil {
  46. blamePart = &BlamePart{sha1, make([]string, 0)}
  47. }
  48. if blamePart.Sha != sha1 {
  49. r.lastSha = &sha1
  50. return blamePart, nil
  51. }
  52. } else if line[0] == '\t' {
  53. code := line[1:]
  54. blamePart.Lines = append(blamePart.Lines, code)
  55. }
  56. }
  57. r.lastSha = nil
  58. return blamePart, nil
  59. }
  60. // Close BlameReader - don't run NextPart after invoking that
  61. func (r *BlameReader) Close() error {
  62. process.GetManager().Remove(r.pid)
  63. if err := r.cmd.Wait(); err != nil {
  64. return fmt.Errorf("Wait: %v", err)
  65. }
  66. return nil
  67. }
  68. // CreateBlameReader creates reader for given repository, commit and file
  69. func CreateBlameReader(repoPath, commitID, file string) (*BlameReader, error) {
  70. _, err := git.OpenRepository(repoPath)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return createBlameReader(repoPath, "git", "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. }