|
| 1 | +// Copyright 2020 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package repo |
| 6 | + |
| 7 | +import ( |
| 8 | + "strings" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models" |
| 11 | + "code.gitea.io/gitea/modules/base" |
| 12 | + "code.gitea.io/gitea/modules/context" |
| 13 | + "code.gitea.io/gitea/modules/setting" |
| 14 | + "code.gitea.io/gitea/modules/web" |
| 15 | + "code.gitea.io/gitea/services/forms" |
| 16 | + "code.gitea.io/gitea/services/repository/files" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + tplPatchFile base.TplName = "repo/editor/patch" |
| 21 | +) |
| 22 | + |
| 23 | +// NewDiffPatch render create patch page |
| 24 | +func NewDiffPatch(ctx *context.Context) { |
| 25 | + ctx.Data["RequireHighlightJS"] = true |
| 26 | + |
| 27 | + canCommit := renderCommitRights(ctx) |
| 28 | + |
| 29 | + ctx.Data["TreePath"] = "patch" |
| 30 | + |
| 31 | + ctx.Data["commit_summary"] = "" |
| 32 | + ctx.Data["commit_message"] = "" |
| 33 | + if canCommit { |
| 34 | + ctx.Data["commit_choice"] = frmCommitChoiceDirect |
| 35 | + } else { |
| 36 | + ctx.Data["commit_choice"] = frmCommitChoiceNewBranch |
| 37 | + } |
| 38 | + ctx.Data["new_branch_name"] = GetUniquePatchBranchName(ctx) |
| 39 | + ctx.Data["last_commit"] = ctx.Repo.CommitID |
| 40 | + ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",") |
| 41 | + ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() |
| 42 | + |
| 43 | + ctx.HTML(200, tplPatchFile) |
| 44 | +} |
| 45 | + |
| 46 | +// NewDiffPatchPost response for sending patch page |
| 47 | +func NewDiffPatchPost(ctx *context.Context) { |
| 48 | + form := web.GetForm(ctx).(*forms.EditRepoFileForm) |
| 49 | + |
| 50 | + canCommit := renderCommitRights(ctx) |
| 51 | + branchName := ctx.Repo.BranchName |
| 52 | + if form.CommitChoice == frmCommitChoiceNewBranch { |
| 53 | + branchName = form.NewBranchName |
| 54 | + } |
| 55 | + ctx.Data["RequireHighlightJS"] = true |
| 56 | + ctx.Data["TreePath"] = "patch" |
| 57 | + ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/branch/" + ctx.Repo.BranchName |
| 58 | + ctx.Data["FileContent"] = form.Content |
| 59 | + ctx.Data["commit_summary"] = form.CommitSummary |
| 60 | + ctx.Data["commit_message"] = form.CommitMessage |
| 61 | + ctx.Data["commit_choice"] = form.CommitChoice |
| 62 | + ctx.Data["new_branch_name"] = form.NewBranchName |
| 63 | + ctx.Data["last_commit"] = ctx.Repo.CommitID |
| 64 | + ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",") |
| 65 | + |
| 66 | + if ctx.HasError() { |
| 67 | + ctx.HTML(200, tplPatchFile) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + // Cannot commit to a an existing branch if user doesn't have rights |
| 72 | + if branchName == ctx.Repo.BranchName && !canCommit { |
| 73 | + ctx.Data["Err_NewBranchName"] = true |
| 74 | + ctx.Data["commit_choice"] = frmCommitChoiceNewBranch |
| 75 | + ctx.RenderWithErr(ctx.Tr("repo.editor.cannot_commit_to_protected_branch", branchName), tplEditFile, &form) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + // CommitSummary is optional in the web form, if empty, give it a default message based on add or update |
| 80 | + // `message` will be both the summary and message combined |
| 81 | + message := strings.TrimSpace(form.CommitSummary) |
| 82 | + if len(message) == 0 { |
| 83 | + message = ctx.Tr("repo.editor.patch") |
| 84 | + } |
| 85 | + |
| 86 | + form.CommitMessage = strings.TrimSpace(form.CommitMessage) |
| 87 | + if len(form.CommitMessage) > 0 { |
| 88 | + message += "\n\n" + form.CommitMessage |
| 89 | + } |
| 90 | + |
| 91 | + if _, err := files.ApplyDiffPatch(ctx.Repo.Repository, ctx.User, &files.ApplyDiffPatchOptions{ |
| 92 | + LastCommitID: form.LastCommit, |
| 93 | + OldBranch: ctx.Repo.BranchName, |
| 94 | + NewBranch: branchName, |
| 95 | + Message: message, |
| 96 | + Content: strings.Replace(form.Content, "\r", "", -1), |
| 97 | + }); err != nil { |
| 98 | + if models.IsErrBranchAlreadyExists(err) { |
| 99 | + // For when a user specifies a new branch that already exists |
| 100 | + ctx.Data["Err_NewBranchName"] = true |
| 101 | + if branchErr, ok := err.(models.ErrBranchAlreadyExists); ok { |
| 102 | + ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchErr.BranchName), tplEditFile, &form) |
| 103 | + } else { |
| 104 | + ctx.Error(500, err.Error()) |
| 105 | + } |
| 106 | + } else if models.IsErrCommitIDDoesNotMatch(err) { |
| 107 | + ctx.RenderWithErr(ctx.Tr("repo.editor.file_changed_while_editing", ctx.Repo.RepoLink+"/compare/"+form.LastCommit+"..."+ctx.Repo.CommitID), tplPatchFile, &form) |
| 108 | + } else { |
| 109 | + ctx.RenderWithErr(ctx.Tr("repo.editor.fail_to_apply_patch", err), tplPatchFile, &form) |
| 110 | + } |
| 111 | + } |
| 112 | + ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + ctx.Repo.BranchName + "..." + form.NewBranchName) |
| 113 | +} |
0 commit comments