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.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/gitea/models"
  7. "code.gitea.io/gitea/modules/auth"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  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. // CreateBranch creates new branch in repository
  30. func CreateBranch(ctx *context.Context, form auth.NewBranchForm) {
  31. if !ctx.Repo.CanCreateBranch() {
  32. ctx.Handle(404, "CreateBranch", nil)
  33. return
  34. }
  35. if ctx.HasError() {
  36. ctx.Flash.Error(ctx.GetErrMsg())
  37. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName)
  38. return
  39. }
  40. var err error
  41. if ctx.Repo.IsViewBranch {
  42. err = ctx.Repo.Repository.CreateNewBranch(ctx.User, ctx.Repo.BranchName, form.NewBranchName)
  43. } else {
  44. err = ctx.Repo.Repository.CreateNewBranchFromCommit(ctx.User, ctx.Repo.BranchName, form.NewBranchName)
  45. }
  46. if err != nil {
  47. if models.IsErrTagAlreadyExists(err) {
  48. e := err.(models.ErrTagAlreadyExists)
  49. ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))
  50. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName)
  51. return
  52. }
  53. if models.IsErrBranchAlreadyExists(err) {
  54. e := err.(models.ErrBranchAlreadyExists)
  55. ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", e.BranchName))
  56. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName)
  57. return
  58. }
  59. if models.IsErrBranchNameConflict(err) {
  60. e := err.(models.ErrBranchNameConflict)
  61. ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName))
  62. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName)
  63. return
  64. }
  65. ctx.Handle(500, "CreateNewBranch", err)
  66. return
  67. }
  68. ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName))
  69. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + form.NewBranchName)
  70. }