aboutsummaryrefslogtreecommitdiff
path: root/services/pull/merge_rebase.go
blob: d3bb86d4aa0816315284cc04f31707543fd99d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package pull

import (
	"fmt"

	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/modules/git"
	"code.gitea.io/gitea/modules/log"
)

// doMergeStyleRebase rebaases the tracking branch on the base branch as the current HEAD with or with a merge commit to the original pr branch
func doMergeStyleRebase(ctx *mergeContext, mergeStyle repo_model.MergeStyle, message string) error {
	if err := rebaseTrackingOnToBase(ctx, mergeStyle); err != nil {
		return err
	}

	// Checkout base branch again
	if err := git.NewCommand(ctx, "checkout").AddDynamicArguments(baseBranch).
		Run(ctx.RunOpts()); err != nil {
		log.Error("git checkout base prior to merge post staging rebase %-v: %v\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), ctx.errbuf.String())
		return fmt.Errorf("git checkout base prior to merge post staging rebase  %v: %w\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), ctx.errbuf.String())
	}
	ctx.outbuf.Reset()
	ctx.errbuf.Reset()

	cmd := git.NewCommand(ctx, "merge")
	if mergeStyle == repo_model.MergeStyleRebase {
		cmd.AddArguments("--ff-only")
	} else {
		cmd.AddArguments("--no-ff", "--no-commit")
	}
	cmd.AddDynamicArguments(stagingBranch)

	// Prepare merge with commit
	if err := runMergeCommand(ctx, mergeStyle, cmd); err != nil {
		log.Error("Unable to merge staging into base: %v", err)
		return err
	}
	if mergeStyle == repo_model.MergeStyleRebaseMerge {
		if err := commitAndSignNoAuthor(ctx, message); err != nil {
			log.Error("Unable to make final commit: %v", err)
			return err
		}
	}

	return nil
}