repo_branch.go 3.0 KB

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