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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. "fmt"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/log"
  14. )
  15. // createTemporaryRepo creates a temporary repo with "base" for pr.BaseBranch and "tracking" for pr.HeadBranch
  16. // it also create a second base branch called "original_base"
  17. func createTemporaryRepo(pr *models.PullRequest) (string, error) {
  18. if err := pr.LoadHeadRepo(); err != nil {
  19. log.Error("LoadHeadRepo: %v", err)
  20. return "", fmt.Errorf("LoadHeadRepo: %v", err)
  21. } else if pr.HeadRepo == nil {
  22. log.Error("Pr %d HeadRepo %d does not exist", pr.ID, pr.HeadRepoID)
  23. return "", &models.ErrRepoNotExist{
  24. ID: pr.HeadRepoID,
  25. }
  26. } else if err := pr.LoadBaseRepo(); err != nil {
  27. log.Error("LoadBaseRepo: %v", err)
  28. return "", fmt.Errorf("LoadBaseRepo: %v", err)
  29. } else if pr.BaseRepo == nil {
  30. log.Error("Pr %d BaseRepo %d does not exist", pr.ID, pr.BaseRepoID)
  31. return "", &models.ErrRepoNotExist{
  32. ID: pr.BaseRepoID,
  33. }
  34. } else if err := pr.HeadRepo.GetOwner(); err != nil {
  35. log.Error("HeadRepo.GetOwner: %v", err)
  36. return "", fmt.Errorf("HeadRepo.GetOwner: %v", err)
  37. } else if err := pr.BaseRepo.GetOwner(); err != nil {
  38. log.Error("BaseRepo.GetOwner: %v", err)
  39. return "", fmt.Errorf("BaseRepo.GetOwner: %v", err)
  40. }
  41. // Clone base repo.
  42. tmpBasePath, err := models.CreateTemporaryPath("pull")
  43. if err != nil {
  44. log.Error("CreateTemporaryPath: %v", err)
  45. return "", err
  46. }
  47. baseRepoPath := pr.BaseRepo.RepoPath()
  48. headRepoPath := pr.HeadRepo.RepoPath()
  49. if err := git.InitRepository(tmpBasePath, false); err != nil {
  50. log.Error("git init tmpBasePath: %v", err)
  51. if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
  52. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  53. }
  54. return "", err
  55. }
  56. remoteRepoName := "head_repo"
  57. baseBranch := "base"
  58. // Add head repo remote.
  59. addCacheRepo := func(staging, cache string) error {
  60. p := filepath.Join(staging, ".git", "objects", "info", "alternates")
  61. f, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
  62. if err != nil {
  63. log.Error("Could not create .git/objects/info/alternates file in %s: %v", staging, err)
  64. return err
  65. }
  66. defer f.Close()
  67. data := filepath.Join(cache, "objects")
  68. if _, err := fmt.Fprintln(f, data); err != nil {
  69. log.Error("Could not write to .git/objects/info/alternates file in %s: %v", staging, err)
  70. return err
  71. }
  72. return nil
  73. }
  74. if err := addCacheRepo(tmpBasePath, baseRepoPath); err != nil {
  75. log.Error("Unable to add base repository to temporary repo [%s -> %s]: %v", pr.BaseRepo.FullName(), tmpBasePath, err)
  76. if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
  77. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  78. }
  79. return "", fmt.Errorf("Unable to add base repository to temporary repo [%s -> tmpBasePath]: %v", pr.BaseRepo.FullName(), err)
  80. }
  81. var outbuf, errbuf strings.Builder
  82. if err := git.NewCommand("remote", "add", "-t", pr.BaseBranch, "-m", pr.BaseBranch, "origin", baseRepoPath).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  83. 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())
  84. if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
  85. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  86. }
  87. 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())
  88. }
  89. outbuf.Reset()
  90. errbuf.Reset()
  91. if err := git.NewCommand("fetch", "origin", "--no-tags", "--", pr.BaseBranch+":"+baseBranch, pr.BaseBranch+":original_"+baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  92. 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())
  93. if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
  94. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  95. }
  96. 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())
  97. }
  98. outbuf.Reset()
  99. errbuf.Reset()
  100. if err := git.NewCommand("symbolic-ref", "HEAD", git.BranchPrefix+baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  101. log.Error("Unable to set HEAD as base branch [%s]: %v\n%s\n%s", tmpBasePath, err, outbuf.String(), errbuf.String())
  102. if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
  103. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  104. }
  105. return "", fmt.Errorf("Unable to set HEAD as base branch [tmpBasePath]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  106. }
  107. outbuf.Reset()
  108. errbuf.Reset()
  109. if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil {
  110. log.Error("Unable to add head repository to temporary repo [%s -> %s]: %v", pr.HeadRepo.FullName(), tmpBasePath, err)
  111. if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
  112. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  113. }
  114. return "", fmt.Errorf("Unable to head base repository to temporary repo [%s -> tmpBasePath]: %v", pr.HeadRepo.FullName(), err)
  115. }
  116. if err := git.NewCommand("remote", "add", remoteRepoName, headRepoPath).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  117. 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())
  118. if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
  119. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  120. }
  121. 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())
  122. }
  123. outbuf.Reset()
  124. errbuf.Reset()
  125. trackingBranch := "tracking"
  126. // Fetch head branch
  127. var headBranch string
  128. if pr.Flow == models.PullRequestFlowGithub {
  129. headBranch = git.BranchPrefix + pr.HeadBranch
  130. } else if len(pr.HeadCommitID) == 40 { // for not created pull request
  131. headBranch = pr.HeadCommitID
  132. } else {
  133. headBranch = pr.GetGitRefName()
  134. }
  135. if err := git.NewCommand("fetch", "--no-tags", remoteRepoName, headBranch+":"+trackingBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  136. if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
  137. log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
  138. }
  139. if !git.IsBranchExist(pr.HeadRepo.RepoPath(), pr.HeadBranch) {
  140. return "", models.ErrBranchDoesNotExist{
  141. BranchName: pr.HeadBranch,
  142. }
  143. }
  144. 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())
  145. 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())
  146. }
  147. outbuf.Reset()
  148. errbuf.Reset()
  149. return tmpBasePath, nil
  150. }