diff options
author | Justin Nuß <nuss.justin@gmail.com> | 2014-07-26 11:23:58 +0200 |
---|---|---|
committer | Justin Nuß <nuss.justin@gmail.com> | 2014-07-26 11:23:58 +0200 |
commit | 91480f3791f266369c343c539f8eeec245fa969a (patch) | |
tree | a03ad6062fe4b546367cdb6f9921399458d97441 /modules/git/repo_branch.go | |
parent | 835e85b5ce9921ffd4d50b90b706e02685167331 (diff) | |
parent | 35c75f06a0a4f321021984830d760e67ca0ef8e5 (diff) |
Merge branch 'dev' of https://github.com/gogits/Gogs into issue/281
Conflicts:
modules/base/tool.go
Diffstat (limited to 'modules/git/repo_branch.go')
-rw-r--r-- | modules/git/repo_branch.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go new file mode 100644 index 00000000..726019c1 --- /dev/null +++ b/modules/git/repo_branch.go @@ -0,0 +1,38 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package git + +import ( + "errors" + "strings" + + "github.com/Unknwon/com" +) + +func IsBranchExist(repoPath, branchName string) bool { + _, _, err := com.ExecCmdDir(repoPath, "git", "show-ref", "--verify", "refs/heads/"+branchName) + return err == nil +} + +func (repo *Repository) IsBranchExist(branchName string) bool { + return IsBranchExist(repo.Path, branchName) +} + +func (repo *Repository) GetBranches() ([]string, error) { + stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "show-ref", "--heads") + if err != nil { + return nil, errors.New(stderr) + } + infos := strings.Split(stdout, "\n") + branches := make([]string, len(infos)-1) + for i, info := range infos[:len(infos)-1] { + parts := strings.Split(info, " ") + if len(parts) != 2 { + continue // NOTE: I should believe git will not give me wrong string. + } + branches[i] = strings.TrimPrefix(parts[1], "refs/heads/") + } + return branches, nil +} |