diff.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package hercules
  2. import (
  3. "bufio"
  4. "bytes"
  5. "errors"
  6. "unicode/utf8"
  7. "github.com/sergi/go-diff/diffmatchpatch"
  8. "gopkg.in/src-d/go-git.v4"
  9. "gopkg.in/src-d/go-git.v4/plumbing"
  10. "gopkg.in/src-d/go-git.v4/plumbing/object"
  11. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  12. )
  13. // FileDiff calculates the difference of files which were modified.
  14. type FileDiff struct {
  15. CleanupDisabled bool
  16. }
  17. const (
  18. ConfigFileDiffDisableCleanup = "FileDiff.NoCleanup"
  19. DependencyFileDiff = "file_diff"
  20. )
  21. type FileDiffData struct {
  22. OldLinesOfCode int
  23. NewLinesOfCode int
  24. Diffs []diffmatchpatch.Diff
  25. }
  26. func (diff *FileDiff) Name() string {
  27. return "FileDiff"
  28. }
  29. func (diff *FileDiff) Provides() []string {
  30. arr := [...]string{DependencyFileDiff}
  31. return arr[:]
  32. }
  33. func (diff *FileDiff) Requires() []string {
  34. arr := [...]string{DependencyTreeChanges, DependencyBlobCache}
  35. return arr[:]
  36. }
  37. func (diff *FileDiff) ListConfigurationOptions() []ConfigurationOption {
  38. options := [...]ConfigurationOption{{
  39. Name: ConfigFileDiffDisableCleanup,
  40. Description: "Do not apply additional heuristics to improve diffs.",
  41. Flag: "no-diff-cleanup",
  42. Type: BoolConfigurationOption,
  43. Default: false},
  44. }
  45. return options[:]
  46. }
  47. func (diff *FileDiff) Configure(facts map[string]interface{}) {
  48. if val, exists := facts[ConfigFileDiffDisableCleanup].(bool); exists {
  49. diff.CleanupDisabled = val
  50. }
  51. }
  52. func (diff *FileDiff) Initialize(repository *git.Repository) {}
  53. func (diff *FileDiff) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  54. result := map[string]FileDiffData{}
  55. cache := deps[DependencyBlobCache].(map[plumbing.Hash]*object.Blob)
  56. tree_diff := deps[DependencyTreeChanges].(object.Changes)
  57. for _, change := range tree_diff {
  58. action, err := change.Action()
  59. if err != nil {
  60. return nil, err
  61. }
  62. switch action {
  63. case merkletrie.Modify:
  64. blob_from := cache[change.From.TreeEntry.Hash]
  65. blob_to := cache[change.To.TreeEntry.Hash]
  66. // we are not validating UTF-8 here because for example
  67. // git/git 4f7770c87ce3c302e1639a7737a6d2531fe4b160 fetch-pack.c is invalid UTF-8
  68. str_from, err := BlobToString(blob_from)
  69. if err != nil {
  70. return nil, err
  71. }
  72. str_to, err := BlobToString(blob_to)
  73. if err != nil {
  74. return nil, err
  75. }
  76. dmp := diffmatchpatch.New()
  77. src, dst, _ := dmp.DiffLinesToRunes(str_from, str_to)
  78. diffs := dmp.DiffMainRunes(src, dst, false)
  79. if !diff.CleanupDisabled {
  80. diffs = dmp.DiffCleanupSemanticLossless(diffs)
  81. }
  82. result[change.To.Name] = FileDiffData{
  83. OldLinesOfCode: len(src),
  84. NewLinesOfCode: len(dst),
  85. Diffs: diffs,
  86. }
  87. default:
  88. continue
  89. }
  90. }
  91. return map[string]interface{}{DependencyFileDiff: result}, nil
  92. }
  93. func CountLines(file *object.Blob) (int, error) {
  94. if file == nil {
  95. return -1, errors.New("Blob is nil: probably not cached.")
  96. }
  97. reader, err := file.Reader()
  98. if err != nil {
  99. return -1, err
  100. }
  101. defer checkClose(reader)
  102. var scanner *bufio.Scanner
  103. buffer := make([]byte, bufio.MaxScanTokenSize)
  104. counter := 0
  105. for scanner == nil || scanner.Err() == bufio.ErrTooLong {
  106. if scanner != nil && !utf8.Valid(scanner.Bytes()) {
  107. return -1, errors.New("binary")
  108. }
  109. scanner = bufio.NewScanner(reader)
  110. scanner.Buffer(buffer, 0)
  111. for scanner.Scan() {
  112. if !utf8.Valid(scanner.Bytes()) {
  113. return -1, errors.New("binary")
  114. }
  115. counter++
  116. }
  117. }
  118. return counter, nil
  119. }
  120. func BlobToString(file *object.Blob) (string, error) {
  121. if file == nil {
  122. return "", errors.New("Blob is nil: probably not cached.")
  123. }
  124. reader, err := file.Reader()
  125. if err != nil {
  126. return "", err
  127. }
  128. defer checkClose(reader)
  129. buf := new(bytes.Buffer)
  130. buf.ReadFrom(reader)
  131. return buf.String(), nil
  132. }
  133. func init() {
  134. Registry.Register(&FileDiff{})
  135. }