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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2020 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. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. // GetBranch returns a branch by its name
  12. func GetBranch(repo *models.Repository, branch string) (*git.Branch, error) {
  13. gitRepo, err := git.OpenRepository(repo.RepoPath())
  14. if err != nil {
  15. return nil, err
  16. }
  17. defer gitRepo.Close()
  18. return gitRepo.GetBranch(branch)
  19. }
  20. // GetBranches returns all the branches of a repository
  21. func GetBranches(repo *models.Repository) ([]*git.Branch, error) {
  22. return git.GetBranchesByPath(repo.RepoPath())
  23. }
  24. // checkBranchName validates branch name with existing repository branches
  25. func checkBranchName(repo *models.Repository, name string) error {
  26. gitRepo, err := git.OpenRepository(repo.RepoPath())
  27. if err != nil {
  28. return err
  29. }
  30. defer gitRepo.Close()
  31. branches, err := GetBranches(repo)
  32. if err != nil {
  33. return err
  34. }
  35. for _, branch := range branches {
  36. if branch.Name == name {
  37. return models.ErrBranchAlreadyExists{
  38. BranchName: branch.Name,
  39. }
  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 models.ErrBranchNameConflict{
  43. BranchName: branch.Name,
  44. }
  45. }
  46. }
  47. if _, err := gitRepo.GetTag(name); err == nil {
  48. return models.ErrTagAlreadyExists{
  49. TagName: name,
  50. }
  51. }
  52. return nil
  53. }
  54. // CreateNewBranch creates a new repository branch
  55. func CreateNewBranch(doer *models.User, repo *models.Repository, oldBranchName, branchName string) (err error) {
  56. // Check if branch name can be used
  57. if err := checkBranchName(repo, branchName); err != nil {
  58. return err
  59. }
  60. if !git.IsBranchExist(repo.RepoPath(), oldBranchName) {
  61. return fmt.Errorf("OldBranch: %s does not exist. Cannot create new branch from this", oldBranchName)
  62. }
  63. basePath, err := models.CreateTemporaryPath("branch-maker")
  64. if err != nil {
  65. return err
  66. }
  67. defer func() {
  68. if err := models.RemoveTemporaryPath(basePath); err != nil {
  69. log.Error("CreateNewBranch: RemoveTemporaryPath: %s", err)
  70. }
  71. }()
  72. if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
  73. Bare: true,
  74. Shared: true,
  75. }); err != nil {
  76. log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
  77. return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
  78. }
  79. gitRepo, err := git.OpenRepository(basePath)
  80. if err != nil {
  81. log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
  82. return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
  83. }
  84. defer gitRepo.Close()
  85. if err = gitRepo.CreateBranch(branchName, oldBranchName); err != nil {
  86. log.Error("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
  87. return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
  88. }
  89. if err = git.Push(basePath, git.PushOptions{
  90. Remote: "origin",
  91. Branch: branchName,
  92. Env: models.PushingEnvironment(doer, repo),
  93. }); err != nil {
  94. if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
  95. return err
  96. }
  97. return fmt.Errorf("Push: %v", err)
  98. }
  99. return nil
  100. }
  101. // CreateNewBranchFromCommit creates a new repository branch
  102. func CreateNewBranchFromCommit(doer *models.User, repo *models.Repository, commit, branchName string) (err error) {
  103. // Check if branch name can be used
  104. if err := checkBranchName(repo, branchName); err != nil {
  105. return err
  106. }
  107. basePath, err := models.CreateTemporaryPath("branch-maker")
  108. if err != nil {
  109. return err
  110. }
  111. defer func() {
  112. if err := models.RemoveTemporaryPath(basePath); err != nil {
  113. log.Error("CreateNewBranchFromCommit: RemoveTemporaryPath: %s", err)
  114. }
  115. }()
  116. if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
  117. Bare: true,
  118. Shared: true,
  119. }); err != nil {
  120. log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
  121. return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
  122. }
  123. gitRepo, err := git.OpenRepository(basePath)
  124. if err != nil {
  125. log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
  126. return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
  127. }
  128. defer gitRepo.Close()
  129. if err = gitRepo.CreateBranch(branchName, commit); err != nil {
  130. log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
  131. return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
  132. }
  133. if err = git.Push(basePath, git.PushOptions{
  134. Remote: "origin",
  135. Branch: branchName,
  136. Env: models.PushingEnvironment(doer, repo),
  137. }); err != nil {
  138. if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
  139. return err
  140. }
  141. return fmt.Errorf("Push: %v", err)
  142. }
  143. return nil
  144. }