diff.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.v10/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. l core.Logger
  19. }
  20. const (
  21. // ConfigFileDiffDisableCleanup is the name of the configuration option (FileDiff.Configure())
  22. // to suppress diffmatchpatch.DiffCleanupSemanticLossless() which is supposed to improve
  23. // the human interpretability of diffs.
  24. ConfigFileDiffDisableCleanup = "FileDiff.NoCleanup"
  25. // DependencyFileDiff is the name of the dependency provided by FileDiff.
  26. DependencyFileDiff = "file_diff"
  27. // ConfigFileWhitespaceIgnore is the name of the configuration option (FileDiff.Configure())
  28. // to suppress whitespace changes which can pollute the core diff of the files
  29. ConfigFileWhitespaceIgnore = "FileDiff.WhitespaceIgnore"
  30. )
  31. // FileDiffData is the type of the dependency provided by FileDiff.
  32. type FileDiffData struct {
  33. OldLinesOfCode int
  34. NewLinesOfCode int
  35. Diffs []diffmatchpatch.Diff
  36. }
  37. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  38. func (diff *FileDiff) Name() string {
  39. return "FileDiff"
  40. }
  41. // Provides returns the list of names of entities which are produced by this PipelineItem.
  42. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  43. // to this list. Also used by core.Registry to build the global map of providers.
  44. func (diff *FileDiff) Provides() []string {
  45. return []string{DependencyFileDiff}
  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. return []string{DependencyTreeChanges, DependencyBlobCache}
  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 l, exists := facts[core.ConfigLogger].(core.Logger); exists {
  74. diff.l = l
  75. }
  76. if val, exists := facts[ConfigFileDiffDisableCleanup].(bool); exists {
  77. diff.CleanupDisabled = val
  78. }
  79. if val, exists := facts[ConfigFileWhitespaceIgnore].(bool); exists {
  80. diff.WhitespaceIgnore = val
  81. }
  82. return nil
  83. }
  84. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  85. // calls. The repository which is going to be analysed is supplied as an argument.
  86. func (diff *FileDiff) Initialize(repository *git.Repository) error {
  87. diff.l = core.NewLogger()
  88. return nil
  89. }
  90. func stripWhitespace(str string, ignoreWhitespace bool) string {
  91. if ignoreWhitespace {
  92. response := strings.Replace(str, " ", "", -1)
  93. return response
  94. }
  95. return str
  96. }
  97. // Consume runs this PipelineItem on the next commit data.
  98. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  99. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  100. // This function returns the mapping with analysis results. The keys must be the same as
  101. // in Provides(). If there was an error, nil is returned.
  102. func (diff *FileDiff) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  103. result := map[string]FileDiffData{}
  104. cache := deps[DependencyBlobCache].(map[plumbing.Hash]*CachedBlob)
  105. treeDiff := deps[DependencyTreeChanges].(object.Changes)
  106. for _, change := range treeDiff {
  107. action, err := change.Action()
  108. if err != nil {
  109. return nil, err
  110. }
  111. switch action {
  112. case merkletrie.Modify:
  113. blobFrom := cache[change.From.TreeEntry.Hash]
  114. blobTo := cache[change.To.TreeEntry.Hash]
  115. // we are not validating UTF-8 here because for example
  116. // git/git 4f7770c87ce3c302e1639a7737a6d2531fe4b160 fetch-pack.c is invalid UTF-8
  117. strFrom, strTo := string(blobFrom.Data), string(blobTo.Data)
  118. dmp := diffmatchpatch.New()
  119. dmp.DiffTimeout = time.Hour
  120. src, dst, _ := dmp.DiffLinesToRunes(stripWhitespace(strFrom, diff.WhitespaceIgnore), stripWhitespace(strTo, diff.WhitespaceIgnore))
  121. diffs := dmp.DiffMainRunes(src, dst, false)
  122. if !diff.CleanupDisabled {
  123. diffs = dmp.DiffCleanupMerge(dmp.DiffCleanupSemanticLossless(diffs))
  124. }
  125. result[change.To.Name] = FileDiffData{
  126. OldLinesOfCode: len(src),
  127. NewLinesOfCode: len(dst),
  128. Diffs: diffs,
  129. }
  130. default:
  131. continue
  132. }
  133. }
  134. return map[string]interface{}{DependencyFileDiff: result}, nil
  135. }
  136. // Fork clones this PipelineItem.
  137. func (diff *FileDiff) Fork(n int) []core.PipelineItem {
  138. return core.ForkSamePipelineItem(diff, n)
  139. }
  140. func init() {
  141. core.Registry.Register(&FileDiff{})
  142. }