gitea源码

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