You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

branch.go 5.1 kB

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