|
|
|
@@ -5,6 +5,7 @@ |
|
|
|
package repo |
|
|
|
|
|
|
|
import ( |
|
|
|
"code.gitea.io/gitea/routers/response" |
|
|
|
repo_service "code.gitea.io/gitea/services/repository" |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
@@ -803,11 +804,15 @@ func RenameFilePost(ctx *context.Context, form auth.RenameRepoFileForm) { |
|
|
|
} |
|
|
|
|
|
|
|
func renameFilePost(ctx *context.Context, form auth.RenameRepoFileForm) { |
|
|
|
//需要校验参数 commitId和 |
|
|
|
if form.TreePath == "" || form.LastCommit == "" { |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError("param error")) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
canCommit := renderCommitRights(ctx) |
|
|
|
branchName := ctx.Repo.BranchName |
|
|
|
if ctx.HasError() { |
|
|
|
ctx.HTML(200, tplEditFile) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Flash.ErrorMsg)) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
@@ -815,15 +820,11 @@ func renameFilePost(ctx *context.Context, form auth.RenameRepoFileForm) { |
|
|
|
if branchName == ctx.Repo.BranchName && !canCommit { |
|
|
|
ctx.Data["Err_NewBranchName"] = true |
|
|
|
ctx.Data["commit_choice"] = frmCommitChoiceNewBranch |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.cannot_commit_to_protected_branch", branchName), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.cannot_commit_to_protected_branch", branchName))) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
message := ctx.Tr("repo.editor.update", form.TreePath) |
|
|
|
|
|
|
|
if len(message) > 0 { |
|
|
|
message += "\n\n" + message |
|
|
|
} |
|
|
|
message := ctx.Tr("repo.editor.rename", ctx.Repo.TreePath, form.TreePath) |
|
|
|
|
|
|
|
if err := repofiles.RenameRepoFile(ctx.Repo.Repository, ctx.User, &repofiles.RenameRepoFileOptions{ |
|
|
|
LastCommitID: form.LastCommit, |
|
|
|
@@ -834,62 +835,60 @@ func renameFilePost(ctx *context.Context, form auth.RenameRepoFileForm) { |
|
|
|
}); err != nil { |
|
|
|
// This is where we handle all the errors thrown by repofiles.CreateOrUpdateRepoFile |
|
|
|
if git.IsErrNotExist(err) { |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.file_editing_no_longer_exists", ctx.Repo.TreePath), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.file_editing_no_longer_exists", ctx.Repo.TreePath))) |
|
|
|
} else if models.IsErrLFSFileLocked(err) { |
|
|
|
ctx.Data["Err_TreePath"] = true |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.upload_file_is_locked", err.(models.ErrLFSFileLocked).Path, err.(models.ErrLFSFileLocked).UserName), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.file_editing_no_longer_exists", ctx.Tr("repo.editor.upload_file_is_locked", err.(models.ErrLFSFileLocked).Path, err.(models.ErrLFSFileLocked).UserName)))) |
|
|
|
} else if models.IsErrFilenameInvalid(err) { |
|
|
|
ctx.Data["Err_TreePath"] = true |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.filename_is_invalid", form.TreePath), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.filename_is_invalid", form.TreePath))) |
|
|
|
} else if models.IsErrFilePathInvalid(err) { |
|
|
|
ctx.Data["Err_TreePath"] = true |
|
|
|
if fileErr, ok := err.(models.ErrFilePathInvalid); ok { |
|
|
|
switch fileErr.Type { |
|
|
|
case git.EntryModeSymlink: |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.file_is_a_symlink", fileErr.Path), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.file_is_a_symlink", fileErr.Path))) |
|
|
|
case git.EntryModeTree: |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.filename_is_a_directory", fileErr.Path), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.filename_is_a_directory", fileErr.Path))) |
|
|
|
case git.EntryModeBlob: |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.directory_is_a_file", fileErr.Path), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.directory_is_a_file", fileErr.Path))) |
|
|
|
default: |
|
|
|
ctx.Error(500, err.Error()) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(err.Error())) |
|
|
|
} |
|
|
|
} else { |
|
|
|
ctx.Error(500, err.Error()) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(err.Error())) |
|
|
|
} |
|
|
|
} else if models.IsErrRepoFileAlreadyExists(err) { |
|
|
|
ctx.Data["Err_TreePath"] = true |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.file_already_exists", form.TreePath), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.file_already_exists", form.TreePath))) |
|
|
|
} else if git.IsErrBranchNotExist(err) { |
|
|
|
// For when a user adds/updates a file to a branch that no longer exists |
|
|
|
if branchErr, ok := err.(git.ErrBranchNotExist); ok { |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.branch_does_not_exist", branchErr.Name), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.branch_does_not_exist", branchErr.Name))) |
|
|
|
} else { |
|
|
|
ctx.Error(500, err.Error()) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(err.Error())) |
|
|
|
} |
|
|
|
} else if models.IsErrBranchAlreadyExists(err) { |
|
|
|
// For when a user specifies a new branch that already exists |
|
|
|
ctx.Data["Err_NewBranchName"] = true |
|
|
|
if branchErr, ok := err.(models.ErrBranchAlreadyExists); ok { |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchErr.BranchName), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.branch_already_exists", branchErr.BranchName))) |
|
|
|
} else { |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(err.Error())) |
|
|
|
ctx.Error(500, err.Error()) |
|
|
|
} |
|
|
|
} else if models.IsErrCommitIDDoesNotMatch(err) { |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.file_changed_while_editing"), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.file_changed_while_renaming"))) |
|
|
|
} else if git.IsErrPushOutOfDate(err) { |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.file_changed_while_editing"), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.file_changed_while_renaming"))) |
|
|
|
} else if git.IsErrPushRejected(err) { |
|
|
|
errPushRej := err.(*git.ErrPushRejected) |
|
|
|
if len(errPushRej.Message) == 0 { |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected_no_message"), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.push_rejected_no_message"))) |
|
|
|
} else { |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected", utils.SanitizeFlashErrorString(errPushRej.Message)), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.push_rejected", utils.SanitizeFlashErrorString(errPushRej.Message)))) |
|
|
|
} |
|
|
|
} else { |
|
|
|
ctx.RenderWithErr(ctx.Tr("repo.editor.fail_to_update_file", form.TreePath, utils.SanitizeFlashErrorString(err.Error())), tplEditFile, &form) |
|
|
|
ctx.JSON(http.StatusOK, response.ServerError(ctx.Tr("repo.editor.fail_to_update_file", form.TreePath, utils.SanitizeFlashErrorString(err.Error())))) |
|
|
|
} |
|
|
|
return |
|
|
|
} |
|
|
|
ctx.JSON(http.StatusOK, "success") |
|
|
|
ctx.JSON(http.StatusOK, response.Success()) |
|
|
|
|
|
|
|
} |