diff.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.v9/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{}) error {
  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. return nil
  80. }
  81. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  82. // calls. The repository which is going to be analysed is supplied as an argument.
  83. func (diff *FileDiff) Initialize(repository *git.Repository) error {
  84. return nil
  85. }
  86. func stripWhitespace(str string, ignoreWhitespace bool) string {
  87. if ignoreWhitespace {
  88. response := strings.Replace(str, " ", "", -1)
  89. return response
  90. }
  91. return str
  92. }
  93. // Consume runs this PipelineItem on the next commit data.
  94. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  95. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  96. // This function returns the mapping with analysis results. The keys must be the same as
  97. // in Provides(). If there was an error, nil is returned.
  98. func (diff *FileDiff) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  99. result := map[string]FileDiffData{}
  100. cache := deps[DependencyBlobCache].(map[plumbing.Hash]*CachedBlob)
  101. treeDiff := deps[DependencyTreeChanges].(object.Changes)
  102. for _, change := range treeDiff {
  103. action, err := change.Action()
  104. if err != nil {
  105. return nil, err
  106. }
  107. switch action {
  108. case merkletrie.Modify:
  109. blobFrom := cache[change.From.TreeEntry.Hash]
  110. blobTo := cache[change.To.TreeEntry.Hash]
  111. // we are not validating UTF-8 here because for example
  112. // git/git 4f7770c87ce3c302e1639a7737a6d2531fe4b160 fetch-pack.c is invalid UTF-8
  113. strFrom, strTo := string(blobFrom.Data), string(blobTo.Data)
  114. dmp := diffmatchpatch.New()
  115. src, dst, _ := dmp.DiffLinesToRunes(stripWhitespace(strFrom, diff.WhitespaceIgnore), stripWhitespace(strTo, diff.WhitespaceIgnore))
  116. diffs := dmp.DiffMainRunes(src, dst, false)
  117. if !diff.CleanupDisabled {
  118. diffs = dmp.DiffCleanupMerge(dmp.DiffCleanupSemanticLossless(diffs))
  119. }
  120. result[change.To.Name] = FileDiffData{
  121. OldLinesOfCode: len(src),
  122. NewLinesOfCode: len(dst),
  123. Diffs: diffs,
  124. }
  125. default:
  126. continue
  127. }
  128. }
  129. return map[string]interface{}{DependencyFileDiff: result}, nil
  130. }
  131. // Fork clones this PipelineItem.
  132. func (diff *FileDiff) Fork(n int) []core.PipelineItem {
  133. return core.ForkSamePipelineItem(diff, n)
  134. }
  135. func init() {
  136. core.Registry.Register(&FileDiff{})
  137. }