gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "maps"
  8. "path"
  9. "sort"
  10. "code.gitea.io/gitea/modules/log"
  11. )
  12. // GetCommitsInfo gets information of all commits that are corresponding to these entries
  13. func (tes Entries) GetCommitsInfo(ctx context.Context, repoLink string, commit *Commit, treePath string) ([]CommitInfo, *Commit, error) {
  14. entryPaths := make([]string, len(tes)+1)
  15. // Get the commit for the treePath itself
  16. entryPaths[0] = ""
  17. for i, entry := range tes {
  18. entryPaths[i+1] = entry.Name()
  19. }
  20. var err error
  21. var revs map[string]*Commit
  22. if commit.repo.LastCommitCache != nil {
  23. var unHitPaths []string
  24. revs, unHitPaths, err = getLastCommitForPathsByCache(commit.ID.String(), treePath, entryPaths, commit.repo.LastCommitCache)
  25. if err != nil {
  26. return nil, nil, err
  27. }
  28. if len(unHitPaths) > 0 {
  29. sort.Strings(unHitPaths)
  30. commits, err := GetLastCommitForPaths(ctx, commit, treePath, unHitPaths)
  31. if err != nil {
  32. return nil, nil, err
  33. }
  34. maps.Copy(revs, commits)
  35. }
  36. } else {
  37. sort.Strings(entryPaths)
  38. revs, err = GetLastCommitForPaths(ctx, commit, treePath, entryPaths)
  39. }
  40. if err != nil {
  41. return nil, nil, err
  42. }
  43. commitsInfo := make([]CommitInfo, len(tes))
  44. for i, entry := range tes {
  45. commitsInfo[i] = CommitInfo{
  46. Entry: entry,
  47. }
  48. // Check if we have found a commit for this entry in time
  49. if entryCommit, ok := revs[entry.Name()]; ok {
  50. commitsInfo[i].Commit = entryCommit
  51. } else {
  52. log.Debug("missing commit for %s", entry.Name())
  53. }
  54. // If the entry is a submodule, add a submodule file for this
  55. if entry.IsSubModule() {
  56. commitsInfo[i].SubmoduleFile, err = GetCommitInfoSubmoduleFile(repoLink, path.Join(treePath, entry.Name()), commit, entry.ID)
  57. if err != nil {
  58. return nil, nil, err
  59. }
  60. }
  61. }
  62. // Retrieve the commit for the treePath itself (see above). We basically
  63. // get it for free during the tree traversal, and it's used for listing
  64. // pages to display information about the newest commit for a given path.
  65. var treeCommit *Commit
  66. var ok bool
  67. if treePath == "" {
  68. treeCommit = commit
  69. } else if treeCommit, ok = revs[""]; ok {
  70. treeCommit.repo = commit.repo
  71. }
  72. return commitsInfo, treeCommit, nil
  73. }
  74. func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) {
  75. var unHitEntryPaths []string
  76. results := make(map[string]*Commit)
  77. for _, p := range paths {
  78. lastCommit, err := cache.Get(commitID, path.Join(treePath, p))
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. if lastCommit != nil {
  83. results[p] = lastCommit
  84. continue
  85. }
  86. unHitEntryPaths = append(unHitEntryPaths, p)
  87. }
  88. return results, unHitEntryPaths, nil
  89. }
  90. // GetLastCommitForPaths returns last commit information
  91. func GetLastCommitForPaths(ctx context.Context, commit *Commit, treePath string, paths []string) (map[string]*Commit, error) {
  92. // We read backwards from the commit to obtain all of the commits
  93. revs, err := WalkGitLog(ctx, commit.repo, commit, treePath, paths...)
  94. if err != nil {
  95. return nil, err
  96. }
  97. commitsMap := map[string]*Commit{}
  98. commitsMap[commit.ID.String()] = commit
  99. commitCommits := map[string]*Commit{}
  100. for path, commitID := range revs {
  101. if len(commitID) == 0 {
  102. continue
  103. }
  104. c, ok := commitsMap[commitID]
  105. if ok {
  106. commitCommits[path] = c
  107. continue
  108. }
  109. c, err := commit.repo.GetCommit(commitID) // Ensure the commit exists in the repository
  110. if err != nil {
  111. return nil, err
  112. }
  113. commitCommits[path] = c
  114. }
  115. return commitCommits, nil
  116. }