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.

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