本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

124 lines
2.5KB

  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. _, err := OpenRepository(repoPath)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return createBlameReader(repoPath, GitExecutable, "blame", commitID, "--porcelain", "--", file)
  74. }
  75. func createBlameReader(dir string, command ...string) (*BlameReader, error) {
  76. cmd := exec.Command(command[0], command[1:]...)
  77. cmd.Dir = dir
  78. cmd.Stderr = os.Stderr
  79. stdout, err := cmd.StdoutPipe()
  80. if err != nil {
  81. return nil, fmt.Errorf("StdoutPipe: %v", err)
  82. }
  83. if err = cmd.Start(); err != nil {
  84. return nil, fmt.Errorf("Start: %v", err)
  85. }
  86. pid := process.GetManager().Add(fmt.Sprintf("GetBlame [repo_path: %s]", dir), cmd)
  87. scanner := bufio.NewScanner(stdout)
  88. return &BlameReader{
  89. cmd,
  90. pid,
  91. stdout,
  92. scanner,
  93. nil,
  94. }, nil
  95. }
上海开阖软件有限公司 沪ICP备12045867号-1