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.

branch.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "github.com/gogits/gogs/models"
  7. "github.com/gogits/gogs/modules/auth"
  8. "github.com/gogits/gogs/modules/base"
  9. "github.com/gogits/gogs/modules/context"
  10. "github.com/gogits/gogs/modules/log"
  11. "net/url"
  12. "strings"
  13. )
  14. const (
  15. BRANCH base.TplName = "repo/branch"
  16. )
  17. func Branches(ctx *context.Context) {
  18. ctx.Data["Title"] = "Branches"
  19. ctx.Data["IsRepoToolbarBranches"] = true
  20. brs, err := ctx.Repo.GitRepo.GetBranches()
  21. if err != nil {
  22. ctx.Handle(500, "repo.Branches(GetBranches)", err)
  23. return
  24. } else if len(brs) == 0 {
  25. ctx.Handle(404, "repo.Branches(GetBranches)", nil)
  26. return
  27. }
  28. ctx.Data["Branches"] = brs
  29. ctx.HTML(200, BRANCH)
  30. }
  31. func NewBranchPost(ctx *context.Context, form auth.NewBranchForm) {
  32. oldBranchName := form.OldBranchName
  33. branchName := form.BranchName
  34. if ctx.HasError() || !ctx.Repo.IsWriter() || branchName == oldBranchName {
  35. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + oldBranchName)
  36. return
  37. }
  38. branchName = url.QueryEscape(strings.Replace(strings.Trim(branchName, " "), " ", "-", -1))
  39. if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
  40. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName)
  41. return
  42. }
  43. if err := ctx.Repo.Repository.CreateNewBranch(ctx.User, oldBranchName, branchName); err != nil {
  44. ctx.Handle(404, "repo.Branches(CreateNewBranch)", err)
  45. log.Error(4, "%s: %v", "EditFile", err)
  46. return
  47. }
  48. // Was successful, so now need to call models.CommitRepoAction() with the new commitID for webhooks and watchers
  49. if branch, err := ctx.Repo.Repository.GetBranch(branchName); err != nil {
  50. log.Error(4, "repo.Repository.GetBranch(%s): %v", branchName, err)
  51. } else if commit, err := branch.GetCommit(); err != nil {
  52. log.Error(4, "branch.GetCommit(): %v", err)
  53. } else {
  54. pc := &models.PushCommits{
  55. Len: 1,
  56. Commits: []*models.PushCommit{models.CommitToPushCommit(commit)},
  57. }
  58. oldCommitID := "0000000000000000000000000000000000000000" // New Branch so we use all 0s
  59. newCommitID := commit.ID.String()
  60. if err := models.CommitRepoAction(ctx.User.ID, ctx.Repo.Owner.ID, ctx.User.LowerName, ctx.Repo.Owner.Email,
  61. ctx.Repo.Repository.ID, ctx.Repo.Owner.LowerName, ctx.Repo.Repository.Name, "refs/heads/"+branchName, pc,
  62. oldCommitID, newCommitID); err != nil {
  63. log.Error(4, "models.CommitRepoAction(branch = %s): %v", branchName, err)
  64. }
  65. models.HookQueue.Add(ctx.Repo.Repository.ID)
  66. }
  67. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName)
  68. }