diff.go 4.5 KB

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