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.

temp_repo.go 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2019 The Gitea Authors.
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package pull
  6. import (
  7. "context"
  8. "fmt"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "code.gitea.io/gitea/models"
  13. repo_model "code.gitea.io/gitea/models/repo"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/log"
  16. repo_module "code.gitea.io/gitea/modules/repository"
  17. )
  18. // createTemporaryRepo creates a temporary repo with "base" for pr.BaseBranch and "tracking" for pr.HeadBranch
  19. // it also create a second base branch called "original_base"
  20. func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, error) {
  21. if err := pr.LoadHeadRepoCtx(ctx); err != nil {
  22. log.Error("LoadHeadRepo: %v", err)
  23. return "", fmt.Errorf("LoadHeadRepo: %v", err)
  24. } else if pr.HeadRepo == nil {
  25. log.Error("Pr %d HeadRepo %d does not exist", pr.ID, pr.HeadRepoID)
  26. return "", &repo_model.ErrRepoNotExist{
  27. ID: pr.HeadRepoID,
  28. }
  29. } else if err := pr.LoadBaseRepoCtx(ctx); err != nil {
  30. log.Error("LoadBaseRepo: %v", err)
  31. return "", fmt.Errorf("LoadBaseRepo: %v", err)
  32. } else if pr.BaseRepo == nil {
  33. log.Error("Pr %d BaseRepo %d does not exist", pr.ID, pr.BaseRepoID)
  34. return "", &repo_model.ErrRepoNotExist{
  35. ID: pr.BaseRepoID,
  36. }
  37. } else if err := pr.HeadRepo.GetOwner(ctx); err != nil {
  38. log.Error("HeadRepo.GetOwner: %v", err)
  39. return "", fmt.Errorf("HeadRepo.GetOwner: %v", err)
  40. } else if err := pr.BaseRepo.GetOwner(ctx); err != nil {
  41. log.Error("BaseRepo.GetOwner: %v", err)
  42. return "", fmt.Errorf("BaseRepo.GetOwner: %v", err)
  43. }
  44. // Clone base repo.
  45. tmpBasePath, err := repo_module.CreateTemporaryPath("pull")
  46. if err != nil {
  47. log.Error("CreateTemporaryPath: %v", err)
  48. return "", err
  49. }
  50. baseRepoPath := pr.BaseRepo.RepoPath()
  51. headRepoPath := pr.HeadRepo.RepoPath()
  52. if err := git.InitRepository(ctx, tmpBasePath, false); err != nil {
  53. log.Error("git init tmpBasePath: %v", err)
  54. if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
  55. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  56. }
  57. return "", err
  58. }
  59. remoteRepoName := "head_repo"
  60. baseBranch := "base"
  61. // Add head repo remote.
  62. addCacheRepo := func(staging, cache string) error {
  63. p := filepath.Join(staging, ".git", "objects", "info", "alternates")
  64. f, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
  65. if err != nil {
  66. log.Error("Could not create .git/objects/info/alternates file in %s: %v", staging, err)
  67. return err
  68. }
  69. defer f.Close()
  70. data := filepath.Join(cache, "objects")
  71. if _, err := fmt.Fprintln(f, data); err != nil {
  72. log.Error("Could not write to .git/objects/info/alternates file in %s: %v", staging, err)
  73. return err
  74. }
  75. return nil
  76. }
  77. if err := addCacheRepo(tmpBasePath, baseRepoPath); err != nil {
  78. log.Error("Unable to add base repository to temporary repo [%s -> %s]: %v", pr.BaseRepo.FullName(), tmpBasePath, err)
  79. if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
  80. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  81. }
  82. return "", fmt.Errorf("Unable to add base repository to temporary repo [%s -> tmpBasePath]: %v", pr.BaseRepo.FullName(), err)
  83. }
  84. var outbuf, errbuf strings.Builder
  85. if err := git.NewCommand(ctx, "remote", "add", "-t", pr.BaseBranch, "-m", pr.BaseBranch, "origin", baseRepoPath).
  86. Run(&git.RunOpts{
  87. Dir: tmpBasePath,
  88. Stdout: &outbuf,
  89. Stderr: &errbuf,
  90. }); err != nil {
  91. log.Error("Unable to add base repository as origin [%s -> %s]: %v\n%s\n%s", pr.BaseRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String())
  92. if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
  93. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  94. }
  95. return "", fmt.Errorf("Unable to add base repository as origin [%s -> tmpBasePath]: %v\n%s\n%s", pr.BaseRepo.FullName(), err, outbuf.String(), errbuf.String())
  96. }
  97. outbuf.Reset()
  98. errbuf.Reset()
  99. if err := git.NewCommand(ctx, "fetch", "origin", "--no-tags", "--", pr.BaseBranch+":"+baseBranch, pr.BaseBranch+":original_"+baseBranch).
  100. Run(&git.RunOpts{
  101. Dir: tmpBasePath,
  102. Stdout: &outbuf,
  103. Stderr: &errbuf,
  104. }); err != nil {
  105. log.Error("Unable to fetch origin base branch [%s:%s -> base, original_base in %s]: %v:\n%s\n%s", pr.BaseRepo.FullName(), pr.BaseBranch, tmpBasePath, err, outbuf.String(), errbuf.String())
  106. if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
  107. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  108. }
  109. return "", fmt.Errorf("Unable to fetch origin base branch [%s:%s -> base, original_base in tmpBasePath]: %v\n%s\n%s", pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
  110. }
  111. outbuf.Reset()
  112. errbuf.Reset()
  113. if err := git.NewCommand(ctx, "symbolic-ref", "HEAD", git.BranchPrefix+baseBranch).
  114. Run(&git.RunOpts{
  115. Dir: tmpBasePath,
  116. Stdout: &outbuf,
  117. Stderr: &errbuf,
  118. }); err != nil {
  119. log.Error("Unable to set HEAD as base branch [%s]: %v\n%s\n%s", tmpBasePath, err, outbuf.String(), errbuf.String())
  120. if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
  121. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  122. }
  123. return "", fmt.Errorf("Unable to set HEAD as base branch [tmpBasePath]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  124. }
  125. outbuf.Reset()
  126. errbuf.Reset()
  127. if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil {
  128. log.Error("Unable to add head repository to temporary repo [%s -> %s]: %v", pr.HeadRepo.FullName(), tmpBasePath, err)
  129. if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
  130. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  131. }
  132. return "", fmt.Errorf("Unable to head base repository to temporary repo [%s -> tmpBasePath]: %v", pr.HeadRepo.FullName(), err)
  133. }
  134. if err := git.NewCommand(ctx, "remote", "add", remoteRepoName, headRepoPath).
  135. Run(&git.RunOpts{
  136. Dir: tmpBasePath,
  137. Stdout: &outbuf,
  138. Stderr: &errbuf,
  139. }); err != nil {
  140. log.Error("Unable to add head repository as head_repo [%s -> %s]: %v\n%s\n%s", pr.HeadRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String())
  141. if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
  142. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  143. }
  144. return "", fmt.Errorf("Unable to add head repository as head_repo [%s -> tmpBasePath]: %v\n%s\n%s", pr.HeadRepo.FullName(), err, outbuf.String(), errbuf.String())
  145. }
  146. outbuf.Reset()
  147. errbuf.Reset()
  148. trackingBranch := "tracking"
  149. // Fetch head branch
  150. var headBranch string
  151. if pr.Flow == models.PullRequestFlowGithub {
  152. headBranch = git.BranchPrefix + pr.HeadBranch
  153. } else if len(pr.HeadCommitID) == 40 { // for not created pull request
  154. headBranch = pr.HeadCommitID
  155. } else {
  156. headBranch = pr.GetGitRefName()
  157. }
  158. if err := git.NewCommand(ctx, "fetch", "--no-tags", remoteRepoName, headBranch+":"+trackingBranch).
  159. Run(&git.RunOpts{
  160. Dir: tmpBasePath,
  161. Stdout: &outbuf,
  162. Stderr: &errbuf,
  163. }); err != nil {
  164. if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
  165. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  166. }
  167. if !git.IsBranchExist(ctx, pr.HeadRepo.RepoPath(), pr.HeadBranch) {
  168. return "", models.ErrBranchDoesNotExist{
  169. BranchName: pr.HeadBranch,
  170. }
  171. }
  172. log.Error("Unable to fetch head_repo head branch [%s:%s -> tracking in %s]: %v:\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, tmpBasePath, err, outbuf.String(), errbuf.String())
  173. return "", fmt.Errorf("Unable to fetch head_repo head branch [%s:%s -> tracking in tmpBasePath]: %v\n%s\n%s", pr.HeadRepo.FullName(), headBranch, err, outbuf.String(), errbuf.String())
  174. }
  175. outbuf.Reset()
  176. errbuf.Reset()
  177. return tmpBasePath, nil
  178. }