repo_branch.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2014 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. "errors"
  7. "strings"
  8. "github.com/Unknwon/com"
  9. )
  10. func IsBranchExist(repoPath, branchName string) bool {
  11. _, _, err := com.ExecCmdDir(repoPath, "git", "show-ref", "--verify", "refs/heads/"+branchName)
  12. return err == nil
  13. }
  14. func (repo *Repository) IsBranchExist(branchName string) bool {
  15. return IsBranchExist(repo.Path, branchName)
  16. }
  17. func (repo *Repository) GetBranches() ([]string, error) {
  18. stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "show-ref", "--heads")
  19. if err != nil {
  20. return nil, errors.New(stderr)
  21. }
  22. infos := strings.Split(stdout, "\n")
  23. branches := make([]string, len(infos)-1)
  24. for i, info := range infos[:len(infos)-1] {
  25. parts := strings.Split(info, " ")
  26. if len(parts) != 2 {
  27. continue // NOTE: I should believe git will not give me wrong string.
  28. }
  29. branches[i] = strings.TrimPrefix(parts[1], "refs/heads/")
  30. }
  31. return branches, nil
  32. }