fast-export: Omit tags that tag trees
[git/mjg.git] / builtin-fast-export.c
blob8c90a2df677030867909ef337c33343a62638a12
1 /*
2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
5 */
6 #include "builtin.h"
7 #include "cache.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "tag.h"
11 #include "diff.h"
12 #include "diffcore.h"
13 #include "log-tree.h"
14 #include "revision.h"
15 #include "decorate.h"
16 #include "string-list.h"
17 #include "utf8.h"
18 #include "parse-options.h"
20 static const char *fast_export_usage[] = {
21 "git fast-export [rev-list-opts]",
22 NULL
25 static int progress;
26 static enum { VERBATIM, WARN, STRIP, ABORT } signed_tag_mode = ABORT;
27 static int fake_missing_tagger;
29 static int parse_opt_signed_tag_mode(const struct option *opt,
30 const char *arg, int unset)
32 if (unset || !strcmp(arg, "abort"))
33 signed_tag_mode = ABORT;
34 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
35 signed_tag_mode = VERBATIM;
36 else if (!strcmp(arg, "warn"))
37 signed_tag_mode = WARN;
38 else if (!strcmp(arg, "strip"))
39 signed_tag_mode = STRIP;
40 else
41 return error("Unknown signed-tag mode: %s", arg);
42 return 0;
45 static struct decoration idnums;
46 static uint32_t last_idnum;
48 static int has_unshown_parent(struct commit *commit)
50 struct commit_list *parent;
52 for (parent = commit->parents; parent; parent = parent->next)
53 if (!(parent->item->object.flags & SHOWN) &&
54 !(parent->item->object.flags & UNINTERESTING))
55 return 1;
56 return 0;
59 /* Since intptr_t is C99, we do not use it here */
60 static inline uint32_t *mark_to_ptr(uint32_t mark)
62 return ((uint32_t *)NULL) + mark;
65 static inline uint32_t ptr_to_mark(void * mark)
67 return (uint32_t *)mark - (uint32_t *)NULL;
70 static inline void mark_object(struct object *object, uint32_t mark)
72 add_decoration(&idnums, object, mark_to_ptr(mark));
75 static inline void mark_next_object(struct object *object)
77 mark_object(object, ++last_idnum);
80 static int get_object_mark(struct object *object)
82 void *decoration = lookup_decoration(&idnums, object);
83 if (!decoration)
84 return 0;
85 return ptr_to_mark(decoration);
88 static void show_progress(void)
90 static int counter = 0;
91 if (!progress)
92 return;
93 if ((++counter % progress) == 0)
94 printf("progress %d objects\n", counter);
97 static void handle_object(const unsigned char *sha1)
99 unsigned long size;
100 enum object_type type;
101 char *buf;
102 struct object *object;
104 if (is_null_sha1(sha1))
105 return;
107 object = parse_object(sha1);
108 if (!object)
109 die ("Could not read blob %s", sha1_to_hex(sha1));
111 if (object->flags & SHOWN)
112 return;
114 buf = read_sha1_file(sha1, &type, &size);
115 if (!buf)
116 die ("Could not read blob %s", sha1_to_hex(sha1));
118 mark_next_object(object);
120 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
121 if (size && fwrite(buf, size, 1, stdout) != 1)
122 die ("Could not write blob %s", sha1_to_hex(sha1));
123 printf("\n");
125 show_progress();
127 object->flags |= SHOWN;
128 free(buf);
131 static void show_filemodify(struct diff_queue_struct *q,
132 struct diff_options *options, void *data)
134 int i;
135 for (i = 0; i < q->nr; i++) {
136 struct diff_filespec *ospec = q->queue[i]->one;
137 struct diff_filespec *spec = q->queue[i]->two;
139 switch (q->queue[i]->status) {
140 case DIFF_STATUS_DELETED:
141 printf("D %s\n", spec->path);
142 break;
144 case DIFF_STATUS_COPIED:
145 case DIFF_STATUS_RENAMED:
146 printf("%c \"%s\" \"%s\"\n", q->queue[i]->status,
147 ospec->path, spec->path);
149 if (!hashcmp(ospec->sha1, spec->sha1) &&
150 ospec->mode == spec->mode)
151 break;
152 /* fallthrough */
154 case DIFF_STATUS_TYPE_CHANGED:
155 case DIFF_STATUS_MODIFIED:
156 case DIFF_STATUS_ADDED:
158 * Links refer to objects in another repositories;
159 * output the SHA-1 verbatim.
161 if (S_ISGITLINK(spec->mode))
162 printf("M %06o %s %s\n", spec->mode,
163 sha1_to_hex(spec->sha1), spec->path);
164 else {
165 struct object *object = lookup_object(spec->sha1);
166 printf("M %06o :%d %s\n", spec->mode,
167 get_object_mark(object), spec->path);
169 break;
171 default:
172 die("Unexpected comparison status '%c' for %s, %s",
173 q->queue[i]->status,
174 ospec->path ? ospec->path : "none",
175 spec->path ? spec->path : "none");
180 static const char *find_encoding(const char *begin, const char *end)
182 const char *needle = "\nencoding ";
183 char *bol, *eol;
185 bol = memmem(begin, end ? end - begin : strlen(begin),
186 needle, strlen(needle));
187 if (!bol)
188 return git_commit_encoding;
189 bol += strlen(needle);
190 eol = strchrnul(bol, '\n');
191 *eol = '\0';
192 return bol;
195 static void handle_commit(struct commit *commit, struct rev_info *rev)
197 int saved_output_format = rev->diffopt.output_format;
198 const char *author, *author_end, *committer, *committer_end;
199 const char *encoding, *message;
200 char *reencoded = NULL;
201 struct commit_list *p;
202 int i;
204 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
206 parse_commit(commit);
207 author = strstr(commit->buffer, "\nauthor ");
208 if (!author)
209 die ("Could not find author in commit %s",
210 sha1_to_hex(commit->object.sha1));
211 author++;
212 author_end = strchrnul(author, '\n');
213 committer = strstr(author_end, "\ncommitter ");
214 if (!committer)
215 die ("Could not find committer in commit %s",
216 sha1_to_hex(commit->object.sha1));
217 committer++;
218 committer_end = strchrnul(committer, '\n');
219 message = strstr(committer_end, "\n\n");
220 encoding = find_encoding(committer_end, message);
221 if (message)
222 message += 2;
224 if (commit->parents &&
225 get_object_mark(&commit->parents->item->object) != 0) {
226 parse_commit(commit->parents->item);
227 diff_tree_sha1(commit->parents->item->tree->object.sha1,
228 commit->tree->object.sha1, "", &rev->diffopt);
230 else
231 diff_root_tree_sha1(commit->tree->object.sha1,
232 "", &rev->diffopt);
234 /* Export the referenced blobs, and remember the marks. */
235 for (i = 0; i < diff_queued_diff.nr; i++)
236 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
237 handle_object(diff_queued_diff.queue[i]->two->sha1);
239 mark_next_object(&commit->object);
240 if (!is_encoding_utf8(encoding))
241 reencoded = reencode_string(message, "UTF-8", encoding);
242 if (!commit->parents)
243 printf("reset %s\n", (const char*)commit->util);
244 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
245 (const char *)commit->util, last_idnum,
246 (int)(author_end - author), author,
247 (int)(committer_end - committer), committer,
248 (unsigned)(reencoded
249 ? strlen(reencoded) : message
250 ? strlen(message) : 0),
251 reencoded ? reencoded : message ? message : "");
252 free(reencoded);
254 for (i = 0, p = commit->parents; p; p = p->next) {
255 int mark = get_object_mark(&p->item->object);
256 if (!mark)
257 continue;
258 if (i == 0)
259 printf("from :%d\n", mark);
260 else
261 printf("merge :%d\n", mark);
262 i++;
265 log_tree_diff_flush(rev);
266 rev->diffopt.output_format = saved_output_format;
268 printf("\n");
270 show_progress();
273 static void handle_tail(struct object_array *commits, struct rev_info *revs)
275 struct commit *commit;
276 while (commits->nr) {
277 commit = (struct commit *)commits->objects[commits->nr - 1].item;
278 if (has_unshown_parent(commit))
279 return;
280 handle_commit(commit, revs);
281 commits->nr--;
285 static void handle_tag(const char *name, struct tag *tag)
287 unsigned long size;
288 enum object_type type;
289 char *buf;
290 const char *tagger, *tagger_end, *message;
291 size_t message_size = 0;
292 struct object *tagged;
294 /* Trees have no identifer in fast-export output, thus we have no way
295 * to output tags of trees, tags of tags of trees, etc. Simply omit
296 * such tags.
298 tagged = tag->tagged;
299 while (tagged->type == OBJ_TAG) {
300 tagged = ((struct tag *)tagged)->tagged;
302 if (tagged->type == OBJ_TREE) {
303 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
304 sha1_to_hex(tag->object.sha1));
305 return;
308 buf = read_sha1_file(tag->object.sha1, &type, &size);
309 if (!buf)
310 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
311 message = memmem(buf, size, "\n\n", 2);
312 if (message) {
313 message += 2;
314 message_size = strlen(message);
316 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
317 if (!tagger) {
318 if (fake_missing_tagger)
319 tagger = "tagger Unspecified Tagger "
320 "<unspecified-tagger> 0 +0000";
321 else
322 tagger = "";
323 tagger_end = tagger + strlen(tagger);
324 } else {
325 tagger++;
326 tagger_end = strchrnul(tagger, '\n');
329 /* handle signed tags */
330 if (message) {
331 const char *signature = strstr(message,
332 "\n-----BEGIN PGP SIGNATURE-----\n");
333 if (signature)
334 switch(signed_tag_mode) {
335 case ABORT:
336 die ("Encountered signed tag %s; use "
337 "--signed-tag=<mode> to handle it.",
338 sha1_to_hex(tag->object.sha1));
339 case WARN:
340 warning ("Exporting signed tag %s",
341 sha1_to_hex(tag->object.sha1));
342 /* fallthru */
343 case VERBATIM:
344 break;
345 case STRIP:
346 message_size = signature + 1 - message;
347 break;
351 if (!prefixcmp(name, "refs/tags/"))
352 name += 10;
353 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
354 name, get_object_mark(tag->tagged),
355 (int)(tagger_end - tagger), tagger,
356 tagger == tagger_end ? "" : "\n",
357 (int)message_size, (int)message_size, message ? message : "");
360 static void get_tags_and_duplicates(struct object_array *pending,
361 struct string_list *extra_refs)
363 struct tag *tag;
364 int i;
366 for (i = 0; i < pending->nr; i++) {
367 struct object_array_entry *e = pending->objects + i;
368 unsigned char sha1[20];
369 struct commit *commit = commit;
370 char *full_name;
372 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
373 continue;
375 switch (e->item->type) {
376 case OBJ_COMMIT:
377 commit = (struct commit *)e->item;
378 break;
379 case OBJ_TAG:
380 tag = (struct tag *)e->item;
382 /* handle nested tags */
383 while (tag && tag->object.type == OBJ_TAG) {
384 parse_object(tag->object.sha1);
385 string_list_append(full_name, extra_refs)->util = tag;
386 tag = (struct tag *)tag->tagged;
388 if (!tag)
389 die ("Tag %s points nowhere?", e->name);
390 switch(tag->object.type) {
391 case OBJ_COMMIT:
392 commit = (struct commit *)tag;
393 break;
394 case OBJ_BLOB:
395 handle_object(tag->object.sha1);
396 continue;
397 default: /* OBJ_TAG (nested tags) is already handled */
398 warning("Tag points to object of unexpected type %s, skipping.",
399 typename(tag->object.type));
400 continue;
402 break;
403 default:
404 warning("%s: Unexpected object of type %s, skipping.",
405 e->name,
406 typename(e->item->type));
407 continue;
409 if (commit->util)
410 /* more than one name for the same object */
411 string_list_append(full_name, extra_refs)->util = commit;
412 else
413 commit->util = full_name;
417 static void handle_tags_and_duplicates(struct string_list *extra_refs)
419 struct commit *commit;
420 int i;
422 for (i = extra_refs->nr - 1; i >= 0; i--) {
423 const char *name = extra_refs->items[i].string;
424 struct object *object = extra_refs->items[i].util;
425 switch (object->type) {
426 case OBJ_TAG:
427 handle_tag(name, (struct tag *)object);
428 break;
429 case OBJ_COMMIT:
430 /* create refs pointing to already seen commits */
431 commit = (struct commit *)object;
432 printf("reset %s\nfrom :%d\n\n", name,
433 get_object_mark(&commit->object));
434 show_progress();
435 break;
440 static void export_marks(char *file)
442 unsigned int i;
443 uint32_t mark;
444 struct object_decoration *deco = idnums.hash;
445 FILE *f;
447 f = fopen(file, "w");
448 if (!f)
449 error("Unable to open marks file %s for writing", file);
451 for (i = 0; i < idnums.size; i++) {
452 if (deco->base && deco->base->type == 1) {
453 mark = ptr_to_mark(deco->decoration);
454 fprintf(f, ":%"PRIu32" %s\n", mark,
455 sha1_to_hex(deco->base->sha1));
457 deco++;
460 if (ferror(f) || fclose(f))
461 error("Unable to write marks file %s.", file);
464 static void import_marks(char *input_file)
466 char line[512];
467 FILE *f = fopen(input_file, "r");
468 if (!f)
469 die("cannot read %s: %s", input_file, strerror(errno));
471 while (fgets(line, sizeof(line), f)) {
472 uint32_t mark;
473 char *line_end, *mark_end;
474 unsigned char sha1[20];
475 struct object *object;
477 line_end = strchr(line, '\n');
478 if (line[0] != ':' || !line_end)
479 die("corrupt mark line: %s", line);
480 *line_end = '\0';
482 mark = strtoumax(line + 1, &mark_end, 10);
483 if (!mark || mark_end == line + 1
484 || *mark_end != ' ' || get_sha1(mark_end + 1, sha1))
485 die("corrupt mark line: %s", line);
487 object = parse_object(sha1);
488 if (!object)
489 die ("Could not read blob %s", sha1_to_hex(sha1));
491 if (object->flags & SHOWN)
492 error("Object %s already has a mark", sha1);
494 mark_object(object, mark);
495 if (last_idnum < mark)
496 last_idnum = mark;
498 object->flags |= SHOWN;
500 fclose(f);
503 int cmd_fast_export(int argc, const char **argv, const char *prefix)
505 struct rev_info revs;
506 struct object_array commits = { 0, 0, NULL };
507 struct string_list extra_refs = { NULL, 0, 0, 0 };
508 struct commit *commit;
509 char *export_filename = NULL, *import_filename = NULL;
510 struct option options[] = {
511 OPT_INTEGER(0, "progress", &progress,
512 "show progress after <n> objects"),
513 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, "mode",
514 "select handling of signed tags",
515 parse_opt_signed_tag_mode),
516 OPT_STRING(0, "export-marks", &export_filename, "FILE",
517 "Dump marks to this file"),
518 OPT_STRING(0, "import-marks", &import_filename, "FILE",
519 "Import marks from this file"),
520 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger,
521 "Fake a tagger when tags lack one"),
522 OPT_END()
525 if (argc == 1)
526 usage_with_options (fast_export_usage, options);
528 /* we handle encodings */
529 git_config(git_default_config, NULL);
531 init_revisions(&revs, prefix);
532 revs.topo_order = 1;
533 argc = setup_revisions(argc, argv, &revs, NULL);
534 argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0);
535 if (argc > 1)
536 usage_with_options (fast_export_usage, options);
538 if (import_filename)
539 import_marks(import_filename);
541 get_tags_and_duplicates(&revs.pending, &extra_refs);
543 if (prepare_revision_walk(&revs))
544 die("revision walk setup failed");
545 revs.diffopt.format_callback = show_filemodify;
546 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
547 while ((commit = get_revision(&revs))) {
548 if (has_unshown_parent(commit)) {
549 struct commit_list *parent = commit->parents;
550 add_object_array(&commit->object, NULL, &commits);
551 for (; parent; parent = parent->next)
552 if (!parent->item->util)
553 parent->item->util = commit->util;
555 else {
556 handle_commit(commit, &revs);
557 handle_tail(&commits, &revs);
561 handle_tags_and_duplicates(&extra_refs);
563 if (export_filename)
564 export_marks(export_filename);
566 return 0;