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.

update_rebase.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package pull
  4. import (
  5. "context"
  6. "fmt"
  7. "strings"
  8. issues_model "code.gitea.io/gitea/models/issues"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/log"
  13. repo_module "code.gitea.io/gitea/modules/repository"
  14. "code.gitea.io/gitea/modules/setting"
  15. )
  16. // updateHeadByRebaseOnToBase handles updating a PR's head branch by rebasing it on the PR current base branch
  17. func updateHeadByRebaseOnToBase(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User, message string) error {
  18. // "Clone" base repo and add the cache headers for the head repo and branch
  19. mergeCtx, cancel, err := createTemporaryRepoForMerge(ctx, pr, doer, "")
  20. if err != nil {
  21. return err
  22. }
  23. defer cancel()
  24. // Determine the old merge-base before the rebase - we use this for LFS push later on
  25. oldMergeBase, _, _ := git.NewCommand(ctx, "merge-base").AddDashesAndList(baseBranch, trackingBranch).RunStdString(&git.RunOpts{Dir: mergeCtx.tmpBasePath})
  26. oldMergeBase = strings.TrimSpace(oldMergeBase)
  27. // Rebase the tracking branch on to the base as the staging branch
  28. if err := rebaseTrackingOnToBase(mergeCtx, repo_model.MergeStyleRebaseUpdate); err != nil {
  29. return err
  30. }
  31. if setting.LFS.StartServer {
  32. // Now we need to ensure that the head repository contains any LFS objects between the new base and the old mergebase
  33. // It's questionable about where this should go - either after or before the push
  34. // I think in the interests of data safety - failures to push to the lfs should prevent
  35. // the push as you can always re-rebase.
  36. if err := LFSPush(ctx, mergeCtx.tmpBasePath, baseBranch, oldMergeBase, &issues_model.PullRequest{
  37. HeadRepoID: pr.BaseRepoID,
  38. BaseRepoID: pr.HeadRepoID,
  39. }); err != nil {
  40. log.Error("Unable to push lfs objects between %s and %s up to head branch in %-v: %v", baseBranch, oldMergeBase, pr, err)
  41. return err
  42. }
  43. }
  44. // Now determine who the pushing author should be
  45. var headUser *user_model.User
  46. if err := pr.HeadRepo.LoadOwner(ctx); err != nil {
  47. if !user_model.IsErrUserNotExist(err) {
  48. log.Error("Can't find user: %d for head repository in %-v - %v", pr.HeadRepo.OwnerID, pr, err)
  49. return err
  50. }
  51. log.Error("Can't find user: %d for head repository in %-v - defaulting to doer: %-v - %v", pr.HeadRepo.OwnerID, pr, doer, err)
  52. headUser = doer
  53. } else {
  54. headUser = pr.HeadRepo.Owner
  55. }
  56. pushCmd := git.NewCommand(ctx, "push", "-f", "head_repo").
  57. AddDynamicArguments(stagingBranch + ":" + git.BranchPrefix + pr.HeadBranch)
  58. // Push back to the head repository.
  59. // TODO: this cause an api call to "/api/internal/hook/post-receive/...",
  60. // that prevents us from doint the whole merge in one db transaction
  61. mergeCtx.outbuf.Reset()
  62. mergeCtx.errbuf.Reset()
  63. if err := pushCmd.Run(&git.RunOpts{
  64. Env: repo_module.FullPushingEnvironment(
  65. headUser,
  66. doer,
  67. pr.HeadRepo,
  68. pr.HeadRepo.Name,
  69. pr.ID,
  70. ),
  71. Dir: mergeCtx.tmpBasePath,
  72. Stdout: mergeCtx.outbuf,
  73. Stderr: mergeCtx.errbuf,
  74. }); err != nil {
  75. if strings.Contains(mergeCtx.errbuf.String(), "non-fast-forward") {
  76. return &git.ErrPushOutOfDate{
  77. StdOut: mergeCtx.outbuf.String(),
  78. StdErr: mergeCtx.errbuf.String(),
  79. Err: err,
  80. }
  81. } else if strings.Contains(mergeCtx.errbuf.String(), "! [remote rejected]") {
  82. err := &git.ErrPushRejected{
  83. StdOut: mergeCtx.outbuf.String(),
  84. StdErr: mergeCtx.errbuf.String(),
  85. Err: err,
  86. }
  87. err.GenerateMessage()
  88. return err
  89. }
  90. return fmt.Errorf("git push: %s", mergeCtx.errbuf.String())
  91. }
  92. mergeCtx.outbuf.Reset()
  93. mergeCtx.errbuf.Reset()
  94. return nil
  95. }