code.gitea.io/gitea@v1.21.7/routers/web/repo/patch.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"strings"
     8  
     9  	"code.gitea.io/gitea/models"
    10  	git_model "code.gitea.io/gitea/models/git"
    11  	"code.gitea.io/gitea/models/unit"
    12  	"code.gitea.io/gitea/modules/base"
    13  	"code.gitea.io/gitea/modules/context"
    14  	"code.gitea.io/gitea/modules/setting"
    15  	"code.gitea.io/gitea/modules/util"
    16  	"code.gitea.io/gitea/modules/web"
    17  	"code.gitea.io/gitea/services/forms"
    18  	"code.gitea.io/gitea/services/repository/files"
    19  )
    20  
    21  const (
    22  	tplPatchFile base.TplName = "repo/editor/patch"
    23  )
    24  
    25  // NewDiffPatch render create patch page
    26  func NewDiffPatch(ctx *context.Context) {
    27  	canCommit := renderCommitRights(ctx)
    28  
    29  	ctx.Data["PageIsPatch"] = true
    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["PageIsPatch"] = true
    56  	ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
    57  	ctx.Data["FileContent"] = form.Content
    58  	ctx.Data["commit_summary"] = form.CommitSummary
    59  	ctx.Data["commit_message"] = form.CommitMessage
    60  	ctx.Data["commit_choice"] = form.CommitChoice
    61  	ctx.Data["new_branch_name"] = form.NewBranchName
    62  	ctx.Data["last_commit"] = ctx.Repo.CommitID
    63  	ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
    64  
    65  	if ctx.HasError() {
    66  		ctx.HTML(200, tplPatchFile)
    67  		return
    68  	}
    69  
    70  	// Cannot commit to a an existing branch if user doesn't have rights
    71  	if branchName == ctx.Repo.BranchName && !canCommit {
    72  		ctx.Data["Err_NewBranchName"] = true
    73  		ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
    74  		ctx.RenderWithErr(ctx.Tr("repo.editor.cannot_commit_to_protected_branch", branchName), tplEditFile, &form)
    75  		return
    76  	}
    77  
    78  	// CommitSummary is optional in the web form, if empty, give it a default message based on add or update
    79  	// `message` will be both the summary and message combined
    80  	message := strings.TrimSpace(form.CommitSummary)
    81  	if len(message) == 0 {
    82  		message = ctx.Tr("repo.editor.patch")
    83  	}
    84  
    85  	form.CommitMessage = strings.TrimSpace(form.CommitMessage)
    86  	if len(form.CommitMessage) > 0 {
    87  		message += "\n\n" + form.CommitMessage
    88  	}
    89  
    90  	fileResponse, err := files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.Doer, &files.ApplyDiffPatchOptions{
    91  		LastCommitID: form.LastCommit,
    92  		OldBranch:    ctx.Repo.BranchName,
    93  		NewBranch:    branchName,
    94  		Message:      message,
    95  		Content:      strings.ReplaceAll(form.Content, "\r", ""),
    96  	})
    97  	if err != nil {
    98  		if git_model.IsErrBranchAlreadyExists(err) {
    99  			// User has specified a branch that already exists
   100  			branchErr := err.(git_model.ErrBranchAlreadyExists)
   101  			ctx.Data["Err_NewBranchName"] = true
   102  			ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchErr.BranchName), tplEditFile, &form)
   103  			return
   104  		} else if models.IsErrCommitIDDoesNotMatch(err) {
   105  			ctx.RenderWithErr(ctx.Tr("repo.editor.file_changed_while_editing", ctx.Repo.RepoLink+"/compare/"+form.LastCommit+"..."+ctx.Repo.CommitID), tplPatchFile, &form)
   106  			return
   107  		} else {
   108  			ctx.RenderWithErr(ctx.Tr("repo.editor.fail_to_apply_patch", err), tplPatchFile, &form)
   109  			return
   110  		}
   111  	}
   112  
   113  	if form.CommitChoice == frmCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) {
   114  		ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName))
   115  	} else {
   116  		ctx.Redirect(ctx.Repo.RepoLink + "/commit/" + fileResponse.Commit.SHA)
   117  	}
   118  }