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_nogogit.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. "fmt"
  8. "io"
  9. "path"
  10. "sort"
  11. "code.gitea.io/gitea/modules/log"
  12. )
  13. // GetCommitsInfo gets information of all commits that are corresponding to these entries
  14. func (tes Entries) GetCommitsInfo(ctx context.Context, 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. var err error
  22. var revs map[string]*Commit
  23. if commit.repo.LastCommitCache != nil {
  24. var unHitPaths []string
  25. revs, unHitPaths, err = getLastCommitForPathsByCache(ctx, commit.ID.String(), treePath, entryPaths, commit.repo.LastCommitCache)
  26. if err != nil {
  27. return nil, nil, err
  28. }
  29. if len(unHitPaths) > 0 {
  30. sort.Strings(unHitPaths)
  31. commits, err := GetLastCommitForPaths(ctx, commit, treePath, unHitPaths)
  32. if err != nil {
  33. return nil, nil, err
  34. }
  35. for pth, found := range commits {
  36. revs[pth] = found
  37. }
  38. }
  39. } else {
  40. sort.Strings(entryPaths)
  41. revs, err = GetLastCommitForPaths(ctx, commit, treePath, entryPaths)
  42. }
  43. if err != nil {
  44. return nil, nil, err
  45. }
  46. commitsInfo := make([]CommitInfo, len(tes))
  47. for i, entry := range tes {
  48. commitsInfo[i] = CommitInfo{
  49. Entry: entry,
  50. }
  51. // Check if we have found a commit for this entry in time
  52. if entryCommit, ok := revs[entry.Name()]; ok {
  53. commitsInfo[i].Commit = entryCommit
  54. } else {
  55. log.Debug("missing commit for %s", entry.Name())
  56. }
  57. // If the entry if a submodule add a submodule file for this
  58. if entry.IsSubModule() {
  59. subModuleURL := ""
  60. var fullPath string
  61. if len(treePath) > 0 {
  62. fullPath = treePath + "/" + entry.Name()
  63. } else {
  64. fullPath = entry.Name()
  65. }
  66. if subModule, err := commit.GetSubModule(fullPath); err != nil {
  67. return nil, nil, err
  68. } else if subModule != nil {
  69. subModuleURL = subModule.URL
  70. }
  71. subModuleFile := NewSubModuleFile(commitsInfo[i].Commit, subModuleURL, entry.ID.String())
  72. commitsInfo[i].SubModuleFile = subModuleFile
  73. }
  74. }
  75. // Retrieve the commit for the treePath itself (see above). We basically
  76. // get it for free during the tree traversal and it's used for listing
  77. // pages to display information about newest commit for a given path.
  78. var treeCommit *Commit
  79. var ok bool
  80. if treePath == "" {
  81. treeCommit = commit
  82. } else if treeCommit, ok = revs[""]; ok {
  83. treeCommit.repo = commit.repo
  84. }
  85. return commitsInfo, treeCommit, nil
  86. }
  87. func getLastCommitForPathsByCache(ctx context.Context, commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) {
  88. var unHitEntryPaths []string
  89. results := make(map[string]*Commit)
  90. for _, p := range paths {
  91. lastCommit, err := cache.Get(commitID, path.Join(treePath, p))
  92. if err != nil {
  93. return nil, nil, err
  94. }
  95. if lastCommit != nil {
  96. results[p] = lastCommit
  97. continue
  98. }
  99. unHitEntryPaths = append(unHitEntryPaths, p)
  100. }
  101. return results, unHitEntryPaths, nil
  102. }
  103. // GetLastCommitForPaths returns last commit information
  104. func GetLastCommitForPaths(ctx context.Context, commit *Commit, treePath string, paths []string) (map[string]*Commit, error) {
  105. // We read backwards from the commit to obtain all of the commits
  106. revs, err := WalkGitLog(ctx, commit.repo, commit, treePath, paths...)
  107. if err != nil {
  108. return nil, err
  109. }
  110. batchStdinWriter, batchReader, cancel := commit.repo.CatFileBatch(ctx)
  111. defer cancel()
  112. commitsMap := map[string]*Commit{}
  113. commitsMap[commit.ID.String()] = commit
  114. commitCommits := map[string]*Commit{}
  115. for path, commitID := range revs {
  116. c, ok := commitsMap[commitID]
  117. if ok {
  118. commitCommits[path] = c
  119. continue
  120. }
  121. if len(commitID) == 0 {
  122. continue
  123. }
  124. _, err := batchStdinWriter.Write([]byte(commitID + "\n"))
  125. if err != nil {
  126. return nil, err
  127. }
  128. _, typ, size, err := ReadBatchLine(batchReader)
  129. if err != nil {
  130. return nil, err
  131. }
  132. if typ != "commit" {
  133. if err := DiscardFull(batchReader, size+1); err != nil {
  134. return nil, err
  135. }
  136. return nil, fmt.Errorf("unexpected type: %s for commit id: %s", typ, commitID)
  137. }
  138. c, err = CommitFromReader(commit.repo, MustIDFromString(commitID), io.LimitReader(batchReader, size))
  139. if err != nil {
  140. return nil, err
  141. }
  142. if _, err := batchReader.Discard(1); err != nil {
  143. return nil, err
  144. }
  145. commitCommits[path] = c
  146. }
  147. return commitCommits, nil
  148. }