diff.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package hercules
  2. import (
  3. "bufio"
  4. "bytes"
  5. "errors"
  6. "unicode/utf8"
  7. "github.com/sergi/go-diff/diffmatchpatch"
  8. "gopkg.in/src-d/go-git.v4"
  9. "gopkg.in/src-d/go-git.v4/plumbing"
  10. "gopkg.in/src-d/go-git.v4/plumbing/object"
  11. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  12. )
  13. // FileDiff calculates the difference of files which were modified.
  14. // It is a PipelineItem.
  15. type FileDiff struct {
  16. CleanupDisabled bool
  17. }
  18. const (
  19. // ConfigFileDiffDisableCleanup is the name of the configuration option (FileDiff.Configure())
  20. // to suppress diffmatchpatch.DiffCleanupSemanticLossless() which is supposed to improve
  21. // the human interpretability of diffs.
  22. ConfigFileDiffDisableCleanup = "FileDiff.NoCleanup"
  23. // DependencyFileDiff is the name of the dependency provided by FileDiff.
  24. DependencyFileDiff = "file_diff"
  25. )
  26. // FileDiffData is the type of the dependency provided by FileDiff.
  27. type FileDiffData struct {
  28. OldLinesOfCode int
  29. NewLinesOfCode int
  30. Diffs []diffmatchpatch.Diff
  31. }
  32. func (diff *FileDiff) Name() string {
  33. return "FileDiff"
  34. }
  35. func (diff *FileDiff) Provides() []string {
  36. arr := [...]string{DependencyFileDiff}
  37. return arr[:]
  38. }
  39. func (diff *FileDiff) Requires() []string {
  40. arr := [...]string{DependencyTreeChanges, DependencyBlobCache}
  41. return arr[:]
  42. }
  43. func (diff *FileDiff) ListConfigurationOptions() []ConfigurationOption {
  44. options := [...]ConfigurationOption{{
  45. Name: ConfigFileDiffDisableCleanup,
  46. Description: "Do not apply additional heuristics to improve diffs.",
  47. Flag: "no-diff-cleanup",
  48. Type: BoolConfigurationOption,
  49. Default: false},
  50. }
  51. return options[:]
  52. }
  53. func (diff *FileDiff) Configure(facts map[string]interface{}) {
  54. if val, exists := facts[ConfigFileDiffDisableCleanup].(bool); exists {
  55. diff.CleanupDisabled = val
  56. }
  57. }
  58. func (diff *FileDiff) Initialize(repository *git.Repository) {}
  59. func (diff *FileDiff) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  60. result := map[string]FileDiffData{}
  61. cache := deps[DependencyBlobCache].(map[plumbing.Hash]*object.Blob)
  62. treeDiff := deps[DependencyTreeChanges].(object.Changes)
  63. for _, change := range treeDiff {
  64. action, err := change.Action()
  65. if err != nil {
  66. return nil, err
  67. }
  68. switch action {
  69. case merkletrie.Modify:
  70. blobFrom := cache[change.From.TreeEntry.Hash]
  71. blobTo := cache[change.To.TreeEntry.Hash]
  72. // we are not validating UTF-8 here because for example
  73. // git/git 4f7770c87ce3c302e1639a7737a6d2531fe4b160 fetch-pack.c is invalid UTF-8
  74. strFrom, err := BlobToString(blobFrom)
  75. if err != nil {
  76. return nil, err
  77. }
  78. strTo, err := BlobToString(blobTo)
  79. if err != nil {
  80. return nil, err
  81. }
  82. dmp := diffmatchpatch.New()
  83. src, dst, _ := dmp.DiffLinesToRunes(strFrom, strTo)
  84. diffs := dmp.DiffMainRunes(src, dst, false)
  85. if !diff.CleanupDisabled {
  86. diffs = dmp.DiffCleanupSemanticLossless(diffs)
  87. }
  88. result[change.To.Name] = FileDiffData{
  89. OldLinesOfCode: len(src),
  90. NewLinesOfCode: len(dst),
  91. Diffs: diffs,
  92. }
  93. default:
  94. continue
  95. }
  96. }
  97. return map[string]interface{}{DependencyFileDiff: result}, nil
  98. }
  99. // CountLines returns the number of lines in a *object.Blob.
  100. func CountLines(file *object.Blob) (int, error) {
  101. if file == nil {
  102. return -1, errors.New("blob is nil: probably not cached")
  103. }
  104. reader, err := file.Reader()
  105. if err != nil {
  106. return -1, err
  107. }
  108. defer checkClose(reader)
  109. var scanner *bufio.Scanner
  110. buffer := make([]byte, bufio.MaxScanTokenSize)
  111. counter := 0
  112. for scanner == nil || scanner.Err() == bufio.ErrTooLong {
  113. if scanner != nil && !utf8.Valid(scanner.Bytes()) {
  114. return -1, errors.New("binary")
  115. }
  116. scanner = bufio.NewScanner(reader)
  117. scanner.Buffer(buffer, 0)
  118. for scanner.Scan() {
  119. if !utf8.Valid(scanner.Bytes()) {
  120. return -1, errors.New("binary")
  121. }
  122. counter++
  123. }
  124. }
  125. return counter, nil
  126. }
  127. // BlobToString reads *object.Blob and returns its contents as a string.
  128. func BlobToString(file *object.Blob) (string, error) {
  129. if file == nil {
  130. return "", errors.New("blob is nil: probably not cached")
  131. }
  132. reader, err := file.Reader()
  133. if err != nil {
  134. return "", err
  135. }
  136. defer checkClose(reader)
  137. buf := new(bytes.Buffer)
  138. buf.ReadFrom(reader)
  139. return buf.String(), nil
  140. }
  141. func init() {
  142. Registry.Register(&FileDiff{})
  143. }