From 98a4dfbd9aaed44480a2e2e57af37aaf2f074e06 Mon Sep 17 00:00:00 2001 From: Dennis Keitzel Date: Thu, 23 Mar 2017 22:16:10 +0100 Subject: [PATCH] Return empty list of branches if repository is bare. Signed-off-by: Dennis Keitzel --- repo_branch.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/repo_branch.go b/repo_branch.go index 8366b6833..bf9d19811 100644 --- a/repo_branch.go +++ b/repo_branch.go @@ -66,19 +66,15 @@ func (repo *Repository) SetDefaultBranch(name string) error { // GetBranches returns all branches of the repository. func (repo *Repository) GetBranches() ([]string, error) { - stdout, err := NewCommand("show-ref", "--heads").RunInDir(repo.Path) + stdout, err := NewCommand("for-each-ref", "--format=%(refname)", BranchPrefix).RunInDir(repo.Path) if err != nil { return nil, err } - infos := strings.Split(stdout, "\n") - branches := make([]string, len(infos)-1) - for i, info := range infos[:len(infos)-1] { - fields := strings.Fields(info) - if len(fields) != 2 { - continue // NOTE: I should believe git will not give me wrong string. - } - branches[i] = strings.TrimPrefix(fields[1], BranchPrefix) + refs := strings.Split(stdout, "\n") + branches := make([]string, len(refs)-1) + for i, ref := range refs[:len(refs)-1] { + branches[i] = strings.TrimPrefix(ref, BranchPrefix) } return branches, nil }