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.

123 lines
3.1 KiB

  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "fmt"
  7. "strings"
  8. )
  9. // BranchPrefix base dir of the branch information file store on git
  10. const BranchPrefix = "refs/heads/"
  11. // IsReferenceExist returns true if given reference exists in the repository.
  12. func IsReferenceExist(repoPath, name string) bool {
  13. _, err := NewCommand("show-ref", "--verify", name).RunInDir(repoPath)
  14. return err == nil
  15. }
  16. // IsBranchExist returns true if given branch exists in the repository.
  17. func IsBranchExist(repoPath, name string) bool {
  18. return IsReferenceExist(repoPath, BranchPrefix+name)
  19. }
  20. // IsBranchExist returns true if given branch exists in current repository.
  21. func (repo *Repository) IsBranchExist(name string) bool {
  22. return IsBranchExist(repo.Path, name)
  23. }
  24. // Branch represents a Git branch.
  25. type Branch struct {
  26. Name string
  27. Path string
  28. }
  29. // GetHEADBranch returns corresponding branch of HEAD.
  30. func (repo *Repository) GetHEADBranch() (*Branch, error) {
  31. stdout, err := NewCommand("symbolic-ref", "HEAD").RunInDir(repo.Path)
  32. if err != nil {
  33. return nil, err
  34. }
  35. stdout = strings.TrimSpace(stdout)
  36. if !strings.HasPrefix(stdout, BranchPrefix) {
  37. return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
  38. }
  39. return &Branch{
  40. Name: stdout[len(BranchPrefix):],
  41. Path: stdout,
  42. }, nil
  43. }
  44. // SetDefaultBranch sets default branch of repository.
  45. func (repo *Repository) SetDefaultBranch(name string) error {
  46. _, err := NewCommand("symbolic-ref", "HEAD", BranchPrefix+name).RunInDir(repo.Path)
  47. return err
  48. }
  49. // GetBranches returns all branches of the repository.
  50. func (repo *Repository) GetBranches() ([]string, error) {
  51. stdout, err := NewCommand("for-each-ref", "--format=%(refname)", BranchPrefix).RunInDir(repo.Path)
  52. if err != nil {
  53. return nil, err
  54. }
  55. refs := strings.Split(stdout, "\n")
  56. branches := make([]string, len(refs)-1)
  57. for i, ref := range refs[:len(refs)-1] {
  58. branches[i] = strings.TrimPrefix(ref, BranchPrefix)
  59. }
  60. return branches, nil
  61. }
  62. // DeleteBranchOptions Option(s) for delete branch
  63. type DeleteBranchOptions struct {
  64. Force bool
  65. }
  66. // DeleteBranch delete a branch by name on repository.
  67. func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
  68. cmd := NewCommand("branch")
  69. if opts.Force {
  70. cmd.AddArguments("-D")
  71. } else {
  72. cmd.AddArguments("-d")
  73. }
  74. cmd.AddArguments(name)
  75. _, err := cmd.RunInDir(repo.Path)
  76. return err
  77. }
  78. // CreateBranch create a new branch
  79. func (repo *Repository) CreateBranch(branch, newBranch string) error {
  80. cmd := NewCommand("branch")
  81. cmd.AddArguments(branch, newBranch)
  82. _, err := cmd.RunInDir(repo.Path)
  83. return err
  84. }
  85. // AddRemote adds a new remote to repository.
  86. func (repo *Repository) AddRemote(name, url string, fetch bool) error {
  87. cmd := NewCommand("remote", "add")
  88. if fetch {
  89. cmd.AddArguments("-f")
  90. }
  91. cmd.AddArguments(name, url)
  92. _, err := cmd.RunInDir(repo.Path)
  93. return err
  94. }
  95. // RemoveRemote removes a remote from repository.
  96. func (repo *Repository) RemoveRemote(name string) error {
  97. _, err := NewCommand("remote", "remove", name).RunInDir(repo.Path)
  98. return err
  99. }