@@ -23,10 +23,10 @@ func (repo *Repository) IsBranchExist(name string) bool {
23
23
24
24
// GetBranches returns all branches of the repository.
25
25
func (repo * Repository ) GetBranches () ([]string , error ) {
26
- return callShowRef (repo .Path , BranchPrefix , "--heads " )
26
+ return callBranch (repo .Path , "--list " )
27
27
}
28
28
29
- func callShowRef (repoPath , prefix , arg string ) ([]string , error ) {
29
+ func callBranch (repoPath , arg string ) ([]string , error ) {
30
30
var branchNames []string
31
31
32
32
stdoutReader , stdoutWriter := io .Pipe ()
@@ -37,7 +37,7 @@ func callShowRef(repoPath, prefix, arg string) ([]string, error) {
37
37
38
38
go func () {
39
39
stderrBuilder := & strings.Builder {}
40
- err := NewCommand ("show-ref " , arg ).RunInDirPipeline (repoPath , stdoutWriter , stderrBuilder )
40
+ err := NewCommand ("branch " , arg ).RunInDirPipeline (repoPath , stdoutWriter , stderrBuilder )
41
41
if err != nil {
42
42
if stderrBuilder .Len () == 0 {
43
43
_ = stdoutWriter .Close ()
@@ -51,32 +51,19 @@ func callShowRef(repoPath, prefix, arg string) ([]string, error) {
51
51
52
52
bufReader := bufio .NewReader (stdoutReader )
53
53
for {
54
- // The output of show-ref is simply a list:
55
- // <sha> SP <ref> LF
56
- _ , err := bufReader .ReadSlice (' ' )
57
- for err == bufio .ErrBufferFull {
58
- // This shouldn't happen but we'll tolerate it for the sake of peace
59
- _ , err = bufReader .ReadSlice (' ' )
60
- }
61
- if err == io .EOF {
62
- return branchNames , nil
63
- }
64
- if err != nil {
65
- return nil , err
66
- }
67
-
54
+ // The output of branch is simply a list:
55
+ // LF
68
56
branchName , err := bufReader .ReadString ('\n' )
69
57
if err == io .EOF {
70
- // This shouldn't happen... but we'll tolerate it for the sake of peace
71
58
return branchNames , nil
72
59
}
73
60
if err != nil {
61
+ // This shouldn't happen... but we'll tolerate it for the sake of peace
74
62
return nil , err
75
63
}
76
- branchName = strings .TrimPrefix (branchName , prefix )
77
- if len (branchName ) > 0 {
78
- branchName = branchName [:len (branchName )- 1 ]
79
- }
64
+ // Current branch will have '*' as prefix.
65
+ branchName = strings .TrimPrefix (branchName , "*" )
66
+ branchName = strings .TrimSpace (branchName )
80
67
branchNames = append (branchNames , branchName )
81
68
}
82
69
}
0 commit comments