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_gogit.go 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. //go:build gogit
  5. package git
  6. import (
  7. "context"
  8. "path"
  9. "github.com/emirpasic/gods/trees/binaryheap"
  10. "github.com/go-git/go-git/v5/plumbing"
  11. "github.com/go-git/go-git/v5/plumbing/object"
  12. cgobject "github.com/go-git/go-git/v5/plumbing/object/commitgraph"
  13. )
  14. // GetCommitsInfo gets information of all commits that are corresponding to these entries
  15. func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath string, cache *LastCommitCache) ([]CommitInfo, *Commit, error) {
  16. entryPaths := make([]string, len(tes)+1)
  17. // Get the commit for the treePath itself
  18. entryPaths[0] = ""
  19. for i, entry := range tes {
  20. entryPaths[i+1] = entry.Name()
  21. }
  22. commitNodeIndex, commitGraphFile := commit.repo.CommitNodeIndex()
  23. if commitGraphFile != nil {
  24. defer commitGraphFile.Close()
  25. }
  26. c, err := commitNodeIndex.Get(commit.ID)
  27. if err != nil {
  28. return nil, nil, err
  29. }
  30. var revs map[string]*object.Commit
  31. if cache != nil {
  32. var unHitPaths []string
  33. revs, unHitPaths, err = getLastCommitForPathsByCache(commit.ID.String(), treePath, entryPaths, cache)
  34. if err != nil {
  35. return nil, nil, err
  36. }
  37. if len(unHitPaths) > 0 {
  38. revs2, err := GetLastCommitForPaths(ctx, cache, c, treePath, unHitPaths)
  39. if err != nil {
  40. return nil, nil, err
  41. }
  42. for k, v := range revs2 {
  43. revs[k] = v
  44. }
  45. }
  46. } else {
  47. revs, err = GetLastCommitForPaths(ctx, nil, c, treePath, entryPaths)
  48. }
  49. if err != nil {
  50. return nil, nil, err
  51. }
  52. commit.repo.gogitStorage.Close()
  53. commitsInfo := make([]CommitInfo, len(tes))
  54. for i, entry := range tes {
  55. commitsInfo[i] = CommitInfo{
  56. Entry: entry,
  57. }
  58. // Check if we have found a commit for this entry in time
  59. if rev, ok := revs[entry.Name()]; ok {
  60. entryCommit := convertCommit(rev)
  61. commitsInfo[i].Commit = entryCommit
  62. }
  63. // If the entry if a submodule add a submodule file for this
  64. if entry.IsSubModule() {
  65. subModuleURL := ""
  66. var fullPath string
  67. if len(treePath) > 0 {
  68. fullPath = treePath + "/" + entry.Name()
  69. } else {
  70. fullPath = entry.Name()
  71. }
  72. if subModule, err := commit.GetSubModule(fullPath); err != nil {
  73. return nil, nil, err
  74. } else if subModule != nil {
  75. subModuleURL = subModule.URL
  76. }
  77. subModuleFile := NewSubModuleFile(commitsInfo[i].Commit, subModuleURL, entry.ID.String())
  78. commitsInfo[i].SubModuleFile = subModuleFile
  79. }
  80. }
  81. // Retrieve the commit for the treePath itself (see above). We basically
  82. // get it for free during the tree traversal and it's used for listing
  83. // pages to display information about newest commit for a given path.
  84. var treeCommit *Commit
  85. if treePath == "" {
  86. treeCommit = commit
  87. } else if rev, ok := revs[""]; ok {
  88. treeCommit = convertCommit(rev)
  89. treeCommit.repo = commit.repo
  90. }
  91. return commitsInfo, treeCommit, nil
  92. }
  93. type commitAndPaths struct {
  94. commit cgobject.CommitNode
  95. // Paths that are still on the branch represented by commit
  96. paths []string
  97. // Set of hashes for the paths
  98. hashes map[string]plumbing.Hash
  99. }
  100. func getCommitTree(c cgobject.CommitNode, treePath string) (*object.Tree, error) {
  101. tree, err := c.Tree()
  102. if err != nil {
  103. return nil, err
  104. }
  105. // Optimize deep traversals by focusing only on the specific tree
  106. if treePath != "" {
  107. tree, err = tree.Tree(treePath)
  108. if err != nil {
  109. return nil, err
  110. }
  111. }
  112. return tree, nil
  113. }
  114. func getFileHashes(c cgobject.CommitNode, treePath string, paths []string) (map[string]plumbing.Hash, error) {
  115. tree, err := getCommitTree(c, treePath)
  116. if err == object.ErrDirectoryNotFound {
  117. // The whole tree didn't exist, so return empty map
  118. return make(map[string]plumbing.Hash), nil
  119. }
  120. if err != nil {
  121. return nil, err
  122. }
  123. hashes := make(map[string]plumbing.Hash)
  124. for _, path := range paths {
  125. if path != "" {
  126. entry, err := tree.FindEntry(path)
  127. if err == nil {
  128. hashes[path] = entry.Hash
  129. }
  130. } else {
  131. hashes[path] = tree.Hash
  132. }
  133. }
  134. return hashes, nil
  135. }
  136. func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*object.Commit, []string, error) {
  137. var unHitEntryPaths []string
  138. results := make(map[string]*object.Commit)
  139. for _, p := range paths {
  140. lastCommit, err := cache.Get(commitID, path.Join(treePath, p))
  141. if err != nil {
  142. return nil, nil, err
  143. }
  144. if lastCommit != nil {
  145. results[p] = lastCommit.(*object.Commit)
  146. continue
  147. }
  148. unHitEntryPaths = append(unHitEntryPaths, p)
  149. }
  150. return results, unHitEntryPaths, nil
  151. }
  152. // GetLastCommitForPaths returns last commit information
  153. func GetLastCommitForPaths(ctx context.Context, cache *LastCommitCache, c cgobject.CommitNode, treePath string, paths []string) (map[string]*object.Commit, error) {
  154. refSha := c.ID().String()
  155. // We do a tree traversal with nodes sorted by commit time
  156. heap := binaryheap.NewWith(func(a, b interface{}) int {
  157. if a.(*commitAndPaths).commit.CommitTime().Before(b.(*commitAndPaths).commit.CommitTime()) {
  158. return 1
  159. }
  160. return -1
  161. })
  162. resultNodes := make(map[string]cgobject.CommitNode)
  163. initialHashes, err := getFileHashes(c, treePath, paths)
  164. if err != nil {
  165. return nil, err
  166. }
  167. // Start search from the root commit and with full set of paths
  168. heap.Push(&commitAndPaths{c, paths, initialHashes})
  169. heaploop:
  170. for {
  171. select {
  172. case <-ctx.Done():
  173. if ctx.Err() == context.DeadlineExceeded {
  174. break heaploop
  175. }
  176. return nil, ctx.Err()
  177. default:
  178. }
  179. cIn, ok := heap.Pop()
  180. if !ok {
  181. break
  182. }
  183. current := cIn.(*commitAndPaths)
  184. // Load the parent commits for the one we are currently examining
  185. numParents := current.commit.NumParents()
  186. var parents []cgobject.CommitNode
  187. for i := 0; i < numParents; i++ {
  188. parent, err := current.commit.ParentNode(i)
  189. if err != nil {
  190. break
  191. }
  192. parents = append(parents, parent)
  193. }
  194. // Examine the current commit and set of interesting paths
  195. pathUnchanged := make([]bool, len(current.paths))
  196. parentHashes := make([]map[string]plumbing.Hash, len(parents))
  197. for j, parent := range parents {
  198. parentHashes[j], err = getFileHashes(parent, treePath, current.paths)
  199. if err != nil {
  200. break
  201. }
  202. for i, path := range current.paths {
  203. if parentHashes[j][path] == current.hashes[path] {
  204. pathUnchanged[i] = true
  205. }
  206. }
  207. }
  208. var remainingPaths []string
  209. for i, pth := range current.paths {
  210. // The results could already contain some newer change for the same path,
  211. // so don't override that and bail out on the file early.
  212. if resultNodes[pth] == nil {
  213. if pathUnchanged[i] {
  214. // The path existed with the same hash in at least one parent so it could
  215. // not have been changed in this commit directly.
  216. remainingPaths = append(remainingPaths, pth)
  217. } else {
  218. // There are few possible cases how can we get here:
  219. // - The path didn't exist in any parent, so it must have been created by
  220. // this commit.
  221. // - The path did exist in the parent commit, but the hash of the file has
  222. // changed.
  223. // - We are looking at a merge commit and the hash of the file doesn't
  224. // match any of the hashes being merged. This is more common for directories,
  225. // but it can also happen if a file is changed through conflict resolution.
  226. resultNodes[pth] = current.commit
  227. if err := cache.Put(refSha, path.Join(treePath, pth), current.commit.ID().String()); err != nil {
  228. return nil, err
  229. }
  230. }
  231. }
  232. }
  233. if len(remainingPaths) > 0 {
  234. // Add the parent nodes along with remaining paths to the heap for further
  235. // processing.
  236. for j, parent := range parents {
  237. // Combine remainingPath with paths available on the parent branch
  238. // and make union of them
  239. remainingPathsForParent := make([]string, 0, len(remainingPaths))
  240. newRemainingPaths := make([]string, 0, len(remainingPaths))
  241. for _, path := range remainingPaths {
  242. if parentHashes[j][path] == current.hashes[path] {
  243. remainingPathsForParent = append(remainingPathsForParent, path)
  244. } else {
  245. newRemainingPaths = append(newRemainingPaths, path)
  246. }
  247. }
  248. if remainingPathsForParent != nil {
  249. heap.Push(&commitAndPaths{parent, remainingPathsForParent, parentHashes[j]})
  250. }
  251. if len(newRemainingPaths) == 0 {
  252. break
  253. } else {
  254. remainingPaths = newRemainingPaths
  255. }
  256. }
  257. }
  258. }
  259. // Post-processing
  260. result := make(map[string]*object.Commit)
  261. for path, commitNode := range resultNodes {
  262. var err error
  263. result[path], err = commitNode.Commit()
  264. if err != nil {
  265. return nil, err
  266. }
  267. }
  268. return result, nil
  269. }