Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit 43e5216

Browse files
cybelunny
authored andcommitted
Return empty list of branches if repository is bare. (#39)
Signed-off-by: Dennis Keitzel <[email protected]>
1 parent 4c374b3 commit 43e5216

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

repo_branch.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,15 @@ func (repo *Repository) SetDefaultBranch(name string) error {
6666

6767
// GetBranches returns all branches of the repository.
6868
func (repo *Repository) GetBranches() ([]string, error) {
69-
stdout, err := NewCommand("show-ref", "--heads").RunInDir(repo.Path)
69+
stdout, err := NewCommand("for-each-ref", "--format=%(refname)", BranchPrefix).RunInDir(repo.Path)
7070
if err != nil {
7171
return nil, err
7272
}
7373

74-
infos := strings.Split(stdout, "\n")
75-
branches := make([]string, len(infos)-1)
76-
for i, info := range infos[:len(infos)-1] {
77-
fields := strings.Fields(info)
78-
if len(fields) != 2 {
79-
continue // NOTE: I should believe git will not give me wrong string.
80-
}
81-
branches[i] = strings.TrimPrefix(fields[1], BranchPrefix)
74+
refs := strings.Split(stdout, "\n")
75+
branches := make([]string, len(refs)-1)
76+
for i, ref := range refs[:len(refs)-1] {
77+
branches[i] = strings.TrimPrefix(ref, BranchPrefix)
8278
}
8379
return branches, nil
8480
}

0 commit comments

Comments
 (0)