branch.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. log "gopkg.in/clog.v1"
  7. "github.com/gogits/git-module"
  8. "github.com/gogits/gogs/modules/base"
  9. "github.com/gogits/gogs/modules/context"
  10. )
  11. const (
  12. BRANCH base.TplName = "repo/branch"
  13. )
  14. func Branches(ctx *context.Context) {
  15. ctx.Data["Title"] = "Branches"
  16. ctx.Data["IsRepoToolbarBranches"] = true
  17. brs, err := ctx.Repo.GitRepo.GetBranches()
  18. if err != nil {
  19. ctx.Handle(500, "repo.Branches(GetBranches)", err)
  20. return
  21. } else if len(brs) == 0 {
  22. ctx.Handle(404, "repo.Branches(GetBranches)", nil)
  23. return
  24. }
  25. ctx.Data["Branches"] = brs
  26. ctx.HTML(200, BRANCH)
  27. }
  28. func DeleteBranchPost(ctx *context.Context) {
  29. branchName := ctx.Params("*")
  30. commitID := ctx.Query("commit")
  31. defer func() {
  32. redirectTo := ctx.Query("redirect_to")
  33. if len(redirectTo) == 0 {
  34. redirectTo = ctx.Repo.RepoLink
  35. }
  36. ctx.Redirect(redirectTo)
  37. }()
  38. if !ctx.Repo.GitRepo.IsBranchExist(branchName) {
  39. return
  40. }
  41. if len(commitID) > 0 {
  42. branchCommitID, err := ctx.Repo.GitRepo.GetBranchCommitID(branchName)
  43. if err != nil {
  44. log.Error(2, "GetBranchCommitID: %v", err)
  45. return
  46. }
  47. if branchCommitID != commitID {
  48. ctx.Flash.Error(ctx.Tr("repo.pulls.delete_branch_has_new_commits"))
  49. return
  50. }
  51. }
  52. if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  53. Force: true,
  54. }); err != nil {
  55. log.Error(2, "DeleteBranch '%s': %v", branchName, err)
  56. return
  57. }
  58. }