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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. "time"
  9. "code.gitea.io/gitea/modules/git"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/Unknwon/com"
  12. )
  13. // discardLocalRepoBranchChanges discards local commits/changes of
  14. // given branch to make sure it is even to remote branch.
  15. func discardLocalRepoBranchChanges(localPath, branch string) error {
  16. if !com.IsExist(localPath) {
  17. return nil
  18. }
  19. // No need to check if nothing in the repository.
  20. if !git.IsBranchExist(localPath, branch) {
  21. return nil
  22. }
  23. refName := "origin/" + branch
  24. if err := git.ResetHEAD(localPath, true, refName); err != nil {
  25. return fmt.Errorf("git reset --hard %s: %v", refName, err)
  26. }
  27. return nil
  28. }
  29. // DiscardLocalRepoBranchChanges discards the local repository branch changes
  30. func (repo *Repository) DiscardLocalRepoBranchChanges(branch string) error {
  31. return discardLocalRepoBranchChanges(repo.LocalCopyPath(), branch)
  32. }
  33. // checkoutNewBranch checks out to a new branch from the a branch name.
  34. func checkoutNewBranch(repoPath, localPath, oldBranch, newBranch string) error {
  35. if err := git.Checkout(localPath, git.CheckoutOptions{
  36. Timeout: time.Duration(setting.Git.Timeout.Pull) * time.Second,
  37. Branch: newBranch,
  38. OldBranch: oldBranch,
  39. }); err != nil {
  40. return fmt.Errorf("git checkout -b %s %s: %v", newBranch, oldBranch, err)
  41. }
  42. return nil
  43. }
  44. // CheckoutNewBranch checks out a new branch
  45. func (repo *Repository) CheckoutNewBranch(oldBranch, newBranch string) error {
  46. return checkoutNewBranch(repo.RepoPath(), repo.LocalCopyPath(), oldBranch, newBranch)
  47. }
  48. // deleteLocalBranch deletes a branch from a local repo cache
  49. // First checks out default branch to avoid trying to delete the currently checked out branch
  50. func deleteLocalBranch(localPath, defaultBranch, deleteBranch string) error {
  51. if !com.IsExist(localPath) {
  52. return nil
  53. }
  54. if !git.IsBranchExist(localPath, deleteBranch) {
  55. return nil
  56. }
  57. // Must NOT have branch currently checked out
  58. // Checkout default branch first
  59. if err := git.Checkout(localPath, git.CheckoutOptions{
  60. Timeout: time.Duration(setting.Git.Timeout.Pull) * time.Second,
  61. Branch: defaultBranch,
  62. }); err != nil {
  63. return fmt.Errorf("git checkout %s: %v", defaultBranch, err)
  64. }
  65. cmd := git.NewCommand("branch")
  66. cmd.AddArguments("-D")
  67. cmd.AddArguments(deleteBranch)
  68. _, err := cmd.RunInDir(localPath)
  69. return err
  70. }
  71. // DeleteLocalBranch deletes a branch from the local repo
  72. func (repo *Repository) DeleteLocalBranch(branchName string) error {
  73. return deleteLocalBranch(repo.LocalCopyPath(), repo.DefaultBranch, branchName)
  74. }
  75. // CanCreateBranch returns true if repository meets the requirements for creating new branches.
  76. func (repo *Repository) CanCreateBranch() bool {
  77. return !repo.IsMirror
  78. }
  79. // GetBranch returns a branch by its name
  80. func (repo *Repository) GetBranch(branch string) (*git.Branch, error) {
  81. gitRepo, err := git.OpenRepository(repo.RepoPath())
  82. if err != nil {
  83. return nil, err
  84. }
  85. return gitRepo.GetBranch(branch)
  86. }
  87. // GetBranches returns all the branches of a repository
  88. func (repo *Repository) GetBranches() ([]*git.Branch, error) {
  89. return git.GetBranchesByPath(repo.RepoPath())
  90. }
  91. // CheckBranchName validates branch name with existing repository branches
  92. func (repo *Repository) CheckBranchName(name string) error {
  93. gitRepo, err := git.OpenRepository(repo.RepoPath())
  94. if err != nil {
  95. return err
  96. }
  97. branches, err := repo.GetBranches()
  98. if err != nil {
  99. return err
  100. }
  101. for _, branch := range branches {
  102. if branch.Name == name {
  103. return ErrBranchAlreadyExists{branch.Name}
  104. } else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) ||
  105. (len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) {
  106. return ErrBranchNameConflict{branch.Name}
  107. }
  108. }
  109. if _, err := gitRepo.GetTag(name); err == nil {
  110. return ErrTagAlreadyExists{name}
  111. }
  112. return nil
  113. }
  114. // CreateNewBranch creates a new repository branch
  115. func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName string) (err error) {
  116. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  117. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  118. // Check if branch name can be used
  119. if err := repo.CheckBranchName(branchName); err != nil {
  120. return err
  121. }
  122. localPath := repo.LocalCopyPath()
  123. if err = discardLocalRepoBranchChanges(localPath, oldBranchName); err != nil {
  124. return fmt.Errorf("discardLocalRepoChanges: %v", err)
  125. } else if err = repo.UpdateLocalCopyBranch(oldBranchName); err != nil {
  126. return fmt.Errorf("UpdateLocalCopyBranch: %v", err)
  127. }
  128. if err = repo.CheckoutNewBranch(oldBranchName, branchName); err != nil {
  129. return fmt.Errorf("CreateNewBranch: %v", err)
  130. }
  131. if err = git.Push(localPath, git.PushOptions{
  132. Remote: "origin",
  133. Branch: branchName,
  134. }); err != nil {
  135. return fmt.Errorf("Push: %v", err)
  136. }
  137. return nil
  138. }
  139. // updateLocalCopyToCommit pulls latest changes of given commit from repoPath to localPath.
  140. // It creates a new clone if local copy does not exist.
  141. // This function checks out target commit by default, it is safe to assume subsequent
  142. // operations are operating against target commit when caller has confidence for no race condition.
  143. func updateLocalCopyToCommit(repoPath, localPath, commit string) error {
  144. if !com.IsExist(localPath) {
  145. if err := git.Clone(repoPath, localPath, git.CloneRepoOptions{
  146. Timeout: time.Duration(setting.Git.Timeout.Clone) * time.Second,
  147. }); err != nil {
  148. return fmt.Errorf("git clone: %v", err)
  149. }
  150. } else {
  151. _, err := git.NewCommand("fetch", "origin").RunInDir(localPath)
  152. if err != nil {
  153. return fmt.Errorf("git fetch origin: %v", err)
  154. }
  155. if err := git.ResetHEAD(localPath, true, "HEAD"); err != nil {
  156. return fmt.Errorf("git reset --hard HEAD: %v", err)
  157. }
  158. }
  159. if err := git.Checkout(localPath, git.CheckoutOptions{
  160. Branch: commit,
  161. }); err != nil {
  162. return fmt.Errorf("git checkout %s: %v", commit, err)
  163. }
  164. return nil
  165. }
  166. // updateLocalCopyToCommit makes sure local copy of repository is at given commit.
  167. func (repo *Repository) updateLocalCopyToCommit(commit string) error {
  168. return updateLocalCopyToCommit(repo.RepoPath(), repo.LocalCopyPath(), commit)
  169. }
  170. // CreateNewBranchFromCommit creates a new repository branch
  171. func (repo *Repository) CreateNewBranchFromCommit(doer *User, commit, branchName string) (err error) {
  172. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  173. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  174. // Check if branch name can be used
  175. if err := repo.CheckBranchName(branchName); err != nil {
  176. return err
  177. }
  178. localPath := repo.LocalCopyPath()
  179. if err = repo.updateLocalCopyToCommit(commit); err != nil {
  180. return fmt.Errorf("UpdateLocalCopyBranch: %v", err)
  181. }
  182. if err = repo.CheckoutNewBranch(commit, branchName); err != nil {
  183. return fmt.Errorf("CheckoutNewBranch: %v", err)
  184. }
  185. if err = git.Push(localPath, git.PushOptions{
  186. Remote: "origin",
  187. Branch: branchName,
  188. }); err != nil {
  189. return fmt.Errorf("Push: %v", err)
  190. }
  191. return nil
  192. }