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