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