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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2021 The Gitea 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 repository
  5. import (
  6. "context"
  7. "errors"
  8. "fmt"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. user_model "code.gitea.io/gitea/models/user"
  13. "code.gitea.io/gitea/modules/git"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/notification"
  16. repo_module "code.gitea.io/gitea/modules/repository"
  17. pull_service "code.gitea.io/gitea/services/pull"
  18. )
  19. // CreateNewBranch creates a new repository branch
  20. func CreateNewBranch(doer *user_model.User, repo *repo_model.Repository, oldBranchName, branchName string) (err error) {
  21. // Check if branch name can be used
  22. if err := checkBranchName(git.DefaultContext, repo, branchName); err != nil {
  23. return err
  24. }
  25. if !git.IsBranchExist(git.DefaultContext, repo.RepoPath(), oldBranchName) {
  26. return models.ErrBranchDoesNotExist{
  27. BranchName: oldBranchName,
  28. }
  29. }
  30. if err := git.Push(git.DefaultContext, repo.RepoPath(), git.PushOptions{
  31. Remote: repo.RepoPath(),
  32. Branch: fmt.Sprintf("%s:%s%s", oldBranchName, git.BranchPrefix, branchName),
  33. Env: models.PushingEnvironment(doer, repo),
  34. }); err != nil {
  35. if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
  36. return err
  37. }
  38. return fmt.Errorf("Push: %v", err)
  39. }
  40. return nil
  41. }
  42. // GetBranch returns a branch by its name
  43. func GetBranch(repo *repo_model.Repository, branch string) (*git.Branch, error) {
  44. if len(branch) == 0 {
  45. return nil, fmt.Errorf("GetBranch: empty string for branch")
  46. }
  47. gitRepo, err := git.OpenRepository(repo.RepoPath())
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer gitRepo.Close()
  52. return gitRepo.GetBranch(branch)
  53. }
  54. // GetBranches returns branches from the repository, skipping skip initial branches and
  55. // returning at most limit branches, or all branches if limit is 0.
  56. func GetBranches(repo *repo_model.Repository, skip, limit int) ([]*git.Branch, int, error) {
  57. return git.GetBranchesByPath(repo.RepoPath(), skip, limit)
  58. }
  59. // checkBranchName validates branch name with existing repository branches
  60. func checkBranchName(ctx context.Context, repo *repo_model.Repository, name string) error {
  61. _, err := git.WalkReferences(ctx, repo.RepoPath(), func(refName string) error {
  62. branchRefName := strings.TrimPrefix(refName, git.BranchPrefix)
  63. switch {
  64. case branchRefName == name:
  65. return models.ErrBranchAlreadyExists{
  66. BranchName: name,
  67. }
  68. // If branchRefName like a/b but we want to create a branch named a then we have a conflict
  69. case strings.HasPrefix(branchRefName, name+"/"):
  70. return models.ErrBranchNameConflict{
  71. BranchName: branchRefName,
  72. }
  73. // Conversely if branchRefName like a but we want to create a branch named a/b then we also have a conflict
  74. case strings.HasPrefix(name, branchRefName+"/"):
  75. return models.ErrBranchNameConflict{
  76. BranchName: branchRefName,
  77. }
  78. case refName == git.TagPrefix+name:
  79. return models.ErrTagAlreadyExists{
  80. TagName: name,
  81. }
  82. }
  83. return nil
  84. })
  85. return err
  86. }
  87. // CreateNewBranchFromCommit creates a new repository branch
  88. func CreateNewBranchFromCommit(doer *user_model.User, repo *repo_model.Repository, commit, branchName string) (err error) {
  89. // Check if branch name can be used
  90. if err := checkBranchName(git.DefaultContext, repo, branchName); err != nil {
  91. return err
  92. }
  93. if err := git.Push(git.DefaultContext, repo.RepoPath(), git.PushOptions{
  94. Remote: repo.RepoPath(),
  95. Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName),
  96. Env: models.PushingEnvironment(doer, repo),
  97. }); err != nil {
  98. if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
  99. return err
  100. }
  101. return fmt.Errorf("Push: %v", err)
  102. }
  103. return nil
  104. }
  105. // RenameBranch rename a branch
  106. func RenameBranch(repo *repo_model.Repository, doer *user_model.User, gitRepo *git.Repository, from, to string) (string, error) {
  107. if from == to {
  108. return "target_exist", nil
  109. }
  110. if gitRepo.IsBranchExist(to) {
  111. return "target_exist", nil
  112. }
  113. if !gitRepo.IsBranchExist(from) {
  114. return "from_not_exist", nil
  115. }
  116. if err := models.RenameBranch(repo, from, to, func(isDefault bool) error {
  117. err2 := gitRepo.RenameBranch(from, to)
  118. if err2 != nil {
  119. return err2
  120. }
  121. if isDefault {
  122. err2 = gitRepo.SetDefaultBranch(to)
  123. if err2 != nil {
  124. return err2
  125. }
  126. }
  127. return nil
  128. }); err != nil {
  129. return "", err
  130. }
  131. notification.NotifyDeleteRef(doer, repo, "branch", git.BranchPrefix+from)
  132. notification.NotifyCreateRef(doer, repo, "branch", git.BranchPrefix+to)
  133. return "", nil
  134. }
  135. // enmuerates all branch related errors
  136. var (
  137. ErrBranchIsDefault = errors.New("branch is default")
  138. ErrBranchIsProtected = errors.New("branch is protected")
  139. )
  140. // DeleteBranch delete branch
  141. func DeleteBranch(doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string) error {
  142. if branchName == repo.DefaultBranch {
  143. return ErrBranchIsDefault
  144. }
  145. isProtected, err := models.IsProtectedBranch(repo.ID, branchName)
  146. if err != nil {
  147. return err
  148. }
  149. if isProtected {
  150. return ErrBranchIsProtected
  151. }
  152. commit, err := gitRepo.GetBranchCommit(branchName)
  153. if err != nil {
  154. return err
  155. }
  156. if err := gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  157. Force: true,
  158. }); err != nil {
  159. return err
  160. }
  161. if err := pull_service.CloseBranchPulls(doer, repo.ID, branchName); err != nil {
  162. return err
  163. }
  164. // Don't return error below this
  165. if err := PushUpdate(
  166. &repo_module.PushUpdateOptions{
  167. RefFullName: git.BranchPrefix + branchName,
  168. OldCommitID: commit.ID.String(),
  169. NewCommitID: git.EmptySHA,
  170. PusherID: doer.ID,
  171. PusherName: doer.Name,
  172. RepoUserName: repo.OwnerName,
  173. RepoName: repo.Name,
  174. }); err != nil {
  175. log.Error("Update: %v", err)
  176. }
  177. if err := models.AddDeletedBranch(repo.ID, branchName, commit.ID.String(), doer.ID); err != nil {
  178. log.Warn("AddDeletedBranch: %v", err)
  179. }
  180. return nil
  181. }