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