diff.go 5.3 KB

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