@@ -63,42 +63,40 @@ func (repo *Repository) IsBranchExist(name string) bool {
63
63
// GetBranchNames returns branches from the repository, skipping skip initial branches and
64
64
// returning at most limit branches, or all branches if limit is 0.
65
65
func (repo * Repository ) GetBranchNames (skip , limit int ) ([]string , int , error ) {
66
- return callShowRef (repo .Ctx , repo .Path , BranchPrefix , BranchPrefix + " --sort=-committerdate" , skip , limit )
66
+ return callShowRef (repo .Ctx , repo .Path , BranchPrefix , [] string { BranchPrefix , " --sort=-committerdate"} , skip , limit )
67
67
}
68
68
69
69
// WalkReferences walks all the references from the repository
70
70
func WalkReferences (ctx context.Context , repoPath string , walkfn func (sha1 , refname string ) error ) (int , error ) {
71
- return walkShowRef (ctx , repoPath , "" , 0 , 0 , walkfn )
71
+ return walkShowRef (ctx , repoPath , nil , 0 , 0 , walkfn )
72
72
}
73
73
74
74
// WalkReferences walks all the references from the repository
75
75
// refType should be empty, ObjectTag or ObjectBranch. All other values are equivalent to empty.
76
76
func (repo * Repository ) WalkReferences (refType ObjectType , skip , limit int , walkfn func (sha1 , refname string ) error ) (int , error ) {
77
- var arg string
77
+ var args [] string
78
78
switch refType {
79
79
case ObjectTag :
80
- arg = TagPrefix + " --sort=-taggerdate"
80
+ args = [] string { TagPrefix , " --sort=-taggerdate"}
81
81
case ObjectBranch :
82
- arg = BranchPrefix + " --sort=-committerdate"
83
- default :
84
- arg = ""
82
+ args = []string {BranchPrefix , "--sort=-committerdate" }
85
83
}
86
84
87
- return walkShowRef (repo .Ctx , repo .Path , arg , skip , limit , walkfn )
85
+ return walkShowRef (repo .Ctx , repo .Path , args , skip , limit , walkfn )
88
86
}
89
87
90
88
// callShowRef return refs, if limit = 0 it will not limit
91
- func callShowRef (ctx context.Context , repoPath , prefix , arg string , skip , limit int ) (branchNames []string , countAll int , err error ) {
92
- countAll , err = walkShowRef (ctx , repoPath , arg , skip , limit , func (_ , branchName string ) error {
93
- branchName = strings .TrimPrefix (branchName , prefix )
89
+ func callShowRef (ctx context.Context , repoPath , trimPrefix string , extraArgs [] string , skip , limit int ) (branchNames []string , countAll int , err error ) {
90
+ countAll , err = walkShowRef (ctx , repoPath , extraArgs , skip , limit , func (_ , branchName string ) error {
91
+ branchName = strings .TrimPrefix (branchName , trimPrefix )
94
92
branchNames = append (branchNames , branchName )
95
93
96
94
return nil
97
95
})
98
96
return branchNames , countAll , err
99
97
}
100
98
101
- func walkShowRef (ctx context.Context , repoPath , arg string , skip , limit int , walkfn func (sha1 , refname string ) error ) (countAll int , err error ) {
99
+ func walkShowRef (ctx context.Context , repoPath string , extraArgs [] string , skip , limit int , walkfn func (sha1 , refname string ) error ) (countAll int , err error ) {
102
100
stdoutReader , stdoutWriter := io .Pipe ()
103
101
defer func () {
104
102
_ = stdoutReader .Close ()
@@ -108,9 +106,7 @@ func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, wal
108
106
go func () {
109
107
stderrBuilder := & strings.Builder {}
110
108
args := []string {"for-each-ref" , "--format=%(objectname) %(refname)" }
111
- if arg != "" {
112
- args = append (args , strings .Fields (arg )... )
113
- }
109
+ args = append (args , extraArgs ... )
114
110
err := NewCommand (ctx , args ... ).Run (& RunOpts {
115
111
Dir : repoPath ,
116
112
Stdout : stdoutWriter ,
@@ -194,7 +190,7 @@ func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, wal
194
190
// GetRefsBySha returns all references filtered with prefix that belong to a sha commit hash
195
191
func (repo * Repository ) GetRefsBySha (sha , prefix string ) ([]string , error ) {
196
192
var revList []string
197
- _ , err := walkShowRef (repo .Ctx , repo .Path , "" , 0 , 0 , func (walkSha , refname string ) error {
193
+ _ , err := walkShowRef (repo .Ctx , repo .Path , nil , 0 , 0 , func (walkSha , refname string ) error {
198
194
if walkSha == sha && strings .HasPrefix (refname , prefix ) {
199
195
revList = append (revList , refname )
200
196
}
0 commit comments