diff.go 5.1 KB

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