diff.go 4.7 KB

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