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