本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

182 lines
4.3KB

  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. brs, err := gitRepo.GetBranches()
  92. if err != nil {
  93. return nil, err
  94. }
  95. branches := make([]*Branch, len(brs))
  96. for i := range brs {
  97. branches[i] = &Branch{
  98. Path: path,
  99. Name: brs[i],
  100. gitRepo: gitRepo,
  101. }
  102. }
  103. return branches, nil
  104. }
  105. // DeleteBranchOptions Option(s) for delete branch
  106. type DeleteBranchOptions struct {
  107. Force bool
  108. }
  109. // DeleteBranch delete a branch by name on repository.
  110. func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
  111. cmd := NewCommand("branch")
  112. if opts.Force {
  113. cmd.AddArguments("-D")
  114. } else {
  115. cmd.AddArguments("-d")
  116. }
  117. cmd.AddArguments("--", name)
  118. _, err := cmd.RunInDir(repo.Path)
  119. return err
  120. }
  121. // CreateBranch create a new branch
  122. func (repo *Repository) CreateBranch(branch, oldbranchOrCommit string) error {
  123. cmd := NewCommand("branch")
  124. cmd.AddArguments("--", branch, oldbranchOrCommit)
  125. _, err := cmd.RunInDir(repo.Path)
  126. return err
  127. }
  128. // AddRemote adds a new remote to repository.
  129. func (repo *Repository) AddRemote(name, url string, fetch bool) error {
  130. cmd := NewCommand("remote", "add")
  131. if fetch {
  132. cmd.AddArguments("-f")
  133. }
  134. cmd.AddArguments(name, url)
  135. _, err := cmd.RunInDir(repo.Path)
  136. return err
  137. }
  138. // RemoveRemote removes a remote from repository.
  139. func (repo *Repository) RemoveRemote(name string) error {
  140. _, err := NewCommand("remote", "rm", name).RunInDir(repo.Path)
  141. return err
  142. }
  143. // GetCommit returns the head commit of a branch
  144. func (branch *Branch) GetCommit() (*Commit, error) {
  145. return branch.gitRepo.GetBranchCommit(branch.Name)
  146. }
上海开阖软件有限公司 沪ICP备12045867号-1