You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commit_info.go 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "github.com/emirpasic/gods/trees/binaryheap"
  7. "gopkg.in/src-d/go-git.v4/plumbing"
  8. "gopkg.in/src-d/go-git.v4/plumbing/object"
  9. cgobject "gopkg.in/src-d/go-git.v4/plumbing/object/commitgraph"
  10. )
  11. // GetCommitsInfo gets information of all commits that are corresponding to these entries
  12. func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache LastCommitCache) ([][]interface{}, *Commit, error) {
  13. entryPaths := make([]string, len(tes)+1)
  14. // Get the commit for the treePath itself
  15. entryPaths[0] = ""
  16. for i, entry := range tes {
  17. entryPaths[i+1] = entry.Name()
  18. }
  19. commitNodeIndex, commitGraphFile := commit.repo.CommitNodeIndex()
  20. if commitGraphFile != nil {
  21. defer commitGraphFile.Close()
  22. }
  23. c, err := commitNodeIndex.Get(plumbing.Hash(commit.ID))
  24. if err != nil {
  25. return nil, nil, err
  26. }
  27. revs, err := getLastCommitForPaths(c, treePath, entryPaths)
  28. if err != nil {
  29. return nil, nil, err
  30. }
  31. commit.repo.gogitStorage.Close()
  32. commitsInfo := make([][]interface{}, len(tes))
  33. for i, entry := range tes {
  34. if rev, ok := revs[entry.Name()]; ok {
  35. entryCommit := convertCommit(rev)
  36. if entry.IsSubModule() {
  37. subModuleURL := ""
  38. var fullPath string
  39. if len(treePath) > 0 {
  40. fullPath = treePath + "/" + entry.Name()
  41. } else {
  42. fullPath = entry.Name()
  43. }
  44. if subModule, err := commit.GetSubModule(fullPath); err != nil {
  45. return nil, nil, err
  46. } else if subModule != nil {
  47. subModuleURL = subModule.URL
  48. }
  49. subModuleFile := NewSubModuleFile(entryCommit, subModuleURL, entry.ID.String())
  50. commitsInfo[i] = []interface{}{entry, subModuleFile}
  51. } else {
  52. commitsInfo[i] = []interface{}{entry, entryCommit}
  53. }
  54. } else {
  55. commitsInfo[i] = []interface{}{entry, nil}
  56. }
  57. }
  58. // Retrieve the commit for the treePath itself (see above). We basically
  59. // get it for free during the tree traversal and it's used for listing
  60. // pages to display information about newest commit for a given path.
  61. var treeCommit *Commit
  62. if treePath == "" {
  63. treeCommit = commit
  64. } else if rev, ok := revs[""]; ok {
  65. treeCommit = convertCommit(rev)
  66. }
  67. return commitsInfo, treeCommit, nil
  68. }
  69. type commitAndPaths struct {
  70. commit cgobject.CommitNode
  71. // Paths that are still on the branch represented by commit
  72. paths []string
  73. // Set of hashes for the paths
  74. hashes map[string]plumbing.Hash
  75. }
  76. func getCommitTree(c cgobject.CommitNode, treePath string) (*object.Tree, error) {
  77. tree, err := c.Tree()
  78. if err != nil {
  79. return nil, err
  80. }
  81. // Optimize deep traversals by focusing only on the specific tree
  82. if treePath != "" {
  83. tree, err = tree.Tree(treePath)
  84. if err != nil {
  85. return nil, err
  86. }
  87. }
  88. return tree, nil
  89. }
  90. func getFileHashes(c cgobject.CommitNode, treePath string, paths []string) (map[string]plumbing.Hash, error) {
  91. tree, err := getCommitTree(c, treePath)
  92. if err == object.ErrDirectoryNotFound {
  93. // The whole tree didn't exist, so return empty map
  94. return make(map[string]plumbing.Hash), nil
  95. }
  96. if err != nil {
  97. return nil, err
  98. }
  99. hashes := make(map[string]plumbing.Hash)
  100. for _, path := range paths {
  101. if path != "" {
  102. entry, err := tree.FindEntry(path)
  103. if err == nil {
  104. hashes[path] = entry.Hash
  105. }
  106. } else {
  107. hashes[path] = tree.Hash
  108. }
  109. }
  110. return hashes, nil
  111. }
  112. func getLastCommitForPaths(c cgobject.CommitNode, treePath string, paths []string) (map[string]*object.Commit, error) {
  113. // We do a tree traversal with nodes sorted by commit time
  114. heap := binaryheap.NewWith(func(a, b interface{}) int {
  115. if a.(*commitAndPaths).commit.CommitTime().Before(b.(*commitAndPaths).commit.CommitTime()) {
  116. return 1
  117. }
  118. return -1
  119. })
  120. resultNodes := make(map[string]cgobject.CommitNode)
  121. initialHashes, err := getFileHashes(c, treePath, paths)
  122. if err != nil {
  123. return nil, err
  124. }
  125. // Start search from the root commit and with full set of paths
  126. heap.Push(&commitAndPaths{c, paths, initialHashes})
  127. for {
  128. cIn, ok := heap.Pop()
  129. if !ok {
  130. break
  131. }
  132. current := cIn.(*commitAndPaths)
  133. // Load the parent commits for the one we are currently examining
  134. numParents := current.commit.NumParents()
  135. var parents []cgobject.CommitNode
  136. for i := 0; i < numParents; i++ {
  137. parent, err := current.commit.ParentNode(i)
  138. if err != nil {
  139. break
  140. }
  141. parents = append(parents, parent)
  142. }
  143. // Examine the current commit and set of interesting paths
  144. pathUnchanged := make([]bool, len(current.paths))
  145. parentHashes := make([]map[string]plumbing.Hash, len(parents))
  146. for j, parent := range parents {
  147. parentHashes[j], err = getFileHashes(parent, treePath, current.paths)
  148. if err != nil {
  149. break
  150. }
  151. for i, path := range current.paths {
  152. if parentHashes[j][path] == current.hashes[path] {
  153. pathUnchanged[i] = true
  154. }
  155. }
  156. }
  157. var remainingPaths []string
  158. for i, path := range current.paths {
  159. // The results could already contain some newer change for the same path,
  160. // so don't override that and bail out on the file early.
  161. if resultNodes[path] == nil {
  162. if pathUnchanged[i] {
  163. // The path existed with the same hash in at least one parent so it could
  164. // not have been changed in this commit directly.
  165. remainingPaths = append(remainingPaths, path)
  166. } else {
  167. // There are few possible cases how can we get here:
  168. // - The path didn't exist in any parent, so it must have been created by
  169. // this commit.
  170. // - The path did exist in the parent commit, but the hash of the file has
  171. // changed.
  172. // - We are looking at a merge commit and the hash of the file doesn't
  173. // match any of the hashes being merged. This is more common for directories,
  174. // but it can also happen if a file is changed through conflict resolution.
  175. resultNodes[path] = current.commit
  176. }
  177. }
  178. }
  179. if len(remainingPaths) > 0 {
  180. // Add the parent nodes along with remaining paths to the heap for further
  181. // processing.
  182. for j, parent := range parents {
  183. // Combine remainingPath with paths available on the parent branch
  184. // and make union of them
  185. remainingPathsForParent := make([]string, 0, len(remainingPaths))
  186. newRemainingPaths := make([]string, 0, len(remainingPaths))
  187. for _, path := range remainingPaths {
  188. if parentHashes[j][path] == current.hashes[path] {
  189. remainingPathsForParent = append(remainingPathsForParent, path)
  190. } else {
  191. newRemainingPaths = append(newRemainingPaths, path)
  192. }
  193. }
  194. if remainingPathsForParent != nil {
  195. heap.Push(&commitAndPaths{parent, remainingPathsForParent, parentHashes[j]})
  196. }
  197. if len(newRemainingPaths) == 0 {
  198. break
  199. } else {
  200. remainingPaths = newRemainingPaths
  201. }
  202. }
  203. }
  204. }
  205. // Post-processing
  206. result := make(map[string]*object.Commit)
  207. for path, commitNode := range resultNodes {
  208. var err error
  209. result[path], err = commitNode.Commit()
  210. if err != nil {
  211. return nil, err
  212. }
  213. }
  214. return result, nil
  215. }