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