gitea源码

editor_apply_patch.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "net/http"
  6. "strings"
  7. "code.gitea.io/gitea/modules/util"
  8. "code.gitea.io/gitea/services/context"
  9. "code.gitea.io/gitea/services/forms"
  10. "code.gitea.io/gitea/services/repository/files"
  11. )
  12. func NewDiffPatch(ctx *context.Context) {
  13. prepareEditorCommitFormOptions(ctx, "_diffpatch")
  14. if ctx.Written() {
  15. return
  16. }
  17. ctx.Data["PageIsPatch"] = true
  18. ctx.HTML(http.StatusOK, tplPatchFile)
  19. }
  20. // NewDiffPatchPost response for sending patch page
  21. func NewDiffPatchPost(ctx *context.Context) {
  22. parsed := prepareEditorCommitSubmittedForm[*forms.EditRepoFileForm](ctx)
  23. if ctx.Written() {
  24. return
  25. }
  26. defaultCommitMessage := ctx.Locale.TrString("repo.editor.patch")
  27. _, err := files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.Doer, &files.ApplyDiffPatchOptions{
  28. LastCommitID: parsed.form.LastCommit,
  29. OldBranch: parsed.OldBranchName,
  30. NewBranch: parsed.NewBranchName,
  31. Message: parsed.GetCommitMessage(defaultCommitMessage),
  32. Content: strings.ReplaceAll(parsed.form.Content.Value(), "\r\n", "\n"),
  33. Author: parsed.GitCommitter,
  34. Committer: parsed.GitCommitter,
  35. })
  36. if err != nil {
  37. err = util.ErrorWrapLocale(err, "repo.editor.fail_to_apply_patch")
  38. }
  39. if err != nil {
  40. editorHandleFileOperationError(ctx, parsed.NewBranchName, err)
  41. return
  42. }
  43. redirectForCommitChoice(ctx, parsed, parsed.form.TreePath)
  44. }