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