Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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