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