From e3bb55e534667f77fdd7f95aae10a2b8568461f5 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Wed, 18 Jan 2023 02:54:41 +0100 Subject: [PATCH 1/3] Pull Requests: show command line instructions closer to chosen merge style Only matches the default command though, does not manually update if you switch. But for Blender we will likely have only one option anyway. --- routers/web/repo/issue.go | 8 ++++++++ templates/repo/issue/view_content/pull.tmpl | 2 +- .../repo/issue/view_content/pull_merge_instruction.tmpl | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index b081092c57fc2..2ff14f927f412 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1664,6 +1664,14 @@ func ViewIssue(ctx *context.Context) { ctx.Data["MergeStyle"] = mergeStyle + if mergeStyle == repo_model.MergeStyleRebase { + ctx.Data["MergeInstructionsCommand"] = "merge --ff-only" + } 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 { ctx.ServerError("GetDefaultMergeMessage", err) diff --git a/templates/repo/issue/view_content/pull.tmpl b/templates/repo/issue/view_content/pull.tmpl index 1f94001db06eb..ae9f81a1686b2 100644 --- a/templates/repo/issue/view_content/pull.tmpl +++ b/templates/repo/issue/view_content/pull.tmpl @@ -417,7 +417,7 @@
{{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}} {{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}}
From 50ef1e3266930bb66b94339fe3ef0e3e72680e91 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Wed, 18 Jan 2023 03:41:10 +0100 Subject: [PATCH 2/3] Pull Requests: auto add # to commit message on rebase If the # is not already mentioned somewhere in the commit message, the following is added at the end of the commit message: Pull Request #123 To be upstreamable, this should at least be made optional behavior if it is considered a good feature to have at all. It also only changes the last commit in the stack which is not ideal. --- options/locale/locale_en-US.ini | 1 + routers/web/repo/issue.go | 2 ++ services/pull/merge.go | 18 ++++++++++++++++++ templates/repo/issue/view_content/pull.tmpl | 3 +++ 4 files changed, 24 insertions(+) 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 2ff14f927f412..a62ab04b40392 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1663,9 +1663,11 @@ 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 { diff --git a/services/pull/merge.go b/services/pull/merge.go index d0ec943cfa6b8..4871ece304d87 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 existing_message strings.Builder + cmd_show := git.NewCommand(ctx, "show", "--format=%B", "-s") + if err := cmd_show.Run(&git.RunOpts{Dir: tmpBasePath, Stdout: &existing_message}); err != nil { + log.Error("Failed to get commit message for Pull Request #%d: %v", pr.Index, err) + return "", err + } + + new_message := strings.TrimSpace(existing_message.String()) + if match, _ := regexp.MatchString(fmt.Sprintf("#\\b%d\\b", pr.Index), new_message); !match { + new_message = new_message + fmt.Sprintf("\n\nPull Request #%d", pr.Index) + cmd_amend := git.NewCommand(ctx, "commit", "--amend", "-m").AddDynamicArguments(new_message) + if err := cmd_amend.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 ae9f81a1686b2..2d4105860f3c9 100644 --- a/templates/repo/issue/view_content/pull.tmpl +++ b/templates/repo/issue/view_content/pull.tmpl @@ -419,6 +419,9 @@ {{if .ShowMergeInstructions}} {{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 ...) */}}
From 7c05930a4aa44a2834590fd4439fac9d8a22afb0 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Wed, 25 Jan 2023 15:12:21 -0500 Subject: [PATCH 3/3] placate lint --- services/pull/merge.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/services/pull/merge.go b/services/pull/merge.go index 4871ece304d87..3fe5a6381da29 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -511,18 +511,18 @@ func rawMerge(ctx context.Context, pr *issues_model.PullRequest, doer *user_mode } } else if mergeStyle == repo_model.MergeStyleRebase { // Append Pull Request #123 to commit message - var existing_message strings.Builder - cmd_show := git.NewCommand(ctx, "show", "--format=%B", "-s") - if err := cmd_show.Run(&git.RunOpts{Dir: tmpBasePath, Stdout: &existing_message}); err != nil { + 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 } - new_message := strings.TrimSpace(existing_message.String()) - if match, _ := regexp.MatchString(fmt.Sprintf("#\\b%d\\b", pr.Index), new_message); !match { - new_message = new_message + fmt.Sprintf("\n\nPull Request #%d", pr.Index) - cmd_amend := git.NewCommand(ctx, "commit", "--amend", "-m").AddDynamicArguments(new_message) - if err := cmd_amend.Run(&git.RunOpts{Dir: tmpBasePath}); err != nil { + 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 }