@@ -61,14 +61,29 @@ func (repo *Repository) IsBranchExist(name string) bool {
61
61
return repo .IsReferenceExist (BranchPrefix + name )
62
62
}
63
63
64
- // GetBranches returns branches from the repository, skipping skip initial branches and
64
+ // GetBranchNames returns branches from the repository, skipping skip initial branches and
65
65
// returning at most limit branches, or all branches if limit is 0.
66
- func (repo * Repository ) GetBranches (skip , limit int ) ([]string , int , error ) {
66
+ func (repo * Repository ) GetBranchNames (skip , limit int ) ([]string , int , error ) {
67
67
return callShowRef (repo .Ctx , repo .Path , BranchPrefix , "--heads" , skip , limit )
68
68
}
69
69
70
+ // WalkReferences walks all the references from the repository
71
+ func WalkReferences (ctx context.Context , repoPath string , walkfn func (string ) error ) (int , error ) {
72
+ return walkShowRef (ctx , repoPath , "" , 0 , 0 , walkfn )
73
+ }
74
+
70
75
// callShowRef return refs, if limit = 0 it will not limit
71
76
func callShowRef (ctx context.Context , repoPath , prefix , arg string , skip , limit int ) (branchNames []string , countAll int , err error ) {
77
+ countAll , err = walkShowRef (ctx , repoPath , arg , skip , limit , func (branchName string ) error {
78
+ branchName = strings .TrimPrefix (branchName , prefix )
79
+ branchNames = append (branchNames , branchName )
80
+
81
+ return nil
82
+ })
83
+ return
84
+ }
85
+
86
+ func walkShowRef (ctx context.Context , repoPath , arg string , skip , limit int , walkfn func (string ) error ) (countAll int , err error ) {
72
87
stdoutReader , stdoutWriter := io .Pipe ()
73
88
defer func () {
74
89
_ = stdoutReader .Close ()
@@ -77,7 +92,11 @@ func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit
77
92
78
93
go func () {
79
94
stderrBuilder := & strings.Builder {}
80
- err := NewCommandContext (ctx , "show-ref" , arg ).RunInDirPipeline (repoPath , stdoutWriter , stderrBuilder )
95
+ args := []string {"show-ref" }
96
+ if arg != "" {
97
+ args = append (args , arg )
98
+ }
99
+ err := NewCommandContext (ctx , args ... ).RunInDirPipeline (repoPath , stdoutWriter , stderrBuilder )
81
100
if err != nil {
82
101
if stderrBuilder .Len () == 0 {
83
102
_ = stdoutWriter .Close ()
@@ -94,10 +113,10 @@ func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit
94
113
for i < skip {
95
114
_ , isPrefix , err := bufReader .ReadLine ()
96
115
if err == io .EOF {
97
- return branchNames , i , nil
116
+ return i , nil
98
117
}
99
118
if err != nil {
100
- return nil , 0 , err
119
+ return 0 , err
101
120
}
102
121
if ! isPrefix {
103
122
i ++
@@ -112,39 +131,42 @@ func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit
112
131
_ , err = bufReader .ReadSlice (' ' )
113
132
}
114
133
if err == io .EOF {
115
- return branchNames , i , nil
134
+ return i , nil
116
135
}
117
136
if err != nil {
118
- return nil , 0 , err
137
+ return 0 , err
119
138
}
120
139
121
140
branchName , err := bufReader .ReadString ('\n' )
122
141
if err == io .EOF {
123
142
// This shouldn't happen... but we'll tolerate it for the sake of peace
124
- return branchNames , i , nil
143
+ return i , nil
125
144
}
126
145
if err != nil {
127
- return nil , i , err
146
+ return i , err
128
147
}
129
- branchName = strings . TrimPrefix ( branchName , prefix )
148
+
130
149
if len (branchName ) > 0 {
131
150
branchName = branchName [:len (branchName )- 1 ]
132
151
}
133
- branchNames = append (branchNames , branchName )
152
+ err = walkfn (branchName )
153
+ if err != nil {
154
+ return i , err
155
+ }
134
156
i ++
135
157
}
136
158
// count all refs
137
159
for limit != 0 {
138
160
_ , isPrefix , err := bufReader .ReadLine ()
139
161
if err == io .EOF {
140
- return branchNames , i , nil
162
+ return i , nil
141
163
}
142
164
if err != nil {
143
- return nil , 0 , err
165
+ return 0 , err
144
166
}
145
167
if ! isPrefix {
146
168
i ++
147
169
}
148
170
}
149
- return branchNames , i , nil
171
+ return i , nil
150
172
}
0 commit comments