Skip to content

Commit f7529e6

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8e32060 commit f7529e6

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

fast-import.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ struct recent_command {
132132
};
133133

134134
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);
135136

136137
/* Configured limits on output */
137138
static unsigned long max_depth = 50;
@@ -232,6 +233,29 @@ static void parse_get_mark(const char *p);
232233
static void parse_cat_blob(const char *p);
233234
static void parse_ls(const char *p, struct branch *b);
234235

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+
235259
static void write_branch_report(FILE *rpt, struct branch *b)
236260
{
237261
fprintf(rpt, "%s:\n", b->name);
@@ -260,8 +284,6 @@ static void write_branch_report(FILE *rpt, struct branch *b)
260284
fputc('\n', rpt);
261285
}
262286

263-
static void dump_marks_helper(FILE *, uintmax_t, struct mark_set *);
264-
265287
static void write_crash_report(const char *err)
266288
{
267289
char *loc = git_pathdup("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
@@ -340,7 +362,7 @@ static void write_crash_report(const char *err)
340362
if (export_marks_file)
341363
fprintf(rpt, " exported to %s\n", export_marks_file);
342364
else
343-
dump_marks_helper(rpt, 0, marks);
365+
for_each_mark(marks, 0, dump_marks_fn, rpt);
344366

345367
fputc('\n', rpt);
346368
fputs("-------------------\n", rpt);
@@ -1655,26 +1677,6 @@ static void dump_tags(void)
16551677
strbuf_release(&err);
16561678
}
16571679

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-
16781680
static void dump_marks(void)
16791681
{
16801682
struct lock_file mark_lock = LOCK_INIT;
@@ -1704,7 +1706,7 @@ static void dump_marks(void)
17041706
return;
17051707
}
17061708

1707-
dump_marks_helper(f, 0, marks);
1709+
for_each_mark(marks, 0, dump_marks_fn, f);
17081710
if (commit_lock_file(&mark_lock)) {
17091711
failure |= error_errno("Unable to write file %s",
17101712
export_marks_file);

0 commit comments

Comments
 (0)