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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "context"
  7. "errors"
  8. "fmt"
  9. "strings"
  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(ctx context.Context, repoPath, name string) bool {
  15. _, _, err := NewCommand(ctx, "show-ref", "--verify").AddDashesAndList(name).RunStdString(&RunOpts{Dir: repoPath})
  16. return err == nil
  17. }
  18. // IsBranchExist returns true if given branch exists in the repository.
  19. func IsBranchExist(ctx context.Context, repoPath, name string) bool {
  20. return IsReferenceExist(ctx, repoPath, BranchPrefix+name)
  21. }
  22. // Branch represents a Git branch.
  23. type Branch struct {
  24. Name string
  25. Path string
  26. gitRepo *Repository
  27. }
  28. // GetHEADBranch returns corresponding branch of HEAD.
  29. func (repo *Repository) GetHEADBranch() (*Branch, error) {
  30. if repo == nil {
  31. return nil, fmt.Errorf("nil repo")
  32. }
  33. stdout, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repo.Path})
  34. if err != nil {
  35. return nil, err
  36. }
  37. stdout = strings.TrimSpace(stdout)
  38. if !strings.HasPrefix(stdout, BranchPrefix) {
  39. return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
  40. }
  41. return &Branch{
  42. Name: stdout[len(BranchPrefix):],
  43. Path: stdout,
  44. gitRepo: repo,
  45. }, nil
  46. }
  47. func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) {
  48. stdout, _, err := NewCommand(ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repoPath})
  49. if err != nil {
  50. return "", err
  51. }
  52. stdout = strings.TrimSpace(stdout)
  53. if !strings.HasPrefix(stdout, BranchPrefix) {
  54. return "", errors.New("the HEAD is not a branch: " + stdout)
  55. }
  56. return strings.TrimPrefix(stdout, BranchPrefix), nil
  57. }
  58. // GetBranch returns a branch by it's name
  59. func (repo *Repository) GetBranch(branch string) (*Branch, error) {
  60. if !repo.IsBranchExist(branch) {
  61. return nil, ErrBranchNotExist{branch}
  62. }
  63. return &Branch{
  64. Path: repo.Path,
  65. Name: branch,
  66. gitRepo: repo,
  67. }, nil
  68. }
  69. // GetBranches returns a slice of *git.Branch
  70. func (repo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) {
  71. brs, countAll, err := repo.GetBranchNames(skip, limit)
  72. if err != nil {
  73. return nil, 0, err
  74. }
  75. branches := make([]*Branch, len(brs))
  76. for i := range brs {
  77. branches[i] = &Branch{
  78. Path: repo.Path,
  79. Name: brs[i],
  80. gitRepo: repo,
  81. }
  82. }
  83. return branches, countAll, nil
  84. }
  85. // DeleteBranchOptions Option(s) for delete branch
  86. type DeleteBranchOptions struct {
  87. Force bool
  88. }
  89. // DeleteBranch delete a branch by name on repository.
  90. func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
  91. cmd := NewCommand(repo.Ctx, "branch")
  92. if opts.Force {
  93. cmd.AddArguments("-D")
  94. } else {
  95. cmd.AddArguments("-d")
  96. }
  97. cmd.AddDashesAndList(name)
  98. _, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
  99. return err
  100. }
  101. // CreateBranch create a new branch
  102. func (repo *Repository) CreateBranch(branch, oldbranchOrCommit string) error {
  103. cmd := NewCommand(repo.Ctx, "branch")
  104. cmd.AddDashesAndList(branch, oldbranchOrCommit)
  105. _, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
  106. return err
  107. }
  108. // AddRemote adds a new remote to repository.
  109. func (repo *Repository) AddRemote(name, url string, fetch bool) error {
  110. cmd := NewCommand(repo.Ctx, "remote", "add")
  111. if fetch {
  112. cmd.AddArguments("-f")
  113. }
  114. cmd.AddDynamicArguments(name, url)
  115. _, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
  116. return err
  117. }
  118. // RemoveRemote removes a remote from repository.
  119. func (repo *Repository) RemoveRemote(name string) error {
  120. _, _, err := NewCommand(repo.Ctx, "remote", "rm").AddDynamicArguments(name).RunStdString(&RunOpts{Dir: repo.Path})
  121. return err
  122. }
  123. // GetCommit returns the head commit of a branch
  124. func (branch *Branch) GetCommit() (*Commit, error) {
  125. return branch.gitRepo.GetBranchCommit(branch.Name)
  126. }
  127. // RenameBranch rename a branch
  128. func (repo *Repository) RenameBranch(from, to string) error {
  129. _, _, err := NewCommand(repo.Ctx, "branch", "-m").AddDynamicArguments(from, to).RunStdString(&RunOpts{Dir: repo.Path})
  130. return err
  131. }