Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

commit_info_nogogit.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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) ([]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 commit.repo.LastCommitCache != nil {
  25. var unHitPaths []string
  26. revs, unHitPaths, err = getLastCommitForPathsByCache(ctx, commit.ID.String(), treePath, entryPaths, commit.repo.LastCommitCache)
  27. if err != nil {
  28. return nil, nil, err
  29. }
  30. if len(unHitPaths) > 0 {
  31. sort.Strings(unHitPaths)
  32. commits, err := GetLastCommitForPaths(ctx, 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, 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. var unHitEntryPaths []string
  90. results := make(map[string]*Commit)
  91. for _, p := range paths {
  92. lastCommit, err := cache.Get(commitID, path.Join(treePath, p))
  93. if err != nil {
  94. return nil, nil, err
  95. }
  96. if lastCommit != nil {
  97. results[p] = lastCommit
  98. continue
  99. }
  100. unHitEntryPaths = append(unHitEntryPaths, p)
  101. }
  102. return results, unHitEntryPaths, nil
  103. }
  104. // GetLastCommitForPaths returns last commit information
  105. func GetLastCommitForPaths(ctx context.Context, commit *Commit, treePath string, paths []string) (map[string]*Commit, error) {
  106. // We read backwards from the commit to obtain all of the commits
  107. revs, err := WalkGitLog(ctx, commit.repo, commit, treePath, paths...)
  108. if err != nil {
  109. return nil, err
  110. }
  111. batchStdinWriter, batchReader, cancel := commit.repo.CatFileBatch(ctx)
  112. defer cancel()
  113. commitsMap := map[string]*Commit{}
  114. commitsMap[commit.ID.String()] = commit
  115. commitCommits := map[string]*Commit{}
  116. for path, commitID := range revs {
  117. c, ok := commitsMap[commitID]
  118. if ok {
  119. commitCommits[path] = c
  120. continue
  121. }
  122. if len(commitID) == 0 {
  123. continue
  124. }
  125. _, err := batchStdinWriter.Write([]byte(commitID + "\n"))
  126. if err != nil {
  127. return nil, err
  128. }
  129. _, typ, size, err := ReadBatchLine(batchReader)
  130. if err != nil {
  131. return nil, err
  132. }
  133. if typ != "commit" {
  134. return nil, fmt.Errorf("unexpected type: %s for commit id: %s", typ, commitID)
  135. }
  136. c, err = CommitFromReader(commit.repo, MustIDFromString(commitID), io.LimitReader(batchReader, size))
  137. if err != nil {
  138. return nil, err
  139. }
  140. if _, err := batchReader.Discard(1); err != nil {
  141. return nil, err
  142. }
  143. commitCommits[path] = c
  144. }
  145. return commitCommits, nil
  146. }