builtin-fast-export: Add importing and exporting of revision marks
This adds the --import-marks and --export-marks to fast-export. These import and export the marks used to for all revisions exported in a similar fashion to what fast-import does. The format is the same as fast-import, so you can create a bidirectional importer / exporter by using the same marks file on both sides. Signed-off-by: Pieter de Bie <pdebie@ai.rug.nl> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pieter de Bie committed
Jun 11, 2008 at 13:17 UTC
df6a7ff7ac55d320afa1b8a59393122d6ca0f6c4
3 files changed
+137
-6
Documentation/git-fast-export.txt
+20
@@ -36,6 +36,26 @@ when encountering a signed tag. With 'strip', the tags will be made
36
unsigned, with 'verbatim', they will be silently exported
37
and with 'warn', they will be exported, but you will see a warning.
38
39
+--export-marks=<file>::
40
+ Dumps the internal marks table to <file> when complete.
41
+ Marks are written one per line as `:markid SHA-1`. Only marks
42
+ for revisions are dumped; marks for blobs are ignored.
43
+ Backends can use this file to validate imports after they
44
+ have been completed, or to save the marks table across
45
+ incremental runs. As <file> is only opened and truncated
46
+ at completion, the same path can also be safely given to
47
+ \--import-marks.
48
+
49
+--import-marks=<file>::
50
+ Before processing any input, load the marks specified in
51
+ <file>. The input file must exist, must be readable, and
52
+ must use the same format as produced by \--export-marks.
53
++
54
+Any commits that have already been marked will not be exported again.
55
+If the backend uses a similar \--import-marks file, this allows for
56
+incremental bidirectional exporting of the repository by keeping the
57
+marks the same across runs.
58
+
59
60
EXAMPLES
61
--------
builtin-fast-export.c
+93
-6
@@ -56,10 +56,24 @@ static int has_unshown_parent(struct commit *commit)
56
}
57
58
/* Since intptr_t is C99, we do not use it here */
59
-static void mark_object(struct object *object)
59
+static inline uint32_t *mark_to_ptr(uint32_t mark)
60
{
61
- last_idnum++;
62
- add_decoration(&idnums, object, ((uint32_t *)NULL) + last_idnum);
61
+ return ((uint32_t *)NULL) + mark;
62
+}
63
+
64
+static inline uint32_t ptr_to_mark(void * mark)
65
+{
66
+ return (uint32_t *)mark - (uint32_t *)NULL;
67
+}
68
+
69
+static inline void mark_object(struct object *object, uint32_t mark)
70
+{
71
+ add_decoration(&idnums, object, mark_to_ptr(mark));
72
+}
73
+
74
+static inline void mark_next_object(struct object *object)
75
+{
76
+ mark_object(object, ++last_idnum);
77
}
78
79
static int get_object_mark(struct object *object)
@@ -67,7 +81,7 @@ static int get_object_mark(struct object *object)
81
void *decoration = lookup_decoration(&idnums, object);
82
if (!decoration)
83
return 0;
70
- return (uint32_t *)decoration - (uint32_t *)NULL;
84
+ return ptr_to_mark(decoration);
85
}
86
87
static void show_progress(void)
@@ -100,7 +114,7 @@ static void handle_object(const unsigned char *sha1)
114
if (!buf)
115
die ("Could not read blob %s", sha1_to_hex(sha1));
116
103
- mark_object(object);
117
+ mark_next_object(object);
118
119
printf("blob\nmark :%d\ndata %lu\n", last_idnum, size);
120
if (size && fwrite(buf, size, 1, stdout) != 1)
@@ -185,7 +199,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev)
199
for (i = 0; i < diff_queued_diff.nr; i++)
200
handle_object(diff_queued_diff.queue[i]->two->sha1);
201
188
- mark_object(&commit->object);
202
+ mark_next_object(&commit->object);
203
if (!is_encoding_utf8(encoding))
204
reencoded = reencode_string(message, "UTF-8", encoding);
205
if (!commit->parents)
@@ -354,18 +368,85 @@ static void handle_tags_and_duplicates(struct path_list *extra_refs)
368
}
369
}
370
371
+static void export_marks(char *file)
372
+{
373
+ unsigned int i;
374
+ uint32_t mark;
375
+ struct object_decoration *deco = idnums.hash;
376
+ FILE *f;
377
+
378
+ f = fopen(file, "w");
379
+ if (!f)
380
+ error("Unable to open marks file %s for writing", file);
381
+
382
+ for (i = 0; i < idnums.size; ++i) {
383
+ deco++;
384
+ if (deco && deco->base && deco->base->type == 1) {
385
+ mark = ptr_to_mark(deco->decoration);
386
+ fprintf(f, ":%u %s\n", mark, sha1_to_hex(deco->base->sha1));
387
+ }
388
+ }
389
+
390
+ if (ferror(f) || fclose(f))
391
+ error("Unable to write marks file %s.", file);
392
+}
393
+
394
+static void import_marks(char * input_file)
395
+{
396
+ char line[512];
397
+ FILE *f = fopen(input_file, "r");
398
+ if (!f)
399
+ die("cannot read %s: %s", input_file, strerror(errno));
400
+
401
+ while (fgets(line, sizeof(line), f)) {
402
+ uint32_t mark;
403
+ char *line_end, *mark_end;
404
+ unsigned char sha1[20];
405
+ struct object *object;
406
+
407
+ line_end = strchr(line, '\n');
408
+ if (line[0] != ':' || !line_end)
409
+ die("corrupt mark line: %s", line);
410
+ *line_end = 0;
411
+
412
+ mark = strtoumax(line + 1, &mark_end, 10);
413
+ if (!mark || mark_end == line + 1
414
+ || *mark_end != ' ' || get_sha1(mark_end + 1, sha1))
415
+ die("corrupt mark line: %s", line);
416
+
417
+ object = parse_object(sha1);
418
+ if (!object)
419
+ die ("Could not read blob %s", sha1_to_hex(sha1));
420
+
421
+ if (object->flags & SHOWN)
422
+ error("Object %s already has a mark", sha1);
423
+
424
+ mark_object(object, mark);
425
+ if (last_idnum < mark)
426
+ last_idnum = mark;
427
+
428
+ object->flags |= SHOWN;
429
+ }
430
+ fclose(f);
431
+}
432
+
433
int cmd_fast_export(int argc, const char **argv, const char *prefix)
434
{
435
struct rev_info revs;
436
struct object_array commits = { 0, 0, NULL };
437
struct path_list extra_refs = { NULL, 0, 0, 0 };
438
struct commit *commit;
439
+ char *export_filename = NULL, *import_filename = NULL;
440
struct option options[] = {
441
OPT_INTEGER(0, "progress", &progress,
442
"show progress after <n> objects"),
443
OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, "mode",
444
"select handling of signed tags",
445
parse_opt_signed_tag_mode),
446
+ OPT_STRING(0, "export-marks", &export_filename, "FILE",
447
+ "Dump marks to this file"),
448
+ OPT_STRING(0, "import-marks", &import_filename, "FILE",
449
+ "Import marks from this file"),
450
OPT_END()
451
};
452
@@ -378,6 +459,9 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
459
if (argc > 1)
460
usage_with_options (fast_export_usage, options);
461
462
+ if (import_filename)
463
+ import_marks(import_filename);
464
+
465
get_tags_and_duplicates(&revs.pending, &extra_refs);
466
467
if (prepare_revision_walk(&revs))
@@ -400,5 +484,8 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
484
485
handle_tags_and_duplicates(&extra_refs);
486
487
+ if (export_filename)
488
+ export_marks(export_filename);
489
+
490
return 0;
491
}
t/t9301-fast-export.sh
+24
@@ -77,6 +77,30 @@ test_expect_success 'iso-8859-1' '
77
git fast-import &&
78
git cat-file commit i18n | grep "Áéí óú")
79
80
+'
81
+test_expect_success 'import/export-marks' '
82
+
83
+ git checkout -b marks master &&
84
+ git fast-export --export-marks=tmp-marks HEAD &&
85
+ test -s tmp-marks &&
86
+ cp tmp-marks ~ &&
87
+ test $(wc -l < tmp-marks) -eq 3 &&
88
+ test $(
89
+ git fast-export --import-marks=tmp-marks\
90
+ --export-marks=tmp-marks HEAD |
91
+ grep ^commit |
92
+ wc -l) \
93
+ -eq 0 &&
94
+ echo change > file &&
95
+ git commit -m "last commit" file &&
96
+ test $(
97
+ git fast-export --import-marks=tmp-marks \
98
+ --export-marks=tmp-marks HEAD |
99
+ grep ^commit\ |
100
+ wc -l) \
101
+ -eq 1 &&
102
+ test $(wc -l < tmp-marks) -eq 4
103
+
104
'
105
106
cat > signed-tag-import << EOF