diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 63387df281e3..b94aa1b7ba05 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -41,16 +41,49 @@ const ( frmCommitChoiceNewBranch string = "commit-to-new-branch" ) +func canCreateBasePullRequest(ctx *context.Context) bool { + baseRepo := ctx.Repo.Repository.BaseRepo + return baseRepo != nil && baseRepo.UnitEnabled(ctx, unit.TypePullRequests) +} + func renderCommitRights(ctx *context.Context) bool { canCommitToBranch, err := ctx.Repo.CanCommitToBranch(ctx, ctx.Doer) if err != nil { log.Error("CanCommitToBranch: %v", err) } ctx.Data["CanCommitToBranch"] = canCommitToBranch + ctx.Data["CanCreatePullRequest"] = ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) || canCreateBasePullRequest(ctx) return canCommitToBranch.CanCommitToBranch } +// redirectForCommitChoice redirects after committing the edit to a branch +func redirectForCommitChoice(ctx *context.Context, commitChoice, newBranchName, treePath string) { + if commitChoice == frmCommitChoiceNewBranch { + // Redirect to a pull request when possible + redirectToPullRequest := false + repo := ctx.Repo.Repository + baseBranch := ctx.Repo.BranchName + headBranch := newBranchName + if repo.UnitEnabled(ctx, unit.TypePullRequests) { + redirectToPullRequest = true + } else if canCreateBasePullRequest(ctx) { + redirectToPullRequest = true + baseBranch = repo.BaseRepo.DefaultBranch + headBranch = repo.Owner.Name + "/" + repo.Name + ":" + headBranch + repo = repo.BaseRepo + } + + if redirectToPullRequest { + ctx.Redirect(repo.Link() + "/compare/" + util.PathEscapeSegments(baseBranch) + "..." + util.PathEscapeSegments(headBranch)) + return + } + } + + // Redirect to viewing file or folder + ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(newBranchName) + "/" + util.PathEscapeSegments(treePath)) +} + // getParentTreeFields returns list of parent tree names and corresponding tree paths // based on given tree path. func getParentTreeFields(treePath string) (treeNames, treePaths []string) { @@ -331,11 +364,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b _ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty") } - if form.CommitChoice == frmCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) { - ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName)) - } else { - ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(branchName) + "/" + util.PathEscapeSegments(form.TreePath)) - } + redirectForCommitChoice(ctx, form.CommitChoice, branchName, form.TreePath) } // EditFilePost response for editing file @@ -517,26 +546,23 @@ func DeleteFilePost(ctx *context.Context) { } ctx.Flash.Success(ctx.Tr("repo.editor.file_delete_success", ctx.Repo.TreePath)) - if form.CommitChoice == frmCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) { - ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName)) - } else { - treePath := path.Dir(ctx.Repo.TreePath) - if treePath == "." { - treePath = "" // the file deleted was in the root, so we return the user to the root directory - } - if len(treePath) > 0 { - // Need to get the latest commit since it changed - commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.BranchName) - if err == nil && commit != nil { - // We have the comment, now find what directory we can return the user to - // (must have entries) - treePath = GetClosestParentWithFiles(treePath, commit) - } else { - treePath = "" // otherwise return them to the root of the repo - } - } - ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(branchName) + "/" + util.PathEscapeSegments(treePath)) + treePath := path.Dir(ctx.Repo.TreePath) + if treePath == "." { + treePath = "" // the file deleted was in the root, so we return the user to the root directory } + if len(treePath) > 0 { + // Need to get the latest commit since it changed + commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.BranchName) + if err == nil && commit != nil { + // We have the comment, now find what directory we can return the user to + // (must have entries) + treePath = GetClosestParentWithFiles(treePath, commit) + } else { + treePath = "" // otherwise return them to the root of the repo + } + } + + redirectForCommitChoice(ctx, form.CommitChoice, branchName, treePath) } // UploadFile render upload file page @@ -722,11 +748,7 @@ func UploadFilePost(ctx *context.Context) { _ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty") } - if form.CommitChoice == frmCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) { - ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName)) - } else { - ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(branchName) + "/" + util.PathEscapeSegments(form.TreePath)) - } + redirectForCommitChoice(ctx, form.CommitChoice, branchName, form.TreePath) } func cleanUploadFileName(name string) string { diff --git a/templates/repo/editor/commit_form.tmpl b/templates/repo/editor/commit_form.tmpl index db798d92e885..6a3f86f9fc97 100644 --- a/templates/repo/editor/commit_form.tmpl +++ b/templates/repo/editor/commit_form.tmpl @@ -41,17 +41,15 @@ {{if not .Repository.IsEmpty}}
- {{$pullRequestEnabled := .Repository.UnitEnabled $.Context $.UnitTypePullRequests}} - {{$prUnit := .Repository.MustGetUnit $.Context $.UnitTypePullRequests}}
- {{if $pullRequestEnabled}} + {{if .CanCreatePullRequest}} {{else}} {{end}}