(c *context.Context, f form.DeleteRepoFile)
| 339 | } |
| 340 | |
| 341 | func DeleteFilePost(c *context.Context, f form.DeleteRepoFile) { |
| 342 | c.PageIs("Delete") |
| 343 | c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + c.Repo.BranchName |
| 344 | |
| 345 | // 🚨 SECURITY: Prevent path traversal. |
| 346 | c.Repo.TreePath = pathutil.Clean(c.Repo.TreePath) |
| 347 | c.Data["TreePath"] = c.Repo.TreePath |
| 348 | |
| 349 | oldBranchName := c.Repo.BranchName |
| 350 | branchName := oldBranchName |
| 351 | |
| 352 | if f.IsNewBrnach() { |
| 353 | branchName = f.NewBranchName |
| 354 | } |
| 355 | c.Data["commit_summary"] = f.CommitSummary |
| 356 | c.Data["commit_message"] = f.CommitMessage |
| 357 | c.Data["commit_choice"] = f.CommitChoice |
| 358 | c.Data["new_branch_name"] = branchName |
| 359 | |
| 360 | if c.HasError() { |
| 361 | c.HTML(http.StatusBadRequest, tmplEditorDelete) |
| 362 | return |
| 363 | } |
| 364 | |
| 365 | if oldBranchName != branchName { |
| 366 | if _, err := c.Repo.Repository.GetBranch(branchName); err == nil { |
| 367 | c.FormErr("NewBranchName") |
| 368 | c.RenderWithErr(c.Tr("repo.editor.branch_already_exists", branchName), http.StatusUnprocessableEntity, tmplEditorDelete, &f) |
| 369 | return |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | message := strings.TrimSpace(f.CommitSummary) |
| 374 | if message == "" { |
| 375 | message = c.Tr("repo.editor.delete", c.Repo.TreePath) |
| 376 | } |
| 377 | |
| 378 | f.CommitMessage = strings.TrimSpace(f.CommitMessage) |
| 379 | if len(f.CommitMessage) > 0 { |
| 380 | message += "\n\n" + f.CommitMessage |
| 381 | } |
| 382 | |
| 383 | if err := c.Repo.Repository.DeleteRepoFile(c.User, database.DeleteRepoFileOptions{ |
| 384 | LastCommitID: c.Repo.CommitID, |
| 385 | OldBranch: oldBranchName, |
| 386 | NewBranch: branchName, |
| 387 | TreePath: c.Repo.TreePath, |
| 388 | Message: message, |
| 389 | }); err != nil { |
| 390 | log.Error("Failed to delete repo file: %v", err) |
| 391 | c.RenderWithErr(c.Tr("repo.editor.fail_to_delete_file", c.Repo.TreePath, errInternalServerError), http.StatusInternalServerError, tmplEditorDelete, &f) |
| 392 | return |
| 393 | } |
| 394 | |
| 395 | if f.IsNewBrnach() && c.Repo.PullRequest.Allowed { |
| 396 | c.Redirect(c.Repo.PullRequestURL(oldBranchName, f.NewBranchName)) |
| 397 | } else { |
| 398 | c.Flash.Success(c.Tr("repo.editor.file_delete_success", c.Repo.TreePath)) |
nothing calls this directly
no test coverage detected