1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*
* Generic reference iterator infrastructure. See refs-internal.h for
* documentation about the design and use of reference iterators.
*/
#include "cache.h"
#include "refs.h"
#include "refs/refs-internal.h"
#include "iterator.h"
int ref_iterator_advance(struct ref_iterator *ref_iterator)
{
return ref_iterator->vtable->advance(ref_iterator);
}
int ref_iterator_peel(struct ref_iterator *ref_iterator,
struct object_id *peeled)
{
return ref_iterator->vtable->peel(ref_iterator, peeled);
}
int ref_iterator_abort(struct ref_iterator *ref_iterator)
{
return ref_iterator->vtable->abort(ref_iterator);
}
void base_ref_iterator_init(struct ref_iterator *iter,
struct ref_iterator_vtable *vtable)
{
iter->vtable = vtable;
iter->refname = NULL;
iter->oid = NULL;
iter->flags = 0;
}
void base_ref_iterator_free(struct ref_iterator *iter)
{
/* Help make use-after-free bugs fail quickly: */
iter->vtable = NULL;
free(iter);
}
struct empty_ref_iterator {
struct ref_iterator base;
};
static int empty_ref_iterator_advance(struct ref_iterator *ref_iterator)
{
return ref_iterator_abort(ref_iterator);
}
static int empty_ref_iterator_peel(struct ref_iterator *ref_iterator,
struct object_id *peeled)
{
die("BUG: peel called for empty iterator");
}
static int empty_ref_iterator_abort(struct ref_iterator *ref_iterator)
{
base_ref_iterator_free(ref_iterator);
return ITER_DONE;
}
static struct ref_iterator_vtable empty_ref_iterator_vtable = {
empty_ref_iterator_advance,
empty_ref_iterator_peel,
empty_ref_iterator_abort
};
struct ref_iterator *empty_ref_iterator_begin(void)
{
struct empty_ref_iterator *iter = xcalloc(1, sizeof(*iter));
struct ref_iterator *ref_iterator = &iter->base;
base_ref_iterator_init(ref_iterator, &empty_ref_iterator_vtable);
return ref_iterator;
}
int is_empty_ref_iterator(struct ref_iterator *ref_iterator)
{
return ref_iterator->vtable == &empty_ref_iterator_vtable;
}
struct merge_ref_iterator {
struct ref_iterator base;
struct ref_iterator *iter0, *iter1;
ref_iterator_select_fn *select;
void *cb_data;
/*
* A pointer to iter0 or iter1 (whichever is supplying the
* current value), or NULL if advance has not yet been called.
*/
struct ref_iterator **current;
};
static int merge_ref_iterator_advance(struct ref_iterator *ref_iterator)
{
struct merge_ref_iterator *iter =
(struct merge_ref_iterator *)ref_iterator;
int ok;
if (!iter->current) {
/* Initialize: advance both iterators to their first entries */
if ((ok = ref_iterator_advance(iter->iter0)) != ITER_OK) {
iter->iter0 = NULL;
if (ok == ITER_ERROR)
goto error;
}
if ((ok = ref_iterator_advance(iter->iter1)) != ITER_OK) {
iter->iter1 = NULL;
if (ok == ITER_ERROR)
goto error;
}
} else {
/*
* Advance the current iterator past the just-used
* entry:
*/
if ((ok = ref_iterator_advance(*iter->current)) != ITER_OK) {
*iter->current = NULL;
if (ok == ITER_ERROR)
goto error;
}
}
/* Loop until we find an entry that we can yield. */
while (1) {
struct ref_iterator **secondary;
enum iterator_selection selection =
iter->select(iter->iter0, iter->iter1, iter->cb_data);
if (selection == ITER_SELECT_DONE) {
return ref_iterator_abort(ref_iterator);
} else if (selection == ITER_SELECT_ERROR) {
ref_iterator_abort(ref_iterator);
return ITER_ERROR;
}
if ((selection & ITER_CURRENT_SELECTION_MASK) == 0) {
iter->current = &iter->iter0;
secondary = &iter->iter1;
} else {
iter->current = &iter->iter1;
secondary = &iter->iter0;
}
if (selection & ITER_SKIP_SECONDARY) {
if ((ok = ref_iterator_advance(*secondary)) != ITER_OK) {
*secondary = NULL;
if (ok == ITER_ERROR)
goto error;
}
}
if (selection & ITER_YIELD_CURRENT) {
iter->base.refname = (*iter->current)->refname;
iter->base.oid = (*iter->current)->oid;
iter->base.flags = (*iter->current)->flags;
return ITER_OK;
}
}
error:
ref_iterator_abort(ref_iterator);
return ITER_ERROR;
}
static int merge_ref_iterator_peel(struct ref_iterator *ref_iterator,
struct object_id *peeled)
{
struct merge_ref_iterator *iter =
(struct merge_ref_iterator *)ref_iterator;
if (!iter->current) {
die("BUG: peel called before advance for merge iterator");
}
return ref_iterator_peel(*iter->current, peeled);
}
static int merge_ref_iterator_abort(struct ref_iterator *ref_iterator)
{
struct merge_ref_iterator *iter =
(struct merge_ref_iterator *)ref_iterator;
int ok = ITER_DONE;
if (iter->iter0) {
if (ref_iterator_abort(iter->iter0) != ITER_DONE)
ok = ITER_ERROR;
}
if (iter->iter1) {
if (ref_iterator_abort(iter->iter1) != ITER_DONE)
ok = ITER_ERROR;
}
base_ref_iterator_free(ref_iterator);
return ok;
}
static struct ref_iterator_vtable merge_ref_iterator_vtable = {
merge_ref_iterator_advance,
merge_ref_iterator_peel,
merge_ref_iterator_abort
};
struct ref_iterator *merge_ref_iterator_begin(
struct ref_iterator *iter0, struct ref_iterator *iter1,
ref_iterator_select_fn *select, void *cb_data)
{
struct merge_ref_iterator *iter = xcalloc(1, sizeof(*iter));
struct ref_iterator *ref_iterator = &iter->base;
/*
* We can't do the same kind of is_empty_ref_iterator()-style
* optimization here as overlay_ref_iterator_begin() does,
* because we don't know the semantics of the select function.
* It might, for example, implement "intersect" by passing
* references through only if they exist in both iterators.
*/
base_ref_iterator_init(ref_iterator, &merge_ref_iterator_vtable);
iter->iter0 = iter0;
iter->iter1 = iter1;
iter->select = select;
iter->cb_data = cb_data;
iter->current = NULL;
return ref_iterator;
}
/*
* A ref_iterator_select_fn that overlays the items from front on top
* of those from back (like loose refs over packed refs). See
* overlay_ref_iterator_begin().
*/
static enum iterator_selection overlay_iterator_select(
struct ref_iterator *front, struct ref_iterator *back,
void *cb_data)
{
int cmp;
if (!back)
return front ? ITER_SELECT_0 : ITER_SELECT_DONE;
else if (!front)
return ITER_SELECT_1;
cmp = strcmp(front->refname, back->refname);
if (cmp < 0)
return ITER_SELECT_0;
else if (cmp > 0)
return ITER_SELECT_1;
else
return ITER_SELECT_0_SKIP_1;
}
struct ref_iterator *overlay_ref_iterator_begin(
struct ref_iterator *front, struct ref_iterator *back)
{
/*
* Optimization: if one of the iterators is empty, return the
* other one rather than incurring the overhead of wrapping
* them.
*/
if (is_empty_ref_iterator(front)) {
ref_iterator_abort(front);
return back;
} else if (is_empty_ref_iterator(back)) {
ref_iterator_abort(back);
return front;
}
return merge_ref_iterator_begin(front, back,
overlay_iterator_select, NULL);
}
struct prefix_ref_iterator {
struct ref_iterator base;
struct ref_iterator *iter0;
char *prefix;
int trim;
};
static int prefix_ref_iterator_advance(struct ref_iterator *ref_iterator)
{
struct prefix_ref_iterator *iter =
(struct prefix_ref_iterator *)ref_iterator;
int ok;
while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
if (!starts_with(iter->iter0->refname, iter->prefix))
continue;
iter->base.refname = iter->iter0->refname + iter->trim;
iter->base.oid = iter->iter0->oid;
iter->base.flags = iter->iter0->flags;
return ITER_OK;
}
iter->iter0 = NULL;
if (ref_iterator_abort(ref_iterator) != ITER_DONE)
return ITER_ERROR;
return ok;
}
static int prefix_ref_iterator_peel(struct ref_iterator *ref_iterator,
struct object_id *peeled)
{
struct prefix_ref_iterator *iter =
(struct prefix_ref_iterator *)ref_iterator;
return ref_iterator_peel(iter->iter0, peeled);
}
static int prefix_ref_iterator_abort(struct ref_iterator *ref_iterator)
{
struct prefix_ref_iterator *iter =
(struct prefix_ref_iterator *)ref_iterator;
int ok = ITER_DONE;
if (iter->iter0)
ok = ref_iterator_abort(iter->iter0);
free(iter->prefix);
base_ref_iterator_free(ref_iterator);
return ok;
}
static struct ref_iterator_vtable prefix_ref_iterator_vtable = {
prefix_ref_iterator_advance,
prefix_ref_iterator_peel,
prefix_ref_iterator_abort
};
struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
const char *prefix,
int trim)
{
struct prefix_ref_iterator *iter;
struct ref_iterator *ref_iterator;
if (!*prefix && !trim)
return iter0; /* optimization: no need to wrap iterator */
iter = xcalloc(1, sizeof(*iter));
ref_iterator = &iter->base;
base_ref_iterator_init(ref_iterator, &prefix_ref_iterator_vtable);
iter->iter0 = iter0;
iter->prefix = xstrdup(prefix);
iter->trim = trim;
return ref_iterator;
}
struct ref_iterator *current_ref_iter = NULL;
int do_for_each_ref_iterator(struct ref_iterator *iter,
each_ref_fn fn, void *cb_data)
{
int retval = 0, ok;
struct ref_iterator *old_ref_iter = current_ref_iter;
current_ref_iter = iter;
while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
retval = fn(iter->refname, iter->oid, iter->flags, cb_data);
if (retval) {
/*
* If ref_iterator_abort() returns ITER_ERROR,
* we ignore that error in deference to the
* callback function's return value.
*/
ref_iterator_abort(iter);
goto out;
}
}
out:
current_ref_iter = old_ref_iter;
if (ok == ITER_ERROR)
return -1;
return retval;
}
|