diff.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package plumbing
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/sergi/go-diff/diffmatchpatch"
  6. "gopkg.in/src-d/go-git.v4"
  7. "gopkg.in/src-d/go-git.v4/plumbing"
  8. "gopkg.in/src-d/go-git.v4/plumbing/object"
  9. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  10. "gopkg.in/src-d/hercules.v9/internal/core"
  11. )
  12. // FileDiff calculates the difference of files which were modified.
  13. // It is a PipelineItem.
  14. type FileDiff struct {
  15. core.NoopMerger
  16. CleanupDisabled bool
  17. WhitespaceIgnore bool
  18. }
  19. const (
  20. // ConfigFileDiffDisableCleanup is the name of the configuration option (FileDiff.Configure())
  21. // to suppress diffmatchpatch.DiffCleanupSemanticLossless() which is supposed to improve
  22. // the human interpretability of diffs.
  23. ConfigFileDiffDisableCleanup = "FileDiff.NoCleanup"
  24. // DependencyFileDiff is the name of the dependency provided by FileDiff.
  25. DependencyFileDiff = "file_diff"
  26. // ConfigFileWhitespaceIgnore is the name of the configuration option (FileDiff.Configure())
  27. // to suppress whitespace changes which can pollute the core diff of the files
  28. ConfigFileWhitespaceIgnore = "FileDiff.WhitespaceIgnore"
  29. )
  30. // FileDiffData is the type of the dependency provided by FileDiff.
  31. type FileDiffData struct {
  32. OldLinesOfCode int
  33. NewLinesOfCode int
  34. Diffs []diffmatchpatch.Diff
  35. }
  36. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  37. func (diff *FileDiff) Name() string {
  38. return "FileDiff"
  39. }
  40. // Provides returns the list of names of entities which are produced by this PipelineItem.
  41. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  42. // to this list. Also used by core.Registry to build the global map of providers.
  43. func (diff *FileDiff) Provides() []string {
  44. arr := [...]string{DependencyFileDiff}
  45. return arr[:]
  46. }
  47. // Requires returns the list of names of entities which are needed by this PipelineItem.
  48. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  49. // entities are Provides() upstream.
  50. func (diff *FileDiff) Requires() []string {
  51. arr := [...]string{DependencyTreeChanges, DependencyBlobCache}
  52. return arr[:]
  53. }
  54. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  55. func (diff *FileDiff) ListConfigurationOptions() []core.ConfigurationOption {
  56. options := [...]core.ConfigurationOption{
  57. {
  58. Name: ConfigFileDiffDisableCleanup,
  59. Description: "Do not apply additional heuristics to improve diffs.",
  60. Flag: "no-diff-cleanup",
  61. Type: core.BoolConfigurationOption,
  62. Default: false},
  63. {
  64. Name: ConfigFileWhitespaceIgnore,
  65. Description: "Ignore whitespace when computing diffs.",
  66. Flag: "no-diff-whitespace",
  67. Type: core.BoolConfigurationOption,
  68. Default: false},
  69. }
  70. return options[:]
  71. }
  72. // Configure sets the properties previously published by ListConfigurationOptions().
  73. func (diff *FileDiff) Configure(facts map[string]interface{}) error {
  74. if val, exists := facts[ConfigFileDiffDisableCleanup].(bool); exists {
  75. diff.CleanupDisabled = val
  76. }
  77. if val, exists := facts[ConfigFileWhitespaceIgnore].(bool); exists {
  78. diff.WhitespaceIgnore = val
  79. }
  80. return nil
  81. }
  82. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  83. // calls. The repository which is going to be analysed is supplied as an argument.
  84. func (diff *FileDiff) Initialize(repository *git.Repository) error {
  85. return nil
  86. }
  87. func stripWhitespace(str string, ignoreWhitespace bool) string {
  88. if ignoreWhitespace {
  89. response := strings.Replace(str, " ", "", -1)
  90. return response
  91. }
  92. return str
  93. }
  94. // Consume runs this PipelineItem on the next commit data.
  95. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  96. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  97. // This function returns the mapping with analysis results. The keys must be the same as
  98. // in Provides(). If there was an error, nil is returned.
  99. func (diff *FileDiff) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  100. result := map[string]FileDiffData{}
  101. cache := deps[DependencyBlobCache].(map[plumbing.Hash]*CachedBlob)
  102. treeDiff := deps[DependencyTreeChanges].(object.Changes)
  103. for _, change := range treeDiff {
  104. action, err := change.Action()
  105. if err != nil {
  106. return nil, err
  107. }
  108. switch action {
  109. case merkletrie.Modify:
  110. blobFrom := cache[change.From.TreeEntry.Hash]
  111. blobTo := cache[change.To.TreeEntry.Hash]
  112. // we are not validating UTF-8 here because for example
  113. // git/git 4f7770c87ce3c302e1639a7737a6d2531fe4b160 fetch-pack.c is invalid UTF-8
  114. strFrom, strTo := string(blobFrom.Data), string(blobTo.Data)
  115. dmp := diffmatchpatch.New()
  116. dmp.DiffTimeout = time.Hour
  117. src, dst, _ := dmp.DiffLinesToRunes(stripWhitespace(strFrom, diff.WhitespaceIgnore), stripWhitespace(strTo, diff.WhitespaceIgnore))
  118. diffs := dmp.DiffMainRunes(src, dst, false)
  119. if !diff.CleanupDisabled {
  120. diffs = dmp.DiffCleanupMerge(dmp.DiffCleanupSemanticLossless(diffs))
  121. }
  122. result[change.To.Name] = FileDiffData{
  123. OldLinesOfCode: len(src),
  124. NewLinesOfCode: len(dst),
  125. Diffs: diffs,
  126. }
  127. default:
  128. continue
  129. }
  130. }
  131. return map[string]interface{}{DependencyFileDiff: result}, nil
  132. }
  133. // Fork clones this PipelineItem.
  134. func (diff *FileDiff) Fork(n int) []core.PipelineItem {
  135. return core.ForkSamePipelineItem(diff, n)
  136. }
  137. func init() {
  138. core.Registry.Register(&FileDiff{})
  139. }