fast-import: add a generic function to iterate over marks
Currently, we can iterate over marks only to dump them to a file. In the future, we'll want to perform an arbitrary operation over the items of a mark set. Add a function, for_each_mark, that iterates over marks in a set and performs an arbitrary callback function for each mark. Switch the mark dumping routine to use this function now that it's available. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Feb 22, 2020 at 20:17 UTC
d9db599ca85b008faedc1d5e771e28b6fb648c4e
1 file changed
+26
-24
fast-import.c
+26
-24
@@ -132,6 +132,7 @@ struct recent_command {
132
};
133
134
typedef void (*mark_set_inserter_t)(struct mark_set *s, struct object_id *oid, uintmax_t mark);
135
+typedef void (*each_mark_fn_t)(uintmax_t mark, void *obj, void *cbp);
136
137
/* Configured limits on output */
138
static unsigned long max_depth = 50;
@@ -232,6 +233,29 @@ static void parse_get_mark(const char *p);
233
static void parse_cat_blob(const char *p);
234
static void parse_ls(const char *p, struct branch *b);
235
236
+static void for_each_mark(struct mark_set *m, uintmax_t base, each_mark_fn_t callback, void *p)
237
+{
238
+ uintmax_t k;
239
+ if (m->shift) {
240
+ for (k = 0; k < 1024; k++) {
241
+ if (m->data.sets[k])
242
+ for_each_mark(m->data.sets[k], base + (k << m->shift), callback, p);
243
+ }
244
+ } else {
245
+ for (k = 0; k < 1024; k++) {
246
+ if (m->data.marked[k])
247
+ callback(base + k, m->data.marked[k], p);
248
+ }
249
+ }
250
+}
251
+
252
+static void dump_marks_fn(uintmax_t mark, void *object, void *cbp) {
253
+ struct object_entry *e = object;
254
+ FILE *f = cbp;
255
+
256
+ fprintf(f, ":%" PRIuMAX " %s\n", mark, oid_to_hex(&e->idx.oid));
257
+}
258
+
259
static void write_branch_report(FILE *rpt, struct branch *b)
260
{
261
fprintf(rpt, "%s:\n", b->name);
@@ -260,8 +284,6 @@ static void write_branch_report(FILE *rpt, struct branch *b)
284
fputc('\n', rpt);
285
}
286
263
-static void dump_marks_helper(FILE *, uintmax_t, struct mark_set *);
264
-
287
static void write_crash_report(const char *err)
288
{
289
char *loc = git_pathdup("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
@@ -340,7 +362,7 @@ static void write_crash_report(const char *err)
362
if (export_marks_file)
363
fprintf(rpt, " exported to %s\n", export_marks_file);
364
else
343
- dump_marks_helper(rpt, 0, marks);
365
+ for_each_mark(marks, 0, dump_marks_fn, rpt);
366
367
fputc('\n', rpt);
368
fputs("-------------------\n", rpt);
@@ -1655,26 +1677,6 @@ static void dump_tags(void)
1677
strbuf_release(&err);
1678
}
1679
1658
-static void dump_marks_helper(FILE *f,
1659
- uintmax_t base,
1660
- struct mark_set *m)
1661
-{
1662
- uintmax_t k;
1663
- if (m->shift) {
1664
- for (k = 0; k < 1024; k++) {
1665
- if (m->data.sets[k])
1666
- dump_marks_helper(f, base + (k << m->shift),
1667
- m->data.sets[k]);
1668
- }
1669
- } else {
1670
- for (k = 0; k < 1024; k++) {
1671
- if (m->data.marked[k])
1672
- fprintf(f, ":%" PRIuMAX " %s\n", base + k,
1673
- oid_to_hex(&m->data.marked[k]->idx.oid));
1674
- }
1675
- }
1676
-}
1677
-
1680
static void dump_marks(void)
1681
{
1682
struct lock_file mark_lock = LOCK_INIT;
@@ -1704,7 +1706,7 @@ static void dump_marks(void)
1706
return;
1707
}
1708
1707
- dump_marks_helper(f, 0, marks);
1709
+ for_each_mark(marks, 0, dump_marks_fn, f);
1710
if (commit_lock_file(&mark_lock)) {
1711
failure |= error_errno("Unable to write file %s",
1712
export_marks_file);