check_and_emit_line(): rename and refactor
[git/platforms.git] / builtin-fast-export.c
blob45786ef1b740c1e21c1cffeeea89cb0250f446c5
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 "path-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 static inline uint32_t *mark_to_ptr(uint32_t mark)
61 return ((uint32_t *)NULL) + mark;
64 static inline uint32_t ptr_to_mark(void * mark)
66 return (uint32_t *)mark - (uint32_t *)NULL;
69 static inline void mark_object(struct object *object, uint32_t mark)
71 add_decoration(&idnums, object, mark_to_ptr(mark));
74 static inline void mark_next_object(struct object *object)
76 mark_object(object, ++last_idnum);
79 static int get_object_mark(struct object *object)
81 void *decoration = lookup_decoration(&idnums, object);
82 if (!decoration)
83 return 0;
84 return ptr_to_mark(decoration);
87 static void show_progress(void)
89 static int counter = 0;
90 if (!progress)
91 return;
92 if ((++counter % progress) == 0)
93 printf("progress %d objects\n", counter);
96 static void handle_object(const unsigned char *sha1)
98 unsigned long size;
99 enum object_type type;
100 char *buf;
101 struct object *object;
103 if (is_null_sha1(sha1))
104 return;
106 object = parse_object(sha1);
107 if (!object)
108 die ("Could not read blob %s", sha1_to_hex(sha1));
110 if (object->flags & SHOWN)
111 return;
113 buf = read_sha1_file(sha1, &type, &size);
114 if (!buf)
115 die ("Could not read blob %s", sha1_to_hex(sha1));
117 mark_next_object(object);
119 printf("blob\nmark :%d\ndata %lu\n", last_idnum, size);
120 if (size && fwrite(buf, size, 1, stdout) != 1)
121 die ("Could not write blob %s", sha1_to_hex(sha1));
122 printf("\n");
124 show_progress();
126 object->flags |= SHOWN;
127 free(buf);
130 static void show_filemodify(struct diff_queue_struct *q,
131 struct diff_options *options, void *data)
133 int i;
134 for (i = 0; i < q->nr; i++) {
135 struct diff_filespec *spec = q->queue[i]->two;
136 if (is_null_sha1(spec->sha1))
137 printf("D %s\n", spec->path);
138 else {
139 struct object *object = lookup_object(spec->sha1);
140 printf("M %06o :%d %s\n", spec->mode,
141 get_object_mark(object), spec->path);
146 static const char *find_encoding(const char *begin, const char *end)
148 const char *needle = "\nencoding ";
149 char *bol, *eol;
151 bol = memmem(begin, end ? end - begin : strlen(begin),
152 needle, strlen(needle));
153 if (!bol)
154 return git_commit_encoding;
155 bol += strlen(needle);
156 eol = strchrnul(bol, '\n');
157 *eol = '\0';
158 return bol;
161 static void handle_commit(struct commit *commit, struct rev_info *rev)
163 int saved_output_format = rev->diffopt.output_format;
164 const char *author, *author_end, *committer, *committer_end;
165 const char *encoding, *message;
166 char *reencoded = NULL;
167 struct commit_list *p;
168 int i;
170 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
172 parse_commit(commit);
173 author = strstr(commit->buffer, "\nauthor ");
174 if (!author)
175 die ("Could not find author in commit %s",
176 sha1_to_hex(commit->object.sha1));
177 author++;
178 author_end = strchrnul(author, '\n');
179 committer = strstr(author_end, "\ncommitter ");
180 if (!committer)
181 die ("Could not find committer in commit %s",
182 sha1_to_hex(commit->object.sha1));
183 committer++;
184 committer_end = strchrnul(committer, '\n');
185 message = strstr(committer_end, "\n\n");
186 encoding = find_encoding(committer_end, message);
187 if (message)
188 message += 2;
190 if (commit->parents) {
191 parse_commit(commit->parents->item);
192 diff_tree_sha1(commit->parents->item->tree->object.sha1,
193 commit->tree->object.sha1, "", &rev->diffopt);
195 else
196 diff_root_tree_sha1(commit->tree->object.sha1,
197 "", &rev->diffopt);
199 for (i = 0; i < diff_queued_diff.nr; i++)
200 handle_object(diff_queued_diff.queue[i]->two->sha1);
202 mark_next_object(&commit->object);
203 if (!is_encoding_utf8(encoding))
204 reencoded = reencode_string(message, "UTF-8", encoding);
205 if (!commit->parents)
206 printf("reset %s\n", (const char*)commit->util);
207 printf("commit %s\nmark :%d\n%.*s\n%.*s\ndata %u\n%s",
208 (const char *)commit->util, last_idnum,
209 (int)(author_end - author), author,
210 (int)(committer_end - committer), committer,
211 (unsigned)(reencoded
212 ? strlen(reencoded) : message
213 ? strlen(message) : 0),
214 reencoded ? reencoded : message ? message : "");
215 free(reencoded);
217 for (i = 0, p = commit->parents; p; p = p->next) {
218 int mark = get_object_mark(&p->item->object);
219 if (!mark)
220 continue;
221 if (i == 0)
222 printf("from :%d\n", mark);
223 else
224 printf("merge :%d\n", mark);
225 i++;
228 log_tree_diff_flush(rev);
229 rev->diffopt.output_format = saved_output_format;
231 printf("\n");
233 show_progress();
236 static void handle_tail(struct object_array *commits, struct rev_info *revs)
238 struct commit *commit;
239 while (commits->nr) {
240 commit = (struct commit *)commits->objects[commits->nr - 1].item;
241 if (has_unshown_parent(commit))
242 return;
243 handle_commit(commit, revs);
244 commits->nr--;
248 static void handle_tag(const char *name, struct tag *tag)
250 unsigned long size;
251 enum object_type type;
252 char *buf;
253 const char *tagger, *tagger_end, *message;
254 size_t message_size = 0;
256 buf = read_sha1_file(tag->object.sha1, &type, &size);
257 if (!buf)
258 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
259 message = memmem(buf, size, "\n\n", 2);
260 if (message) {
261 message += 2;
262 message_size = strlen(message);
264 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
265 if (!tagger)
266 die ("No tagger for tag %s", sha1_to_hex(tag->object.sha1));
267 tagger++;
268 tagger_end = strchrnul(tagger, '\n');
270 /* handle signed tags */
271 if (message) {
272 const char *signature = strstr(message,
273 "\n-----BEGIN PGP SIGNATURE-----\n");
274 if (signature)
275 switch(signed_tag_mode) {
276 case ABORT:
277 die ("Encountered signed tag %s; use "
278 "--signed-tag=<mode> to handle it.",
279 sha1_to_hex(tag->object.sha1));
280 case WARN:
281 warning ("Exporting signed tag %s",
282 sha1_to_hex(tag->object.sha1));
283 /* fallthru */
284 case VERBATIM:
285 break;
286 case STRIP:
287 message_size = signature + 1 - message;
288 break;
292 if (!prefixcmp(name, "refs/tags/"))
293 name += 10;
294 printf("tag %s\nfrom :%d\n%.*s\ndata %d\n%.*s\n",
295 name, get_object_mark(tag->tagged),
296 (int)(tagger_end - tagger), tagger,
297 (int)message_size, (int)message_size, message ? message : "");
300 static void get_tags_and_duplicates(struct object_array *pending,
301 struct path_list *extra_refs)
303 struct tag *tag;
304 int i;
306 for (i = 0; i < pending->nr; i++) {
307 struct object_array_entry *e = pending->objects + i;
308 unsigned char sha1[20];
309 struct commit *commit = commit;
310 char *full_name;
312 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
313 continue;
315 switch (e->item->type) {
316 case OBJ_COMMIT:
317 commit = (struct commit *)e->item;
318 break;
319 case OBJ_TAG:
320 tag = (struct tag *)e->item;
321 while (tag && tag->object.type == OBJ_TAG) {
322 path_list_insert(full_name, extra_refs)->util = tag;
323 tag = (struct tag *)tag->tagged;
325 if (!tag)
326 die ("Tag %s points nowhere?", e->name);
327 switch(tag->object.type) {
328 case OBJ_COMMIT:
329 commit = (struct commit *)tag;
330 break;
331 case OBJ_BLOB:
332 handle_object(tag->object.sha1);
333 continue;
335 break;
336 default:
337 die ("Unexpected object of type %s",
338 typename(e->item->type));
340 if (commit->util)
341 /* more than one name for the same object */
342 path_list_insert(full_name, extra_refs)->util = commit;
343 else
344 commit->util = full_name;
348 static void handle_tags_and_duplicates(struct path_list *extra_refs)
350 struct commit *commit;
351 int i;
353 for (i = extra_refs->nr - 1; i >= 0; i--) {
354 const char *name = extra_refs->items[i].path;
355 struct object *object = extra_refs->items[i].util;
356 switch (object->type) {
357 case OBJ_TAG:
358 handle_tag(name, (struct tag *)object);
359 break;
360 case OBJ_COMMIT:
361 /* create refs pointing to already seen commits */
362 commit = (struct commit *)object;
363 printf("reset %s\nfrom :%d\n\n", name,
364 get_object_mark(&commit->object));
365 show_progress();
366 break;
371 static void export_marks(char *file)
373 unsigned int i;
374 uint32_t mark;
375 struct object_decoration *deco = idnums.hash;
376 FILE *f;
378 f = fopen(file, "w");
379 if (!f)
380 error("Unable to open marks file %s for writing", file);
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));
390 if (ferror(f) || fclose(f))
391 error("Unable to write marks file %s.", file);
394 static void import_marks(char * input_file)
396 char line[512];
397 FILE *f = fopen(input_file, "r");
398 if (!f)
399 die("cannot read %s: %s", input_file, strerror(errno));
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;
407 line_end = strchr(line, '\n');
408 if (line[0] != ':' || !line_end)
409 die("corrupt mark line: %s", line);
410 *line_end = 0;
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);
417 object = parse_object(sha1);
418 if (!object)
419 die ("Could not read blob %s", sha1_to_hex(sha1));
421 if (object->flags & SHOWN)
422 error("Object %s already has a mark", sha1);
424 mark_object(object, mark);
425 if (last_idnum < mark)
426 last_idnum = mark;
428 object->flags |= SHOWN;
430 fclose(f);
433 int cmd_fast_export(int argc, const char **argv, const char *prefix)
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()
453 /* we handle encodings */
454 git_config(git_default_config, NULL);
456 init_revisions(&revs, prefix);
457 argc = setup_revisions(argc, argv, &revs, NULL);
458 argc = parse_options(argc, argv, options, fast_export_usage, 0);
459 if (argc > 1)
460 usage_with_options (fast_export_usage, options);
462 if (import_filename)
463 import_marks(import_filename);
465 get_tags_and_duplicates(&revs.pending, &extra_refs);
467 if (prepare_revision_walk(&revs))
468 die("revision walk setup failed");
469 revs.diffopt.format_callback = show_filemodify;
470 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
471 while ((commit = get_revision(&revs))) {
472 if (has_unshown_parent(commit)) {
473 struct commit_list *parent = commit->parents;
474 add_object_array(&commit->object, NULL, &commits);
475 for (; parent; parent = parent->next)
476 if (!parent->item->util)
477 parent->item->util = commit->util;
479 else {
480 handle_commit(commit, &revs);
481 handle_tail(&commits, &revs);
485 handle_tags_and_duplicates(&extra_refs);
487 if (export_filename)
488 export_marks(export_filename);
490 return 0;