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.

91 lines
2.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "code.gitea.io/git"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/log"
  11. )
  12. const (
  13. tplBranch base.TplName = "repo/branch"
  14. )
  15. // Branches render repository branch page
  16. func Branches(ctx *context.Context) {
  17. ctx.Data["Title"] = "Branches"
  18. ctx.Data["IsRepoToolbarBranches"] = true
  19. brs, err := ctx.Repo.GitRepo.GetBranches()
  20. if err != nil {
  21. ctx.Handle(500, "repo.Branches(GetBranches)", err)
  22. return
  23. } else if len(brs) == 0 {
  24. ctx.Handle(404, "repo.Branches(GetBranches)", nil)
  25. return
  26. }
  27. ctx.Data["Branches"] = brs
  28. ctx.HTML(200, tplBranch)
  29. }
  30. // DeleteBranchPost responses for delete merged branch
  31. func DeleteBranchPost(ctx *context.Context) {
  32. branchName := ctx.Params(":name")
  33. commitID := ctx.Query("commit")
  34. defer func() {
  35. redirectTo := ctx.Query("redirect_to")
  36. if len(redirectTo) == 0 {
  37. redirectTo = ctx.Repo.RepoLink
  38. }
  39. ctx.JSON(200, map[string]interface{}{
  40. "redirect": redirectTo,
  41. })
  42. }()
  43. fullBranchName := ctx.Repo.Owner.Name + "/" + branchName
  44. if !ctx.Repo.GitRepo.IsBranchExist(branchName) || branchName == "master" {
  45. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  46. return
  47. }
  48. if len(commitID) > 0 {
  49. branchCommitID, err := ctx.Repo.GitRepo.GetBranchCommitID(branchName)
  50. if err != nil {
  51. log.Error(4, "GetBranchCommitID: %v", err)
  52. return
  53. }
  54. if branchCommitID != commitID {
  55. ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_has_new_commits", fullBranchName))
  56. return
  57. }
  58. }
  59. if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  60. Force: true,
  61. }); err != nil {
  62. log.Error(4, "DeleteBranch: %v", err)
  63. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  64. return
  65. }
  66. issueID := ctx.QueryInt64("issue_id")
  67. if issueID > 0 {
  68. if err := models.AddDeletePRBranchComment(ctx.User, ctx.Repo.Repository, issueID, branchName); err != nil {
  69. log.Error(4, "DeleteBranch: %v", err)
  70. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  71. return
  72. }
  73. }
  74. ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
  75. }