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.

merge_prepare.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package pull
  4. import (
  5. "bufio"
  6. "bytes"
  7. "context"
  8. "fmt"
  9. "io"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. "code.gitea.io/gitea/models"
  15. issues_model "code.gitea.io/gitea/models/issues"
  16. repo_model "code.gitea.io/gitea/models/repo"
  17. user_model "code.gitea.io/gitea/models/user"
  18. "code.gitea.io/gitea/modules/git"
  19. "code.gitea.io/gitea/modules/log"
  20. asymkey_service "code.gitea.io/gitea/services/asymkey"
  21. )
  22. type mergeContext struct {
  23. *prContext
  24. doer *user_model.User
  25. sig *git.Signature
  26. committer *git.Signature
  27. signKeyID string // empty for no-sign, non-empty to sign
  28. env []string
  29. }
  30. func (ctx *mergeContext) RunOpts() *git.RunOpts {
  31. ctx.outbuf.Reset()
  32. ctx.errbuf.Reset()
  33. return &git.RunOpts{
  34. Env: ctx.env,
  35. Dir: ctx.tmpBasePath,
  36. Stdout: ctx.outbuf,
  37. Stderr: ctx.errbuf,
  38. }
  39. }
  40. func createTemporaryRepoForMerge(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User, expectedHeadCommitID string) (mergeCtx *mergeContext, cancel context.CancelFunc, err error) {
  41. // Clone base repo.
  42. prCtx, cancel, err := createTemporaryRepoForPR(ctx, pr)
  43. if err != nil {
  44. log.Error("createTemporaryRepoForPR: %v", err)
  45. return nil, cancel, err
  46. }
  47. mergeCtx = &mergeContext{
  48. prContext: prCtx,
  49. doer: doer,
  50. }
  51. if expectedHeadCommitID != "" {
  52. trackingCommitID, _, err := git.NewCommand(ctx, "show-ref", "--hash").AddDynamicArguments(git.BranchPrefix + trackingBranch).RunStdString(&git.RunOpts{Dir: mergeCtx.tmpBasePath})
  53. if err != nil {
  54. defer cancel()
  55. log.Error("failed to get sha of head branch in %-v: show-ref[%s] --hash refs/heads/tracking: %v", mergeCtx.pr, mergeCtx.tmpBasePath, err)
  56. return nil, nil, fmt.Errorf("unable to get sha of head branch in %v %w", pr, err)
  57. }
  58. if strings.TrimSpace(trackingCommitID) != expectedHeadCommitID {
  59. defer cancel()
  60. return nil, nil, models.ErrSHADoesNotMatch{
  61. GivenSHA: expectedHeadCommitID,
  62. CurrentSHA: trackingCommitID,
  63. }
  64. }
  65. }
  66. mergeCtx.outbuf.Reset()
  67. mergeCtx.errbuf.Reset()
  68. if err := prepareTemporaryRepoForMerge(mergeCtx); err != nil {
  69. defer cancel()
  70. return nil, nil, err
  71. }
  72. mergeCtx.sig = doer.NewGitSig()
  73. mergeCtx.committer = mergeCtx.sig
  74. // Determine if we should sign
  75. sign, keyID, signer, _ := asymkey_service.SignMerge(ctx, mergeCtx.pr, mergeCtx.doer, mergeCtx.tmpBasePath, "HEAD", trackingBranch)
  76. if sign {
  77. mergeCtx.signKeyID = keyID
  78. if pr.BaseRepo.GetTrustModel() == repo_model.CommitterTrustModel || pr.BaseRepo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
  79. mergeCtx.committer = signer
  80. }
  81. }
  82. commitTimeStr := time.Now().Format(time.RFC3339)
  83. // Because this may call hooks we should pass in the environment
  84. mergeCtx.env = append(os.Environ(),
  85. "GIT_AUTHOR_NAME="+mergeCtx.sig.Name,
  86. "GIT_AUTHOR_EMAIL="+mergeCtx.sig.Email,
  87. "GIT_AUTHOR_DATE="+commitTimeStr,
  88. "GIT_COMMITTER_NAME="+mergeCtx.committer.Name,
  89. "GIT_COMMITTER_EMAIL="+mergeCtx.committer.Email,
  90. "GIT_COMMITTER_DATE="+commitTimeStr,
  91. )
  92. return mergeCtx, cancel, nil
  93. }
  94. // prepareTemporaryRepoForMerge takes a repository that has been created using createTemporaryRepo
  95. // it then sets up the sparse-checkout and other things
  96. func prepareTemporaryRepoForMerge(ctx *mergeContext) error {
  97. infoPath := filepath.Join(ctx.tmpBasePath, ".git", "info")
  98. if err := os.MkdirAll(infoPath, 0o700); err != nil {
  99. log.Error("%-v Unable to create .git/info in %s: %v", ctx.pr, ctx.tmpBasePath, err)
  100. return fmt.Errorf("Unable to create .git/info in tmpBasePath: %w", err)
  101. }
  102. // Enable sparse-checkout
  103. // Here we use the .git/info/sparse-checkout file as described in the git documentation
  104. sparseCheckoutListFile, err := os.OpenFile(filepath.Join(infoPath, "sparse-checkout"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
  105. if err != nil {
  106. log.Error("%-v Unable to write .git/info/sparse-checkout file in %s: %v", ctx.pr, ctx.tmpBasePath, err)
  107. return fmt.Errorf("Unable to write .git/info/sparse-checkout file in tmpBasePath: %w", err)
  108. }
  109. defer sparseCheckoutListFile.Close() // we will close it earlier but we need to ensure it is closed if there is an error
  110. if err := getDiffTree(ctx, ctx.tmpBasePath, baseBranch, trackingBranch, sparseCheckoutListFile); err != nil {
  111. log.Error("%-v getDiffTree(%s, %s, %s): %v", ctx.pr, ctx.tmpBasePath, baseBranch, trackingBranch, err)
  112. return fmt.Errorf("getDiffTree: %w", err)
  113. }
  114. if err := sparseCheckoutListFile.Close(); err != nil {
  115. log.Error("%-v Unable to close .git/info/sparse-checkout file in %s: %v", ctx.pr, ctx.tmpBasePath, err)
  116. return fmt.Errorf("Unable to close .git/info/sparse-checkout file in tmpBasePath: %w", err)
  117. }
  118. setConfig := func(key, value string) error {
  119. if err := git.NewCommand(ctx, "config", "--local").AddDynamicArguments(key, value).
  120. Run(ctx.RunOpts()); err != nil {
  121. log.Error("git config [%s -> %q]: %v\n%s\n%s", key, value, err, ctx.outbuf.String(), ctx.errbuf.String())
  122. return fmt.Errorf("git config [%s -> %q]: %w\n%s\n%s", key, value, err, ctx.outbuf.String(), ctx.errbuf.String())
  123. }
  124. ctx.outbuf.Reset()
  125. ctx.errbuf.Reset()
  126. return nil
  127. }
  128. // Switch off LFS process (set required, clean and smudge here also)
  129. if err := setConfig("filter.lfs.process", ""); err != nil {
  130. return err
  131. }
  132. if err := setConfig("filter.lfs.required", "false"); err != nil {
  133. return err
  134. }
  135. if err := setConfig("filter.lfs.clean", ""); err != nil {
  136. return err
  137. }
  138. if err := setConfig("filter.lfs.smudge", ""); err != nil {
  139. return err
  140. }
  141. if err := setConfig("core.sparseCheckout", "true"); err != nil {
  142. return err
  143. }
  144. // Read base branch index
  145. if err := git.NewCommand(ctx, "read-tree", "HEAD").
  146. Run(ctx.RunOpts()); err != nil {
  147. log.Error("git read-tree HEAD: %v\n%s\n%s", err, ctx.outbuf.String(), ctx.errbuf.String())
  148. return fmt.Errorf("Unable to read base branch in to the index: %w\n%s\n%s", err, ctx.outbuf.String(), ctx.errbuf.String())
  149. }
  150. ctx.outbuf.Reset()
  151. ctx.errbuf.Reset()
  152. return nil
  153. }
  154. // getDiffTree returns a string containing all the files that were changed between headBranch and baseBranch
  155. // the filenames are escaped so as to fit the format required for .git/info/sparse-checkout
  156. func getDiffTree(ctx context.Context, repoPath, baseBranch, headBranch string, out io.Writer) error {
  157. diffOutReader, diffOutWriter, err := os.Pipe()
  158. if err != nil {
  159. log.Error("Unable to create os.Pipe for %s", repoPath)
  160. return err
  161. }
  162. defer func() {
  163. _ = diffOutReader.Close()
  164. _ = diffOutWriter.Close()
  165. }()
  166. scanNullTerminatedStrings := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
  167. if atEOF && len(data) == 0 {
  168. return 0, nil, nil
  169. }
  170. if i := bytes.IndexByte(data, '\x00'); i >= 0 {
  171. return i + 1, data[0:i], nil
  172. }
  173. if atEOF {
  174. return len(data), data, nil
  175. }
  176. return 0, nil, nil
  177. }
  178. err = git.NewCommand(ctx, "diff-tree", "--no-commit-id", "--name-only", "-r", "-r", "-z", "--root").AddDynamicArguments(baseBranch, headBranch).
  179. Run(&git.RunOpts{
  180. Dir: repoPath,
  181. Stdout: diffOutWriter,
  182. PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
  183. // Close the writer end of the pipe to begin processing
  184. _ = diffOutWriter.Close()
  185. defer func() {
  186. // Close the reader on return to terminate the git command if necessary
  187. _ = diffOutReader.Close()
  188. }()
  189. // Now scan the output from the command
  190. scanner := bufio.NewScanner(diffOutReader)
  191. scanner.Split(scanNullTerminatedStrings)
  192. for scanner.Scan() {
  193. filepath := scanner.Text()
  194. // escape '*', '?', '[', spaces and '!' prefix
  195. filepath = escapedSymbols.ReplaceAllString(filepath, `\$1`)
  196. // no necessary to escape the first '#' symbol because the first symbol is '/'
  197. fmt.Fprintf(out, "/%s\n", filepath)
  198. }
  199. return scanner.Err()
  200. },
  201. })
  202. return err
  203. }
  204. // rebaseTrackingOnToBase checks out the tracking branch as staging and rebases it on to the base branch
  205. // if there is a conflict it will return a models.ErrRebaseConflicts
  206. func rebaseTrackingOnToBase(ctx *mergeContext, mergeStyle repo_model.MergeStyle) error {
  207. // Checkout head branch
  208. if err := git.NewCommand(ctx, "checkout", "-b").AddDynamicArguments(stagingBranch, trackingBranch).
  209. Run(ctx.RunOpts()); err != nil {
  210. return fmt.Errorf("unable to git checkout tracking as staging in temp repo for %v: %w\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), ctx.errbuf.String())
  211. }
  212. ctx.outbuf.Reset()
  213. ctx.errbuf.Reset()
  214. // Rebase before merging
  215. if err := git.NewCommand(ctx, "rebase").AddDynamicArguments(baseBranch).
  216. Run(ctx.RunOpts()); err != nil {
  217. // Rebase will leave a REBASE_HEAD file in .git if there is a conflict
  218. if _, statErr := os.Stat(filepath.Join(ctx.tmpBasePath, ".git", "REBASE_HEAD")); statErr == nil {
  219. var commitSha string
  220. ok := false
  221. failingCommitPaths := []string{
  222. filepath.Join(ctx.tmpBasePath, ".git", "rebase-apply", "original-commit"), // Git < 2.26
  223. filepath.Join(ctx.tmpBasePath, ".git", "rebase-merge", "stopped-sha"), // Git >= 2.26
  224. }
  225. for _, failingCommitPath := range failingCommitPaths {
  226. if _, statErr := os.Stat(failingCommitPath); statErr == nil {
  227. commitShaBytes, readErr := os.ReadFile(failingCommitPath)
  228. if readErr != nil {
  229. // Abandon this attempt to handle the error
  230. return fmt.Errorf("unable to git rebase staging on to base in temp repo for %v: %w\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), ctx.errbuf.String())
  231. }
  232. commitSha = strings.TrimSpace(string(commitShaBytes))
  233. ok = true
  234. break
  235. }
  236. }
  237. if !ok {
  238. log.Error("Unable to determine failing commit sha for failing rebase in temp repo for %-v. Cannot cast as models.ErrRebaseConflicts.", ctx.pr)
  239. return fmt.Errorf("unable to git rebase staging on to base in temp repo for %v: %w\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), ctx.errbuf.String())
  240. }
  241. log.Debug("Conflict when rebasing staging on to base in %-v at %s: %v\n%s\n%s", ctx.pr, commitSha, err, ctx.outbuf.String(), ctx.errbuf.String())
  242. return models.ErrRebaseConflicts{
  243. CommitSHA: commitSha,
  244. Style: mergeStyle,
  245. StdOut: ctx.outbuf.String(),
  246. StdErr: ctx.errbuf.String(),
  247. Err: err,
  248. }
  249. }
  250. return fmt.Errorf("unable to git rebase staging on to base in temp repo for %v: %w\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), ctx.errbuf.String())
  251. }
  252. ctx.outbuf.Reset()
  253. ctx.errbuf.Reset()
  254. return nil
  255. }