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.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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(EscapeUrl(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(EscapeUrl(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.PushCommit{
  57. commit.ID.String(),
  58. commit.Message(),
  59. commit.Author.Email,
  60. commit.Author.Name,
  61. }},
  62. }
  63. oldCommitID := "0000000000000000000000000000000000000000" // New Branch so we use all 0s
  64. newCommitID := commit.ID.String()
  65. if err := models.CommitRepoAction(ctx.User.ID, ctx.Repo.Owner.ID, ctx.User.LowerName, ctx.Repo.Owner.Email,
  66. ctx.Repo.Repository.ID, ctx.Repo.Owner.LowerName, ctx.Repo.Repository.Name, "refs/heads/"+branchName, pc,
  67. oldCommitID, newCommitID); err != nil {
  68. log.Error(4, "models.CommitRepoAction(branch = %s): %v", branchName, err)
  69. }
  70. models.HookQueue.Add(ctx.Repo.Repository.ID)
  71. }
  72. ctx.Redirect(EscapeUrl(ctx.Repo.RepoLink + "/src/" + branchName))
  73. }