renames.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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++
  71. d++
  72. } else if added[a].Less(&deleted[d]) {
  73. still_added = append(still_added, added[a].change)
  74. a++
  75. } else {
  76. still_deleted = append(still_deleted, deleted[d].change)
  77. d++
  78. }
  79. }
  80. for ; a < added.Len(); a++ {
  81. still_added = append(still_added, added[a].change)
  82. }
  83. for ; d < deleted.Len(); d++ {
  84. still_deleted = append(still_deleted, deleted[d].change)
  85. }
  86. // Stage 2 - apply the similarity threshold
  87. // n^2 but actually linear
  88. // We sort the blobs by size and do the single linear scan.
  89. added_blobs := make(sortableBlobs, 0, still_added.Len())
  90. deleted_blobs := make(sortableBlobs, 0, still_deleted.Len())
  91. for _, change := range still_added {
  92. blob := cache[change.To.TreeEntry.Hash]
  93. added_blobs = append(
  94. added_blobs, sortableBlob{change: change, size: blob.Size})
  95. }
  96. for _, change := range still_deleted {
  97. blob := cache[change.From.TreeEntry.Hash]
  98. deleted_blobs = append(
  99. deleted_blobs, sortableBlob{change: change, size: blob.Size})
  100. }
  101. sort.Sort(added_blobs)
  102. sort.Sort(deleted_blobs)
  103. d_start := 0
  104. for a = 0; a < added_blobs.Len(); a++ {
  105. my_blob := cache[added_blobs[a].change.To.TreeEntry.Hash]
  106. my_size := added_blobs[a].size
  107. for d = d_start; d < deleted_blobs.Len() && !ra.sizesAreClose(my_size, deleted_blobs[d].size); d++ {
  108. }
  109. d_start = d
  110. found_match := false
  111. for d = d_start; d < deleted_blobs.Len() && ra.sizesAreClose(my_size, deleted_blobs[d].size); d++ {
  112. blobsAreClose, err := ra.blobsAreClose(
  113. my_blob, cache[deleted_blobs[d].change.From.TreeEntry.Hash])
  114. if err != nil {
  115. return nil, err
  116. }
  117. if blobsAreClose {
  118. found_match = true
  119. reduced_changes = append(
  120. reduced_changes,
  121. &object.Change{From: deleted_blobs[d].change.From,
  122. To: added_blobs[a].change.To})
  123. break
  124. }
  125. }
  126. if found_match {
  127. added_blobs = append(added_blobs[:a], added_blobs[a+1:]...)
  128. a--
  129. deleted_blobs = append(deleted_blobs[:d], deleted_blobs[d+1:]...)
  130. }
  131. }
  132. // Stage 3 - we give up, everything left are independent additions and deletions
  133. for _, blob := range added_blobs {
  134. reduced_changes = append(reduced_changes, blob.change)
  135. }
  136. for _, blob := range deleted_blobs {
  137. reduced_changes = append(reduced_changes, blob.change)
  138. }
  139. return map[string]interface{}{"renamed_changes": reduced_changes}, nil
  140. }
  141. func (ra *RenameAnalysis) Finalize() interface{} {
  142. return nil
  143. }
  144. func (ra *RenameAnalysis) sizesAreClose(size1 int64, size2 int64) bool {
  145. return abs64(size1-size2)*100/max64(1, min64(size1, size2)) <=
  146. int64(100-ra.SimilarityThreshold)
  147. }
  148. func (ra *RenameAnalysis) blobsAreClose(
  149. blob1 *object.Blob, blob2 *object.Blob) (bool, error) {
  150. str_from, err := blobToString(blob1)
  151. if err != nil {
  152. return false, err
  153. }
  154. str_to, err := blobToString(blob2)
  155. if err != nil {
  156. return false, err
  157. }
  158. dmp := diffmatchpatch.New()
  159. src, dst, _ := dmp.DiffLinesToRunes(str_from, str_to)
  160. diffs := dmp.DiffMainRunes(src, dst, false)
  161. common := 0
  162. for _, edit := range diffs {
  163. if edit.Type == diffmatchpatch.DiffEqual {
  164. common += utf8.RuneCountInString(edit.Text)
  165. }
  166. }
  167. return common*100/max(1, min(len(src), len(dst))) >= ra.SimilarityThreshold, nil
  168. }
  169. type sortableChange struct {
  170. change *object.Change
  171. hash plumbing.Hash
  172. }
  173. type sortableChanges []sortableChange
  174. func (change *sortableChange) Less(other *sortableChange) bool {
  175. for x := 0; x < 20; x++ {
  176. if change.hash[x] < other.hash[x] {
  177. return true
  178. }
  179. }
  180. return false
  181. }
  182. func (slice sortableChanges) Len() int {
  183. return len(slice)
  184. }
  185. func (slice sortableChanges) Less(i, j int) bool {
  186. return slice[i].Less(&slice[j])
  187. }
  188. func (slice sortableChanges) Swap(i, j int) {
  189. slice[i], slice[j] = slice[j], slice[i]
  190. }
  191. type sortableBlob struct {
  192. change *object.Change
  193. size int64
  194. }
  195. type sortableBlobs []sortableBlob
  196. func (change *sortableBlob) Less(other *sortableBlob) bool {
  197. return change.size < other.size
  198. }
  199. func (slice sortableBlobs) Len() int {
  200. return len(slice)
  201. }
  202. func (slice sortableBlobs) Less(i, j int) bool {
  203. return slice[i].Less(&slice[j])
  204. }
  205. func (slice sortableBlobs) Swap(i, j int) {
  206. slice[i], slice[j] = slice[j], slice[i]
  207. }