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.

repo_branch.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. // CanCreateBranch returns true if repository meets the requirements for creating new branches.
  12. func (repo *Repository) CanCreateBranch() bool {
  13. return !repo.IsMirror
  14. }
  15. // GetBranch returns a branch by its name
  16. func (repo *Repository) GetBranch(branch string) (*git.Branch, error) {
  17. gitRepo, err := git.OpenRepository(repo.RepoPath())
  18. if err != nil {
  19. return nil, err
  20. }
  21. return gitRepo.GetBranch(branch)
  22. }
  23. // GetBranches returns all the branches of a repository
  24. func (repo *Repository) GetBranches() ([]*git.Branch, error) {
  25. return git.GetBranchesByPath(repo.RepoPath())
  26. }
  27. // CheckBranchName validates branch name with existing repository branches
  28. func (repo *Repository) CheckBranchName(name string) error {
  29. gitRepo, err := git.OpenRepository(repo.RepoPath())
  30. if err != nil {
  31. return err
  32. }
  33. branches, err := repo.GetBranches()
  34. if err != nil {
  35. return err
  36. }
  37. for _, branch := range branches {
  38. if branch.Name == name {
  39. return ErrBranchAlreadyExists{branch.Name}
  40. } else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) ||
  41. (len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) {
  42. return ErrBranchNameConflict{branch.Name}
  43. }
  44. }
  45. if _, err := gitRepo.GetTag(name); err == nil {
  46. return ErrTagAlreadyExists{name}
  47. }
  48. return nil
  49. }
  50. // CreateNewBranch creates a new repository branch
  51. func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName string) (err error) {
  52. // Check if branch name can be used
  53. if err := repo.CheckBranchName(branchName); err != nil {
  54. return err
  55. }
  56. if !git.IsBranchExist(repo.RepoPath(), oldBranchName) {
  57. return fmt.Errorf("OldBranch: %s does not exist. Cannot create new branch from this", oldBranchName)
  58. }
  59. basePath, err := CreateTemporaryPath("branch-maker")
  60. if err != nil {
  61. return err
  62. }
  63. defer func() {
  64. if err := RemoveTemporaryPath(basePath); err != nil {
  65. log.Error("CreateNewBranch: RemoveTemporaryPath: %s", err)
  66. }
  67. }()
  68. if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
  69. Bare: true,
  70. Shared: true,
  71. }); err != nil {
  72. log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
  73. return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
  74. }
  75. gitRepo, err := git.OpenRepository(basePath)
  76. if err != nil {
  77. log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
  78. return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
  79. }
  80. if err = gitRepo.CreateBranch(branchName, oldBranchName); err != nil {
  81. log.Error("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
  82. return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
  83. }
  84. if err = git.Push(basePath, git.PushOptions{
  85. Remote: "origin",
  86. Branch: branchName,
  87. Env: PushingEnvironment(doer, repo),
  88. }); err != nil {
  89. return fmt.Errorf("Push: %v", err)
  90. }
  91. return nil
  92. }
  93. // CreateNewBranchFromCommit creates a new repository branch
  94. func (repo *Repository) CreateNewBranchFromCommit(doer *User, commit, branchName string) (err error) {
  95. // Check if branch name can be used
  96. if err := repo.CheckBranchName(branchName); err != nil {
  97. return err
  98. }
  99. basePath, err := CreateTemporaryPath("branch-maker")
  100. if err != nil {
  101. return err
  102. }
  103. defer func() {
  104. if err := RemoveTemporaryPath(basePath); err != nil {
  105. log.Error("CreateNewBranchFromCommit: RemoveTemporaryPath: %s", err)
  106. }
  107. }()
  108. if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
  109. Bare: true,
  110. Shared: true,
  111. }); err != nil {
  112. log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
  113. return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
  114. }
  115. gitRepo, err := git.OpenRepository(basePath)
  116. if err != nil {
  117. log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
  118. return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
  119. }
  120. if err = gitRepo.CreateBranch(branchName, commit); err != nil {
  121. log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
  122. return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
  123. }
  124. if err = git.Push(basePath, git.PushOptions{
  125. Remote: "origin",
  126. Branch: branchName,
  127. Env: PushingEnvironment(doer, repo),
  128. }); err != nil {
  129. return fmt.Errorf("Push: %v", err)
  130. }
  131. return nil
  132. }