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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. return fmt.Errorf("Push: %v", err)
  95. }
  96. return nil
  97. }
  98. // CreateNewBranchFromCommit creates a new repository branch
  99. func CreateNewBranchFromCommit(doer *models.User, repo *models.Repository, commit, branchName string) (err error) {
  100. // Check if branch name can be used
  101. if err := checkBranchName(repo, branchName); err != nil {
  102. return err
  103. }
  104. basePath, err := models.CreateTemporaryPath("branch-maker")
  105. if err != nil {
  106. return err
  107. }
  108. defer func() {
  109. if err := models.RemoveTemporaryPath(basePath); err != nil {
  110. log.Error("CreateNewBranchFromCommit: RemoveTemporaryPath: %s", err)
  111. }
  112. }()
  113. if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
  114. Bare: true,
  115. Shared: true,
  116. }); err != nil {
  117. log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
  118. return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
  119. }
  120. gitRepo, err := git.OpenRepository(basePath)
  121. if err != nil {
  122. log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
  123. return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
  124. }
  125. defer gitRepo.Close()
  126. if err = gitRepo.CreateBranch(branchName, commit); err != nil {
  127. log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
  128. return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
  129. }
  130. if err = git.Push(basePath, git.PushOptions{
  131. Remote: "origin",
  132. Branch: branchName,
  133. Env: models.PushingEnvironment(doer, repo),
  134. }); err != nil {
  135. return fmt.Errorf("Push: %v", err)
  136. }
  137. return nil
  138. }