2025-06-21 19:20:51 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
2025-12-11 16:15:40 -08:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2025-06-21 19:20:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
"code.gitea.io/gitea/services/context"
|
|
|
|
|
"code.gitea.io/gitea/services/forms"
|
|
|
|
|
"code.gitea.io/gitea/services/repository/files"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func CherryPick(ctx *context.Context) {
|
2025-11-30 06:58:15 +03:00
|
|
|
prepareEditorPage(ctx, "_cherrypick")
|
2025-06-21 19:20:51 +08:00
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fromCommitID := ctx.PathParam("sha")
|
|
|
|
|
ctx.Data["FromCommitID"] = fromCommitID
|
|
|
|
|
cherryPickCommit, err := ctx.Repo.GitRepo.GetCommit(fromCommitID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
HandleGitError(ctx, "GetCommit", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ctx.FormString("cherry-pick-type") == "revert" {
|
|
|
|
|
ctx.Data["CherryPickType"] = "revert"
|
|
|
|
|
ctx.Data["commit_summary"] = "revert " + ctx.PathParam("sha")
|
|
|
|
|
ctx.Data["commit_message"] = "revert " + cherryPickCommit.Message()
|
|
|
|
|
} else {
|
|
|
|
|
ctx.Data["CherryPickType"] = "cherry-pick"
|
2025-12-13 02:56:05 +08:00
|
|
|
ctx.Data["commit_summary"], ctx.Data["commit_message"], _ = strings.Cut(cherryPickCommit.Message(), "\n")
|
2025-06-21 19:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.HTML(http.StatusOK, tplCherryPick)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CherryPickPost(ctx *context.Context) {
|
|
|
|
|
fromCommitID := ctx.PathParam("sha")
|
2025-06-22 14:43:43 +02:00
|
|
|
parsed := prepareEditorCommitSubmittedForm[*forms.CherryPickForm](ctx)
|
2025-06-21 19:20:51 +08:00
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defaultCommitMessage := util.Iif(parsed.form.Revert, ctx.Locale.TrString("repo.commit.revert-header", fromCommitID), ctx.Locale.TrString("repo.commit.cherry-pick-header", fromCommitID))
|
|
|
|
|
opts := &files.ApplyDiffPatchOptions{
|
|
|
|
|
LastCommitID: parsed.form.LastCommit,
|
2025-06-22 14:43:43 +02:00
|
|
|
OldBranch: parsed.OldBranchName,
|
|
|
|
|
NewBranch: parsed.NewBranchName,
|
2025-06-21 19:20:51 +08:00
|
|
|
Message: parsed.GetCommitMessage(defaultCommitMessage),
|
|
|
|
|
Author: parsed.GitCommitter,
|
|
|
|
|
Committer: parsed.GitCommitter,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// First try the simple plain read-tree -m approach
|
|
|
|
|
opts.Content = fromCommitID
|
|
|
|
|
if _, err := files.CherryPick(ctx, ctx.Repo.Repository, ctx.Doer, parsed.form.Revert, opts); err != nil {
|
|
|
|
|
// Drop through to the "apply" method
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
|
if parsed.form.Revert {
|
2025-12-11 16:15:40 -08:00
|
|
|
err = gitrepo.GetReverseRawDiff(ctx, ctx.Repo.Repository, fromCommitID, buf)
|
2025-06-21 19:20:51 +08:00
|
|
|
} else {
|
2026-01-23 10:10:11 +08:00
|
|
|
err = git.GetRawDiff(ctx.Repo.GitRepo, fromCommitID, git.RawDiffPatch, buf)
|
2025-06-21 19:20:51 +08:00
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
opts.Content = buf.String()
|
|
|
|
|
_, err = files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.Doer, opts)
|
|
|
|
|
if err != nil {
|
2025-10-21 22:06:56 -07:00
|
|
|
err = util.ErrorWrapTranslatable(err, "repo.editor.fail_to_apply_patch")
|
2025-06-21 19:20:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2025-06-22 14:43:43 +02:00
|
|
|
editorHandleFileOperationError(ctx, parsed.NewBranchName, err)
|
2025-06-21 19:20:51 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
redirectForCommitChoice(ctx, parsed, parsed.form.TreePath)
|
|
|
|
|
}
|