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