renames.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package hercules
  2. import (
  3. "sort"
  4. "unicode/utf8"
  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. )
  11. type RenameAnalysis struct {
  12. // SimilarityThreshold adjusts the heuristic to determine file renames.
  13. // It has the same units as cgit's -X rename-threshold or -M. Better to
  14. // set it to the default value of 90 (90%).
  15. SimilarityThreshold int
  16. repository *git.Repository
  17. }
  18. func (ra *RenameAnalysis) Name() string {
  19. return "RenameAnalysis"
  20. }
  21. func (ra *RenameAnalysis) Provides() []string {
  22. arr := [...]string{"renamed_changes"}
  23. return arr[:]
  24. }
  25. func (ra *RenameAnalysis) Requires() []string {
  26. arr := [...]string{"blob_cache", "changes"}
  27. return arr[:]
  28. }
  29. func (ra *RenameAnalysis) Initialize(repository *git.Repository) {
  30. if ra.SimilarityThreshold < 0 || ra.SimilarityThreshold > 100 {
  31. panic("hercules.RenameAnalysis: an invalid SimilarityThreshold was specified")
  32. }
  33. ra.repository = repository
  34. }
  35. func (ra *RenameAnalysis) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  36. changes := deps["changes"].(object.Changes)
  37. cache := deps["blob_cache"].(map[plumbing.Hash]*object.Blob)
  38. reduced_changes := make(object.Changes, 0, changes.Len())
  39. // Stage 1 - find renames by matching the hashes
  40. // n log(n)
  41. // We sort additions and deletions by hash and then do the single scan along
  42. // both slices.
  43. deleted := make(sortableChanges, 0, changes.Len())
  44. added := make(sortableChanges, 0, changes.Len())
  45. for _, change := range changes {
  46. action, err := change.Action()
  47. if err != nil {
  48. return nil, err
  49. }
  50. switch action {
  51. case merkletrie.Insert:
  52. added = append(added, sortableChange{change, change.To.TreeEntry.Hash})
  53. case merkletrie.Delete:
  54. deleted = append(deleted, sortableChange{change, change.From.TreeEntry.Hash})
  55. case merkletrie.Modify:
  56. reduced_changes = append(reduced_changes, change)
  57. }
  58. }
  59. sort.Sort(deleted)
  60. sort.Sort(added)
  61. a := 0
  62. d := 0
  63. still_deleted := make(object.Changes, 0, deleted.Len())
  64. still_added := make(object.Changes, 0, added.Len())
  65. for a < added.Len() && d < deleted.Len() {
  66. if added[a].hash == deleted[d].hash {
  67. reduced_changes = append(
  68. reduced_changes,
  69. &object.Change{From: deleted[d].change.From, To: added[a].change.To})
  70. a++; d++
  71. } else if added[a].Less(&deleted[d]) {
  72. still_added = append(still_added, added[a].change)
  73. a++
  74. } else {
  75. still_deleted = append(still_deleted, deleted[d].change)
  76. d++
  77. }
  78. }
  79. for ; a < added.Len(); a++ {
  80. still_added = append(still_added, added[a].change)
  81. }
  82. for ; d < deleted.Len(); d++ {
  83. still_deleted = append(still_deleted, deleted[d].change)
  84. }
  85. // Stage 2 - apply the similarity threshold
  86. // n^2 but actually linear
  87. // We sort the blobs by size and do the single linear scan.
  88. added_blobs := make(sortableBlobs, 0, still_added.Len())
  89. deleted_blobs := make(sortableBlobs, 0, still_deleted.Len())
  90. for _, change := range still_added {
  91. blob := cache[change.To.TreeEntry.Hash]
  92. added_blobs = append(
  93. added_blobs, sortableBlob{change: change, size: blob.Size})
  94. }
  95. for _, change := range still_deleted {
  96. blob := cache[change.From.TreeEntry.Hash]
  97. deleted_blobs = append(
  98. deleted_blobs, sortableBlob{change: change, size: blob.Size})
  99. }
  100. sort.Sort(added_blobs)
  101. sort.Sort(deleted_blobs)
  102. d_start := 0
  103. for a = 0; a < added_blobs.Len(); a++ {
  104. my_blob := cache[added_blobs[a].change.To.TreeEntry.Hash]
  105. my_size := added_blobs[a].size
  106. for d = d_start; d < deleted_blobs.Len() && !ra.sizesAreClose(my_size, deleted_blobs[d].size); d++ {
  107. }
  108. d_start = d
  109. found_match := false
  110. for d = d_start; d < deleted_blobs.Len() && ra.sizesAreClose(my_size, deleted_blobs[d].size); d++ {
  111. blobsAreClose, err := ra.blobsAreClose(
  112. my_blob, cache[deleted_blobs[d].change.From.TreeEntry.Hash])
  113. if err != nil {
  114. return nil, err
  115. }
  116. if blobsAreClose {
  117. found_match = true
  118. reduced_changes = append(
  119. reduced_changes,
  120. &object.Change{From: deleted_blobs[d].change.From,
  121. To: added_blobs[a].change.To})
  122. break
  123. }
  124. }
  125. if found_match {
  126. added_blobs = append(added_blobs[:a], added_blobs[a+1:]...)
  127. a--
  128. deleted_blobs = append(deleted_blobs[:d], deleted_blobs[d+1:]...)
  129. }
  130. }
  131. // Stage 3 - we give up, everything left are independent additions and deletions
  132. for _, blob := range added_blobs {
  133. reduced_changes = append(reduced_changes, blob.change)
  134. }
  135. for _, blob := range deleted_blobs {
  136. reduced_changes = append(reduced_changes, blob.change)
  137. }
  138. return map[string]interface{}{"renamed_changes": reduced_changes}, nil
  139. }
  140. func (ra *RenameAnalysis) Finalize() interface{} {
  141. return nil
  142. }
  143. func (ra *RenameAnalysis) sizesAreClose(size1 int64, size2 int64) bool {
  144. return abs64(size1-size2)*100/max64(1, min64(size1, size2)) <=
  145. int64(100-ra.SimilarityThreshold)
  146. }
  147. func (ra *RenameAnalysis) blobsAreClose(
  148. blob1 *object.Blob, blob2 *object.Blob) (bool, error) {
  149. str_from, err := blobToString(blob1)
  150. if err != nil {
  151. return false, err
  152. }
  153. str_to, err := blobToString(blob2)
  154. if err != nil {
  155. return false, err
  156. }
  157. dmp := diffmatchpatch.New()
  158. src, dst, _ := dmp.DiffLinesToRunes(str_from, str_to)
  159. diffs := dmp.DiffMainRunes(src, dst, false)
  160. common := 0
  161. for _, edit := range diffs {
  162. if edit.Type == diffmatchpatch.DiffEqual {
  163. common += utf8.RuneCountInString(edit.Text)
  164. }
  165. }
  166. return common*100/max(1, min(len(src), len(dst))) >= ra.SimilarityThreshold, nil
  167. }
  168. type sortableChange struct {
  169. change *object.Change
  170. hash plumbing.Hash
  171. }
  172. type sortableChanges []sortableChange
  173. func (change *sortableChange) Less(other *sortableChange) bool {
  174. for x := 0; x < 20; x++ {
  175. if change.hash[x] < other.hash[x] {
  176. return true
  177. }
  178. }
  179. return false
  180. }
  181. func (slice sortableChanges) Len() int {
  182. return len(slice)
  183. }
  184. func (slice sortableChanges) Less(i, j int) bool {
  185. return slice[i].Less(&slice[j])
  186. }
  187. func (slice sortableChanges) Swap(i, j int) {
  188. slice[i], slice[j] = slice[j], slice[i]
  189. }
  190. type sortableBlob struct {
  191. change *object.Change
  192. size int64
  193. }
  194. type sortableBlobs []sortableBlob
  195. func (change *sortableBlob) Less(other *sortableBlob) bool {
  196. return change.size < other.size
  197. }
  198. func (slice sortableBlobs) Len() int {
  199. return len(slice)
  200. }
  201. func (slice sortableBlobs) Less(i, j int) bool {
  202. return slice[i].Less(&slice[j])
  203. }
  204. func (slice sortableBlobs) Swap(i, j int) {
  205. slice[i], slice[j] = slice[j], slice[i]
  206. }