]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fast-export.c
sequencer.c: use commit-slab to associate todo items to commits
[thirdparty/git.git] / builtin / fast-export.c
CommitLineData
f2dc849e
JS
1/*
2 * "git fast-export" builtin command
3 *
4 * Copyright (C) 2007 Johannes E. Schindelin
5 */
6#include "builtin.h"
7#include "cache.h"
b2141fc1 8#include "config.h"
fb58c8d5 9#include "refs.h"
f2dc849e
JS
10#include "commit.h"
11#include "object.h"
12#include "tag.h"
13#include "diff.h"
14#include "diffcore.h"
15#include "log-tree.h"
16#include "revision.h"
17#include "decorate.h"
c455c87c 18#include "string-list.h"
f2dc849e
JS
19#include "utf8.h"
20#include "parse-options.h"
6280dfdc 21#include "quote.h"
03e9010c 22#include "remote.h"
a8722750 23#include "blob.h"
f2dc849e
JS
24
25static const char *fast_export_usage[] = {
3b787b96 26 N_("git fast-export [rev-list-opts]"),
f2dc849e
JS
27 NULL
28};
29
30static int progress;
cd16c59b 31static enum { ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = ABORT;
d7a10c31 32static enum { ERROR, DROP, REWRITE } tag_of_filtered_mode = ERROR;
4e46a8d6 33static int fake_missing_tagger;
82670a5c 34static int use_done_feature;
79559f27 35static int no_data;
7f40ab09 36static int full_tree;
1d844ee7 37static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
03e9010c
FC
38static struct refspec *refspecs;
39static int refspecs_nr;
a8722750 40static int anonymize;
f2dc849e
JS
41
42static int parse_opt_signed_tag_mode(const struct option *opt,
43 const char *arg, int unset)
44{
45 if (unset || !strcmp(arg, "abort"))
46 signed_tag_mode = ABORT;
ee4bc371
JS
47 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
48 signed_tag_mode = VERBATIM;
f2dc849e
JS
49 else if (!strcmp(arg, "warn"))
50 signed_tag_mode = WARN;
cd16c59b
JK
51 else if (!strcmp(arg, "warn-strip"))
52 signed_tag_mode = WARN_STRIP;
f2dc849e
JS
53 else if (!strcmp(arg, "strip"))
54 signed_tag_mode = STRIP;
55 else
04a74b6c 56 return error("Unknown signed-tags mode: %s", arg);
f2dc849e
JS
57 return 0;
58}
59
2d8ad469
EN
60static int parse_opt_tag_of_filtered_mode(const struct option *opt,
61 const char *arg, int unset)
62{
63 if (unset || !strcmp(arg, "abort"))
d7a10c31 64 tag_of_filtered_mode = ERROR;
2d8ad469
EN
65 else if (!strcmp(arg, "drop"))
66 tag_of_filtered_mode = DROP;
67 else if (!strcmp(arg, "rewrite"))
68 tag_of_filtered_mode = REWRITE;
69 else
70 return error("Unknown tag-of-filtered mode: %s", arg);
71 return 0;
72}
73
f2dc849e
JS
74static struct decoration idnums;
75static uint32_t last_idnum;
76
77static int has_unshown_parent(struct commit *commit)
78{
79 struct commit_list *parent;
80
81 for (parent = commit->parents; parent; parent = parent->next)
82 if (!(parent->item->object.flags & SHOWN) &&
83 !(parent->item->object.flags & UNINTERESTING))
84 return 1;
85 return 0;
86}
87
a8722750
JK
88struct anonymized_entry {
89 struct hashmap_entry hash;
90 const char *orig;
91 size_t orig_len;
92 const char *anon;
93 size_t anon_len;
94};
95
7663cdc8
SB
96static int anonymized_entry_cmp(const void *unused_cmp_data,
97 const void *va, const void *vb,
98 const void *unused_keydata)
a8722750
JK
99{
100 const struct anonymized_entry *a = va, *b = vb;
101 return a->orig_len != b->orig_len ||
102 memcmp(a->orig, b->orig, a->orig_len);
103}
104
105/*
106 * Basically keep a cache of X->Y so that we can repeatedly replace
107 * the same anonymized string with another. The actual generation
108 * is farmed out to the generate function.
109 */
110static const void *anonymize_mem(struct hashmap *map,
111 void *(*generate)(const void *, size_t *),
112 const void *orig, size_t *len)
113{
114 struct anonymized_entry key, *ret;
115
116 if (!map->cmpfn)
7663cdc8 117 hashmap_init(map, anonymized_entry_cmp, NULL, 0);
a8722750
JK
118
119 hashmap_entry_init(&key, memhash(orig, *len));
120 key.orig = orig;
121 key.orig_len = *len;
122 ret = hashmap_get(map, &key, NULL);
123
124 if (!ret) {
125 ret = xmalloc(sizeof(*ret));
126 hashmap_entry_init(&ret->hash, key.hash.hash);
127 ret->orig = xstrdup(orig);
128 ret->orig_len = *len;
129 ret->anon = generate(orig, len);
130 ret->anon_len = *len;
131 hashmap_put(map, ret);
132 }
133
134 *len = ret->anon_len;
135 return ret->anon;
136}
137
138/*
139 * We anonymize each component of a path individually,
140 * so that paths a/b and a/c will share a common root.
141 * The paths are cached via anonymize_mem so that repeated
142 * lookups for "a" will yield the same value.
143 */
144static void anonymize_path(struct strbuf *out, const char *path,
145 struct hashmap *map,
146 void *(*generate)(const void *, size_t *))
147{
148 while (*path) {
149 const char *end_of_component = strchrnul(path, '/');
150 size_t len = end_of_component - path;
151 const char *c = anonymize_mem(map, generate, path, &len);
152 strbuf_add(out, c, len);
153 path = end_of_component;
154 if (*path)
155 strbuf_addch(out, *path++);
156 }
157}
158
f2dc849e 159/* Since intptr_t is C99, we do not use it here */
df6a7ff7 160static inline uint32_t *mark_to_ptr(uint32_t mark)
f2dc849e 161{
df6a7ff7
PB
162 return ((uint32_t *)NULL) + mark;
163}
164
165static inline uint32_t ptr_to_mark(void * mark)
166{
167 return (uint32_t *)mark - (uint32_t *)NULL;
168}
169
170static inline void mark_object(struct object *object, uint32_t mark)
171{
172 add_decoration(&idnums, object, mark_to_ptr(mark));
173}
174
175static inline void mark_next_object(struct object *object)
176{
177 mark_object(object, ++last_idnum);
f2dc849e
JS
178}
179
180static int get_object_mark(struct object *object)
181{
182 void *decoration = lookup_decoration(&idnums, object);
183 if (!decoration)
184 return 0;
df6a7ff7 185 return ptr_to_mark(decoration);
f2dc849e
JS
186}
187
188static void show_progress(void)
189{
190 static int counter = 0;
191 if (!progress)
192 return;
193 if ((++counter % progress) == 0)
194 printf("progress %d objects\n", counter);
195}
196
a8722750
JK
197/*
198 * Ideally we would want some transformation of the blob data here
199 * that is unreversible, but would still be the same size and have
200 * the same data relationship to other blobs (so that we get the same
201 * delta and packing behavior as the original). But the first and last
202 * requirements there are probably mutually exclusive, so let's take
203 * the easy way out for now, and just generate arbitrary content.
204 *
205 * There's no need to cache this result with anonymize_mem, since
206 * we already handle blob content caching with marks.
207 */
208static char *anonymize_blob(unsigned long *size)
209{
210 static int counter;
211 struct strbuf out = STRBUF_INIT;
212 strbuf_addf(&out, "anonymous blob %d", counter++);
213 *size = out.len;
214 return strbuf_detach(&out, NULL);
215}
216
273f8ee8 217static void export_blob(const struct object_id *oid)
f2dc849e
JS
218{
219 unsigned long size;
220 enum object_type type;
221 char *buf;
222 struct object *object;
30b939c3 223 int eaten;
f2dc849e 224
79559f27
GI
225 if (no_data)
226 return;
227
273f8ee8 228 if (is_null_oid(oid))
f2dc849e
JS
229 return;
230
273f8ee8 231 object = lookup_object(oid->hash);
30b939c3 232 if (object && object->flags & SHOWN)
f2dc849e
JS
233 return;
234
a8722750
JK
235 if (anonymize) {
236 buf = anonymize_blob(&size);
3aca1fc6 237 object = (struct object *)lookup_blob(oid);
a8722750
JK
238 eaten = 0;
239 } else {
b4f5aca4 240 buf = read_object_file(oid, &type, &size);
a8722750 241 if (!buf)
273f8ee8 242 die ("Could not read blob %s", oid_to_hex(oid));
17e65451 243 if (check_object_signature(oid, buf, size, type_name(type)) < 0)
273f8ee8 244 die("sha1 mismatch in blob %s", oid_to_hex(oid));
c251c83d 245 object = parse_object_buffer(oid, type, size, buf, &eaten);
a8722750
JK
246 }
247
30b939c3 248 if (!object)
273f8ee8 249 die("Could not read blob %s", oid_to_hex(oid));
f2dc849e 250
df6a7ff7 251 mark_next_object(object);
f2dc849e 252
6e1c2344 253 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
b0fe0d72 254 if (size && fwrite(buf, size, 1, stdout) != 1)
273f8ee8 255 die_errno ("Could not write blob '%s'", oid_to_hex(oid));
f2dc849e
JS
256 printf("\n");
257
258 show_progress();
259
260 object->flags |= SHOWN;
30b939c3
JK
261 if (!eaten)
262 free(buf);
f2dc849e
JS
263}
264
060df624
EN
265static int depth_first(const void *a_, const void *b_)
266{
267 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
268 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
269 const char *name_a, *name_b;
270 int len_a, len_b, len;
271 int cmp;
272
273 name_a = a->one ? a->one->path : a->two->path;
274 name_b = b->one ? b->one->path : b->two->path;
275
276 len_a = strlen(name_a);
277 len_b = strlen(name_b);
278 len = (len_a < len_b) ? len_a : len_b;
279
280 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
281 cmp = memcmp(name_a, name_b, len);
282 if (cmp)
283 return cmp;
4ce6fb80
JS
284 cmp = len_b - len_a;
285 if (cmp)
286 return cmp;
287 /*
288 * Move 'R'ename entries last so that all references of the file
289 * appear in the output before it is renamed (e.g., when a file
290 * was copied and renamed in the same commit).
291 */
292 return (a->status == 'R') - (b->status == 'R');
060df624
EN
293}
294
a8722750 295static void print_path_1(const char *path)
6280dfdc
JK
296{
297 int need_quote = quote_c_style(path, NULL, NULL, 0);
298 if (need_quote)
299 quote_c_style(path, NULL, stdout, 0);
ff59f6da
JS
300 else if (strchr(path, ' '))
301 printf("\"%s\"", path);
6280dfdc
JK
302 else
303 printf("%s", path);
304}
305
a8722750
JK
306static void *anonymize_path_component(const void *path, size_t *len)
307{
308 static int counter;
309 struct strbuf out = STRBUF_INIT;
310 strbuf_addf(&out, "path%d", counter++);
311 return strbuf_detach(&out, len);
312}
313
314static void print_path(const char *path)
315{
316 if (!anonymize)
317 print_path_1(path);
318 else {
319 static struct hashmap paths;
320 static struct strbuf anon = STRBUF_INIT;
321
322 anonymize_path(&anon, path, &paths, anonymize_path_component);
323 print_path_1(anon.buf);
324 strbuf_reset(&anon);
325 }
326}
327
273f8ee8 328static void *generate_fake_oid(const void *old, size_t *len)
a8722750
JK
329{
330 static uint32_t counter = 1; /* avoid null sha1 */
273f8ee8 331 unsigned char *out = xcalloc(GIT_SHA1_RAWSZ, 1);
332 put_be32(out + GIT_SHA1_RAWSZ - 4, counter++);
a8722750
JK
333 return out;
334}
335
273f8ee8 336static const unsigned char *anonymize_sha1(const struct object_id *oid)
a8722750
JK
337{
338 static struct hashmap sha1s;
273f8ee8 339 size_t len = GIT_SHA1_RAWSZ;
340 return anonymize_mem(&sha1s, generate_fake_oid, oid, &len);
a8722750
JK
341}
342
f2dc849e
JS
343static void show_filemodify(struct diff_queue_struct *q,
344 struct diff_options *options, void *data)
345{
346 int i;
b3e8ca89 347 struct string_list *changed = data;
060df624
EN
348
349 /*
350 * Handle files below a directory first, in case they are all deleted
351 * and the directory changes to a file or symlink.
352 */
9ed0d8d6 353 QSORT(q->queue, q->nr, depth_first);
060df624 354
f2dc849e 355 for (i = 0; i < q->nr; i++) {
ae7c5dce 356 struct diff_filespec *ospec = q->queue[i]->one;
f2dc849e 357 struct diff_filespec *spec = q->queue[i]->two;
ae7c5dce
AG
358
359 switch (q->queue[i]->status) {
360 case DIFF_STATUS_DELETED:
6280dfdc
JK
361 printf("D ");
362 print_path(spec->path);
b3e8ca89 363 string_list_insert(changed, spec->path);
6280dfdc 364 putchar('\n');
ae7c5dce
AG
365 break;
366
367 case DIFF_STATUS_COPIED:
368 case DIFF_STATUS_RENAMED:
b3e8ca89
JT
369 /*
370 * If a change in the file corresponding to ospec->path
371 * has been observed, we cannot trust its contents
372 * because the diff is calculated based on the prior
373 * contents, not the current contents. So, declare a
374 * copy or rename only if there was no change observed.
375 */
376 if (!string_list_has_string(changed, ospec->path)) {
377 printf("%c ", q->queue[i]->status);
378 print_path(ospec->path);
379 putchar(' ');
380 print_path(spec->path);
381 string_list_insert(changed, spec->path);
382 putchar('\n');
383
384 if (!oidcmp(&ospec->oid, &spec->oid) &&
385 ospec->mode == spec->mode)
386 break;
387 }
ae7c5dce
AG
388 /* fallthrough */
389
390 case DIFF_STATUS_TYPE_CHANGED:
391 case DIFF_STATUS_MODIFIED:
392 case DIFF_STATUS_ADDED:
03db4525
AG
393 /*
394 * Links refer to objects in another repositories;
395 * output the SHA-1 verbatim.
396 */
79559f27 397 if (no_data || S_ISGITLINK(spec->mode))
6280dfdc 398 printf("M %06o %s ", spec->mode,
a8722750 399 sha1_to_hex(anonymize ?
273f8ee8 400 anonymize_sha1(&spec->oid) :
a0d12c44 401 spec->oid.hash));
03db4525 402 else {
a0d12c44 403 struct object *object = lookup_object(spec->oid.hash);
6280dfdc
JK
404 printf("M %06o :%d ", spec->mode,
405 get_object_mark(object));
03db4525 406 }
6280dfdc 407 print_path(spec->path);
b3e8ca89 408 string_list_insert(changed, spec->path);
6280dfdc 409 putchar('\n');
ae7c5dce
AG
410 break;
411
412 default:
413 die("Unexpected comparison status '%c' for %s, %s",
414 q->queue[i]->status,
415 ospec->path ? ospec->path : "none",
416 spec->path ? spec->path : "none");
f2dc849e
JS
417 }
418 }
419}
420
421static const char *find_encoding(const char *begin, const char *end)
422{
423 const char *needle = "\nencoding ";
424 char *bol, *eol;
425
426 bol = memmem(begin, end ? end - begin : strlen(begin),
427 needle, strlen(needle));
428 if (!bol)
429 return git_commit_encoding;
430 bol += strlen(needle);
431 eol = strchrnul(bol, '\n');
432 *eol = '\0';
433 return bol;
434}
435
a8722750
JK
436static void *anonymize_ref_component(const void *old, size_t *len)
437{
438 static int counter;
439 struct strbuf out = STRBUF_INIT;
440 strbuf_addf(&out, "ref%d", counter++);
441 return strbuf_detach(&out, len);
442}
443
444static const char *anonymize_refname(const char *refname)
445{
446 /*
447 * If any of these prefixes is found, we will leave it intact
448 * so that tags remain tags and so forth.
449 */
450 static const char *prefixes[] = {
451 "refs/heads/",
452 "refs/tags/",
453 "refs/remotes/",
454 "refs/"
455 };
456 static struct hashmap refs;
457 static struct strbuf anon = STRBUF_INIT;
458 int i;
459
460 /*
461 * We also leave "master" as a special case, since it does not reveal
462 * anything interesting.
463 */
464 if (!strcmp(refname, "refs/heads/master"))
465 return refname;
466
467 strbuf_reset(&anon);
468 for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
469 if (skip_prefix(refname, prefixes[i], &refname)) {
470 strbuf_addstr(&anon, prefixes[i]);
471 break;
472 }
473 }
474
475 anonymize_path(&anon, refname, &refs, anonymize_ref_component);
476 return anon.buf;
477}
478
479/*
480 * We do not even bother to cache commit messages, as they are unlikely
481 * to be repeated verbatim, and it is not that interesting when they are.
482 */
483static char *anonymize_commit_message(const char *old)
484{
485 static int counter;
486 return xstrfmt("subject %d\n\nbody\n", counter++);
487}
488
489static struct hashmap idents;
490static void *anonymize_ident(const void *old, size_t *len)
491{
492 static int counter;
493 struct strbuf out = STRBUF_INIT;
494 strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter);
495 counter++;
496 return strbuf_detach(&out, len);
497}
498
499/*
500 * Our strategy here is to anonymize the names and email addresses,
501 * but keep timestamps intact, as they influence things like traversal
502 * order (and by themselves should not be too revealing).
503 */
504static void anonymize_ident_line(const char **beg, const char **end)
505{
506 static struct strbuf buffers[] = { STRBUF_INIT, STRBUF_INIT };
507 static unsigned which_buffer;
508
509 struct strbuf *out;
510 struct ident_split split;
511 const char *end_of_header;
512
513 out = &buffers[which_buffer++];
514 which_buffer %= ARRAY_SIZE(buffers);
515 strbuf_reset(out);
516
517 /* skip "committer", "author", "tagger", etc */
518 end_of_header = strchr(*beg, ' ');
519 if (!end_of_header)
520 die("BUG: malformed line fed to anonymize_ident_line: %.*s",
521 (int)(*end - *beg), *beg);
522 end_of_header++;
523 strbuf_add(out, *beg, end_of_header - *beg);
524
525 if (!split_ident_line(&split, end_of_header, *end - end_of_header) &&
526 split.date_begin) {
527 const char *ident;
528 size_t len;
529
530 len = split.mail_end - split.name_begin;
531 ident = anonymize_mem(&idents, anonymize_ident,
532 split.name_begin, &len);
533 strbuf_add(out, ident, len);
534 strbuf_addch(out, ' ');
535 strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
536 } else {
537 strbuf_addstr(out, "Malformed Ident <malformed@example.com> 0 -0000");
538 }
539
540 *beg = out->buf;
541 *end = out->buf + out->len;
542}
543
b3e8ca89
JT
544static void handle_commit(struct commit *commit, struct rev_info *rev,
545 struct string_list *paths_of_changed_objects)
f2dc849e
JS
546{
547 int saved_output_format = rev->diffopt.output_format;
bc6b8fc1 548 const char *commit_buffer;
f2dc849e
JS
549 const char *author, *author_end, *committer, *committer_end;
550 const char *encoding, *message;
551 char *reencoded = NULL;
552 struct commit_list *p;
a8722750 553 const char *refname;
f2dc849e
JS
554 int i;
555
556 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
557
683ff884 558 parse_commit_or_die(commit);
8597ea3a 559 commit_buffer = get_commit_buffer(commit, NULL);
bc6b8fc1 560 author = strstr(commit_buffer, "\nauthor ");
f2dc849e
JS
561 if (!author)
562 die ("Could not find author in commit %s",
f2fd0760 563 oid_to_hex(&commit->object.oid));
f2dc849e
JS
564 author++;
565 author_end = strchrnul(author, '\n');
566 committer = strstr(author_end, "\ncommitter ");
567 if (!committer)
568 die ("Could not find committer in commit %s",
f2fd0760 569 oid_to_hex(&commit->object.oid));
f2dc849e
JS
570 committer++;
571 committer_end = strchrnul(committer, '\n');
572 message = strstr(committer_end, "\n\n");
573 encoding = find_encoding(committer_end, message);
574 if (message)
575 message += 2;
576
ebeec7db 577 if (commit->parents &&
4087a02e
EN
578 get_object_mark(&commit->parents->item->object) != 0 &&
579 !full_tree) {
683ff884 580 parse_commit_or_die(commit->parents->item);
66f414f8
BW
581 diff_tree_oid(&commit->parents->item->tree->object.oid,
582 &commit->tree->object.oid, "", &rev->diffopt);
f2dc849e
JS
583 }
584 else
7b8dea0c
BW
585 diff_root_tree_oid(&commit->tree->object.oid,
586 "", &rev->diffopt);
f2dc849e 587
03db4525 588 /* Export the referenced blobs, and remember the marks. */
f2dc849e 589 for (i = 0; i < diff_queued_diff.nr; i++)
03db4525 590 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
273f8ee8 591 export_blob(&diff_queued_diff.queue[i]->two->oid);
f2dc849e 592
a8722750
JK
593 refname = commit->util;
594 if (anonymize) {
595 refname = anonymize_refname(refname);
596 anonymize_ident_line(&committer, &committer_end);
597 anonymize_ident_line(&author, &author_end);
598 }
599
df6a7ff7 600 mark_next_object(&commit->object);
a8722750
JK
601 if (anonymize)
602 reencoded = anonymize_commit_message(message);
603 else if (!is_encoding_utf8(encoding))
f2dc849e 604 reencoded = reencode_string(message, "UTF-8", encoding);
d8933f01 605 if (!commit->parents)
a8722750 606 printf("reset %s\n", refname);
6e1c2344 607 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
a8722750 608 refname, last_idnum,
f2dc849e
JS
609 (int)(author_end - author), author,
610 (int)(committer_end - committer), committer,
611 (unsigned)(reencoded
612 ? strlen(reencoded) : message
613 ? strlen(message) : 0),
614 reencoded ? reencoded : message ? message : "");
8e0f7003 615 free(reencoded);
bc6b8fc1 616 unuse_commit_buffer(commit, commit_buffer);
f2dc849e
JS
617
618 for (i = 0, p = commit->parents; p; p = p->next) {
619 int mark = get_object_mark(&p->item->object);
620 if (!mark)
621 continue;
622 if (i == 0)
623 printf("from :%d\n", mark);
f2dc849e 624 else
5070b49e 625 printf("merge :%d\n", mark);
f2dc849e
JS
626 i++;
627 }
f2dc849e 628
4087a02e
EN
629 if (full_tree)
630 printf("deleteall\n");
f2dc849e 631 log_tree_diff_flush(rev);
b3e8ca89 632 string_list_clear(paths_of_changed_objects, 0);
f2dc849e
JS
633 rev->diffopt.output_format = saved_output_format;
634
635 printf("\n");
636
637 show_progress();
638}
639
a8722750
JK
640static void *anonymize_tag(const void *old, size_t *len)
641{
642 static int counter;
643 struct strbuf out = STRBUF_INIT;
644 strbuf_addf(&out, "tag message %d", counter++);
645 return strbuf_detach(&out, len);
646}
647
b3e8ca89
JT
648static void handle_tail(struct object_array *commits, struct rev_info *revs,
649 struct string_list *paths_of_changed_objects)
f2dc849e
JS
650{
651 struct commit *commit;
652 while (commits->nr) {
71992039 653 commit = (struct commit *)object_array_pop(commits);
be011bbe
654 if (has_unshown_parent(commit)) {
655 /* Queue again, to be handled later */
656 add_object_array(&commit->object, NULL, commits);
f2dc849e 657 return;
be011bbe 658 }
b3e8ca89 659 handle_commit(commit, revs, paths_of_changed_objects);
f2dc849e
JS
660 }
661}
662
663static void handle_tag(const char *name, struct tag *tag)
664{
665 unsigned long size;
666 enum object_type type;
667 char *buf;
668 const char *tagger, *tagger_end, *message;
669 size_t message_size = 0;
02c48cd6 670 struct object *tagged;
2d8ad469
EN
671 int tagged_mark;
672 struct commit *p;
02c48cd6 673
98e023de 674 /* Trees have no identifier in fast-export output, thus we have no way
02c48cd6
EN
675 * to output tags of trees, tags of tags of trees, etc. Simply omit
676 * such tags.
677 */
678 tagged = tag->tagged;
679 while (tagged->type == OBJ_TAG) {
680 tagged = ((struct tag *)tagged)->tagged;
681 }
682 if (tagged->type == OBJ_TREE) {
683 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
f2fd0760 684 oid_to_hex(&tag->object.oid));
02c48cd6
EN
685 return;
686 }
f2dc849e 687
b4f5aca4 688 buf = read_object_file(&tag->object.oid, &type, &size);
f2dc849e 689 if (!buf)
f2fd0760 690 die ("Could not read tag %s", oid_to_hex(&tag->object.oid));
f2dc849e
JS
691 message = memmem(buf, size, "\n\n", 2);
692 if (message) {
693 message += 2;
694 message_size = strlen(message);
695 }
696 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
4e46a8d6
JS
697 if (!tagger) {
698 if (fake_missing_tagger)
699 tagger = "tagger Unspecified Tagger "
700 "<unspecified-tagger> 0 +0000";
701 else
702 tagger = "";
703 tagger_end = tagger + strlen(tagger);
704 } else {
705 tagger++;
706 tagger_end = strchrnul(tagger, '\n');
a8722750
JK
707 if (anonymize)
708 anonymize_ident_line(&tagger, &tagger_end);
709 }
710
711 if (anonymize) {
712 name = anonymize_refname(name);
713 if (message) {
714 static struct hashmap tags;
715 message = anonymize_mem(&tags, anonymize_tag,
716 message, &message_size);
717 }
4e46a8d6 718 }
f2dc849e
JS
719
720 /* handle signed tags */
721 if (message) {
722 const char *signature = strstr(message,
723 "\n-----BEGIN PGP SIGNATURE-----\n");
724 if (signature)
725 switch(signed_tag_mode) {
726 case ABORT:
727 die ("Encountered signed tag %s; use "
04a74b6c 728 "--signed-tags=<mode> to handle it.",
f2fd0760 729 oid_to_hex(&tag->object.oid));
f2dc849e
JS
730 case WARN:
731 warning ("Exporting signed tag %s",
f2fd0760 732 oid_to_hex(&tag->object.oid));
f2dc849e 733 /* fallthru */
ee4bc371 734 case VERBATIM:
f2dc849e 735 break;
cd16c59b
JK
736 case WARN_STRIP:
737 warning ("Stripping signature from tag %s",
f2fd0760 738 oid_to_hex(&tag->object.oid));
cd16c59b 739 /* fallthru */
f2dc849e
JS
740 case STRIP:
741 message_size = signature + 1 - message;
742 break;
743 }
744 }
745
2d8ad469
EN
746 /* handle tag->tagged having been filtered out due to paths specified */
747 tagged = tag->tagged;
748 tagged_mark = get_object_mark(tagged);
749 if (!tagged_mark) {
750 switch(tag_of_filtered_mode) {
751 case ABORT:
752 die ("Tag %s tags unexported object; use "
753 "--tag-of-filtered-object=<mode> to handle it.",
f2fd0760 754 oid_to_hex(&tag->object.oid));
2d8ad469
EN
755 case DROP:
756 /* Ignore this tag altogether */
1efb1e9a 757 free(buf);
2d8ad469
EN
758 return;
759 case REWRITE:
760 if (tagged->type != OBJ_COMMIT) {
761 die ("Tag %s tags unexported %s!",
f2fd0760 762 oid_to_hex(&tag->object.oid),
debca9d2 763 type_name(tagged->type));
2d8ad469
EN
764 }
765 p = (struct commit *)tagged;
766 for (;;) {
767 if (p->parents && p->parents->next)
768 break;
769 if (p->object.flags & UNINTERESTING)
770 break;
771 if (!(p->object.flags & TREESAME))
772 break;
773 if (!p->parents)
774 die ("Can't find replacement commit for tag %s\n",
f2fd0760 775 oid_to_hex(&tag->object.oid));
2d8ad469
EN
776 p = p->parents->item;
777 }
778 tagged_mark = get_object_mark(&p->object);
779 }
780 }
781
59556548 782 if (starts_with(name, "refs/tags/"))
f2dc849e 783 name += 10;
4e46a8d6 784 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
2d8ad469 785 name, tagged_mark,
f2dc849e 786 (int)(tagger_end - tagger), tagger,
4e46a8d6 787 tagger == tagger_end ? "" : "\n",
f2dc849e 788 (int)message_size, (int)message_size, message ? message : "");
1efb1e9a 789 free(buf);
f2dc849e
JS
790}
791
3e9b9cb1
FC
792static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
793{
794 switch (e->item->type) {
795 case OBJ_COMMIT:
796 return (struct commit *)e->item;
797 case OBJ_TAG: {
798 struct tag *tag = (struct tag *)e->item;
799
800 /* handle nested tags */
801 while (tag && tag->object.type == OBJ_TAG) {
c251c83d 802 parse_object(&tag->object.oid);
3e9b9cb1
FC
803 string_list_append(&extra_refs, full_name)->util = tag;
804 tag = (struct tag *)tag->tagged;
805 }
806 if (!tag)
807 die("Tag %s points nowhere?", e->name);
808 return (struct commit *)tag;
809 break;
810 }
811 default:
812 return NULL;
813 }
814}
815
1d844ee7 816static void get_tags_and_duplicates(struct rev_cmdline_info *info)
f2dc849e 817{
f2dc849e
JS
818 int i;
819
49266e8a
FC
820 for (i = 0; i < info->nr; i++) {
821 struct rev_cmdline_entry *e = info->rev + i;
273f8ee8 822 struct object_id oid;
2d242de4 823 struct commit *commit;
f2dc849e
JS
824 char *full_name;
825
49266e8a
FC
826 if (e->flags & UNINTERESTING)
827 continue;
828
cca5fa64 829 if (dwim_ref(e->name, strlen(e->name), &oid, &full_name) != 1)
f2dc849e
JS
830 continue;
831
03e9010c
FC
832 if (refspecs) {
833 char *private;
834 private = apply_refspecs(refspecs, refspecs_nr, full_name);
835 if (private) {
836 free(full_name);
837 full_name = private;
838 }
839 }
840
3e9b9cb1
FC
841 commit = get_commit(e, full_name);
842 if (!commit) {
2d07f6d4
EFL
843 warning("%s: Unexpected object of type %s, skipping.",
844 e->name,
debca9d2 845 type_name(e->item->type));
2d07f6d4 846 continue;
f2dc849e 847 }
f28e7c90 848
3e9b9cb1
FC
849 switch(commit->object.type) {
850 case OBJ_COMMIT:
851 break;
852 case OBJ_BLOB:
273f8ee8 853 export_blob(&commit->object.oid);
3e9b9cb1
FC
854 continue;
855 default: /* OBJ_TAG (nested tags) is already handled */
856 warning("Tag points to object of unexpected type %s, skipping.",
debca9d2 857 type_name(commit->object.type));
3e9b9cb1
FC
858 continue;
859 }
860
f28e7c90
FC
861 /*
862 * This ref will not be updated through a commit, lets make
863 * sure it gets properly updated eventually.
864 */
865 if (commit->util || commit->object.flags & SHOWN)
1d844ee7 866 string_list_append(&extra_refs, full_name)->util = commit;
f28e7c90 867 if (!commit->util)
f2dc849e
JS
868 commit->util = full_name;
869 }
870}
871
1d844ee7 872static void handle_tags_and_duplicates(void)
f2dc849e
JS
873{
874 struct commit *commit;
875 int i;
876
1d844ee7
FC
877 for (i = extra_refs.nr - 1; i >= 0; i--) {
878 const char *name = extra_refs.items[i].string;
879 struct object *object = extra_refs.items[i].util;
f2dc849e
JS
880 switch (object->type) {
881 case OBJ_TAG:
882 handle_tag(name, (struct tag *)object);
883 break;
884 case OBJ_COMMIT:
a8722750
JK
885 if (anonymize)
886 name = anonymize_refname(name);
f2dc849e
JS
887 /* create refs pointing to already seen commits */
888 commit = (struct commit *)object;
889 printf("reset %s\nfrom :%d\n\n", name,
890 get_object_mark(&commit->object));
891 show_progress();
892 break;
893 }
894 }
895}
896
df6a7ff7
PB
897static void export_marks(char *file)
898{
899 unsigned int i;
900 uint32_t mark;
ddd3e312 901 struct decoration_entry *deco = idnums.entries;
df6a7ff7 902 FILE *f;
96d69b55 903 int e = 0;
df6a7ff7 904
ea56518d 905 f = fopen_for_writing(file);
df6a7ff7 906 if (!f)
bb6ad28c 907 die_errno("Unable to open marks file %s for writing.", file);
df6a7ff7 908
69913575
JH
909 for (i = 0; i < idnums.size; i++) {
910 if (deco->base && deco->base->type == 1) {
df6a7ff7 911 mark = ptr_to_mark(deco->decoration);
96d69b55 912 if (fprintf(f, ":%"PRIu32" %s\n", mark,
f2fd0760 913 oid_to_hex(&deco->base->oid)) < 0) {
96d69b55
MA
914 e = 1;
915 break;
916 }
df6a7ff7 917 }
69913575 918 deco++;
df6a7ff7
PB
919 }
920
96d69b55
MA
921 e |= ferror(f);
922 e |= fclose(f);
923 if (e)
df6a7ff7
PB
924 error("Unable to write marks file %s.", file);
925}
926
69913575 927static void import_marks(char *input_file)
df6a7ff7
PB
928{
929 char line[512];
23a9e071 930 FILE *f = xfopen(input_file, "r");
df6a7ff7
PB
931
932 while (fgets(line, sizeof(line), f)) {
933 uint32_t mark;
934 char *line_end, *mark_end;
273f8ee8 935 struct object_id oid;
df6a7ff7 936 struct object *object;
47bd9bf8 937 struct commit *commit;
e6812cfa 938 enum object_type type;
df6a7ff7
PB
939
940 line_end = strchr(line, '\n');
941 if (line[0] != ':' || !line_end)
942 die("corrupt mark line: %s", line);
69913575 943 *line_end = '\0';
df6a7ff7
PB
944
945 mark = strtoumax(line + 1, &mark_end, 10);
946 if (!mark || mark_end == line + 1
273f8ee8 947 || *mark_end != ' ' || get_oid_hex(mark_end + 1, &oid))
df6a7ff7
PB
948 die("corrupt mark line: %s", line);
949
c4458ecd
AP
950 if (last_idnum < mark)
951 last_idnum = mark;
952
abef9020 953 type = oid_object_info(&oid, NULL);
e6812cfa 954 if (type < 0)
273f8ee8 955 die("object not found: %s", oid_to_hex(&oid));
e6812cfa
FC
956
957 if (type != OBJ_COMMIT)
958 /* only commits */
c4458ecd 959 continue;
df6a7ff7 960
bc83266a 961 commit = lookup_commit(&oid);
47bd9bf8 962 if (!commit)
273f8ee8 963 die("not a commit? can't happen: %s", oid_to_hex(&oid));
47bd9bf8
FC
964
965 object = &commit->object;
e6812cfa 966
df6a7ff7 967 if (object->flags & SHOWN)
273f8ee8 968 error("Object %s already has a mark", oid_to_hex(&oid));
df6a7ff7
PB
969
970 mark_object(object, mark);
df6a7ff7
PB
971
972 object->flags |= SHOWN;
973 }
974 fclose(f);
975}
976
60ed2643
FC
977static void handle_deletes(void)
978{
979 int i;
980 for (i = 0; i < refspecs_nr; i++) {
981 struct refspec *refspec = &refspecs[i];
982 if (*refspec->src)
983 continue;
984
985 printf("reset %s\nfrom %s\n\n",
986 refspec->dst, sha1_to_hex(null_sha1));
987 }
988}
989
f2dc849e
JS
990int cmd_fast_export(int argc, const char **argv, const char *prefix)
991{
992 struct rev_info revs;
3cd47459 993 struct object_array commits = OBJECT_ARRAY_INIT;
f2dc849e 994 struct commit *commit;
df6a7ff7 995 char *export_filename = NULL, *import_filename = NULL;
c4458ecd 996 uint32_t lastimportid;
03e9010c 997 struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
b3e8ca89 998 struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
f2dc849e
JS
999 struct option options[] = {
1000 OPT_INTEGER(0, "progress", &progress,
3b787b96
NTND
1001 N_("show progress after <n> objects")),
1002 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
1003 N_("select handling of signed tags"),
f2dc849e 1004 parse_opt_signed_tag_mode),
3b787b96
NTND
1005 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
1006 N_("select handling of tags that tag filtered objects"),
2d8ad469 1007 parse_opt_tag_of_filtered_mode),
3b787b96
NTND
1008 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
1009 N_("Dump marks to this file")),
1010 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
1011 N_("Import marks from this file")),
d5d09d47
SB
1012 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
1013 N_("Fake a tagger when tags lack one")),
1014 OPT_BOOL(0, "full-tree", &full_tree,
1015 N_("Output full tree for each commit")),
1016 OPT_BOOL(0, "use-done-feature", &use_done_feature,
3b787b96
NTND
1017 N_("Use the done feature to terminate the stream")),
1018 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
03e9010c
FC
1019 OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"),
1020 N_("Apply refspec to exported refs")),
a8722750 1021 OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")),
f2dc849e
JS
1022 OPT_END()
1023 };
1024
dcfdbdf0
MV
1025 if (argc == 1)
1026 usage_with_options (fast_export_usage, options);
1027
f2dc849e 1028 /* we handle encodings */
ef90d6d4 1029 git_config(git_default_config, NULL);
f2dc849e
JS
1030
1031 init_revisions(&revs, prefix);
668f3aa7 1032 revs.topo_order = 1;
2374502c 1033 revs.show_source = 1;
32164131 1034 revs.rewrite_parents = 1;
8b2f86a7
FC
1035 argc = parse_options(argc, argv, prefix, options, fast_export_usage,
1036 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
f2dc849e 1037 argc = setup_revisions(argc, argv, &revs, NULL);
f2dc849e
JS
1038 if (argc > 1)
1039 usage_with_options (fast_export_usage, options);
1040
03e9010c
FC
1041 if (refspecs_list.nr) {
1042 const char **refspecs_str;
1043 int i;
1044
b32fa95f 1045 ALLOC_ARRAY(refspecs_str, refspecs_list.nr);
03e9010c
FC
1046 for (i = 0; i < refspecs_list.nr; i++)
1047 refspecs_str[i] = refspecs_list.items[i].string;
1048
1049 refspecs_nr = refspecs_list.nr;
1050 refspecs = parse_fetch_refspec(refspecs_nr, refspecs_str);
1051
1052 string_list_clear(&refspecs_list, 1);
1053 free(refspecs_str);
1054 }
1055
82670a5c
SR
1056 if (use_done_feature)
1057 printf("feature done\n");
1058
df6a7ff7
PB
1059 if (import_filename)
1060 import_marks(import_filename);
c4458ecd 1061 lastimportid = last_idnum;
df6a7ff7 1062
afe069d1 1063 if (import_filename && revs.prune_data.nr)
4087a02e
EN
1064 full_tree = 1;
1065
1d844ee7 1066 get_tags_and_duplicates(&revs.cmdline);
f2dc849e 1067
3d51e1b5
MK
1068 if (prepare_revision_walk(&revs))
1069 die("revision walk setup failed");
f2dc849e 1070 revs.diffopt.format_callback = show_filemodify;
b3e8ca89 1071 revs.diffopt.format_callback_data = &paths_of_changed_objects;
0d1e0e78 1072 revs.diffopt.flags.recursive = 1;
f2dc849e
JS
1073 while ((commit = get_revision(&revs))) {
1074 if (has_unshown_parent(commit)) {
f2dc849e 1075 add_object_array(&commit->object, NULL, &commits);
f2dc849e
JS
1076 }
1077 else {
b3e8ca89
JT
1078 handle_commit(commit, &revs, &paths_of_changed_objects);
1079 handle_tail(&commits, &revs, &paths_of_changed_objects);
f2dc849e
JS
1080 }
1081 }
1082
1d844ee7 1083 handle_tags_and_duplicates();
60ed2643 1084 handle_deletes();
f2dc849e 1085
c4458ecd 1086 if (export_filename && lastimportid != last_idnum)
df6a7ff7
PB
1087 export_marks(export_filename);
1088
82670a5c
SR
1089 if (use_done_feature)
1090 printf("done\n");
1091
03e9010c
FC
1092 free_refspec(refspecs_nr, refspecs);
1093
f2dc849e
JS
1094 return 0;
1095}