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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // SetDefaultBranch sets default branch of repository.
  48. func (repo *Repository) SetDefaultBranch(name string) error {
  49. _, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").AddDynamicArguments(BranchPrefix + name).RunStdString(&RunOpts{Dir: repo.Path})
  50. return err
  51. }
  52. // GetDefaultBranch gets default branch of repository.
  53. func (repo *Repository) GetDefaultBranch() (string, error) {
  54. stdout, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repo.Path})
  55. if err != nil {
  56. return "", err
  57. }
  58. stdout = strings.TrimSpace(stdout)
  59. if !strings.HasPrefix(stdout, BranchPrefix) {
  60. return "", errors.New("the HEAD is not a branch: " + stdout)
  61. }
  62. return strings.TrimPrefix(stdout, BranchPrefix), nil
  63. }
  64. // GetBranch returns a branch by it's name
  65. func (repo *Repository) GetBranch(branch string) (*Branch, error) {
  66. if !repo.IsBranchExist(branch) {
  67. return nil, ErrBranchNotExist{branch}
  68. }
  69. return &Branch{
  70. Path: repo.Path,
  71. Name: branch,
  72. gitRepo: repo,
  73. }, nil
  74. }
  75. // GetBranchesByPath returns a branch by it's path
  76. // if limit = 0 it will not limit
  77. func GetBranchesByPath(ctx context.Context, path string, skip, limit int) ([]*Branch, int, error) {
  78. gitRepo, err := OpenRepository(ctx, path)
  79. if err != nil {
  80. return nil, 0, err
  81. }
  82. defer gitRepo.Close()
  83. return gitRepo.GetBranches(skip, limit)
  84. }
  85. // GetBranchCommitID returns a branch commit ID by its name
  86. func GetBranchCommitID(ctx context.Context, path, branch string) (string, error) {
  87. gitRepo, err := OpenRepository(ctx, path)
  88. if err != nil {
  89. return "", err
  90. }
  91. defer gitRepo.Close()
  92. return gitRepo.GetBranchCommitID(branch)
  93. }
  94. // GetBranches returns a slice of *git.Branch
  95. func (repo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) {
  96. brs, countAll, err := repo.GetBranchNames(skip, limit)
  97. if err != nil {
  98. return nil, 0, err
  99. }
  100. branches := make([]*Branch, len(brs))
  101. for i := range brs {
  102. branches[i] = &Branch{
  103. Path: repo.Path,
  104. Name: brs[i],
  105. gitRepo: repo,
  106. }
  107. }
  108. return branches, countAll, nil
  109. }
  110. // DeleteBranchOptions Option(s) for delete branch
  111. type DeleteBranchOptions struct {
  112. Force bool
  113. }
  114. // DeleteBranch delete a branch by name on repository.
  115. func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
  116. cmd := NewCommand(repo.Ctx, "branch")
  117. if opts.Force {
  118. cmd.AddArguments("-D")
  119. } else {
  120. cmd.AddArguments("-d")
  121. }
  122. cmd.AddDashesAndList(name)
  123. _, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
  124. return err
  125. }
  126. // CreateBranch create a new branch
  127. func (repo *Repository) CreateBranch(branch, oldbranchOrCommit string) error {
  128. cmd := NewCommand(repo.Ctx, "branch")
  129. cmd.AddDashesAndList(branch, oldbranchOrCommit)
  130. _, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
  131. return err
  132. }
  133. // AddRemote adds a new remote to repository.
  134. func (repo *Repository) AddRemote(name, url string, fetch bool) error {
  135. cmd := NewCommand(repo.Ctx, "remote", "add")
  136. if fetch {
  137. cmd.AddArguments("-f")
  138. }
  139. cmd.AddDynamicArguments(name, url)
  140. _, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
  141. return err
  142. }
  143. // RemoveRemote removes a remote from repository.
  144. func (repo *Repository) RemoveRemote(name string) error {
  145. _, _, err := NewCommand(repo.Ctx, "remote", "rm").AddDynamicArguments(name).RunStdString(&RunOpts{Dir: repo.Path})
  146. return err
  147. }
  148. // GetCommit returns the head commit of a branch
  149. func (branch *Branch) GetCommit() (*Commit, error) {
  150. return branch.gitRepo.GetBranchCommit(branch.Name)
  151. }
  152. // RenameBranch rename a branch
  153. func (repo *Repository) RenameBranch(from, to string) error {
  154. _, _, err := NewCommand(repo.Ctx, "branch", "-m").AddDynamicArguments(from, to).RunStdString(&RunOpts{Dir: repo.Path})
  155. return err
  156. }