From 6838b29f0f0c961caac5522a84cd47539d5d857a Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Fri, 18 Apr 2025 04:41:13 +0200 Subject: [PATCH 1/2] Default to creating pull request for base repository on file edit * When editing the default branch and there is a base repository, make the default commit choice creating a new branch. * When creating the pull request, default compare it against the base repository default branch. This matches the behavior of creating a pull request based on a branch elsewhere in the UI. This makes it easier to do the right thing for contributing changes from a fork. --- routers/web/repo/editor.go | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index c925b6115147a..4b23f1c554471 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -45,6 +45,11 @@ func canCreateBasePullRequest(ctx *context.Context) bool { return baseRepo != nil && baseRepo.UnitEnabled(ctx, unit.TypePullRequests) } +func defaultCreateNewBranch(ctx *context.Context) bool { + baseRepo := ctx.Repo.Repository.BaseRepo + return canCreateBasePullRequest(ctx) && baseRepo != nil && ctx.Repo.BranchName == baseRepo.DefaultBranch +} + func renderCommitRights(ctx *context.Context) bool { canCommitToBranch, err := ctx.Repo.CanCommitToBranch(ctx, ctx.Doer) if err != nil { @@ -64,13 +69,13 @@ func redirectForCommitChoice(ctx *context.Context, commitChoice, newBranchName, repo := ctx.Repo.Repository baseBranch := ctx.Repo.BranchName headBranch := newBranchName - if repo.UnitEnabled(ctx, unit.TypePullRequests) { - redirectToPullRequest = true - } else if canCreateBasePullRequest(ctx) { + if canCreateBasePullRequest(ctx) { redirectToPullRequest = true baseBranch = repo.BaseRepo.DefaultBranch headBranch = repo.Owner.Name + "/" + repo.Name + ":" + headBranch repo = repo.BaseRepo + } else if repo.UnitEnabled(ctx, unit.TypePullRequests) { + redirectToPullRequest = true } if redirectToPullRequest { @@ -189,7 +194,7 @@ func editFile(ctx *context.Context, isNewFile bool) { ctx.Data["TreePaths"] = treePaths ctx.Data["commit_summary"] = "" ctx.Data["commit_message"] = "" - ctx.Data["commit_choice"] = util.Iif(canCommit, frmCommitChoiceDirect, frmCommitChoiceNewBranch) + ctx.Data["commit_choice"] = util.Iif(canCommit && !defaultCreateNewBranch(ctx), frmCommitChoiceDirect, frmCommitChoiceNewBranch) ctx.Data["new_branch_name"] = GetUniquePatchBranchName(ctx) ctx.Data["last_commit"] = ctx.Repo.CommitID @@ -446,11 +451,7 @@ func DeleteFile(ctx *context.Context) { ctx.Data["commit_summary"] = "" ctx.Data["commit_message"] = "" ctx.Data["last_commit"] = ctx.Repo.CommitID - if canCommit { - ctx.Data["commit_choice"] = frmCommitChoiceDirect - } else { - ctx.Data["commit_choice"] = frmCommitChoiceNewBranch - } + ctx.Data["commit_choice"] = util.Iif(canCommit && !defaultCreateNewBranch(ctx), frmCommitChoiceDirect, frmCommitChoiceNewBranch) ctx.Data["new_branch_name"] = GetUniquePatchBranchName(ctx) ctx.HTML(http.StatusOK, tplDeleteFile) @@ -620,11 +621,7 @@ func UploadFile(ctx *context.Context) { ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL() ctx.Data["commit_summary"] = "" ctx.Data["commit_message"] = "" - if canCommit { - ctx.Data["commit_choice"] = frmCommitChoiceDirect - } else { - ctx.Data["commit_choice"] = frmCommitChoiceNewBranch - } + ctx.Data["commit_choice"] = util.Iif(canCommit && !defaultCreateNewBranch(ctx), frmCommitChoiceDirect, frmCommitChoiceNewBranch) ctx.Data["new_branch_name"] = GetUniquePatchBranchName(ctx) ctx.HTML(http.StatusOK, tplUploadFile) From 49da6e9cecd4c8b6b385da54c11a50f659745308 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Tue, 22 Apr 2025 03:39:26 +0200 Subject: [PATCH 2/2] Add comments --- routers/web/repo/editor.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 4b23f1c554471..1efef17f12655 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -45,6 +45,9 @@ func canCreateBasePullRequest(ctx *context.Context) bool { return baseRepo != nil && baseRepo.UnitEnabled(ctx, unit.TypePullRequests) } +// defaultCreateNewBranch checks if the default commit choice should be to create new branch. +// As a heuristic, always do this the default branch on forks, which is the right choice for +// pull requests, and making further edits to that pull request branch. func defaultCreateNewBranch(ctx *context.Context) bool { baseRepo := ctx.Repo.Repository.BaseRepo return canCreateBasePullRequest(ctx) && baseRepo != nil && ctx.Repo.BranchName == baseRepo.DefaultBranch @@ -69,6 +72,8 @@ func redirectForCommitChoice(ctx *context.Context, commitChoice, newBranchName, repo := ctx.Repo.Repository baseBranch := ctx.Repo.BranchName headBranch := newBranchName + // Prefer to create a pull request to the base repository if possible, matching + // the behavior when creating a pull request from a branch elsewhere in the UI. if canCreateBasePullRequest(ctx) { redirectToPullRequest = true baseBranch = repo.BaseRepo.DefaultBranch