repo_branch.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package git
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/mcuadros/go-version"
  6. )
  7. const BRANCH_PREFIX = "refs/heads/"
  8. // IsReferenceExist returns true if given reference exists in the repository.
  9. func IsReferenceExist(repoPath, name string) bool {
  10. _, err := NewCommand("show-ref", "--verify", name).RunInDir(repoPath)
  11. return err == nil
  12. }
  13. // IsBranchExist returns true if given branch exists in the repository.
  14. func IsBranchExist(repoPath, name string) bool {
  15. return IsReferenceExist(repoPath, BRANCH_PREFIX+name)
  16. }
  17. func (repo *Repository) IsBranchExist(name string) bool {
  18. return IsBranchExist(repo.Path, name)
  19. }
  20. // Branch represents a Git branch.
  21. type Branch struct {
  22. Name string
  23. Path string
  24. }
  25. // GetHEADBranch returns corresponding branch of HEAD.
  26. func (repo *Repository) GetHEADBranch() (*Branch, error) {
  27. stdout, err := NewCommand("symbolic-ref", "HEAD").RunInDir(repo.Path)
  28. if err != nil {
  29. return nil, err
  30. }
  31. stdout = strings.TrimSpace(stdout)
  32. if !strings.HasPrefix(stdout, BRANCH_PREFIX) {
  33. return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
  34. }
  35. return &Branch{
  36. Name: stdout[len(BRANCH_PREFIX):],
  37. Path: stdout,
  38. }, nil
  39. }
  40. // SetDefaultBranch sets default branch of repository.
  41. func (repo *Repository) SetDefaultBranch(name string) error {
  42. if version.Compare(gitVersion, "1.7.10", "<") {
  43. return ErrUnsupportedVersion{"1.7.10"}
  44. }
  45. _, err := NewCommand("symbolic-ref", "HEAD", BRANCH_PREFIX+name).RunInDir(repo.Path)
  46. return err
  47. }
  48. // GetBranches returns all branches of the repository.
  49. func (repo *Repository) GetBranches() ([]string, error) {
  50. stdout, err := NewCommand("show-ref", "--heads").RunInDir(repo.Path)
  51. if err != nil {
  52. return nil, err
  53. }
  54. infos := strings.Split(stdout, "\n")
  55. branches := make([]string, len(infos)-1)
  56. for i, info := range infos[:len(infos)-1] {
  57. fields := strings.Fields(info)
  58. if len(fields) != 2 {
  59. continue // NOTE: I should believe git will not give me wrong string.
  60. }
  61. branches[i] = strings.TrimPrefix(fields[1], BRANCH_PREFIX)
  62. }
  63. return branches, nil
  64. }
  65. // Option(s) for delete branch
  66. type DeleteBranchOptions struct {
  67. Force bool
  68. }
  69. // DeleteBranch deletes a branch from given repository path.
  70. func DeleteBranch(repoPath, name string, opts DeleteBranchOptions) error {
  71. cmd := NewCommand("branch")
  72. if opts.Force {
  73. cmd.AddArguments("-D")
  74. } else {
  75. cmd.AddArguments("-d")
  76. }
  77. cmd.AddArguments(name)
  78. _, err := cmd.RunInDir(repoPath)
  79. return err
  80. }
  81. // DeleteBranch deletes a branch from repository.
  82. func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
  83. return DeleteBranch(repo.Path, name, opts)
  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. }