You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

patch.go 4.1KB

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