Skip to content

Pull Requests: show command line instructions closer to chosen merge style #22542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a id="%[1]s" href="#%[1]s">%[2]s</a>`
pulls.reopened_at = `reopened this pull request <a id="%[1]s" href="#%[1]s">%[2]s</a>`
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 <a class="show-instruction">command line instructions</a>.`
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.
Expand Down
10 changes: 10 additions & 0 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions services/pull/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion templates/repo/issue/view_content/pull.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,10 @@
<div id="pull-request-merge-form"></div>

{{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}}
<div class="mt-3"> {{$.locale.Tr "repo.pulls.rebase_add_message_hint" | Safe}} </div>
{{end}}
{{else}}
{{/* no merge style was set in repo setting: not or ($prUnit.PullRequestsConfig.AllowMerge ...) */}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<div><h3 class="di">{{$.locale.Tr "step2"}} </h3>{{$.locale.Tr "repo.pulls.merge_instruction_step2_desc"}}</div>
<div class="ui secondary segment">
<div>git checkout {{$.Issue.PullRequest.BaseBranch}}</div>
<div>git merge --no-ff {{if ne $.Issue.PullRequest.HeadRepo.ID $.Issue.PullRequest.BaseRepo.ID}}{{$.Issue.PullRequest.HeadRepo.OwnerName}}-{{end}}{{$.Issue.PullRequest.HeadBranch}}</div>
<div>git {{$.MergeCommand}} {{if ne $.Issue.PullRequest.HeadRepo.ID $.Issue.PullRequest.BaseRepo.ID}}{{$.Issue.PullRequest.HeadRepo.OwnerName}}-{{end}}{{$.Issue.PullRequest.HeadBranch}}</div>
<div>git push origin {{$.Issue.PullRequest.BaseBranch}}</div>
</div>
</div>