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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. _, err := repo.gogitRepo.Reference(plumbing.ReferenceName(BranchPrefix+name), true)
  25. if err != nil {
  26. return false
  27. }
  28. return true
  29. }
  30. // Branch represents a Git branch.
  31. type Branch struct {
  32. Name string
  33. Path string
  34. gitRepo *Repository
  35. }
  36. // GetHEADBranch returns corresponding branch of HEAD.
  37. func (repo *Repository) GetHEADBranch() (*Branch, error) {
  38. stdout, err := NewCommand("symbolic-ref", "HEAD").RunInDir(repo.Path)
  39. if err != nil {
  40. return nil, err
  41. }
  42. stdout = strings.TrimSpace(stdout)
  43. if !strings.HasPrefix(stdout, BranchPrefix) {
  44. return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
  45. }
  46. return &Branch{
  47. Name: stdout[len(BranchPrefix):],
  48. Path: stdout,
  49. gitRepo: repo,
  50. }, nil
  51. }
  52. // SetDefaultBranch sets default branch of repository.
  53. func (repo *Repository) SetDefaultBranch(name string) error {
  54. _, err := NewCommand("symbolic-ref", "HEAD", BranchPrefix+name).RunInDir(repo.Path)
  55. return err
  56. }
  57. // GetBranches returns all branches of the repository.
  58. func (repo *Repository) GetBranches() ([]string, error) {
  59. var branchNames []string
  60. branches, err := repo.gogitRepo.Branches()
  61. if err != nil {
  62. return nil, err
  63. }
  64. branches.ForEach(func(branch *plumbing.Reference) error {
  65. branchNames = append(branchNames, strings.TrimPrefix(branch.Name().String(), BranchPrefix))
  66. return nil
  67. })
  68. // TODO: Sort?
  69. return branchNames, nil
  70. }
  71. // GetBranch returns a branch by it's name
  72. func (repo *Repository) GetBranch(branch string) (*Branch, error) {
  73. if !repo.IsBranchExist(branch) {
  74. return nil, ErrBranchNotExist{branch}
  75. }
  76. return &Branch{
  77. Path: repo.Path,
  78. Name: branch,
  79. gitRepo: repo,
  80. }, nil
  81. }
  82. // GetBranchesByPath returns a branch by it's path
  83. func GetBranchesByPath(path string) ([]*Branch, error) {
  84. gitRepo, err := OpenRepository(path)
  85. if err != nil {
  86. return nil, err
  87. }
  88. brs, err := gitRepo.GetBranches()
  89. if err != nil {
  90. return nil, err
  91. }
  92. branches := make([]*Branch, len(brs))
  93. for i := range brs {
  94. branches[i] = &Branch{
  95. Path: path,
  96. Name: brs[i],
  97. gitRepo: gitRepo,
  98. }
  99. }
  100. return branches, nil
  101. }
  102. // DeleteBranchOptions Option(s) for delete branch
  103. type DeleteBranchOptions struct {
  104. Force bool
  105. }
  106. // DeleteBranch delete a branch by name on repository.
  107. func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
  108. cmd := NewCommand("branch")
  109. if opts.Force {
  110. cmd.AddArguments("-D")
  111. } else {
  112. cmd.AddArguments("-d")
  113. }
  114. cmd.AddArguments(name)
  115. _, err := cmd.RunInDir(repo.Path)
  116. return err
  117. }
  118. // CreateBranch create a new branch
  119. func (repo *Repository) CreateBranch(branch, newBranch string) error {
  120. cmd := NewCommand("branch")
  121. cmd.AddArguments(branch, newBranch)
  122. _, err := cmd.RunInDir(repo.Path)
  123. return err
  124. }
  125. // AddRemote adds a new remote to repository.
  126. func (repo *Repository) AddRemote(name, url string, fetch bool) error {
  127. cmd := NewCommand("remote", "add")
  128. if fetch {
  129. cmd.AddArguments("-f")
  130. }
  131. cmd.AddArguments(name, url)
  132. _, err := cmd.RunInDir(repo.Path)
  133. return err
  134. }
  135. // RemoveRemote removes a remote from repository.
  136. func (repo *Repository) RemoveRemote(name string) error {
  137. _, err := NewCommand("remote", "remove", name).RunInDir(repo.Path)
  138. return err
  139. }
  140. // GetCommit returns the head commit of a branch
  141. func (branch *Branch) GetCommit() (*Commit, error) {
  142. return branch.gitRepo.GetBranchCommit(branch.Name)
  143. }