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.5KB

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