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.

81 lines
2.0 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/modules/base"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. const (
  12. tplBranch base.TplName = "repo/branch"
  13. )
  14. // Branches render repository branch page
  15. func Branches(ctx *context.Context) {
  16. ctx.Data["Title"] = "Branches"
  17. ctx.Data["IsRepoToolbarBranches"] = true
  18. brs, err := ctx.Repo.GitRepo.GetBranches()
  19. if err != nil {
  20. ctx.Handle(500, "repo.Branches(GetBranches)", err)
  21. return
  22. } else if len(brs) == 0 {
  23. ctx.Handle(404, "repo.Branches(GetBranches)", nil)
  24. return
  25. }
  26. ctx.Data["Branches"] = brs
  27. ctx.HTML(200, tplBranch)
  28. }
  29. // DeleteBranchPost responses for delete merged branch
  30. func DeleteBranchPost(ctx *context.Context) {
  31. branchName := ctx.Params(":name")
  32. commitID := ctx.Query("commit")
  33. defer func() {
  34. redirectTo := ctx.Query("redirect_to")
  35. if len(redirectTo) == 0 {
  36. redirectTo = ctx.Repo.RepoLink
  37. }
  38. ctx.JSON(200, map[string]interface{}{
  39. "redirect": redirectTo,
  40. })
  41. }()
  42. fullBranchName := ctx.Repo.Owner.Name + "/" + branchName
  43. if !ctx.Repo.GitRepo.IsBranchExist(branchName) || branchName == "master" {
  44. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  45. return
  46. }
  47. if len(commitID) > 0 {
  48. branchCommitID, err := ctx.Repo.GitRepo.GetBranchCommitID(branchName)
  49. if err != nil {
  50. log.Error(4, "GetBranchCommitID: %v", err)
  51. return
  52. }
  53. if branchCommitID != commitID {
  54. ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_has_new_commits", fullBranchName))
  55. return
  56. }
  57. }
  58. if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  59. Force: false,
  60. }); err != nil {
  61. log.Error(4, "DeleteBranch: %v", err)
  62. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
  63. return
  64. }
  65. ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
  66. }