@@ -67,8 +67,26 @@ func (repo *Repository) GetBranches(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 , limit , skip , func (branchName string ) error {
78
+ branchName = strings .TrimPrefix (branchName , prefix )
79
+ if len (branchName ) > 0 {
80
+ branchName = branchName [:len (branchName )- 1 ]
81
+ }
82
+ branchNames = append (branchNames , branchName )
83
+
84
+ return nil
85
+ })
86
+ return
87
+ }
88
+
89
+ func walkShowRef (ctx context.Context , repoPath , arg string , skip , limit int , walkfn func (string ) error ) (countAll int , err error ) {
72
90
stdoutReader , stdoutWriter := io .Pipe ()
73
91
defer func () {
74
92
_ = stdoutReader .Close ()
@@ -77,7 +95,11 @@ func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit
77
95
78
96
go func () {
79
97
stderrBuilder := & strings.Builder {}
80
- err := NewCommandContext (ctx , "show-ref" , arg ).RunInDirPipeline (repoPath , stdoutWriter , stderrBuilder )
98
+ args := []string {"show-ref" }
99
+ if arg != "" {
100
+ args = append (args , arg )
101
+ }
102
+ err := NewCommandContext (ctx , args ... ).RunInDirPipeline (repoPath , stdoutWriter , stderrBuilder )
81
103
if err != nil {
82
104
if stderrBuilder .Len () == 0 {
83
105
_ = stdoutWriter .Close ()
@@ -94,10 +116,10 @@ func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit
94
116
for i < skip {
95
117
_ , isPrefix , err := bufReader .ReadLine ()
96
118
if err == io .EOF {
97
- return branchNames , i , nil
119
+ return i , nil
98
120
}
99
121
if err != nil {
100
- return nil , 0 , err
122
+ return 0 , err
101
123
}
102
124
if ! isPrefix {
103
125
i ++
@@ -112,39 +134,39 @@ func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit
112
134
_ , err = bufReader .ReadSlice (' ' )
113
135
}
114
136
if err == io .EOF {
115
- return branchNames , i , nil
137
+ return i , nil
116
138
}
117
139
if err != nil {
118
- return nil , 0 , err
140
+ return 0 , err
119
141
}
120
142
121
143
branchName , err := bufReader .ReadString ('\n' )
122
144
if err == io .EOF {
123
145
// This shouldn't happen... but we'll tolerate it for the sake of peace
124
- return branchNames , i , nil
146
+ return i , nil
125
147
}
126
148
if err != nil {
127
- return nil , i , err
149
+ return i , err
128
150
}
129
- branchName = strings .TrimPrefix (branchName , prefix )
130
- if len (branchName ) > 0 {
131
- branchName = branchName [:len (branchName )- 1 ]
151
+
152
+ err = walkfn (branchName )
153
+ if err != nil {
154
+ return i , err
132
155
}
133
- branchNames = append (branchNames , branchName )
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