builtin-bundle: add a --basis option that specifies a basis
[git/pieter.git] / builtin-fast-export.c
bloba15d57bd2026d23f132fbc28348a9a9e5c4327f9
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;
28 static int parse_opt_signed_tag_mode(const struct option *opt,
29 const char *arg, int unset)
31 if (unset || !strcmp(arg, "abort"))
32 signed_tag_mode = ABORT;
33 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
34 signed_tag_mode = VERBATIM;
35 else if (!strcmp(arg, "warn"))
36 signed_tag_mode = WARN;
37 else if (!strcmp(arg, "strip"))
38 signed_tag_mode = STRIP;
39 else
40 return error("Unknown signed-tag mode: %s", arg);
41 return 0;
44 static struct decoration idnums;
45 static uint32_t last_idnum;
47 static int has_unshown_parent(struct commit *commit)
49 struct commit_list *parent;
51 for (parent = commit->parents; parent; parent = parent->next)
52 if (!(parent->item->object.flags & SHOWN) &&
53 !(parent->item->object.flags & UNINTERESTING))
54 return 1;
55 return 0;
58 /* Since intptr_t is C99, we do not use it here.
59 * We use pointer addition here to make sure the pointers are aligned.
61 static inline uint32_t *mark_to_ptr(uint32_t mark)
63 return ((uint32_t *)NULL) + mark;
66 static inline uint32_t ptr_to_mark(void * mark)
68 return (uint32_t *)mark - (uint32_t *)NULL;
71 static inline void mark_object(struct object *object, uint32_t mark)
73 add_decoration(&idnums, object, mark_to_ptr(mark));
76 static inline void mark_next_object(struct object *object)
78 mark_object(object, ++last_idnum);
81 static int get_object_mark(struct object *object)
83 void *decoration = lookup_decoration(&idnums, object);
84 if (!decoration)
85 return 0;
86 return ptr_to_mark(decoration);
89 static void show_progress(void)
91 static int counter = 0;
92 if (!progress)
93 return;
94 if ((++counter % progress) == 0)
95 printf("progress %d objects\n", counter);
98 static void handle_object(const unsigned char *sha1)
100 unsigned long size;
101 enum object_type type;
102 char *buf;
103 struct object *object;
105 if (is_null_sha1(sha1))
106 return;
108 object = parse_object(sha1);
109 if (!object)
110 die ("Could not read blob %s", sha1_to_hex(sha1));
112 if (object->flags & SHOWN)
113 return;
115 buf = read_sha1_file(sha1, &type, &size);
116 if (!buf)
117 die ("Could not read blob %s", sha1_to_hex(sha1));
119 mark_next_object(object);
121 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
122 if (size && fwrite(buf, size, 1, stdout) != 1)
123 die ("Could not write blob %s", sha1_to_hex(sha1));
124 printf("\n");
126 show_progress();
128 object->flags |= SHOWN;
129 free(buf);
132 static void show_filemodify(struct diff_queue_struct *q,
133 struct diff_options *options, void *data)
135 int i;
136 for (i = 0; i < q->nr; i++) {
137 struct diff_filespec *ospec = q->queue[i]->one;
138 struct diff_filespec *spec = q->queue[i]->two;
140 switch (q->queue[i]->status) {
141 case DIFF_STATUS_DELETED:
142 printf("D %s\n", spec->path);
143 break;
145 case DIFF_STATUS_COPIED:
146 case DIFF_STATUS_RENAMED:
147 printf("%c \"%s\" \"%s\"\n", q->queue[i]->status,
148 ospec->path, spec->path);
150 if (!hashcmp(ospec->sha1, spec->sha1) &&
151 ospec->mode == spec->mode)
152 break;
153 /* fallthrough */
155 case DIFF_STATUS_TYPE_CHANGED:
156 case DIFF_STATUS_MODIFIED:
157 case DIFF_STATUS_ADDED:
159 * Links refer to objects in another repositories;
160 * output the SHA-1 verbatim.
162 if (S_ISGITLINK(spec->mode))
163 printf("M %06o %s %s\n", spec->mode,
164 sha1_to_hex(spec->sha1), spec->path);
165 else {
166 struct object *object = lookup_object(spec->sha1);
167 printf("M %06o :%d %s\n", spec->mode,
168 get_object_mark(object), spec->path);
170 break;
172 default:
173 die("Unexpected comparison status '%c' for %s, %s",
174 q->queue[i]->status,
175 ospec->path ? ospec->path : "none",
176 spec->path ? spec->path : "none");
181 static const char *find_encoding(const char *begin, const char *end)
183 const char *needle = "\nencoding ";
184 char *bol, *eol;
186 bol = memmem(begin, end ? end - begin : strlen(begin),
187 needle, strlen(needle));
188 if (!bol)
189 return git_commit_encoding;
190 bol += strlen(needle);
191 eol = strchrnul(bol, '\n');
192 *eol = '\0';
193 return bol;
196 static void handle_commit(struct commit *commit, struct rev_info *rev)
198 int saved_output_format = rev->diffopt.output_format;
199 const char *author, *author_end, *committer, *committer_end;
200 const char *encoding, *message;
201 char *reencoded = NULL;
202 struct commit_list *p;
203 int i;
205 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
207 parse_commit(commit);
208 author = strstr(commit->buffer, "\nauthor ");
209 if (!author)
210 die ("Could not find author in commit %s",
211 sha1_to_hex(commit->object.sha1));
212 author++;
213 author_end = strchrnul(author, '\n');
214 committer = strstr(author_end, "\ncommitter ");
215 if (!committer)
216 die ("Could not find committer in commit %s",
217 sha1_to_hex(commit->object.sha1));
218 committer++;
219 committer_end = strchrnul(committer, '\n');
220 message = strstr(committer_end, "\n\n");
221 encoding = find_encoding(committer_end, message);
222 if (message)
223 message += 2;
225 if (commit->parents) {
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;
293 buf = read_sha1_file(tag->object.sha1, &type, &size);
294 if (!buf)
295 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
296 message = memmem(buf, size, "\n\n", 2);
297 if (message) {
298 message += 2;
299 message_size = strlen(message);
301 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
302 if (!tagger)
303 die ("No tagger for tag %s", sha1_to_hex(tag->object.sha1));
304 tagger++;
305 tagger_end = strchrnul(tagger, '\n');
307 /* handle signed tags */
308 if (message) {
309 const char *signature = strstr(message,
310 "\n-----BEGIN PGP SIGNATURE-----\n");
311 if (signature)
312 switch(signed_tag_mode) {
313 case ABORT:
314 die ("Encountered signed tag %s; use "
315 "--signed-tag=<mode> to handle it.",
316 sha1_to_hex(tag->object.sha1));
317 case WARN:
318 warning ("Exporting signed tag %s",
319 sha1_to_hex(tag->object.sha1));
320 /* fallthru */
321 case VERBATIM:
322 break;
323 case STRIP:
324 message_size = signature + 1 - message;
325 break;
329 if (!prefixcmp(name, "refs/tags/"))
330 name += 10;
331 printf("tag %s\nfrom :%d\n%.*s\ndata %d\n%.*s\n",
332 name, get_object_mark(tag->tagged),
333 (int)(tagger_end - tagger), tagger,
334 (int)message_size, (int)message_size, message ? message : "");
337 static void get_tags_and_duplicates(struct object_array *pending,
338 struct string_list *extra_refs)
340 struct tag *tag;
341 int i;
343 for (i = 0; i < pending->nr; i++) {
344 struct object_array_entry *e = pending->objects + i;
345 unsigned char sha1[20];
346 struct commit *commit = commit;
347 char *full_name;
349 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
350 continue;
352 switch (e->item->type) {
353 case OBJ_COMMIT:
354 commit = (struct commit *)e->item;
355 break;
356 case OBJ_TAG:
357 tag = (struct tag *)e->item;
358 while (tag && tag->object.type == OBJ_TAG) {
359 string_list_insert(full_name, extra_refs)->util = tag;
360 tag = (struct tag *)tag->tagged;
362 if (!tag)
363 die ("Tag %s points nowhere?", e->name);
364 switch(tag->object.type) {
365 case OBJ_COMMIT:
366 commit = (struct commit *)tag;
367 break;
368 case OBJ_BLOB:
369 handle_object(tag->object.sha1);
370 continue;
372 break;
373 default:
374 die ("Unexpected object of type %s",
375 typename(e->item->type));
377 if (commit->util)
378 /* more than one name for the same object */
379 string_list_insert(full_name, extra_refs)->util = commit;
380 else
381 commit->util = full_name;
385 static void handle_tags_and_duplicates(struct string_list *extra_refs)
387 struct commit *commit;
388 int i;
390 for (i = extra_refs->nr - 1; i >= 0; i--) {
391 const char *name = extra_refs->items[i].string;
392 struct object *object = extra_refs->items[i].util;
393 switch (object->type) {
394 case OBJ_TAG:
395 handle_tag(name, (struct tag *)object);
396 break;
397 case OBJ_COMMIT:
398 /* create refs pointing to already seen commits */
399 commit = (struct commit *)object;
400 printf("reset %s\nfrom :%d\n\n", name,
401 get_object_mark(&commit->object));
402 show_progress();
403 break;
408 static void export_marks(char *file)
410 unsigned int i;
411 uint32_t mark;
412 struct object_decoration *deco = idnums.hash;
413 FILE *f;
415 f = fopen(file, "w");
416 if (!f)
417 error("Unable to open marks file %s for writing", file);
419 for (i = 0; i < idnums.size; i++) {
420 if (deco->base && deco->base->type == 1) {
421 mark = ptr_to_mark(deco->decoration);
422 fprintf(f, ":%u %s\n", mark, sha1_to_hex(deco->base->sha1));
424 deco++;
427 if (ferror(f) || fclose(f))
428 error("Unable to write marks file %s.", file);
431 static void import_marks(char *input_file)
433 char line[512];
434 FILE *f = fopen(input_file, "r");
435 if (!f)
436 die("cannot read %s: %s", input_file, strerror(errno));
438 while (fgets(line, sizeof(line), f)) {
439 uint32_t mark;
440 char *line_end, *mark_end;
441 unsigned char sha1[20];
442 struct object *object;
444 line_end = strchr(line, '\n');
445 if (line[0] != ':' || !line_end)
446 die("corrupt mark line: %s", line);
447 *line_end = '\0';
449 mark = strtoumax(line + 1, &mark_end, 10);
450 if (!mark || mark_end == line + 1
451 || *mark_end != ' ' || get_sha1(mark_end + 1, sha1))
452 die("corrupt mark line: %s", line);
454 object = parse_object(sha1);
455 if (!object)
456 die ("Could not read blob %s", sha1_to_hex(sha1));
458 if (object->flags & SHOWN)
459 error("Object %s already has a mark", sha1);
461 mark_object(object, mark);
462 if (last_idnum < mark)
463 last_idnum = mark;
465 object->flags |= SHOWN;
467 fclose(f);
470 int cmd_fast_export(int argc, const char **argv, const char *prefix)
472 struct rev_info revs;
473 struct object_array commits = { 0, 0, NULL };
474 struct string_list extra_refs = { NULL, 0, 0, 0 };
475 struct commit *commit;
476 char *export_filename = NULL, *import_filename = NULL;
477 struct option options[] = {
478 OPT_INTEGER(0, "progress", &progress,
479 "show progress after <n> objects"),
480 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, "mode",
481 "select handling of signed tags",
482 parse_opt_signed_tag_mode),
483 OPT_STRING(0, "export-marks", &export_filename, "FILE",
484 "Dump marks to this file"),
485 OPT_STRING(0, "import-marks", &import_filename, "FILE",
486 "Import marks from this file"),
487 OPT_END()
490 /* we handle encodings */
491 git_config(git_default_config, NULL);
493 init_revisions(&revs, prefix);
494 argc = setup_revisions(argc, argv, &revs, NULL);
495 argc = parse_options(argc, argv, options, fast_export_usage, 0);
496 if (argc > 1)
497 usage_with_options (fast_export_usage, options);
499 if (import_filename)
500 import_marks(import_filename);
502 get_tags_and_duplicates(&revs.pending, &extra_refs);
504 if (prepare_revision_walk(&revs))
505 die("revision walk setup failed");
506 revs.diffopt.format_callback = show_filemodify;
507 revs.topo_order = 1;
508 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
509 while ((commit = get_revision(&revs))) {
510 if (has_unshown_parent(commit)) {
511 struct commit_list *parent = commit->parents;
512 add_object_array(&commit->object, NULL, &commits);
513 for (; parent; parent = parent->next)
514 if (!parent->item->util)
515 parent->item->util = commit->util;
517 else {
518 handle_commit(commit, &revs);
519 handle_tail(&commits, &revs);
523 handle_tags_and_duplicates(&extra_refs);
525 if (export_filename)
526 export_marks(export_filename);
528 return 0;