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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2016 The Gogs 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 models
  5. import (
  6. "fmt"
  7. "time"
  8. "code.gitea.io/git"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/Unknwon/com"
  11. )
  12. // Branch holds the branch information
  13. type Branch struct {
  14. Path string
  15. Name string
  16. }
  17. // GetBranchesByPath returns a branch by it's path
  18. func GetBranchesByPath(path string) ([]*Branch, error) {
  19. gitRepo, err := git.OpenRepository(path)
  20. if err != nil {
  21. return nil, err
  22. }
  23. brs, err := gitRepo.GetBranches()
  24. if err != nil {
  25. return nil, err
  26. }
  27. branches := make([]*Branch, len(brs))
  28. for i := range brs {
  29. branches[i] = &Branch{
  30. Path: path,
  31. Name: brs[i],
  32. }
  33. }
  34. return branches, nil
  35. }
  36. // CanCreateBranch returns true if repository meets the requirements for creating new branches.
  37. func (repo *Repository) CanCreateBranch() bool {
  38. return !repo.IsMirror
  39. }
  40. // GetBranch returns a branch by it's name
  41. func (repo *Repository) GetBranch(branch string) (*Branch, error) {
  42. if !git.IsBranchExist(repo.RepoPath(), branch) {
  43. return nil, ErrBranchNotExist{branch}
  44. }
  45. return &Branch{
  46. Path: repo.RepoPath(),
  47. Name: branch,
  48. }, nil
  49. }
  50. // GetBranches returns all the branches of a repository
  51. func (repo *Repository) GetBranches() ([]*Branch, error) {
  52. return GetBranchesByPath(repo.RepoPath())
  53. }
  54. // CheckBranchName validates branch name with existing repository branches
  55. func (repo *Repository) CheckBranchName(name string) error {
  56. gitRepo, err := git.OpenRepository(repo.RepoPath())
  57. if err != nil {
  58. return err
  59. }
  60. if _, err := gitRepo.GetTag(name); err == nil {
  61. return ErrTagAlreadyExists{name}
  62. }
  63. branches, err := repo.GetBranches()
  64. if err != nil {
  65. return err
  66. }
  67. for _, branch := range branches {
  68. if branch.Name == name {
  69. return ErrBranchAlreadyExists{branch.Name}
  70. } else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) ||
  71. (len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) {
  72. return ErrBranchNameConflict{branch.Name}
  73. }
  74. }
  75. return nil
  76. }
  77. // CreateNewBranch creates a new repository branch
  78. func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName string) (err error) {
  79. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  80. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  81. // Check if branch name can be used
  82. if err := repo.CheckBranchName(branchName); err != nil {
  83. return err
  84. }
  85. localPath := repo.LocalCopyPath()
  86. if err = discardLocalRepoBranchChanges(localPath, oldBranchName); err != nil {
  87. return fmt.Errorf("discardLocalRepoChanges: %v", err)
  88. } else if err = repo.UpdateLocalCopyBranch(oldBranchName); err != nil {
  89. return fmt.Errorf("UpdateLocalCopyBranch: %v", err)
  90. }
  91. if err = repo.CheckoutNewBranch(oldBranchName, branchName); err != nil {
  92. return fmt.Errorf("CreateNewBranch: %v", err)
  93. }
  94. if err = git.Push(localPath, git.PushOptions{
  95. Remote: "origin",
  96. Branch: branchName,
  97. }); err != nil {
  98. return fmt.Errorf("Push: %v", err)
  99. }
  100. return nil
  101. }
  102. // updateLocalCopyToCommit pulls latest changes of given commit from repoPath to localPath.
  103. // It creates a new clone if local copy does not exist.
  104. // This function checks out target commit by default, it is safe to assume subsequent
  105. // operations are operating against target commit when caller has confidence for no race condition.
  106. func updateLocalCopyToCommit(repoPath, localPath, commit string) error {
  107. if !com.IsExist(localPath) {
  108. if err := git.Clone(repoPath, localPath, git.CloneRepoOptions{
  109. Timeout: time.Duration(setting.Git.Timeout.Clone) * time.Second,
  110. }); err != nil {
  111. return fmt.Errorf("git clone: %v", err)
  112. }
  113. } else {
  114. _, err := git.NewCommand("fetch", "origin").RunInDir(localPath)
  115. if err != nil {
  116. return fmt.Errorf("git fetch origin: %v", err)
  117. }
  118. if err := git.ResetHEAD(localPath, true, "HEAD"); err != nil {
  119. return fmt.Errorf("git reset --hard HEAD: %v", err)
  120. }
  121. }
  122. if err := git.Checkout(localPath, git.CheckoutOptions{
  123. Branch: commit,
  124. }); err != nil {
  125. return fmt.Errorf("git checkout %s: %v", commit, err)
  126. }
  127. return nil
  128. }
  129. // updateLocalCopyToCommit makes sure local copy of repository is at given commit.
  130. func (repo *Repository) updateLocalCopyToCommit(commit string) error {
  131. return updateLocalCopyToCommit(repo.RepoPath(), repo.LocalCopyPath(), commit)
  132. }
  133. // CreateNewBranchFromCommit creates a new repository branch
  134. func (repo *Repository) CreateNewBranchFromCommit(doer *User, commit, branchName string) (err error) {
  135. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  136. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  137. // Check if branch name can be used
  138. if err := repo.CheckBranchName(branchName); err != nil {
  139. return err
  140. }
  141. localPath := repo.LocalCopyPath()
  142. if err = repo.updateLocalCopyToCommit(commit); err != nil {
  143. return fmt.Errorf("UpdateLocalCopyBranch: %v", err)
  144. }
  145. if err = repo.CheckoutNewBranch(commit, branchName); err != nil {
  146. return fmt.Errorf("CheckoutNewBranch: %v", err)
  147. }
  148. if err = git.Push(localPath, git.PushOptions{
  149. Remote: "origin",
  150. Branch: branchName,
  151. }); err != nil {
  152. return fmt.Errorf("Push: %v", err)
  153. }
  154. return nil
  155. }
  156. // GetCommit returns all the commits of a branch
  157. func (branch *Branch) GetCommit() (*git.Commit, error) {
  158. gitRepo, err := git.OpenRepository(branch.Path)
  159. if err != nil {
  160. return nil, err
  161. }
  162. return gitRepo.GetBranchCommit(branch.Name)
  163. }