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