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