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

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