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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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 git
  6. import (
  7. "fmt"
  8. "strings"
  9. "gopkg.in/src-d/go-git.v4/plumbing"
  10. )
  11. // BranchPrefix base dir of the branch information file store on git
  12. const BranchPrefix = "refs/heads/"
  13. // IsReferenceExist returns true if given reference exists in the repository.
  14. func IsReferenceExist(repoPath, name string) bool {
  15. _, err := NewCommand("show-ref", "--verify", "--", name).RunInDir(repoPath)
  16. return err == nil
  17. }
  18. // IsBranchExist returns true if given branch exists in the repository.
  19. func IsBranchExist(repoPath, name string) bool {
  20. return IsReferenceExist(repoPath, BranchPrefix+name)
  21. }
  22. // IsBranchExist returns true if given branch exists in current repository.
  23. func (repo *Repository) IsBranchExist(name string) bool {
  24. if name == "" {
  25. return false
  26. }
  27. reference, err := repo.gogitRepo.Reference(plumbing.ReferenceName(BranchPrefix+name), true)
  28. if err != nil {
  29. return false
  30. }
  31. return reference.Type() != plumbing.InvalidReference
  32. }
  33. // Branch represents a Git branch.
  34. type Branch struct {
  35. Name string
  36. Path string
  37. gitRepo *Repository
  38. }
  39. // GetHEADBranch returns corresponding branch of HEAD.
  40. func (repo *Repository) GetHEADBranch() (*Branch, error) {
  41. stdout, err := NewCommand("symbolic-ref", "HEAD").RunInDir(repo.Path)
  42. if err != nil {
  43. return nil, err
  44. }
  45. stdout = strings.TrimSpace(stdout)
  46. if !strings.HasPrefix(stdout, BranchPrefix) {
  47. return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
  48. }
  49. return &Branch{
  50. Name: stdout[len(BranchPrefix):],
  51. Path: stdout,
  52. gitRepo: repo,
  53. }, nil
  54. }
  55. // SetDefaultBranch sets default branch of repository.
  56. func (repo *Repository) SetDefaultBranch(name string) error {
  57. _, err := NewCommand("symbolic-ref", "HEAD", BranchPrefix+name).RunInDir(repo.Path)
  58. return err
  59. }
  60. // GetBranches returns all branches of the repository.
  61. func (repo *Repository) GetBranches() ([]string, error) {
  62. var branchNames []string
  63. branches, err := repo.gogitRepo.Branches()
  64. if err != nil {
  65. return nil, err
  66. }
  67. _ = branches.ForEach(func(branch *plumbing.Reference) error {
  68. branchNames = append(branchNames, strings.TrimPrefix(branch.Name().String(), BranchPrefix))
  69. return nil
  70. })
  71. // TODO: Sort?
  72. return branchNames, nil
  73. }
  74. // GetBranch returns a branch by it's name
  75. func (repo *Repository) GetBranch(branch string) (*Branch, error) {
  76. if !repo.IsBranchExist(branch) {
  77. return nil, ErrBranchNotExist{branch}
  78. }
  79. return &Branch{
  80. Path: repo.Path,
  81. Name: branch,
  82. gitRepo: repo,
  83. }, nil
  84. }
  85. // GetBranchesByPath returns a branch by it's path
  86. func GetBranchesByPath(path string) ([]*Branch, error) {
  87. gitRepo, err := OpenRepository(path)
  88. if err != nil {
  89. return nil, err
  90. }
  91. defer gitRepo.Close()
  92. brs, err := gitRepo.GetBranches()
  93. if err != nil {
  94. return nil, err
  95. }
  96. branches := make([]*Branch, len(brs))
  97. for i := range brs {
  98. branches[i] = &Branch{
  99. Path: path,
  100. Name: brs[i],
  101. gitRepo: gitRepo,
  102. }
  103. }
  104. return branches, nil
  105. }
  106. // DeleteBranchOptions Option(s) for delete branch
  107. type DeleteBranchOptions struct {
  108. Force bool
  109. }
  110. // DeleteBranch delete a branch by name on repository.
  111. func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
  112. cmd := NewCommand("branch")
  113. if opts.Force {
  114. cmd.AddArguments("-D")
  115. } else {
  116. cmd.AddArguments("-d")
  117. }
  118. cmd.AddArguments("--", name)
  119. _, err := cmd.RunInDir(repo.Path)
  120. return err
  121. }
  122. // CreateBranch create a new branch
  123. func (repo *Repository) CreateBranch(branch, oldbranchOrCommit string) error {
  124. cmd := NewCommand("branch")
  125. cmd.AddArguments("--", branch, oldbranchOrCommit)
  126. _, err := cmd.RunInDir(repo.Path)
  127. return err
  128. }
  129. // AddRemote adds a new remote to repository.
  130. func (repo *Repository) AddRemote(name, url string, fetch bool) error {
  131. cmd := NewCommand("remote", "add")
  132. if fetch {
  133. cmd.AddArguments("-f")
  134. }
  135. cmd.AddArguments(name, url)
  136. _, err := cmd.RunInDir(repo.Path)
  137. return err
  138. }
  139. // RemoveRemote removes a remote from repository.
  140. func (repo *Repository) RemoveRemote(name string) error {
  141. _, err := NewCommand("remote", "rm", name).RunInDir(repo.Path)
  142. return err
  143. }
  144. // GetCommit returns the head commit of a branch
  145. func (branch *Branch) GetCommit() (*Commit, error) {
  146. return branch.gitRepo.GetBranchCommit(branch.Name)
  147. }