diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 9d1d279b0e85d..52a6c5e2de6ac 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1630,6 +1630,7 @@ pulls.update_not_allowed = You are not allowed to update branch pulls.outdated_with_base_branch = This branch is out-of-date with the base branch pulls.closed_at = `closed this pull request %[2]s` pulls.reopened_at = `reopened this pull request %[2]s` +pulls.rebase_add_message_hint = `The pull request # will be automatically added to the message of the last commit in the stack, if missing.` pulls.merge_instruction_hint = `You can also view command line instructions.` pulls.merge_instruction_step1_desc = From your project repository, check out a new branch and test the changes. pulls.merge_instruction_step2_desc = Merge the changes and update on Gitea. diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index b081092c57fc2..a62ab04b40392 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1663,6 +1663,16 @@ func ViewIssue(ctx *context.Context) { } ctx.Data["MergeStyle"] = mergeStyle + ctx.Data["ShowRebaseAddMessageHint"] = false + + if mergeStyle == repo_model.MergeStyleRebase { + ctx.Data["MergeInstructionsCommand"] = "merge --ff-only" + ctx.Data["ShowRebaseAddMessageHint"] = true + } else if mergeStyle == repo_model.MergeStyleSquash { + ctx.Data["MergeInstructionsCommand"] = "merge --squash" + } else { + ctx.Data["MergeInstructionsCommand"] = "merge --no-ff" + } defaultMergeMessage, defaultMergeBody, err := pull_service.GetDefaultMergeMessage(ctx, ctx.Repo.GitRepo, pull, mergeStyle) if err != nil { diff --git a/services/pull/merge.go b/services/pull/merge.go index d0ec943cfa6b8..3fe5a6381da29 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -509,6 +509,24 @@ func rawMerge(ctx context.Context, pr *issues_model.PullRequest, doer *user_mode log.Error("Unable to make final commit: %v", err) return "", err } + } else if mergeStyle == repo_model.MergeStyleRebase { + // Append Pull Request #123 to commit message + var existingMessage strings.Builder + cmdShow := git.NewCommand(ctx, "show", "--format=%B", "-s") + if err := cmdShow.Run(&git.RunOpts{Dir: tmpBasePath, Stdout: &existingMessage}); err != nil { + log.Error("Failed to get commit message for Pull Request #%d: %v", pr.Index, err) + return "", err + } + + newMessage := strings.TrimSpace(existingMessage.String()) + if match, _ := regexp.MatchString(fmt.Sprintf("#\\b%d\\b", pr.Index), newMessage); !match { + newMessage += fmt.Sprintf("\n\nPull Request #%d", pr.Index) + cmdAmend := git.NewCommand(ctx, "commit", "--amend", "-m").AddDynamicArguments(newMessage) + if err := cmdAmend.Run(&git.RunOpts{Dir: tmpBasePath}); err != nil { + log.Error("Failed to amend commit message with Pull Request #%d: %v", pr.Index, err) + return "", err + } + } } case repo_model.MergeStyleSquash: // Merge with squash diff --git a/templates/repo/issue/view_content/pull.tmpl b/templates/repo/issue/view_content/pull.tmpl index 1f94001db06eb..2d4105860f3c9 100644 --- a/templates/repo/issue/view_content/pull.tmpl +++ b/templates/repo/issue/view_content/pull.tmpl @@ -417,7 +417,10 @@
{{if .ShowMergeInstructions}} - {{template "repo/issue/view_content/pull_merge_instruction" (dict "locale" .locale "Issue" .Issue)}} + {{template "repo/issue/view_content/pull_merge_instruction" (dict "locale" .locale "Issue" .Issue "MergeCommand" .MergeInstructionsCommand)}} + {{end}} + {{if .ShowRebaseAddMessageHint}} +
{{$.locale.Tr "repo.pulls.rebase_add_message_hint" | Safe}}
{{end}} {{else}} {{/* no merge style was set in repo setting: not or ($prUnit.PullRequestsConfig.AllowMerge ...) */}} diff --git a/templates/repo/issue/view_content/pull_merge_instruction.tmpl b/templates/repo/issue/view_content/pull_merge_instruction.tmpl index 816f25cbcf901..509d7d99418b4 100644 --- a/templates/repo/issue/view_content/pull_merge_instruction.tmpl +++ b/templates/repo/issue/view_content/pull_merge_instruction.tmpl @@ -13,7 +13,7 @@

{{$.locale.Tr "step2"}}

{{$.locale.Tr "repo.pulls.merge_instruction_step2_desc"}}
git checkout {{$.Issue.PullRequest.BaseBranch}}
-
git merge --no-ff {{if ne $.Issue.PullRequest.HeadRepo.ID $.Issue.PullRequest.BaseRepo.ID}}{{$.Issue.PullRequest.HeadRepo.OwnerName}}-{{end}}{{$.Issue.PullRequest.HeadBranch}}
+
git {{$.MergeCommand}} {{if ne $.Issue.PullRequest.HeadRepo.ID $.Issue.PullRequest.BaseRepo.ID}}{{$.Issue.PullRequest.HeadRepo.OwnerName}}-{{end}}{{$.Issue.PullRequest.HeadBranch}}
git push origin {{$.Issue.PullRequest.BaseBranch}}