Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/fast-import.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / fast-import.c` 4347 lines ยท 119482 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "builtin.h" 5 #include "abspath.h" 6 #include "environment.h" 7 #include "gettext.h" 8 #include "hex.h" 9 #include "config.h" 10 #include "lockfile.h" 11 #include "object.h" 12 #include "blob.h" 13 #include "tree.h" 14 #include "commit.h" 15 #include "delta.h" 16 #include "pack.h" 17 #include "path.h" 18 #include "read-cache-ll.h" 19 #include "refs.h" 20 #include "csum-file.h" 21 #include "quote.h" 22 #include "dir.h" 23 #include "run-command.h" 24 #include "packfile.h" 25 #include "object-file.h" 26 #include "object-name.h" 27 #include "odb.h" 28 #include "mem-pool.h" 29 #include "commit-reach.h" 30 #include "khash.h" 31 #include "date.h" 32 #include "gpg-interface.h" 33 #include "parse-options.h" 34 35 #define PACK_ID_BITS 16 36 #define MAX_PACK_ID ((1<rawsz * 3) 51 52 struct object_entry { 53 struct pack_idx_entry idx; 54 struct hashmap_entry ent; 55 uint32_t type : TYPE_BITS, 56 pack_id : PACK_ID_BITS, 57 depth : DEPTH_BITS; 58 }; 59 60 static int object_entry_hashcmp(const void *map_data UNUSED, 61 const struct hashmap_entry *eptr, 62 const struct hashmap_entry *entry_or_key, 63 const void *keydata) 64 { 65 const struct object_id *oid = keydata; 66 const struct object_entry *e1, *e2; 67 68 e1 = container_of(eptr, const struct object_entry, ent); 69 if (oid) 70 return oidcmp(&e1->idx.oid, oid); 71 72 e2 = container_of(entry_or_key, const struct object_entry, ent); 73 return oidcmp(&e1->idx.oid, &e2->idx.oid); 74 } 75 76 struct object_entry_pool { 77 struct object_entry_pool *next_pool; 78 struct object_entry *next_free; 79 struct object_entry *end; 80 struct object_entry entries[FLEX_ARRAY]; /* more */ 81 }; 82 83 struct mark_set { 84 union { 85 struct object_id *oids[1024]; 86 struct object_entry *marked[1024]; 87 struct mark_set *sets[1024]; 88 } data; 89 unsigned int shift; 90 }; 91 92 struct last_object { 93 struct strbuf data; 94 off_t offset; 95 unsigned int depth; 96 unsigned no_swap : 1; 97 }; 98 99 struct atom_str { 100 struct atom_str *next_atom; 101 unsigned short str_len; 102 char str_dat[FLEX_ARRAY]; /* more */ 103 }; 104 105 struct tree_content; 106 struct tree_entry { 107 struct tree_content *tree; 108 struct atom_str *name; 109 struct tree_entry_ms { 110 uint16_t mode; 111 struct object_id oid; 112 } versions[2]; 113 }; 114 115 struct tree_content { 116 unsigned int entry_capacity; /* must match avail_tree_content */ 117 unsigned int entry_count; 118 unsigned int delta_depth; 119 struct tree_entry *entries[FLEX_ARRAY]; /* more */ 120 }; 121 122 struct avail_tree_content { 123 unsigned int entry_capacity; /* must match tree_content */ 124 struct avail_tree_content *next_avail; 125 }; 126 127 struct branch { 128 struct branch *table_next_branch; 129 struct branch *active_next_branch; 130 const char *name; 131 struct tree_entry branch_tree; 132 uintmax_t last_commit; 133 uintmax_t num_notes; 134 unsigned active : 1; 135 unsigned delete : 1; 136 unsigned pack_id : PACK_ID_BITS; 137 struct object_id oid; 138 }; 139 140 struct tag { 141 struct tag *next_tag; 142 const char *name; 143 unsigned int pack_id; 144 struct object_id oid; 145 }; 146 147 struct hash_list { 148 struct hash_list *next; 149 struct object_id oid; 150 }; 151 152 typedef enum { 153 WHENSPEC_RAW = 1, 154 WHENSPEC_RAW_PERMISSIVE, 155 WHENSPEC_RFC2822, 156 WHENSPEC_NOW 157 } whenspec_type; 158 159 struct recent_command { 160 struct recent_command *prev; 161 struct recent_command *next; 162 char *buf; 163 }; 164 165 typedef void (*mark_set_inserter_t)(struct mark_set **s, struct object_id *oid, uintmax_t mark); 166 typedef void (*each_mark_fn_t)(uintmax_t mark, void *obj, void *cbp); 167 168 /* Configured limits on output */ 169 static unsigned long max_depth = 50; 170 static off_t max_packsize; 171 static int unpack_limit = 100; 172 static int force_update; 173 174 /* Stats and misc. counters */ 175 static uintmax_t alloc_count; 176 static uintmax_t marks_set_count; 177 static uintmax_t object_count_by_type[1 << TYPE_BITS]; 178 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS]; 179 static uintmax_t delta_count_by_type[1 << TYPE_BITS]; 180 static uintmax_t delta_count_attempts_by_type[1 << TYPE_BITS]; 181 static unsigned long object_count; 182 static unsigned long branch_count; 183 static unsigned long branch_load_count; 184 static int failure; 185 static FILE *pack_edges; 186 static int show_stats = 1; 187 static int quiet; 188 static enum sign_mode signed_tag_mode = SIGN_VERBATIM; 189 static enum sign_mode signed_commit_mode = SIGN_VERBATIM; 190 static const char *signed_commit_keyid; 191 static const char *signed_tag_keyid; 192 193 /* Memory pools */ 194 static struct mem_pool fi_mem_pool = { 195 .block_alloc = 2*1024*1024 - sizeof(struct mp_block), 196 }; 197 198 /* Atom management */ 199 static unsigned int atom_table_sz = 4451; 200 static unsigned int atom_cnt; 201 static struct atom_str **atom_table; 202 203 /* The .pack file being generated */ 204 static struct pack_idx_option pack_idx_opts; 205 static unsigned int pack_id; 206 static struct hashfile *pack_file; 207 static struct packed_git *pack_data; 208 static struct packed_git **all_packs; 209 static off_t pack_size; 210 211 /* Table of objects we've written. */ 212 static unsigned int object_entry_alloc = 5000; 213 static struct object_entry_pool *blocks; 214 static struct hashmap object_table; 215 static struct mark_set *marks; 216 static char *export_marks_file; 217 static char *import_marks_file; 218 static int import_marks_file_from_stream; 219 static int import_marks_file_ignore_missing; 220 static int import_marks_file_done; 221 static int relative_marks_paths; 222 223 /* Our last blob */ 224 static struct last_object last_blob = { 225 .data = STRBUF_INIT, 226 }; 227 228 /* Tree management */ 229 static unsigned int tree_entry_alloc = 1000; 230 static void *avail_tree_entry; 231 static unsigned int avail_tree_table_sz = 100; 232 static struct avail_tree_content **avail_tree_table; 233 static size_t tree_entry_allocd; 234 static struct strbuf old_tree = STRBUF_INIT; 235 static struct strbuf new_tree = STRBUF_INIT; 236 237 /* Branch data */ 238 static unsigned long max_active_branches = 5; 239 static unsigned long cur_active_branches; 240 static unsigned long branch_table_sz = 1039; 241 static struct branch **branch_table; 242 static struct branch *active_branches; 243 244 /* Tag data */ 245 static struct tag *first_tag; 246 static struct tag *last_tag; 247 248 /* Input stream parsing */ 249 static whenspec_type whenspec = WHENSPEC_RAW; 250 static struct strbuf command_buf = STRBUF_INIT; 251 static int unread_command_buf; 252 static struct recent_command cmd_hist = { 253 .prev = &cmd_hist, 254 .next = &cmd_hist, 255 }; 256 static struct recent_command *cmd_tail = &cmd_hist; 257 static struct recent_command *rc_free; 258 static unsigned int cmd_save = 100; 259 static uintmax_t next_mark; 260 static struct strbuf new_data = STRBUF_INIT; 261 static int require_explicit_termination; 262 263 /* Signal handling */ 264 static volatile sig_atomic_t checkpoint_requested; 265 266 /* Submodule marks */ 267 static struct string_list sub_marks_from = STRING_LIST_INIT_DUP; 268 static struct string_list sub_marks_to = STRING_LIST_INIT_DUP; 269 static kh_oid_map_t *sub_oid_map; 270 271 /* Where to write output of cat-blob commands */ 272 static int cat_blob_fd = STDOUT_FILENO; 273 274 /* Command state */ 275 struct fast_import_state { 276 int argc; 277 const char **argv; 278 const char *prefix; 279 int seen_data_command; 280 int allow_unsafe_features; 281 struct option *option; 282 }; 283 284 static void fast_import_state_init(struct fast_import_state *state, 285 int argc, const char **argv, 286 const char *prefix, struct option *option) 287 { 288 memset(state, 0, sizeof(*state)); 289 state->argc = argc; 290 state->argv = argv; 291 state->prefix = prefix; 292 state->option = option; 293 } 294 295 static struct object_entry *dereference(struct object_entry *oe, 296 struct object_id *oid); 297 298 static void parse_argv(struct fast_import_state *state); 299 static void parse_get_mark(struct fast_import_state *state, const char *p); 300 static void parse_cat_blob(struct fast_import_state *state, const char *p); 301 static void parse_ls(struct fast_import_state *state, 302 const char *p, 303 struct branch *b); 304 305 static void for_each_mark(struct mark_set *m, uintmax_t base, each_mark_fn_t callback, void *p) 306 { 307 uintmax_t k; 308 if (m->shift) { 309 for (k = 0; k < 1024; k++) { 310 if (m->data.sets[k]) 311 for_each_mark(m->data.sets[k], base + (k << m->shift), callback, p); 312 } 313 } else { 314 for (k = 0; k < 1024; k++) { 315 if (m->data.marked[k]) 316 callback(base + k, m->data.marked[k], p); 317 } 318 } 319 } 320 321 static void dump_marks_fn(uintmax_t mark, void *object, void *cbp) { 322 struct object_entry *e = object; 323 FILE *f = cbp; 324 325 fprintf(f, ":%" PRIuMAX " %s\n", mark, oid_to_hex(&e->idx.oid)); 326 } 327 328 static void write_branch_report(FILE *rpt, struct branch *b) 329 { 330 fprintf(rpt, "%s:\n", b->name); 331 332 fprintf(rpt, " status :"); 333 if (b->active) 334 fputs(" active", rpt); 335 if (b->branch_tree.tree) 336 fputs(" loaded", rpt); 337 if (is_null_oid(&b->branch_tree.versions[1].oid)) 338 fputs(" dirty", rpt); 339 fputc('\n', rpt); 340 341 fprintf(rpt, " tip commit : %s\n", oid_to_hex(&b->oid)); 342 fprintf(rpt, " old tree : %s\n", 343 oid_to_hex(&b->branch_tree.versions[0].oid)); 344 fprintf(rpt, " cur tree : %s\n", 345 oid_to_hex(&b->branch_tree.versions[1].oid)); 346 fprintf(rpt, " commit clock: %" PRIuMAX "\n", b->last_commit); 347 348 fputs(" last pack : ", rpt); 349 if (b->pack_id < MAX_PACK_ID) 350 fprintf(rpt, "%u", b->pack_id); 351 fputc('\n', rpt); 352 353 fputc('\n', rpt); 354 } 355 356 static void write_crash_report(const char *err) 357 { 358 char *loc = repo_git_path(the_repository, "fast_import_crash_%"PRIuMAX, (uintmax_t) getpid()); 359 FILE *rpt = fopen(loc, "w"); 360 struct branch *b; 361 unsigned long lu; 362 struct recent_command *rc; 363 364 if (!rpt) { 365 error_errno(_("can't write crash report %s"), loc); 366 free(loc); 367 return; 368 } 369 370 fprintf(stderr, _("fast-import: dumping crash report to %s\n"), loc); 371 372 fprintf(rpt, "fast-import crash report:\n"); 373 fprintf(rpt, " fast-import process: %"PRIuMAX"\n", (uintmax_t) getpid()); 374 fprintf(rpt, " parent process : %"PRIuMAX"\n", (uintmax_t) getppid()); 375 fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_MODE(ISO8601))); 376 fputc('\n', rpt); 377 378 fputs("fatal: ", rpt); 379 fputs(err, rpt); 380 fputc('\n', rpt); 381 382 fputc('\n', rpt); 383 fputs("Most Recent Commands Before Crash\n", rpt); 384 fputs("---------------------------------\n", rpt); 385 for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) { 386 if (rc->next == &cmd_hist) 387 fputs("* ", rpt); 388 else 389 fputs(" ", rpt); 390 fputs(rc->buf, rpt); 391 fputc('\n', rpt); 392 } 393 394 fputc('\n', rpt); 395 fputs("Active Branch LRU\n", rpt); 396 fputs("-----------------\n", rpt); 397 fprintf(rpt, " active_branches = %lu cur, %lu max\n", 398 cur_active_branches, 399 max_active_branches); 400 fputc('\n', rpt); 401 fputs(" pos clock name\n", rpt); 402 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt); 403 for (b = active_branches, lu = 0; b; b = b->active_next_branch) 404 fprintf(rpt, " %2lu) %6" PRIuMAX" %s\n", 405 ++lu, b->last_commit, b->name); 406 407 fputc('\n', rpt); 408 fputs("Inactive Branches\n", rpt); 409 fputs("-----------------\n", rpt); 410 for (lu = 0; lu < branch_table_sz; lu++) { 411 for (b = branch_table[lu]; b; b = b->table_next_branch) 412 write_branch_report(rpt, b); 413 } 414 415 if (first_tag) { 416 struct tag *tg; 417 fputc('\n', rpt); 418 fputs("Annotated Tags\n", rpt); 419 fputs("--------------\n", rpt); 420 for (tg = first_tag; tg; tg = tg->next_tag) { 421 fputs(oid_to_hex(&tg->oid), rpt); 422 fputc(' ', rpt); 423 fputs(tg->name, rpt); 424 fputc('\n', rpt); 425 } 426 } 427 428 fputc('\n', rpt); 429 fputs("Marks\n", rpt); 430 fputs("-----\n", rpt); 431 if (export_marks_file) 432 fprintf(rpt, " exported to %s\n", export_marks_file); 433 else 434 for_each_mark(marks, 0, dump_marks_fn, rpt); 435 436 fputc('\n', rpt); 437 fputs("-------------------\n", rpt); 438 fputs("END OF CRASH REPORT\n", rpt); 439 fclose(rpt); 440 free(loc); 441 } 442 443 static void end_packfile(void); 444 static void unkeep_all_packs(void); 445 static void dump_marks(void); 446 447 static NORETURN void die_nicely(const char *err, va_list params) 448 { 449 va_list cp; 450 static int zombie; 451 report_fn die_message_fn = get_die_message_routine(); 452 453 va_copy(cp, params); 454 die_message_fn(err, params); 455 456 if (!zombie) { 457 char message[2 * PATH_MAX]; 458 459 zombie = 1; 460 vsnprintf(message, sizeof(message), err, cp); 461 write_crash_report(message); 462 end_packfile(); 463 unkeep_all_packs(); 464 dump_marks(); 465 } 466 exit(128); 467 } 468 469 #ifndef SIGUSR1 /* Windows, for example */ 470 471 static void set_checkpoint_signal(void) 472 { 473 } 474 475 #else 476 477 static void checkpoint_signal(int signo UNUSED) 478 { 479 checkpoint_requested = 1; 480 } 481 482 static void set_checkpoint_signal(void) 483 { 484 struct sigaction sa; 485 486 memset(&sa, 0, sizeof(sa)); 487 sa.sa_handler = checkpoint_signal; 488 sigemptyset(&sa.sa_mask); 489 sa.sa_flags = SA_RESTART; 490 sigaction(SIGUSR1, &sa, NULL); 491 } 492 493 #endif 494 495 static void alloc_objects(unsigned int cnt) 496 { 497 struct object_entry_pool *b; 498 499 b = xmalloc(sizeof(struct object_entry_pool) 500 + cnt * sizeof(struct object_entry)); 501 b->next_pool = blocks; 502 b->next_free = b->entries; 503 b->end = b->entries + cnt; 504 blocks = b; 505 alloc_count += cnt; 506 } 507 508 static struct object_entry *new_object(struct object_id *oid) 509 { 510 struct object_entry *e; 511 512 if (blocks->next_free == blocks->end) 513 alloc_objects(object_entry_alloc); 514 515 e = blocks->next_free++; 516 oidcpy(&e->idx.oid, oid); 517 return e; 518 } 519 520 static struct object_entry *find_object(struct object_id *oid) 521 { 522 return hashmap_get_entry_from_hash(&object_table, oidhash(oid), oid, 523 struct object_entry, ent); 524 } 525 526 static struct object_entry *insert_object(struct object_id *oid) 527 { 528 struct object_entry *e; 529 unsigned int hash = oidhash(oid); 530 531 e = hashmap_get_entry_from_hash(&object_table, hash, oid, 532 struct object_entry, ent); 533 if (!e) { 534 e = new_object(oid); 535 e->idx.offset = 0; 536 hashmap_entry_init(&e->ent, hash); 537 hashmap_add(&object_table, &e->ent); 538 } 539 540 return e; 541 } 542 543 static void invalidate_pack_id(unsigned int id) 544 { 545 unsigned long lu; 546 struct tag *t; 547 struct hashmap_iter iter; 548 struct object_entry *e; 549 550 hashmap_for_each_entry(&object_table, &iter, e, ent) { 551 if (e->pack_id == id) 552 e->pack_id = MAX_PACK_ID; 553 } 554 555 for (lu = 0; lu < branch_table_sz; lu++) { 556 struct branch *b; 557 558 for (b = branch_table[lu]; b; b = b->table_next_branch) 559 if (b->pack_id == id) 560 b->pack_id = MAX_PACK_ID; 561 } 562 563 for (t = first_tag; t; t = t->next_tag) 564 if (t->pack_id == id) 565 t->pack_id = MAX_PACK_ID; 566 } 567 568 static unsigned int hc_str(const char *s, size_t len) 569 { 570 unsigned int r = 0; 571 while (len-- > 0) 572 r = r * 31 + *s++; 573 return r; 574 } 575 576 static void insert_mark(struct mark_set **top, uintmax_t idnum, struct object_entry *oe) 577 { 578 struct mark_set *s = *top; 579 580 while ((idnum >> s->shift) >= 1024) { 581 s = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set)); 582 s->shift = (*top)->shift + 10; 583 s->data.sets[0] = *top; 584 *top = s; 585 } 586 while (s->shift) { 587 uintmax_t i = idnum >> s->shift; 588 idnum -= i << s->shift; 589 if (!s->data.sets[i]) { 590 s->data.sets[i] = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set)); 591 s->data.sets[i]->shift = s->shift - 10; 592 } 593 s = s->data.sets[i]; 594 } 595 if (!s->data.marked[idnum]) 596 marks_set_count++; 597 s->data.marked[idnum] = oe; 598 } 599 600 static void *find_mark(struct mark_set *s, uintmax_t idnum) 601 { 602 uintmax_t orig_idnum = idnum; 603 struct object_entry *oe = NULL; 604 if ((idnum >> s->shift) < 1024) { 605 while (s && s->shift) { 606 uintmax_t i = idnum >> s->shift; 607 idnum -= i << s->shift; 608 s = s->data.sets[i]; 609 } 610 if (s) 611 oe = s->data.marked[idnum]; 612 } 613 if (!oe) 614 die(_("mark :%" PRIuMAX " not declared"), orig_idnum); 615 return oe; 616 } 617 618 static struct atom_str *to_atom(const char *s, unsigned short len) 619 { 620 unsigned int hc = hc_str(s, len) % atom_table_sz; 621 struct atom_str *c; 622 623 for (c = atom_table[hc]; c; c = c->next_atom) 624 if (c->str_len == len && !strncmp(s, c->str_dat, len)) 625 return c; 626 627 c = mem_pool_alloc(&fi_mem_pool, sizeof(struct atom_str) + len + 1); 628 c->str_len = len; 629 memcpy(c->str_dat, s, len); 630 c->str_dat[len] = 0; 631 c->next_atom = atom_table[hc]; 632 atom_table[hc] = c; 633 atom_cnt++; 634 return c; 635 } 636 637 static struct branch *lookup_branch(const char *name) 638 { 639 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; 640 struct branch *b; 641 642 for (b = branch_table[hc]; b; b = b->table_next_branch) 643 if (!strcmp(name, b->name)) 644 return b; 645 return NULL; 646 } 647 648 static struct branch *new_branch(const char *name) 649 { 650 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; 651 struct branch *b = lookup_branch(name); 652 653 if (b) 654 die(_("invalid attempt to create duplicate branch: %s"), name); 655 if (check_refname_format(name, REFNAME_ALLOW_ONELEVEL)) 656 die(_("branch name doesn't conform to Git standards: %s"), name); 657 658 b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch)); 659 b->name = mem_pool_strdup(&fi_mem_pool, name); 660 b->table_next_branch = branch_table[hc]; 661 b->branch_tree.versions[0].mode = S_IFDIR; 662 b->branch_tree.versions[1].mode = S_IFDIR; 663 b->num_notes = 0; 664 b->active = 0; 665 b->pack_id = MAX_PACK_ID; 666 branch_table[hc] = b; 667 branch_count++; 668 return b; 669 } 670 671 static unsigned int hc_entries(unsigned int cnt) 672 { 673 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8; 674 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1; 675 } 676 677 static struct tree_content *new_tree_content(unsigned int cnt) 678 { 679 struct avail_tree_content *f, *l = NULL; 680 struct tree_content *t; 681 unsigned int hc = hc_entries(cnt); 682 683 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail) 684 if (f->entry_capacity >= cnt) 685 break; 686 687 if (f) { 688 if (l) 689 l->next_avail = f->next_avail; 690 else 691 avail_tree_table[hc] = f->next_avail; 692 } else { 693 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt; 694 f = mem_pool_alloc(&fi_mem_pool, sizeof(*t) + sizeof(t->entries[0]) * cnt); 695 f->entry_capacity = cnt; 696 } 697 698 t = (struct tree_content*)f; 699 t->entry_count = 0; 700 t->delta_depth = 0; 701 return t; 702 } 703 704 static void release_tree_entry(struct tree_entry *e); 705 static void release_tree_content(struct tree_content *t) 706 { 707 struct avail_tree_content *f = (struct avail_tree_content*)t; 708 unsigned int hc = hc_entries(f->entry_capacity); 709 f->next_avail = avail_tree_table[hc]; 710 avail_tree_table[hc] = f; 711 } 712 713 static void release_tree_content_recursive(struct tree_content *t) 714 { 715 unsigned int i; 716 for (i = 0; i < t->entry_count; i++) 717 release_tree_entry(t->entries[i]); 718 release_tree_content(t); 719 } 720 721 static struct tree_content *grow_tree_content( 722 struct tree_content *t, 723 int amt) 724 { 725 struct tree_content *r = new_tree_content(t->entry_count + amt); 726 r->entry_count = t->entry_count; 727 r->delta_depth = t->delta_depth; 728 COPY_ARRAY(r->entries, t->entries, t->entry_count); 729 release_tree_content(t); 730 return r; 731 } 732 733 static struct tree_entry *new_tree_entry(void) 734 { 735 struct tree_entry *e; 736 737 if (!avail_tree_entry) { 738 unsigned int n = tree_entry_alloc; 739 tree_entry_allocd += n * sizeof(struct tree_entry); 740 ALLOC_ARRAY(e, n); 741 avail_tree_entry = e; 742 while (n-- > 1) { 743 *((void**)e) = e + 1; 744 e++; 745 } 746 *((void**)e) = NULL; 747 } 748 749 e = avail_tree_entry; 750 avail_tree_entry = *((void**)e); 751 return e; 752 } 753 754 static void release_tree_entry(struct tree_entry *e) 755 { 756 if (e->tree) 757 release_tree_content_recursive(e->tree); 758 *((void**)e) = avail_tree_entry; 759 avail_tree_entry = e; 760 } 761 762 static struct tree_content *dup_tree_content(struct tree_content *s) 763 { 764 struct tree_content *d; 765 struct tree_entry *a, *b; 766 unsigned int i; 767 768 if (!s) 769 return NULL; 770 d = new_tree_content(s->entry_count); 771 for (i = 0; i < s->entry_count; i++) { 772 a = s->entries[i]; 773 b = new_tree_entry(); 774 memcpy(b, a, sizeof(*a)); 775 if (a->tree && is_null_oid(&b->versions[1].oid)) 776 b->tree = dup_tree_content(a->tree); 777 else 778 b->tree = NULL; 779 d->entries[i] = b; 780 } 781 d->entry_count = s->entry_count; 782 d->delta_depth = s->delta_depth; 783 784 return d; 785 } 786 787 static void start_packfile(void) 788 { 789 struct strbuf tmp_file = STRBUF_INIT; 790 struct packed_git *p; 791 int pack_fd; 792 793 pack_fd = odb_mkstemp(the_repository->objects, &tmp_file, 794 "pack/tmp_pack_XXXXXX"); 795 FLEX_ALLOC_STR(p, pack_name, tmp_file.buf); 796 strbuf_release(&tmp_file); 797 798 p->pack_fd = pack_fd; 799 p->do_not_close = 1; 800 p->repo = the_repository; 801 pack_file = hashfd(the_repository->hash_algo, pack_fd, p->pack_name); 802 803 pack_data = p; 804 pack_size = write_pack_header(pack_file, 0); 805 object_count = 0; 806 807 REALLOC_ARRAY(all_packs, pack_id + 1); 808 all_packs[pack_id] = p; 809 } 810 811 static const char *create_index(void) 812 { 813 const char *tmpfile; 814 struct pack_idx_entry **idx, **c, **last; 815 struct object_entry *e; 816 struct object_entry_pool *o; 817 818 /* Build the table of object IDs. */ 819 ALLOC_ARRAY(idx, object_count); 820 c = idx; 821 for (o = blocks; o; o = o->next_pool) 822 for (e = o->next_free; e-- != o->entries;) 823 if (pack_id == e->pack_id) 824 *c++ = &e->idx; 825 last = idx + object_count; 826 if (c != last) 827 die(_("internal consistency error creating the index")); 828 829 tmpfile = write_idx_file(the_repository, NULL, idx, object_count, 830 &pack_idx_opts, pack_data->hash); 831 free(idx); 832 return tmpfile; 833 } 834 835 static char *keep_pack(const char *curr_index_name) 836 { 837 static const char *keep_msg = "fast-import"; 838 struct strbuf name = STRBUF_INIT; 839 int keep_fd; 840 841 odb_pack_name(pack_data->repo, &name, pack_data->hash, "keep"); 842 keep_fd = safe_create_file_with_leading_directories(pack_data->repo, 843 name.buf); 844 if (keep_fd < 0) 845 die_errno(_("cannot create keep file")); 846 write_or_die(keep_fd, keep_msg, strlen(keep_msg)); 847 if (close(keep_fd)) 848 die_errno(_("failed to write keep file")); 849 850 odb_pack_name(pack_data->repo, &name, pack_data->hash, "pack"); 851 if (finalize_object_file(pack_data->repo, pack_data->pack_name, name.buf)) 852 die(_("cannot store pack file")); 853 854 odb_pack_name(pack_data->repo, &name, pack_data->hash, "idx"); 855 if (finalize_object_file(pack_data->repo, curr_index_name, name.buf)) 856 die(_("cannot store index file")); 857 free((void *)curr_index_name); 858 return strbuf_detach(&name, NULL); 859 } 860 861 static void unkeep_all_packs(void) 862 { 863 struct strbuf name = STRBUF_INIT; 864 int k; 865 866 for (k = 0; k < pack_id; k++) { 867 struct packed_git *p = all_packs[k]; 868 odb_pack_name(p->repo, &name, p->hash, "keep"); 869 unlink_or_warn(name.buf); 870 } 871 strbuf_release(&name); 872 } 873 874 static int loosen_small_pack(const struct packed_git *p) 875 { 876 struct child_process unpack = CHILD_PROCESS_INIT; 877 878 if (lseek(p->pack_fd, 0, SEEK_SET) < 0) 879 die_errno(_("failed seeking to start of '%s'"), p->pack_name); 880 881 unpack.in = p->pack_fd; 882 unpack.git_cmd = 1; 883 unpack.stdout_to_stderr = 1; 884 strvec_push(&unpack.args, "unpack-objects"); 885 if (!show_stats) 886 strvec_push(&unpack.args, "-q"); 887 888 return run_command(&unpack); 889 } 890 891 static void end_packfile(void) 892 { 893 static int running; 894 895 if (running || !pack_data) 896 return; 897 898 running = 1; 899 clear_delta_base_cache(); 900 if (object_count) { 901 struct odb_source_files *files = odb_source_files_downcast(pack_data->repo->objects->source); 902 struct packed_git *new_p; 903 struct object_id cur_pack_oid; 904 char *idx_name; 905 int i; 906 struct branch *b; 907 struct tag *t; 908 909 close_pack_windows(pack_data); 910 finalize_hashfile(pack_file, cur_pack_oid.hash, FSYNC_COMPONENT_PACK, 0); 911 fixup_pack_header_footer(the_hash_algo, pack_data->pack_fd, 912 pack_data->hash, pack_data->pack_name, 913 object_count, cur_pack_oid.hash, 914 pack_size); 915 916 if (object_count <= unpack_limit) { 917 if (!loosen_small_pack(pack_data)) { 918 invalidate_pack_id(pack_id); 919 goto discard_pack; 920 } 921 } 922 923 close(pack_data->pack_fd); 924 idx_name = keep_pack(create_index()); 925 926 /* Register the packfile with core git's machinery. */ 927 new_p = packfile_store_load_pack(files->dirs->packed, idx_name, 1); 928 if (!new_p) 929 die(_("core Git rejected index %s"), idx_name); 930 all_packs[pack_id] = new_p; 931 free(idx_name); 932 933 /* Print the boundary */ 934 if (pack_edges) { 935 fprintf(pack_edges, "%s:", new_p->pack_name); 936 for (i = 0; i < branch_table_sz; i++) { 937 for (b = branch_table[i]; b; b = b->table_next_branch) { 938 if (b->pack_id == pack_id) 939 fprintf(pack_edges, " %s", 940 oid_to_hex(&b->oid)); 941 } 942 } 943 for (t = first_tag; t; t = t->next_tag) { 944 if (t->pack_id == pack_id) 945 fprintf(pack_edges, " %s", 946 oid_to_hex(&t->oid)); 947 } 948 fputc('\n', pack_edges); 949 fflush(pack_edges); 950 } 951 952 pack_id++; 953 } 954 else { 955 discard_pack: 956 close(pack_data->pack_fd); 957 unlink_or_warn(pack_data->pack_name); 958 } 959 FREE_AND_NULL(pack_data); 960 running = 0; 961 962 /* We can't carry a delta across packfiles. */ 963 strbuf_release(&last_blob.data); 964 last_blob.offset = 0; 965 last_blob.depth = 0; 966 } 967 968 static void cycle_packfile(void) 969 { 970 end_packfile(); 971 start_packfile(); 972 } 973 974 static int store_object( 975 enum object_type type, 976 struct strbuf *dat, 977 struct last_object *last, 978 struct object_id *oidout, 979 uintmax_t mark) 980 { 981 struct odb_source_files *files = odb_source_files_downcast(the_repository->objects->source); 982 void *out, *delta; 983 struct object_entry *e; 984 unsigned char hdr[96]; 985 struct object_id oid; 986 unsigned long hdrlen, deltalen = 0; 987 struct git_hash_ctx c; 988 git_zstream s; 989 struct repo_config_values *cfg = repo_config_values(the_repository); 990 991 hdrlen = format_object_header((char *)hdr, sizeof(hdr), type, 992 dat->len); 993 git_hash_init(&c, the_hash_algo); 994 git_hash_update(&c, hdr, hdrlen); 995 git_hash_update(&c, dat->buf, dat->len); 996 git_hash_final_oid(&oid, &c); 997 if (oidout) 998 oidcpy(oidout, &oid); 999 1000 e = insert_object(&oid); 1001 if (mark) 1002 insert_mark(&marks, mark, e); 1003 if (e->idx.offset) { 1004 duplicate_count_by_type[type]++; 1005 return 1; 1006 } 1007 1008 for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { 1009 if (!packfile_list_find_oid(packfile_store_get_packs(dir->packed), &oid)) 1010 continue; 1011 e->type = type; 1012 e->pack_id = MAX_PACK_ID; 1013 e->idx.offset = 1; /* just not zero! */ 1014 duplicate_count_by_type[type]++; 1015 return 1; 1016 } 1017 1018 if (last && last->data.len && last->data.buf && last->depth < max_depth 1019 && dat->len > the_hash_algo->rawsz) { 1020 size_t deltalen_st; 1021 1022 delta_count_attempts_by_type[type]++; 1023 delta = diff_delta(last->data.buf, last->data.len, 1024 dat->buf, dat->len, 1025 &deltalen_st, dat->len - the_hash_algo->rawsz); 1026 deltalen = cast_size_t_to_ulong(deltalen_st); 1027 } else 1028 delta = NULL; 1029 1030 git_deflate_init(&s, cfg->pack_compression_level); 1031 if (delta) { 1032 s.next_in = delta; 1033 s.avail_in = deltalen; 1034 } else { 1035 s.next_in = (void *)dat->buf; 1036 s.avail_in = dat->len; 1037 } 1038 s.avail_out = git_deflate_bound(&s, s.avail_in); 1039 s.next_out = out = xmalloc(s.avail_out); 1040 while (git_deflate(&s, Z_FINISH) == Z_OK) 1041 ; /* nothing */ 1042 git_deflate_end(&s); 1043 1044 /* Determine if we should auto-checkpoint. */ 1045 if ((max_packsize 1046 && (pack_size + PACK_SIZE_THRESHOLD + s.total_out) > max_packsize) 1047 || (pack_size + PACK_SIZE_THRESHOLD + s.total_out) < pack_size) { 1048 1049 /* This new object needs to *not* have the current pack_id. */ 1050 e->pack_id = pack_id + 1; 1051 cycle_packfile(); 1052 1053 /* We cannot carry a delta into the new pack. */ 1054 if (delta) { 1055 FREE_AND_NULL(delta); 1056 1057 git_deflate_init(&s, cfg->pack_compression_level); 1058 s.next_in = (void *)dat->buf; 1059 s.avail_in = dat->len; 1060 s.avail_out = git_deflate_bound(&s, s.avail_in); 1061 s.next_out = out = xrealloc(out, s.avail_out); 1062 while (git_deflate(&s, Z_FINISH) == Z_OK) 1063 ; /* nothing */ 1064 git_deflate_end(&s); 1065 } 1066 } 1067 1068 e->type = type; 1069 e->pack_id = pack_id; 1070 e->idx.offset = pack_size; 1071 object_count++; 1072 object_count_by_type[type]++; 1073 1074 crc32_begin(pack_file); 1075 1076 if (delta) { 1077 off_t ofs = e->idx.offset - last->offset; 1078 unsigned pos = sizeof(hdr) - 1; 1079 1080 delta_count_by_type[type]++; 1081 e->depth = last->depth + 1; 1082 1083 hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr), 1084 OBJ_OFS_DELTA, deltalen); 1085 hashwrite(pack_file, hdr, hdrlen); 1086 pack_size += hdrlen; 1087 1088 hdr[pos] = ofs & 127; 1089 while (ofs >>= 7) 1090 hdr[--pos] = 128 | (--ofs & 127); 1091 hashwrite(pack_file, hdr + pos, sizeof(hdr) - pos); 1092 pack_size += sizeof(hdr) - pos; 1093 } else { 1094 e->depth = 0; 1095 hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr), 1096 type, dat->len); 1097 hashwrite(pack_file, hdr, hdrlen); 1098 pack_size += hdrlen; 1099 } 1100 1101 hashwrite(pack_file, out, s.total_out); 1102 pack_size += s.total_out; 1103 1104 e->idx.crc32 = crc32_end(pack_file); 1105 1106 free(out); 1107 free(delta); 1108 if (last) { 1109 if (last->no_swap) { 1110 last->data = *dat; 1111 } else { 1112 strbuf_swap(&last->data, dat); 1113 } 1114 last->offset = e->idx.offset; 1115 last->depth = e->depth; 1116 } 1117 return 0; 1118 } 1119 1120 static void truncate_pack(struct hashfile_checkpoint *checkpoint) 1121 { 1122 if (hashfile_truncate(pack_file, checkpoint)) 1123 die_errno(_("cannot truncate pack to skip duplicate")); 1124 pack_size = checkpoint->offset; 1125 } 1126 1127 static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark) 1128 { 1129 struct odb_source_files *files = odb_source_files_downcast(the_repository->objects->source); 1130 size_t in_sz = 64 * 1024, out_sz = 64 * 1024; 1131 unsigned char *in_buf = xmalloc(in_sz); 1132 unsigned char *out_buf = xmalloc(out_sz); 1133 struct object_entry *e; 1134 struct object_id oid; 1135 unsigned long hdrlen; 1136 off_t offset; 1137 struct git_hash_ctx c; 1138 git_zstream s; 1139 struct hashfile_checkpoint checkpoint; 1140 struct repo_config_values *cfg = repo_config_values(the_repository); 1141 int status = Z_OK; 1142 1143 /* Determine if we should auto-checkpoint. */ 1144 if ((max_packsize 1145 && (pack_size + PACK_SIZE_THRESHOLD + len) > max_packsize) 1146 || (pack_size + PACK_SIZE_THRESHOLD + len) < pack_size) 1147 cycle_packfile(); 1148 1149 hashfile_checkpoint_init(pack_file, &checkpoint); 1150 hashfile_checkpoint(pack_file, &checkpoint); 1151 offset = checkpoint.offset; 1152 1153 hdrlen = format_object_header((char *)out_buf, out_sz, OBJ_BLOB, len); 1154 1155 git_hash_init(&c, the_hash_algo); 1156 git_hash_update(&c, out_buf, hdrlen); 1157 1158 crc32_begin(pack_file); 1159 1160 git_deflate_init(&s, cfg->pack_compression_level); 1161 1162 hdrlen = encode_in_pack_object_header(out_buf, out_sz, OBJ_BLOB, len); 1163 1164 s.next_out = out_buf + hdrlen; 1165 s.avail_out = out_sz - hdrlen; 1166 1167 while (status != Z_STREAM_END) { 1168 if (0 < len && !s.avail_in) { 1169 size_t cnt = in_sz < len ? in_sz : (size_t)len; 1170 size_t n = fread(in_buf, 1, cnt, stdin); 1171 if (!n && feof(stdin)) 1172 die(_("EOF in data (%" PRIuMAX " bytes remaining)"), len); 1173 1174 git_hash_update(&c, in_buf, n); 1175 s.next_in = in_buf; 1176 s.avail_in = n; 1177 len -= n; 1178 } 1179 1180 status = git_deflate(&s, len ? 0 : Z_FINISH); 1181 1182 if (!s.avail_out || status == Z_STREAM_END) { 1183 size_t n = s.next_out - out_buf; 1184 hashwrite(pack_file, out_buf, n); 1185 pack_size += n; 1186 s.next_out = out_buf; 1187 s.avail_out = out_sz; 1188 } 1189 1190 switch (status) { 1191 case Z_OK: 1192 case Z_BUF_ERROR: 1193 case Z_STREAM_END: 1194 continue; 1195 default: 1196 die(_("unexpected deflate failure: %d"), status); 1197 } 1198 } 1199 git_deflate_end(&s); 1200 git_hash_final_oid(&oid, &c); 1201 1202 if (oidout) 1203 oidcpy(oidout, &oid); 1204 1205 e = insert_object(&oid); 1206 1207 if (mark) 1208 insert_mark(&marks, mark, e); 1209 1210 if (e->idx.offset) { 1211 duplicate_count_by_type[OBJ_BLOB]++; 1212 truncate_pack(&checkpoint); 1213 goto out; 1214 } 1215 1216 for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { 1217 if (!packfile_list_find_oid(packfile_store_get_packs(dir->packed), &oid)) 1218 continue; 1219 e->type = OBJ_BLOB; 1220 e->pack_id = MAX_PACK_ID; 1221 e->idx.offset = 1; /* just not zero! */ 1222 duplicate_count_by_type[OBJ_BLOB]++; 1223 truncate_pack(&checkpoint); 1224 goto out; 1225 } 1226 1227 e->depth = 0; 1228 e->type = OBJ_BLOB; 1229 e->pack_id = pack_id; 1230 e->idx.offset = offset; 1231 e->idx.crc32 = crc32_end(pack_file); 1232 object_count++; 1233 object_count_by_type[OBJ_BLOB]++; 1234 1235 out: 1236 free(in_buf); 1237 free(out_buf); 1238 hashfile_checkpoint_release(&checkpoint); 1239 } 1240 1241 /* All calls must be guarded by find_object() or find_mark() to 1242 * ensure the 'struct object_entry' passed was written by this 1243 * process instance. We unpack the entry by the offset, avoiding 1244 * the need for the corresponding .idx file. This unpacking rule 1245 * works because we only use OBJ_REF_DELTA within the packfiles 1246 * created by fast-import. 1247 * 1248 * oe must not be NULL. Such an oe usually comes from giving 1249 * an unknown SHA-1 to find_object() or an undefined mark to 1250 * find_mark(). Callers must test for this condition and use 1251 * the standard read_sha1_file() when it happens. 1252 * 1253 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from 1254 * find_mark(), where the mark was reloaded from an existing marks 1255 * file and is referencing an object that this fast-import process 1256 * instance did not write out to a packfile. Callers must test for 1257 * this condition and use read_sha1_file() instead. 1258 */ 1259 static void *gfi_unpack_entry( 1260 struct object_entry *oe, 1261 unsigned long *sizep) 1262 { 1263 enum object_type type; 1264 size_t size_st = 0; 1265 void *data; 1266 struct packed_git *p = all_packs[oe->pack_id]; 1267 if (p == pack_data && p->pack_size < (pack_size + the_hash_algo->rawsz)) { 1268 /* The object is stored in the packfile we are writing to 1269 * and we have modified it since the last time we scanned 1270 * back to read a previously written object. If an old 1271 * window covered [p->pack_size, p->pack_size + rawsz) its 1272 * data is stale and is not valid. Closing all windows 1273 * and updating the packfile length ensures we can read 1274 * the newly written data. 1275 */ 1276 close_pack_windows(p); 1277 hashflush(pack_file); 1278 1279 /* We have to offer rawsz bytes additional on the end of 1280 * the packfile as the core unpacker code assumes the 1281 * footer is present at the file end and must promise 1282 * at least rawsz bytes within any window it maps. But 1283 * we don't actually create the footer here. 1284 */ 1285 p->pack_size = pack_size + the_hash_algo->rawsz; 1286 } 1287 data = unpack_entry(the_repository, p, oe->idx.offset, &type, &size_st); 1288 if (sizep) 1289 *sizep = cast_size_t_to_ulong(size_st); 1290 return data; 1291 } 1292 1293 static void load_tree(struct tree_entry *root) 1294 { 1295 struct object_id *oid = &root->versions[1].oid; 1296 struct object_entry *myoe; 1297 struct tree_content *t; 1298 unsigned long size; 1299 char *buf; 1300 const char *c; 1301 1302 root->tree = t = new_tree_content(8); 1303 if (is_null_oid(oid)) 1304 return; 1305 1306 myoe = find_object(oid); 1307 if (myoe && myoe->pack_id != MAX_PACK_ID) { 1308 if (myoe->type != OBJ_TREE) 1309 die(_("not a tree: %s"), oid_to_hex(oid)); 1310 t->delta_depth = myoe->depth; 1311 buf = gfi_unpack_entry(myoe, &size); 1312 if (!buf) 1313 die(_("can't load tree %s"), oid_to_hex(oid)); 1314 } else { 1315 enum object_type type; 1316 size_t size_st = 0; 1317 buf = odb_read_object(the_repository->objects, oid, &type, 1318 &size_st); 1319 size = cast_size_t_to_ulong(size_st); 1320 if (!buf || type != OBJ_TREE) 1321 die(_("can't load tree %s"), oid_to_hex(oid)); 1322 } 1323 1324 c = buf; 1325 while (c != (buf + size)) { 1326 struct tree_entry *e = new_tree_entry(); 1327 1328 if (t->entry_count == t->entry_capacity) 1329 root->tree = t = grow_tree_content(t, t->entry_count); 1330 t->entries[t->entry_count++] = e; 1331 1332 e->tree = NULL; 1333 c = parse_mode(c, &e->versions[1].mode); 1334 if (!c) 1335 die(_("corrupt mode in %s"), oid_to_hex(oid)); 1336 e->versions[0].mode = e->versions[1].mode; 1337 e->name = to_atom(c, strlen(c)); 1338 c += e->name->str_len + 1; 1339 oidread(&e->versions[0].oid, (unsigned char *)c, 1340 the_repository->hash_algo); 1341 oidread(&e->versions[1].oid, (unsigned char *)c, 1342 the_repository->hash_algo); 1343 c += the_hash_algo->rawsz; 1344 } 1345 free(buf); 1346 } 1347 1348 static int tecmp0 (const void *_a, const void *_b) 1349 { 1350 struct tree_entry *a = *((struct tree_entry**)_a); 1351 struct tree_entry *b = *((struct tree_entry**)_b); 1352 return base_name_compare( 1353 a->name->str_dat, a->name->str_len, a->versions[0].mode, 1354 b->name->str_dat, b->name->str_len, b->versions[0].mode); 1355 } 1356 1357 static int tecmp1 (const void *_a, const void *_b) 1358 { 1359 struct tree_entry *a = *((struct tree_entry**)_a); 1360 struct tree_entry *b = *((struct tree_entry**)_b); 1361 return base_name_compare( 1362 a->name->str_dat, a->name->str_len, a->versions[1].mode, 1363 b->name->str_dat, b->name->str_len, b->versions[1].mode); 1364 } 1365 1366 static void mktree(struct tree_content *t, int v, struct strbuf *b) 1367 { 1368 size_t maxlen = 0; 1369 unsigned int i; 1370 1371 if (!v) 1372 QSORT(t->entries, t->entry_count, tecmp0); 1373 else 1374 QSORT(t->entries, t->entry_count, tecmp1); 1375 1376 for (i = 0; i < t->entry_count; i++) { 1377 if (t->entries[i]->versions[v].mode) 1378 maxlen += t->entries[i]->name->str_len + 34; 1379 } 1380 1381 strbuf_reset(b); 1382 strbuf_grow(b, maxlen); 1383 for (i = 0; i < t->entry_count; i++) { 1384 struct tree_entry *e = t->entries[i]; 1385 if (!e->versions[v].mode) 1386 continue; 1387 strbuf_addf(b, "%o %s%c", 1388 (unsigned int)(e->versions[v].mode & ~NO_DELTA), 1389 e->name->str_dat, '\0'); 1390 strbuf_add(b, e->versions[v].oid.hash, the_hash_algo->rawsz); 1391 } 1392 } 1393 1394 static void store_tree(struct tree_entry *root) 1395 { 1396 struct tree_content *t; 1397 unsigned int i, j, del; 1398 struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 }; 1399 struct object_entry *le = NULL; 1400 1401 if (!is_null_oid(&root->versions[1].oid)) 1402 return; 1403 1404 if (!root->tree) 1405 load_tree(root); 1406 t = root->tree; 1407 1408 for (i = 0; i < t->entry_count; i++) { 1409 if (t->entries[i]->tree) 1410 store_tree(t->entries[i]); 1411 } 1412 1413 if (!(root->versions[0].mode & NO_DELTA)) 1414 le = find_object(&root->versions[0].oid); 1415 if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) { 1416 mktree(t, 0, &old_tree); 1417 lo.data = old_tree; 1418 lo.offset = le->idx.offset; 1419 lo.depth = t->delta_depth; 1420 } 1421 1422 mktree(t, 1, &new_tree); 1423 store_object(OBJ_TREE, &new_tree, &lo, &root->versions[1].oid, 0); 1424 1425 t->delta_depth = lo.depth; 1426 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) { 1427 struct tree_entry *e = t->entries[i]; 1428 if (e->versions[1].mode) { 1429 e->versions[0].mode = e->versions[1].mode; 1430 oidcpy(&e->versions[0].oid, &e->versions[1].oid); 1431 t->entries[j++] = e; 1432 } else { 1433 release_tree_entry(e); 1434 del++; 1435 } 1436 } 1437 t->entry_count -= del; 1438 } 1439 1440 static void tree_content_replace( 1441 struct tree_entry *root, 1442 const struct object_id *oid, 1443 const uint16_t mode, 1444 struct tree_content *newtree) 1445 { 1446 if (!S_ISDIR(mode)) 1447 die(_("root cannot be a non-directory")); 1448 oidclr(&root->versions[0].oid, the_repository->hash_algo); 1449 oidcpy(&root->versions[1].oid, oid); 1450 if (root->tree) 1451 release_tree_content_recursive(root->tree); 1452 root->tree = newtree; 1453 } 1454 1455 static int tree_content_set( 1456 struct tree_entry *root, 1457 const char *p, 1458 const struct object_id *oid, 1459 const uint16_t mode, 1460 struct tree_content *subtree) 1461 { 1462 struct tree_content *t; 1463 const char *slash1; 1464 unsigned int i, n; 1465 struct tree_entry *e; 1466 1467 slash1 = strchrnul(p, '/'); 1468 n = slash1 - p; 1469 if (!n) 1470 die(_("empty path component found in input")); 1471 if (!*slash1 && !S_ISDIR(mode) && subtree) 1472 die(_("non-directories cannot have subtrees")); 1473 1474 if (!root->tree) 1475 load_tree(root); 1476 t = root->tree; 1477 for (i = 0; i < t->entry_count; i++) { 1478 e = t->entries[i]; 1479 if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) { 1480 if (!*slash1) { 1481 if (!S_ISDIR(mode) 1482 && e->versions[1].mode == mode 1483 && oideq(&e->versions[1].oid, oid)) 1484 return 0; 1485 e->versions[1].mode = mode; 1486 oidcpy(&e->versions[1].oid, oid); 1487 if (e->tree) 1488 release_tree_content_recursive(e->tree); 1489 e->tree = subtree; 1490 1491 /* 1492 * We need to leave e->versions[0].sha1 alone 1493 * to avoid modifying the preimage tree used 1494 * when writing out the parent directory. 1495 * But after replacing the subdir with a 1496 * completely different one, it's not a good 1497 * delta base any more, and besides, we've 1498 * thrown away the tree entries needed to 1499 * make a delta against it. 1500 * 1501 * So let's just explicitly disable deltas 1502 * for the subtree. 1503 */ 1504 if (S_ISDIR(e->versions[0].mode)) 1505 e->versions[0].mode |= NO_DELTA; 1506 1507 oidclr(&root->versions[1].oid, the_repository->hash_algo); 1508 return 1; 1509 } 1510 if (!S_ISDIR(e->versions[1].mode)) { 1511 e->tree = new_tree_content(8); 1512 e->versions[1].mode = S_IFDIR; 1513 } 1514 if (!e->tree) 1515 load_tree(e); 1516 if (tree_content_set(e, slash1 + 1, oid, mode, subtree)) { 1517 oidclr(&root->versions[1].oid, the_repository->hash_algo); 1518 return 1; 1519 } 1520 return 0; 1521 } 1522 } 1523 1524 if (t->entry_count == t->entry_capacity) 1525 root->tree = t = grow_tree_content(t, t->entry_count); 1526 e = new_tree_entry(); 1527 e->name = to_atom(p, n); 1528 e->versions[0].mode = 0; 1529 oidclr(&e->versions[0].oid, the_repository->hash_algo); 1530 t->entries[t->entry_count++] = e; 1531 if (*slash1) { 1532 e->tree = new_tree_content(8); 1533 e->versions[1].mode = S_IFDIR; 1534 tree_content_set(e, slash1 + 1, oid, mode, subtree); 1535 } else { 1536 e->tree = subtree; 1537 e->versions[1].mode = mode; 1538 oidcpy(&e->versions[1].oid, oid); 1539 } 1540 oidclr(&root->versions[1].oid, the_repository->hash_algo); 1541 return 1; 1542 } 1543 1544 static int tree_content_remove( 1545 struct tree_entry *root, 1546 const char *p, 1547 struct tree_entry *backup_leaf, 1548 int allow_root) 1549 { 1550 struct tree_content *t; 1551 const char *slash1; 1552 unsigned int i, n; 1553 struct tree_entry *e; 1554 1555 slash1 = strchrnul(p, '/'); 1556 n = slash1 - p; 1557 1558 if (!root->tree) 1559 load_tree(root); 1560 1561 if (!*p && allow_root) { 1562 e = root; 1563 goto del_entry; 1564 } 1565 1566 t = root->tree; 1567 for (i = 0; i < t->entry_count; i++) { 1568 e = t->entries[i]; 1569 if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) { 1570 if (*slash1 && !S_ISDIR(e->versions[1].mode)) 1571 /* 1572 * If p names a file in some subdirectory, and a 1573 * file or symlink matching the name of the 1574 * parent directory of p exists, then p cannot 1575 * exist and need not be deleted. 1576 */ 1577 return 1; 1578 if (!*slash1 || !S_ISDIR(e->versions[1].mode)) 1579 goto del_entry; 1580 if (!e->tree) 1581 load_tree(e); 1582 if (tree_content_remove(e, slash1 + 1, backup_leaf, 0)) { 1583 for (n = 0; n < e->tree->entry_count; n++) { 1584 if (e->tree->entries[n]->versions[1].mode) { 1585 oidclr(&root->versions[1].oid, 1586 the_repository->hash_algo); 1587 return 1; 1588 } 1589 } 1590 backup_leaf = NULL; 1591 goto del_entry; 1592 } 1593 return 0; 1594 } 1595 } 1596 return 0; 1597 1598 del_entry: 1599 if (backup_leaf) 1600 memcpy(backup_leaf, e, sizeof(*backup_leaf)); 1601 else if (e->tree) 1602 release_tree_content_recursive(e->tree); 1603 e->tree = NULL; 1604 e->versions[1].mode = 0; 1605 oidclr(&e->versions[1].oid, the_repository->hash_algo); 1606 oidclr(&root->versions[1].oid, the_repository->hash_algo); 1607 return 1; 1608 } 1609 1610 static int tree_content_get( 1611 struct tree_entry *root, 1612 const char *p, 1613 struct tree_entry *leaf, 1614 int allow_root) 1615 { 1616 struct tree_content *t; 1617 const char *slash1; 1618 unsigned int i, n; 1619 struct tree_entry *e; 1620 1621 slash1 = strchrnul(p, '/'); 1622 n = slash1 - p; 1623 if (!n && !allow_root) 1624 die(_("empty path component found in input")); 1625 1626 if (!root->tree) 1627 load_tree(root); 1628 1629 if (!n) { 1630 e = root; 1631 goto found_entry; 1632 } 1633 1634 t = root->tree; 1635 for (i = 0; i < t->entry_count; i++) { 1636 e = t->entries[i]; 1637 if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) { 1638 if (!*slash1) 1639 goto found_entry; 1640 if (!S_ISDIR(e->versions[1].mode)) 1641 return 0; 1642 if (!e->tree) 1643 load_tree(e); 1644 return tree_content_get(e, slash1 + 1, leaf, 0); 1645 } 1646 } 1647 return 0; 1648 1649 found_entry: 1650 memcpy(leaf, e, sizeof(*leaf)); 1651 if (e->tree && is_null_oid(&e->versions[1].oid)) 1652 leaf->tree = dup_tree_content(e->tree); 1653 else 1654 leaf->tree = NULL; 1655 return 1; 1656 } 1657 1658 static int update_branch(struct branch *b) 1659 { 1660 static const char *msg = "fast-import"; 1661 struct ref_transaction *transaction; 1662 struct object_id old_oid; 1663 struct strbuf err = STRBUF_INIT; 1664 static const char *replace_prefix = "refs/replace/"; 1665 1666 if (starts_with(b->name, replace_prefix) && 1667 !strcmp(b->name + strlen(replace_prefix), 1668 oid_to_hex(&b->oid))) { 1669 if (!quiet) 1670 warning(_("dropping %s since it would point to " 1671 "itself (i.e. to %s)"), 1672 b->name, oid_to_hex(&b->oid)); 1673 refs_delete_ref(get_main_ref_store(the_repository), 1674 NULL, b->name, NULL, 0); 1675 return 0; 1676 } 1677 if (is_null_oid(&b->oid)) { 1678 if (b->delete) 1679 refs_delete_ref(get_main_ref_store(the_repository), 1680 NULL, b->name, NULL, 0); 1681 return 0; 1682 } 1683 if (refs_read_ref(get_main_ref_store(the_repository), b->name, &old_oid)) 1684 oidclr(&old_oid, the_repository->hash_algo); 1685 if (!force_update && !is_null_oid(&old_oid)) { 1686 struct commit *old_cmit, *new_cmit; 1687 int ret; 1688 1689 old_cmit = lookup_commit_reference_gently(the_repository, 1690 &old_oid, 0); 1691 new_cmit = lookup_commit_reference_gently(the_repository, 1692 &b->oid, 0); 1693 if (!old_cmit || !new_cmit) 1694 return error(_("branch %s is missing commits."), b->name); 1695 1696 ret = repo_in_merge_bases(the_repository, old_cmit, new_cmit); 1697 if (ret < 0) 1698 exit(128); 1699 if (!ret) { 1700 warning(_("not updating %s" 1701 " (new tip %s does not contain %s)"), 1702 b->name, oid_to_hex(&b->oid), 1703 oid_to_hex(&old_oid)); 1704 return -1; 1705 } 1706 } 1707 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), 1708 0, &err); 1709 if (!transaction || 1710 ref_transaction_update(transaction, b->name, &b->oid, &old_oid, 1711 NULL, NULL, 0, msg, &err) || 1712 ref_transaction_commit(transaction, &err)) { 1713 ref_transaction_free(transaction); 1714 error("%s", err.buf); 1715 strbuf_release(&err); 1716 return -1; 1717 } 1718 ref_transaction_free(transaction); 1719 strbuf_release(&err); 1720 return 0; 1721 } 1722 1723 static void dump_branches(void) 1724 { 1725 unsigned int i; 1726 struct branch *b; 1727 1728 for (i = 0; i < branch_table_sz; i++) { 1729 for (b = branch_table[i]; b; b = b->table_next_branch) 1730 failure |= update_branch(b); 1731 } 1732 } 1733 1734 static void dump_tags(void) 1735 { 1736 static const char *msg = "fast-import"; 1737 struct tag *t; 1738 struct strbuf ref_name = STRBUF_INIT; 1739 struct strbuf err = STRBUF_INIT; 1740 struct ref_transaction *transaction; 1741 1742 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), 1743 0, &err); 1744 if (!transaction) { 1745 failure |= error("%s", err.buf); 1746 goto cleanup; 1747 } 1748 for (t = first_tag; t; t = t->next_tag) { 1749 strbuf_reset(&ref_name); 1750 strbuf_addf(&ref_name, "refs/tags/%s", t->name); 1751 1752 if (ref_transaction_update(transaction, ref_name.buf, 1753 &t->oid, NULL, NULL, NULL, 1754 0, msg, &err)) { 1755 failure |= error("%s", err.buf); 1756 goto cleanup; 1757 } 1758 } 1759 if (ref_transaction_commit(transaction, &err)) 1760 failure |= error("%s", err.buf); 1761 1762 cleanup: 1763 ref_transaction_free(transaction); 1764 strbuf_release(&ref_name); 1765 strbuf_release(&err); 1766 } 1767 1768 static void dump_marks(void) 1769 { 1770 struct lock_file mark_lock = LOCK_INIT; 1771 FILE *f; 1772 1773 if (!export_marks_file || (import_marks_file && !import_marks_file_done)) 1774 return; 1775 1776 if (safe_create_leading_directories_const(the_repository, export_marks_file)) { 1777 failure |= error_errno(_("unable to create leading directories of %s"), 1778 export_marks_file); 1779 return; 1780 } 1781 1782 if (hold_lock_file_for_update(&mark_lock, export_marks_file, 0) < 0) { 1783 failure |= error_errno(_("unable to write marks file %s"), 1784 export_marks_file); 1785 return; 1786 } 1787 1788 f = fdopen_lock_file(&mark_lock, "w"); 1789 if (!f) { 1790 int saved_errno = errno; 1791 rollback_lock_file(&mark_lock); 1792 failure |= error(_("unable to write marks file %s: %s"), 1793 export_marks_file, strerror(saved_errno)); 1794 return; 1795 } 1796 1797 for_each_mark(marks, 0, dump_marks_fn, f); 1798 if (commit_lock_file(&mark_lock)) { 1799 failure |= error_errno(_("unable to write file %s"), 1800 export_marks_file); 1801 return; 1802 } 1803 } 1804 1805 static void insert_object_entry(struct mark_set **s, struct object_id *oid, uintmax_t mark) 1806 { 1807 struct object_entry *e; 1808 e = find_object(oid); 1809 if (!e) { 1810 enum object_type type = odb_read_object_info(the_repository->objects, 1811 oid, NULL); 1812 if (type < 0) 1813 die(_("object not found: %s"), oid_to_hex(oid)); 1814 e = insert_object(oid); 1815 e->type = type; 1816 e->pack_id = MAX_PACK_ID; 1817 e->idx.offset = 1; /* just not zero! */ 1818 } 1819 insert_mark(s, mark, e); 1820 } 1821 1822 static void insert_oid_entry(struct mark_set **s, struct object_id *oid, uintmax_t mark) 1823 { 1824 insert_mark(s, mark, xmemdupz(oid, sizeof(*oid))); 1825 } 1826 1827 static void read_mark_file(struct mark_set **s, FILE *f, mark_set_inserter_t inserter) 1828 { 1829 char line[512]; 1830 while (fgets(line, sizeof(line), f)) { 1831 uintmax_t mark; 1832 char *end; 1833 struct object_id oid; 1834 1835 /* Ensure SHA-1 objects are padded with zeros. */ 1836 memset(oid.hash, 0, sizeof(oid.hash)); 1837 1838 end = strchr(line, '\n'); 1839 if (line[0] != ':' || !end) 1840 die(_("corrupt mark line: %s"), line); 1841 *end = 0; 1842 mark = strtoumax(line + 1, &end, 10); 1843 if (!mark || end == line + 1 1844 || *end != ' ' 1845 || get_oid_hex_any(end + 1, &oid) == GIT_HASH_UNKNOWN) 1846 die(_("corrupt mark line: %s"), line); 1847 inserter(s, &oid, mark); 1848 } 1849 } 1850 1851 static void read_marks(void) 1852 { 1853 FILE *f = fopen(import_marks_file, "r"); 1854 if (f) 1855 ; 1856 else if (import_marks_file_ignore_missing && errno == ENOENT) 1857 goto done; /* Marks file does not exist */ 1858 else 1859 die_errno(_("cannot read '%s'"), import_marks_file); 1860 read_mark_file(&marks, f, insert_object_entry); 1861 fclose(f); 1862 done: 1863 import_marks_file_done = 1; 1864 } 1865 1866 1867 static int read_next_command(struct fast_import_state *state) 1868 { 1869 static int stdin_eof = 0; 1870 1871 if (stdin_eof) { 1872 unread_command_buf = 0; 1873 return EOF; 1874 } 1875 1876 for (;;) { 1877 if (unread_command_buf) { 1878 unread_command_buf = 0; 1879 } else { 1880 struct recent_command *rc; 1881 1882 stdin_eof = strbuf_getline_lf(&command_buf, stdin); 1883 if (stdin_eof) 1884 return EOF; 1885 1886 if (!state->seen_data_command 1887 && !starts_with(command_buf.buf, "feature ") 1888 && !starts_with(command_buf.buf, "option ")) { 1889 parse_argv(state); 1890 } 1891 1892 rc = rc_free; 1893 if (rc) 1894 rc_free = rc->next; 1895 else { 1896 rc = cmd_hist.next; 1897 cmd_hist.next = rc->next; 1898 cmd_hist.next->prev = &cmd_hist; 1899 free(rc->buf); 1900 } 1901 1902 rc->buf = xstrdup(command_buf.buf); 1903 rc->prev = cmd_tail; 1904 rc->next = cmd_hist.prev; 1905 rc->prev->next = rc; 1906 cmd_tail = rc; 1907 } 1908 if (command_buf.buf[0] == '#') 1909 continue; 1910 return 0; 1911 } 1912 } 1913 1914 static void skip_optional_lf(void) 1915 { 1916 int term_char = fgetc(stdin); 1917 if (term_char != '\n' && term_char != EOF) 1918 ungetc(term_char, stdin); 1919 } 1920 1921 static void parse_mark(struct fast_import_state *state) 1922 { 1923 const char *v; 1924 if (skip_prefix(command_buf.buf, "mark :", &v)) { 1925 next_mark = strtoumax(v, NULL, 10); 1926 read_next_command(state); 1927 } 1928 else 1929 next_mark = 0; 1930 } 1931 1932 static void parse_original_identifier(struct fast_import_state *state) 1933 { 1934 const char *v; 1935 if (skip_prefix(command_buf.buf, "original-oid ", &v)) 1936 read_next_command(state); 1937 } 1938 1939 static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res) 1940 { 1941 const char *data; 1942 strbuf_reset(sb); 1943 1944 if (!skip_prefix(command_buf.buf, "data ", &data)) 1945 die(_("expected 'data n' command, found: %s"), command_buf.buf); 1946 1947 if (skip_prefix(data, "<<", &data)) { 1948 char *term = xstrdup(data); 1949 size_t term_len = command_buf.len - (data - command_buf.buf); 1950 1951 for (;;) { 1952 if (strbuf_getline_lf(&command_buf, stdin) == EOF) 1953 die(_("EOF in data (terminator '%s' not found)"), term); 1954 if (term_len == command_buf.len 1955 && !strcmp(term, command_buf.buf)) 1956 break; 1957 strbuf_addbuf(sb, &command_buf); 1958 strbuf_addch(sb, '\n'); 1959 } 1960 free(term); 1961 } 1962 else { 1963 uintmax_t len = strtoumax(data, NULL, 10); 1964 size_t n = 0, length = (size_t)len; 1965 1966 if (limit && limit < len) { 1967 *len_res = len; 1968 return 0; 1969 } 1970 if (length < len) 1971 die(_("data is too large to use in this context")); 1972 1973 while (n < length) { 1974 size_t s = strbuf_fread(sb, length - n, stdin); 1975 if (!s && feof(stdin)) 1976 die(_("EOF in data (%lu bytes remaining)"), 1977 (unsigned long)(length - n)); 1978 n += s; 1979 } 1980 } 1981 1982 skip_optional_lf(); 1983 return 1; 1984 } 1985 1986 static int validate_raw_date(const char *src, struct strbuf *result, int strict) 1987 { 1988 const char *orig_src = src; 1989 char *endp; 1990 unsigned long num; 1991 1992 errno = 0; 1993 1994 num = strtoul(src, &endp, 10); 1995 /* 1996 * NEEDSWORK: perhaps check for reasonable values? For example, we 1997 * could error on values representing times more than a 1998 * day in the future. 1999 */ 2000 if (errno || endp == src || *endp != ' ') 2001 return -1; 2002 2003 src = endp + 1; 2004 if (*src != '-' && *src != '+') 2005 return -1; 2006 2007 num = strtoul(src + 1, &endp, 10); 2008 /* 2009 * NEEDSWORK: check for brokenness other than num > 1400, such as 2010 * (num % 100) >= 60, or ((num % 100) % 15) != 0 ? 2011 */ 2012 if (errno || endp == src + 1 || *endp || /* did not parse */ 2013 (strict && (1400 < num)) /* parsed a broken timezone */ 2014 ) 2015 return -1; 2016 2017 strbuf_addstr(result, orig_src); 2018 return 0; 2019 } 2020 2021 static char *parse_ident(const char *buf) 2022 { 2023 const char *ltgt; 2024 size_t name_len; 2025 struct strbuf ident = STRBUF_INIT; 2026 2027 /* ensure there is a space delimiter even if there is no name */ 2028 if (*buf == '<') 2029 --buf; 2030 2031 ltgt = buf + strcspn(buf, "<>"); 2032 if (*ltgt != '<') 2033 die(_("missing < in ident string: %s"), buf); 2034 if (ltgt != buf && ltgt[-1] != ' ') 2035 die(_("missing space before < in ident string: %s"), buf); 2036 ltgt = ltgt + 1 + strcspn(ltgt + 1, "<>"); 2037 if (*ltgt != '>') 2038 die(_("missing > in ident string: %s"), buf); 2039 ltgt++; 2040 if (*ltgt != ' ') 2041 die(_("missing space after > in ident string: %s"), buf); 2042 ltgt++; 2043 name_len = ltgt - buf; 2044 strbuf_add(&ident, buf, name_len); 2045 2046 switch (whenspec) { 2047 case WHENSPEC_RAW: 2048 if (validate_raw_date(ltgt, &ident, 1) < 0) 2049 die(_("invalid raw date \"%s\" in ident: %s"), ltgt, buf); 2050 break; 2051 case WHENSPEC_RAW_PERMISSIVE: 2052 if (validate_raw_date(ltgt, &ident, 0) < 0) 2053 die(_("invalid raw date \"%s\" in ident: %s"), ltgt, buf); 2054 break; 2055 case WHENSPEC_RFC2822: 2056 if (parse_date(ltgt, &ident) < 0) 2057 die(_("invalid rfc2822 date \"%s\" in ident: %s"), ltgt, buf); 2058 break; 2059 case WHENSPEC_NOW: 2060 if (strcmp("now", ltgt)) 2061 die(_("date in ident must be 'now': %s"), buf); 2062 datestamp(&ident); 2063 break; 2064 } 2065 2066 return strbuf_detach(&ident, NULL); 2067 } 2068 2069 static void parse_and_store_blob( 2070 struct last_object *last, 2071 struct object_id *oidout, 2072 uintmax_t mark) 2073 { 2074 static struct strbuf buf = STRBUF_INIT; 2075 uintmax_t len; 2076 2077 if (parse_data(&buf, repo_settings_get_big_file_threshold(the_repository), &len)) 2078 store_object(OBJ_BLOB, &buf, last, oidout, mark); 2079 else { 2080 if (last) { 2081 strbuf_release(&last->data); 2082 last->offset = 0; 2083 last->depth = 0; 2084 } 2085 stream_blob(len, oidout, mark); 2086 skip_optional_lf(); 2087 } 2088 } 2089 2090 static void parse_new_blob(struct fast_import_state *state) 2091 { 2092 read_next_command(state); 2093 parse_mark(state); 2094 parse_original_identifier(state); 2095 parse_and_store_blob(&last_blob, NULL, next_mark); 2096 } 2097 2098 static void unload_one_branch(void) 2099 { 2100 while (cur_active_branches 2101 && cur_active_branches >= max_active_branches) { 2102 uintmax_t min_commit = ULONG_MAX; 2103 struct branch *e, *l = NULL, *p = NULL; 2104 2105 for (e = active_branches; e; e = e->active_next_branch) { 2106 if (e->last_commit < min_commit) { 2107 p = l; 2108 min_commit = e->last_commit; 2109 } 2110 l = e; 2111 } 2112 2113 if (p) { 2114 e = p->active_next_branch; 2115 p->active_next_branch = e->active_next_branch; 2116 } else { 2117 e = active_branches; 2118 active_branches = e->active_next_branch; 2119 } 2120 e->active = 0; 2121 e->active_next_branch = NULL; 2122 if (e->branch_tree.tree) { 2123 release_tree_content_recursive(e->branch_tree.tree); 2124 e->branch_tree.tree = NULL; 2125 } 2126 cur_active_branches--; 2127 } 2128 } 2129 2130 static void load_branch(struct branch *b) 2131 { 2132 load_tree(&b->branch_tree); 2133 if (!b->active) { 2134 b->active = 1; 2135 b->active_next_branch = active_branches; 2136 active_branches = b; 2137 cur_active_branches++; 2138 branch_load_count++; 2139 } 2140 } 2141 2142 static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes) 2143 { 2144 unsigned char fanout = 0; 2145 while ((num_notes >>= 8)) 2146 fanout++; 2147 return fanout; 2148 } 2149 2150 static void construct_path_with_fanout(const char *hex_sha1, 2151 unsigned char fanout, char *path) 2152 { 2153 unsigned int i = 0, j = 0; 2154 if (fanout >= the_hash_algo->rawsz) 2155 die(_("too large fanout (%u)"), fanout); 2156 while (fanout) { 2157 path[i++] = hex_sha1[j++]; 2158 path[i++] = hex_sha1[j++]; 2159 path[i++] = '/'; 2160 fanout--; 2161 } 2162 memcpy(path + i, hex_sha1 + j, the_hash_algo->hexsz - j); 2163 path[i + the_hash_algo->hexsz - j] = '\0'; 2164 } 2165 2166 static uintmax_t do_change_note_fanout( 2167 struct tree_entry *orig_root, struct tree_entry *root, 2168 char *hex_oid, unsigned int hex_oid_len, 2169 char *fullpath, unsigned int fullpath_len, 2170 unsigned char fanout) 2171 { 2172 struct tree_content *t; 2173 struct tree_entry *e, leaf; 2174 unsigned int i, tmp_hex_oid_len, tmp_fullpath_len; 2175 uintmax_t num_notes = 0; 2176 struct object_id oid; 2177 /* hex oid + '/' between each pair of hex digits + NUL */ 2178 char realpath[GIT_MAX_HEXSZ + ((GIT_MAX_HEXSZ / 2) - 1) + 1]; 2179 const unsigned hexsz = the_hash_algo->hexsz; 2180 2181 if (!root->tree) 2182 load_tree(root); 2183 t = root->tree; 2184 2185 for (i = 0; t && i < t->entry_count; i++) { 2186 e = t->entries[i]; 2187 tmp_hex_oid_len = hex_oid_len + e->name->str_len; 2188 tmp_fullpath_len = fullpath_len; 2189 2190 /* 2191 * We're interested in EITHER existing note entries (entries 2192 * with exactly 40 hex chars in path, not including directory 2193 * separators), OR directory entries that may contain note 2194 * entries (with < 40 hex chars in path). 2195 * Also, each path component in a note entry must be a multiple 2196 * of 2 chars. 2197 */ 2198 if (!e->versions[1].mode || 2199 tmp_hex_oid_len > hexsz || 2200 e->name->str_len % 2) 2201 continue; 2202 2203 /* This _may_ be a note entry, or a subdir containing notes */ 2204 memcpy(hex_oid + hex_oid_len, e->name->str_dat, 2205 e->name->str_len); 2206 if (tmp_fullpath_len) 2207 fullpath[tmp_fullpath_len++] = '/'; 2208 memcpy(fullpath + tmp_fullpath_len, e->name->str_dat, 2209 e->name->str_len); 2210 tmp_fullpath_len += e->name->str_len; 2211 fullpath[tmp_fullpath_len] = '\0'; 2212 2213 if (tmp_hex_oid_len == hexsz && !get_oid_hex(hex_oid, &oid)) { 2214 /* This is a note entry */ 2215 if (fanout == 0xff) { 2216 /* Counting mode, no rename */ 2217 num_notes++; 2218 continue; 2219 } 2220 construct_path_with_fanout(hex_oid, fanout, realpath); 2221 if (!strcmp(fullpath, realpath)) { 2222 /* Note entry is in correct location */ 2223 num_notes++; 2224 continue; 2225 } 2226 2227 /* Rename fullpath to realpath */ 2228 if (!tree_content_remove(orig_root, fullpath, &leaf, 0)) 2229 die(_("failed to remove path %s"), fullpath); 2230 tree_content_set(orig_root, realpath, 2231 &leaf.versions[1].oid, 2232 leaf.versions[1].mode, 2233 leaf.tree); 2234 } else if (S_ISDIR(e->versions[1].mode)) { 2235 /* This is a subdir that may contain note entries */ 2236 num_notes += do_change_note_fanout(orig_root, e, 2237 hex_oid, tmp_hex_oid_len, 2238 fullpath, tmp_fullpath_len, fanout); 2239 } 2240 2241 /* The above may have reallocated the current tree_content */ 2242 t = root->tree; 2243 } 2244 return num_notes; 2245 } 2246 2247 static uintmax_t change_note_fanout(struct tree_entry *root, 2248 unsigned char fanout) 2249 { 2250 /* 2251 * The size of path is due to one slash between every two hex digits, 2252 * plus the terminating NUL. Note that there is no slash at the end, so 2253 * the number of slashes is one less than half the number of hex 2254 * characters. 2255 */ 2256 char hex_oid[GIT_MAX_HEXSZ], path[GIT_MAX_HEXSZ + (GIT_MAX_HEXSZ / 2) - 1 + 1]; 2257 return do_change_note_fanout(root, root, hex_oid, 0, path, 0, fanout); 2258 } 2259 2260 static int parse_mapped_oid_hex(const char *hex, struct object_id *oid, const char **end) 2261 { 2262 int algo; 2263 khiter_t it; 2264 2265 /* Make SHA-1 object IDs have all-zero padding. */ 2266 memset(oid->hash, 0, sizeof(oid->hash)); 2267 2268 algo = parse_oid_hex_any(hex, oid, end); 2269 if (algo == GIT_HASH_UNKNOWN) 2270 return -1; 2271 2272 it = kh_get_oid_map(sub_oid_map, *oid); 2273 /* No such object? */ 2274 if (it == kh_end(sub_oid_map)) { 2275 /* If we're using the same algorithm, pass it through. */ 2276 if (hash_algos[algo].format_id == the_hash_algo->format_id) 2277 return 0; 2278 return -1; 2279 } 2280 oidcpy(oid, kh_value(sub_oid_map, it)); 2281 return 0; 2282 } 2283 2284 /* 2285 * Given a pointer into a string, parse a mark reference: 2286 * 2287 * idnum ::= ':' bigint; 2288 * 2289 * Update *endptr to point to the first character after the value. 2290 * 2291 * Complain if the following character is not what is expected, 2292 * either a space or end of the string. 2293 */ 2294 static uintmax_t parse_mark_ref(const char *p, char **endptr) 2295 { 2296 uintmax_t mark; 2297 2298 assert(*p == ':'); 2299 p++; 2300 mark = strtoumax(p, endptr, 10); 2301 if (*endptr == p) 2302 die(_("no value after ':' in mark: %s"), command_buf.buf); 2303 return mark; 2304 } 2305 2306 /* 2307 * Parse the mark reference, and complain if this is not the end of 2308 * the string. 2309 */ 2310 static uintmax_t parse_mark_ref_eol(const char *p) 2311 { 2312 char *end; 2313 uintmax_t mark; 2314 2315 mark = parse_mark_ref(p, &end); 2316 if (*end != '\0') 2317 die(_("garbage after mark: %s"), command_buf.buf); 2318 return mark; 2319 } 2320 2321 /* 2322 * Parse the mark reference, demanding a trailing space. Update *p to 2323 * point to the first character after the space. 2324 */ 2325 static uintmax_t parse_mark_ref_space(const char **p) 2326 { 2327 uintmax_t mark; 2328 char *end; 2329 2330 mark = parse_mark_ref(*p, &end); 2331 if (*end++ != ' ') 2332 die(_("missing space after mark: %s"), command_buf.buf); 2333 *p = end; 2334 return mark; 2335 } 2336 2337 /* 2338 * Parse the path string into the strbuf. The path can either be quoted with 2339 * escape sequences or unquoted without escape sequences. Unquoted strings may 2340 * contain spaces only if `is_last_field` is nonzero; otherwise, it stops 2341 * parsing at the first space. 2342 */ 2343 static void parse_path(struct strbuf *sb, const char *p, const char **endp, 2344 int is_last_field, const char *field) 2345 { 2346 if (*p == '"') { 2347 if (unquote_c_style(sb, p, endp)) 2348 die(_("invalid %s: %s"), field, command_buf.buf); 2349 if (strlen(sb->buf) != sb->len) 2350 die(_("NUL in %s: %s"), field, command_buf.buf); 2351 } else { 2352 /* 2353 * Unless we are parsing the last field of a line, 2354 * SP is the end of this field. 2355 */ 2356 *endp = is_last_field 2357 ? p + strlen(p) 2358 : strchrnul(p, ' '); 2359 strbuf_add(sb, p, *endp - p); 2360 } 2361 } 2362 2363 /* 2364 * Parse the path string into the strbuf, and complain if this is not the end of 2365 * the string. Unquoted strings may contain spaces. 2366 */ 2367 static void parse_path_eol(struct strbuf *sb, const char *p, const char *field) 2368 { 2369 const char *end; 2370 2371 parse_path(sb, p, &end, 1, field); 2372 if (*end) 2373 die(_("garbage after %s: %s"), field, command_buf.buf); 2374 } 2375 2376 /* 2377 * Parse the path string into the strbuf, and ensure it is followed by a space. 2378 * Unquoted strings may not contain spaces. Update *endp to point to the first 2379 * character after the space. 2380 */ 2381 static void parse_path_space(struct strbuf *sb, const char *p, 2382 const char **endp, const char *field) 2383 { 2384 parse_path(sb, p, endp, 0, field); 2385 if (**endp != ' ') 2386 die(_("missing space after %s: %s"), field, command_buf.buf); 2387 (*endp)++; 2388 } 2389 2390 static void file_change_m(struct fast_import_state *state, 2391 const char *p, 2392 struct branch *b) 2393 { 2394 static struct strbuf path = STRBUF_INIT; 2395 struct object_entry *oe; 2396 struct object_id oid; 2397 uint16_t mode, inline_data = 0; 2398 2399 p = parse_mode(p, &mode); 2400 if (!p) 2401 die(_("corrupt mode: %s"), command_buf.buf); 2402 switch (mode) { 2403 case 0644: 2404 case 0755: 2405 mode |= S_IFREG; 2406 case S_IFREG | 0644: 2407 case S_IFREG | 0755: 2408 case S_IFLNK: 2409 case S_IFDIR: 2410 case S_IFGITLINK: 2411 /* ok */ 2412 break; 2413 default: 2414 die(_("corrupt mode: %s"), command_buf.buf); 2415 } 2416 2417 if (*p == ':') { 2418 oe = find_mark(marks, parse_mark_ref_space(&p)); 2419 oidcpy(&oid, &oe->idx.oid); 2420 } else if (skip_prefix(p, "inline ", &p)) { 2421 inline_data = 1; 2422 oe = NULL; /* not used with inline_data, but makes gcc happy */ 2423 } else { 2424 if (parse_mapped_oid_hex(p, &oid, &p)) 2425 die(_("invalid dataref: %s"), command_buf.buf); 2426 oe = find_object(&oid); 2427 if (*p++ != ' ') 2428 die(_("missing space after SHA1: %s"), command_buf.buf); 2429 } 2430 2431 strbuf_reset(&path); 2432 parse_path_eol(&path, p, "path"); 2433 2434 /* Git does not track empty, non-toplevel directories. */ 2435 if (S_ISDIR(mode) && 2436 is_empty_tree_oid(&oid, the_repository->hash_algo) && 2437 *path.buf) { 2438 tree_content_remove(&b->branch_tree, path.buf, NULL, 0); 2439 return; 2440 } 2441 2442 if (S_ISGITLINK(mode)) { 2443 if (inline_data) 2444 die(_("Git links cannot be specified 'inline': %s"), 2445 command_buf.buf); 2446 else if (oe) { 2447 if (oe->type != OBJ_COMMIT) 2448 die(_("not a commit (actually a %s): %s"), 2449 type_name(oe->type), command_buf.buf); 2450 } 2451 /* 2452 * Accept the sha1 without checking; it expected to be in 2453 * another repository. 2454 */ 2455 } else if (inline_data) { 2456 if (S_ISDIR(mode)) 2457 die(_("directories cannot be specified 'inline': %s"), 2458 command_buf.buf); 2459 while (read_next_command(state) != EOF) { 2460 const char *v; 2461 if (skip_prefix(command_buf.buf, "cat-blob ", &v)) 2462 parse_cat_blob(state, v); 2463 else { 2464 parse_and_store_blob(&last_blob, &oid, 0); 2465 break; 2466 } 2467 } 2468 } else { 2469 enum object_type expected = S_ISDIR(mode) ? 2470 OBJ_TREE: OBJ_BLOB; 2471 enum object_type type = oe ? oe->type : 2472 odb_read_object_info(the_repository->objects, 2473 &oid, NULL); 2474 if (type < 0) 2475 die(_("%s not found: %s"), 2476 S_ISDIR(mode) ? _("tree") : _("blob"), 2477 command_buf.buf); 2478 if (type != expected) 2479 die(_("not a %s (actually a %s): %s"), 2480 type_name(expected), type_name(type), 2481 command_buf.buf); 2482 } 2483 2484 if (!*path.buf) { 2485 tree_content_replace(&b->branch_tree, &oid, mode, NULL); 2486 return; 2487 } 2488 2489 if (!verify_path(path.buf, mode)) 2490 die(_("invalid path '%s'"), path.buf); 2491 tree_content_set(&b->branch_tree, path.buf, &oid, mode, NULL); 2492 } 2493 2494 static void file_change_d(const char *p, struct branch *b) 2495 { 2496 static struct strbuf path = STRBUF_INIT; 2497 2498 strbuf_reset(&path); 2499 parse_path_eol(&path, p, "path"); 2500 tree_content_remove(&b->branch_tree, path.buf, NULL, 1); 2501 } 2502 2503 static void file_change_cr(const char *p, struct branch *b, int rename) 2504 { 2505 static struct strbuf source = STRBUF_INIT; 2506 static struct strbuf dest = STRBUF_INIT; 2507 struct tree_entry leaf; 2508 2509 strbuf_reset(&source); 2510 parse_path_space(&source, p, &p, "source"); 2511 strbuf_reset(&dest); 2512 parse_path_eol(&dest, p, "dest"); 2513 2514 memset(&leaf, 0, sizeof(leaf)); 2515 if (rename) 2516 tree_content_remove(&b->branch_tree, source.buf, &leaf, 1); 2517 else 2518 tree_content_get(&b->branch_tree, source.buf, &leaf, 1); 2519 if (!leaf.versions[1].mode) 2520 die(_("path %s not in branch"), source.buf); 2521 if (!*dest.buf) { /* C "path/to/subdir" "" */ 2522 tree_content_replace(&b->branch_tree, 2523 &leaf.versions[1].oid, 2524 leaf.versions[1].mode, 2525 leaf.tree); 2526 return; 2527 } 2528 if (!verify_path(dest.buf, leaf.versions[1].mode)) 2529 die(_("invalid path '%s'"), dest.buf); 2530 tree_content_set(&b->branch_tree, dest.buf, 2531 &leaf.versions[1].oid, 2532 leaf.versions[1].mode, 2533 leaf.tree); 2534 } 2535 2536 static void note_change_n(struct fast_import_state *state, 2537 const char *p, 2538 struct branch *b, 2539 unsigned char *old_fanout) 2540 { 2541 struct object_entry *oe; 2542 struct branch *s; 2543 struct object_id oid, commit_oid; 2544 char path[GIT_MAX_RAWSZ * 3]; 2545 uint16_t inline_data = 0; 2546 unsigned char new_fanout; 2547 2548 /* 2549 * When loading a branch, we don't traverse its tree to count the real 2550 * number of notes (too expensive to do this for all non-note refs). 2551 * This means that recently loaded notes refs might incorrectly have 2552 * b->num_notes == 0, and consequently, old_fanout might be wrong. 2553 * 2554 * Fix this by traversing the tree and counting the number of notes 2555 * when b->num_notes == 0. If the notes tree is truly empty, the 2556 * calculation should not take long. 2557 */ 2558 if (b->num_notes == 0 && *old_fanout == 0) { 2559 /* Invoke change_note_fanout() in "counting mode". */ 2560 b->num_notes = change_note_fanout(&b->branch_tree, 0xff); 2561 *old_fanout = convert_num_notes_to_fanout(b->num_notes); 2562 } 2563 2564 /* Now parse the notemodify command. */ 2565 /* or 'inline' */ 2566 if (*p == ':') { 2567 oe = find_mark(marks, parse_mark_ref_space(&p)); 2568 oidcpy(&oid, &oe->idx.oid); 2569 } else if (skip_prefix(p, "inline ", &p)) { 2570 inline_data = 1; 2571 oe = NULL; /* not used with inline_data, but makes gcc happy */ 2572 } else { 2573 if (parse_mapped_oid_hex(p, &oid, &p)) 2574 die(_("invalid dataref: %s"), command_buf.buf); 2575 oe = find_object(&oid); 2576 if (*p++ != ' ') 2577 die(_("missing space after SHA1: %s"), command_buf.buf); 2578 } 2579 2580 /* */ 2581 s = lookup_branch(p); 2582 if (s) { 2583 if (is_null_oid(&s->oid)) 2584 die(_("can't add a note on empty branch.")); 2585 oidcpy(&commit_oid, &s->oid); 2586 } else if (*p == ':') { 2587 uintmax_t commit_mark = parse_mark_ref_eol(p); 2588 struct object_entry *commit_oe = find_mark(marks, commit_mark); 2589 if (commit_oe->type != OBJ_COMMIT) 2590 die(_("mark :%" PRIuMAX " not a commit"), commit_mark); 2591 oidcpy(&commit_oid, &commit_oe->idx.oid); 2592 } else if (!repo_get_oid(the_repository, p, &commit_oid)) { 2593 size_t size; 2594 char *buf = odb_read_object_peeled(the_repository->objects, 2595 &commit_oid, OBJ_COMMIT, &size, 2596 &commit_oid); 2597 if (!buf || size < the_hash_algo->hexsz + 6) 2598 die(_("not a valid commit: %s"), p); 2599 free(buf); 2600 } else 2601 die(_("invalid ref name or SHA1 expression: %s"), p); 2602 2603 if (inline_data) { 2604 read_next_command(state); 2605 parse_and_store_blob(&last_blob, &oid, 0); 2606 } else if (oe) { 2607 if (oe->type != OBJ_BLOB) 2608 die(_("not a blob (actually a %s): %s"), 2609 type_name(oe->type), command_buf.buf); 2610 } else if (!is_null_oid(&oid)) { 2611 enum object_type type = odb_read_object_info(the_repository->objects, &oid, 2612 NULL); 2613 if (type < 0) 2614 die(_("blob not found: %s"), command_buf.buf); 2615 if (type != OBJ_BLOB) 2616 die(_("not a blob (actually a %s): %s"), 2617 type_name(type), command_buf.buf); 2618 } 2619 2620 construct_path_with_fanout(oid_to_hex(&commit_oid), *old_fanout, path); 2621 if (tree_content_remove(&b->branch_tree, path, NULL, 0)) 2622 b->num_notes--; 2623 2624 if (is_null_oid(&oid)) 2625 return; /* nothing to insert */ 2626 2627 b->num_notes++; 2628 new_fanout = convert_num_notes_to_fanout(b->num_notes); 2629 construct_path_with_fanout(oid_to_hex(&commit_oid), new_fanout, path); 2630 tree_content_set(&b->branch_tree, path, &oid, S_IFREG | 0644, NULL); 2631 } 2632 2633 static void file_change_deleteall(struct branch *b) 2634 { 2635 release_tree_content_recursive(b->branch_tree.tree); 2636 oidclr(&b->branch_tree.versions[0].oid, the_repository->hash_algo); 2637 oidclr(&b->branch_tree.versions[1].oid, the_repository->hash_algo); 2638 load_tree(&b->branch_tree); 2639 b->num_notes = 0; 2640 } 2641 2642 static void parse_from_commit(struct branch *b, char *buf, unsigned long size) 2643 { 2644 if (!buf || size < the_hash_algo->hexsz + 6) 2645 die(_("not a valid commit: %s"), oid_to_hex(&b->oid)); 2646 if (memcmp("tree ", buf, 5) 2647 || get_oid_hex(buf + 5, &b->branch_tree.versions[1].oid)) 2648 die(_("the commit %s is corrupt"), oid_to_hex(&b->oid)); 2649 oidcpy(&b->branch_tree.versions[0].oid, 2650 &b->branch_tree.versions[1].oid); 2651 } 2652 2653 static void parse_from_existing(struct branch *b) 2654 { 2655 if (is_null_oid(&b->oid)) { 2656 oidclr(&b->branch_tree.versions[0].oid, the_repository->hash_algo); 2657 oidclr(&b->branch_tree.versions[1].oid, the_repository->hash_algo); 2658 } else { 2659 unsigned long size; 2660 char *buf; 2661 struct object_entry *e = find_object(&b->oid); 2662 2663 while (e && e->type == OBJ_TAG) 2664 e = dereference(e, &b->oid); 2665 2666 if (e && e->type == OBJ_COMMIT && e->pack_id != MAX_PACK_ID) { 2667 buf = gfi_unpack_entry(e, &size); 2668 } else { 2669 size_t size_st = 0; 2670 buf = odb_read_object_peeled(the_repository->objects, &b->oid, 2671 OBJ_COMMIT, &size_st, &b->oid); 2672 size = cast_size_t_to_ulong(size_st); 2673 } 2674 parse_from_commit(b, buf, size); 2675 free(buf); 2676 } 2677 } 2678 2679 static int parse_objectish(struct fast_import_state *state, 2680 struct branch *b, 2681 const char *objectish) 2682 { 2683 struct branch *s; 2684 struct object_id oid; 2685 2686 oidcpy(&oid, &b->branch_tree.versions[1].oid); 2687 2688 s = lookup_branch(objectish); 2689 if (b == s) 2690 die(_("can't create a branch from itself: %s"), b->name); 2691 else if (s) { 2692 struct object_id *t = &s->branch_tree.versions[1].oid; 2693 oidcpy(&b->oid, &s->oid); 2694 oidcpy(&b->branch_tree.versions[0].oid, t); 2695 oidcpy(&b->branch_tree.versions[1].oid, t); 2696 } else if (*objectish == ':') { 2697 uintmax_t idnum = parse_mark_ref_eol(objectish); 2698 struct object_entry *oe = find_mark(marks, idnum); 2699 if (oe->type != OBJ_COMMIT) 2700 die(_("mark :%" PRIuMAX " not a commit"), idnum); 2701 if (!oideq(&b->oid, &oe->idx.oid)) { 2702 oidcpy(&b->oid, &oe->idx.oid); 2703 if (oe->pack_id != MAX_PACK_ID) { 2704 unsigned long size; 2705 char *buf = gfi_unpack_entry(oe, &size); 2706 parse_from_commit(b, buf, size); 2707 free(buf); 2708 } else 2709 parse_from_existing(b); 2710 } 2711 } else if (!repo_get_oid(the_repository, objectish, &b->oid)) { 2712 parse_from_existing(b); 2713 if (is_null_oid(&b->oid)) 2714 b->delete = 1; 2715 } 2716 else 2717 die(_("invalid ref name or SHA1 expression: %s"), objectish); 2718 2719 if (b->branch_tree.tree && !oideq(&oid, &b->branch_tree.versions[1].oid)) { 2720 release_tree_content_recursive(b->branch_tree.tree); 2721 b->branch_tree.tree = NULL; 2722 } 2723 2724 read_next_command(state); 2725 return 1; 2726 } 2727 2728 static int parse_from(struct fast_import_state *state, struct branch *b) 2729 { 2730 const char *from; 2731 2732 if (!skip_prefix(command_buf.buf, "from ", &from)) 2733 return 0; 2734 2735 return parse_objectish(state, b, from); 2736 } 2737 2738 static int parse_objectish_with_prefix(struct fast_import_state *state, 2739 struct branch *b, 2740 const char *prefix) 2741 { 2742 const char *base; 2743 2744 if (!skip_prefix(command_buf.buf, prefix, &base)) 2745 return 0; 2746 2747 return parse_objectish(state, b, base); 2748 } 2749 2750 static struct hash_list *parse_merge(struct fast_import_state *state, 2751 unsigned int *count) 2752 { 2753 struct hash_list *list = NULL, **tail = &list, *n; 2754 const char *from; 2755 struct branch *s; 2756 2757 *count = 0; 2758 while (skip_prefix(command_buf.buf, "merge ", &from)) { 2759 n = xmalloc(sizeof(*n)); 2760 s = lookup_branch(from); 2761 if (s) 2762 oidcpy(&n->oid, &s->oid); 2763 else if (*from == ':') { 2764 uintmax_t idnum = parse_mark_ref_eol(from); 2765 struct object_entry *oe = find_mark(marks, idnum); 2766 if (oe->type != OBJ_COMMIT) 2767 die(_("mark :%" PRIuMAX " not a commit"), idnum); 2768 oidcpy(&n->oid, &oe->idx.oid); 2769 } else if (!repo_get_oid(the_repository, from, &n->oid)) { 2770 size_t size; 2771 char *buf = odb_read_object_peeled(the_repository->objects, 2772 &n->oid, OBJ_COMMIT, 2773 &size, &n->oid); 2774 if (!buf || size < the_hash_algo->hexsz + 6) 2775 die(_("not a valid commit: %s"), from); 2776 free(buf); 2777 } else 2778 die(_("invalid ref name or SHA1 expression: %s"), from); 2779 2780 n->next = NULL; 2781 *tail = n; 2782 tail = &n->next; 2783 2784 (*count)++; 2785 read_next_command(state); 2786 } 2787 return list; 2788 } 2789 2790 struct signature_data { 2791 char *hash_algo; /* "sha1" or "sha256" */ 2792 char *sig_format; /* "openpgp", "x509", "ssh", or "unknown" */ 2793 struct strbuf data; /* The actual signature data */ 2794 }; 2795 2796 static void parse_one_signature(struct fast_import_state *state, 2797 struct signature_data *sig, 2798 const char *v) 2799 { 2800 char *args = xstrdup(v); /* Will be freed when sig->hash_algo is freed */ 2801 char *space = strchr(args, ' '); 2802 2803 if (!space) 2804 die(_("expected gpgsig format: 'gpgsig ', " 2805 "got 'gpgsig %s'"), args); 2806 *space = '\0'; 2807 2808 sig->hash_algo = args; 2809 sig->sig_format = space + 1; 2810 2811 /* Validate hash algorithm */ 2812 if (strcmp(sig->hash_algo, "sha1") && 2813 strcmp(sig->hash_algo, "sha256")) 2814 die(_("unknown git hash algorithm in gpgsig: '%s'"), sig->hash_algo); 2815 2816 /* Validate signature format */ 2817 if (!valid_signature_format(sig->sig_format)) 2818 die(_("invalid signature format in gpgsig: '%s'"), sig->sig_format); 2819 if (!strcmp(sig->sig_format, "unknown")) 2820 warning(_("'unknown' signature format in gpgsig")); 2821 2822 /* Read signature data */ 2823 read_next_command(state); 2824 parse_data(&sig->data, 0, NULL); 2825 } 2826 2827 static void discard_one_signature(struct fast_import_state *state) 2828 { 2829 struct strbuf data = STRBUF_INIT; 2830 2831 read_next_command(state); 2832 parse_data(&data, 0, NULL); 2833 strbuf_release(&data); 2834 } 2835 2836 static void add_gpgsig_to_commit(struct strbuf *commit_data, 2837 const char *header, 2838 struct signature_data *sig) 2839 { 2840 struct string_list siglines = STRING_LIST_INIT_NODUP; 2841 2842 if (!sig || !sig->hash_algo) 2843 return; 2844 2845 strbuf_addstr(commit_data, header); 2846 string_list_split_in_place(&siglines, sig->data.buf, "\n", -1); 2847 strbuf_add_separated_string_list(commit_data, "\n ", &siglines); 2848 strbuf_addch(commit_data, '\n'); 2849 string_list_clear(&siglines, 1); 2850 strbuf_release(&sig->data); 2851 free(sig->hash_algo); 2852 } 2853 2854 static void store_signature(struct signature_data *stored_sig, 2855 struct signature_data *new_sig, 2856 const char *hash_type) 2857 { 2858 if (stored_sig->hash_algo) { 2859 warning(_("multiple %s signatures found, " 2860 "ignoring additional signature"), 2861 hash_type); 2862 strbuf_release(&new_sig->data); 2863 free(new_sig->hash_algo); 2864 } else { 2865 *stored_sig = *new_sig; 2866 } 2867 } 2868 2869 static void import_one_signature(struct fast_import_state *state, 2870 struct signature_data *sig_sha1, 2871 struct signature_data *sig_sha256, 2872 const char *v) 2873 { 2874 struct signature_data sig = { NULL, NULL, STRBUF_INIT }; 2875 2876 parse_one_signature(state, &sig, v); 2877 2878 if (!strcmp(sig.hash_algo, "sha1")) 2879 store_signature(sig_sha1, &sig, "SHA-1"); 2880 else if (!strcmp(sig.hash_algo, "sha256")) 2881 store_signature(sig_sha256, &sig, "SHA-256"); 2882 else 2883 die(_("parse_one_signature() returned unknown hash algo")); 2884 } 2885 2886 static void finalize_commit_buffer(struct strbuf *new_data, 2887 struct signature_data *sig_sha1, 2888 struct signature_data *sig_sha256, 2889 struct strbuf *msg) 2890 { 2891 add_gpgsig_to_commit(new_data, "gpgsig ", sig_sha1); 2892 add_gpgsig_to_commit(new_data, "gpgsig-sha256 ", sig_sha256); 2893 2894 strbuf_addch(new_data, '\n'); 2895 strbuf_addbuf(new_data, msg); 2896 } 2897 2898 static void warn_invalid_signature(struct signature_check *check, 2899 const char *msg, enum sign_mode mode) 2900 { 2901 const char *signer = check->signer ? check->signer : _("unknown"); 2902 const char *subject; 2903 int subject_len = find_commit_subject(msg, &subject); 2904 2905 switch (mode) { 2906 case SIGN_STRIP_IF_INVALID: 2907 if (subject_len > 100) 2908 warning(_("stripping invalid signature for commit '%.100s...'\n" 2909 " allegedly by %s"), subject, signer); 2910 else if (subject_len > 0) 2911 warning(_("stripping invalid signature for commit '%.*s'\n" 2912 " allegedly by %s"), subject_len, subject, signer); 2913 else 2914 warning(_("stripping invalid signature for commit\n" 2915 " allegedly by %s"), signer); 2916 break; 2917 case SIGN_SIGN_IF_INVALID: 2918 if (subject_len > 100) 2919 warning(_("replacing invalid signature for commit '%.100s...'\n" 2920 " allegedly by %s"), subject, signer); 2921 else if (subject_len > 0) 2922 warning(_("replacing invalid signature for commit '%.*s'\n" 2923 " allegedly by %s"), subject_len, subject, signer); 2924 else 2925 warning(_("replacing invalid signature for commit\n" 2926 " allegedly by %s"), signer); 2927 break; 2928 default: 2929 BUG("unsupported signing mode"); 2930 } 2931 } 2932 2933 static void handle_signature_if_invalid(struct strbuf *new_data, 2934 struct signature_data *sig_sha1, 2935 struct signature_data *sig_sha256, 2936 struct strbuf *msg, 2937 enum sign_mode mode) 2938 { 2939 struct strbuf tmp_buf = STRBUF_INIT; 2940 struct signature_check signature_check = { 0 }; 2941 int ret; 2942 2943 /* Check signature in a temporary commit buffer */ 2944 strbuf_addbuf(&tmp_buf, new_data); 2945 finalize_commit_buffer(&tmp_buf, sig_sha1, sig_sha256, msg); 2946 ret = verify_commit_buffer(tmp_buf.buf, tmp_buf.len, &signature_check); 2947 2948 if (ret) { 2949 if (mode == SIGN_ABORT_IF_INVALID) 2950 die(_("aborting due to invalid signature")); 2951 2952 warn_invalid_signature(&signature_check, msg->buf, mode); 2953 2954 if (mode == SIGN_SIGN_IF_INVALID) { 2955 struct strbuf signature = STRBUF_INIT; 2956 struct strbuf payload = STRBUF_INIT; 2957 2958 /* 2959 * NEEDSWORK: To properly support interoperability mode 2960 * when signing commit signatures, the commit buffer 2961 * must be provided in both the repository and 2962 * compatibility object formats. As currently 2963 * implemented, only the repository object format is 2964 * considered meaning compatibility signatures cannot be 2965 * generated. Thus, attempting to sign commit signatures 2966 * in interoperability mode is currently unsupported. 2967 */ 2968 if (the_repository->compat_hash_algo) 2969 die(_("signing commits in interoperability mode is unsupported")); 2970 2971 strbuf_addstr(&payload, signature_check.payload); 2972 if (sign_buffer(&payload, &signature, signed_commit_keyid, 2973 SIGN_BUFFER_USE_DEFAULT_KEY)) 2974 die(_("failed to sign commit object")); 2975 add_header_signature(new_data, &signature, the_hash_algo); 2976 2977 strbuf_release(&signature); 2978 strbuf_release(&payload); 2979 } 2980 2981 finalize_commit_buffer(new_data, NULL, NULL, msg); 2982 } else { 2983 strbuf_swap(new_data, &tmp_buf); 2984 } 2985 2986 signature_check_clear(&signature_check); 2987 strbuf_release(&tmp_buf); 2988 } 2989 2990 static void parse_new_commit(struct fast_import_state *state, const char *arg) 2991 { 2992 static struct strbuf msg = STRBUF_INIT; 2993 struct signature_data sig_sha1 = { NULL, NULL, STRBUF_INIT }; 2994 struct signature_data sig_sha256 = { NULL, NULL, STRBUF_INIT }; 2995 struct branch *b; 2996 char *author = NULL; 2997 char *committer = NULL; 2998 char *encoding = NULL; 2999 struct hash_list *merge_list = NULL; 3000 unsigned int merge_count; 3001 unsigned char prev_fanout, new_fanout; 3002 const char *v; 3003 3004 b = lookup_branch(arg); 3005 if (!b) 3006 b = new_branch(arg); 3007 3008 read_next_command(state); 3009 parse_mark(state); 3010 parse_original_identifier(state); 3011 if (skip_prefix(command_buf.buf, "author ", &v)) { 3012 author = parse_ident(v); 3013 read_next_command(state); 3014 } 3015 if (skip_prefix(command_buf.buf, "committer ", &v)) { 3016 committer = parse_ident(v); 3017 read_next_command(state); 3018 } 3019 if (!committer) 3020 die(_("expected committer but didn't get one")); 3021 3022 while (skip_prefix(command_buf.buf, "gpgsig ", &v)) { 3023 switch (signed_commit_mode) { 3024 3025 /* First, modes that don't need the signature to be parsed */ 3026 case SIGN_ABORT: 3027 die(_("encountered signed commit; use " 3028 "--signed-commits= to handle it")); 3029 case SIGN_WARN_STRIP: 3030 warning(_("stripping a commit signature")); 3031 /* fallthru */ 3032 case SIGN_STRIP: 3033 discard_one_signature(state); 3034 break; 3035 3036 /* Second, modes that parse the signature */ 3037 case SIGN_WARN_VERBATIM: 3038 warning(_("importing a commit signature verbatim")); 3039 /* fallthru */ 3040 case SIGN_VERBATIM: 3041 case SIGN_STRIP_IF_INVALID: 3042 case SIGN_SIGN_IF_INVALID: 3043 case SIGN_ABORT_IF_INVALID: 3044 import_one_signature(state, &sig_sha1, &sig_sha256, v); 3045 break; 3046 3047 /* Third, BUG */ 3048 default: 3049 BUG("invalid signed_commit_mode value %d", signed_commit_mode); 3050 } 3051 read_next_command(state); 3052 } 3053 3054 if (skip_prefix(command_buf.buf, "encoding ", &v)) { 3055 encoding = xstrdup(v); 3056 read_next_command(state); 3057 } 3058 parse_data(&msg, 0, NULL); 3059 read_next_command(state); 3060 parse_from(state, b); 3061 merge_list = parse_merge(state, &merge_count); 3062 3063 /* ensure the branch is active/loaded */ 3064 if (!b->branch_tree.tree || !max_active_branches) { 3065 unload_one_branch(); 3066 load_branch(b); 3067 } 3068 3069 prev_fanout = convert_num_notes_to_fanout(b->num_notes); 3070 3071 /* file_change* */ 3072 while (command_buf.len > 0) { 3073 if (skip_prefix(command_buf.buf, "M ", &v)) 3074 file_change_m(state, v, b); 3075 else if (skip_prefix(command_buf.buf, "D ", &v)) 3076 file_change_d(v, b); 3077 else if (skip_prefix(command_buf.buf, "R ", &v)) 3078 file_change_cr(v, b, 1); 3079 else if (skip_prefix(command_buf.buf, "C ", &v)) 3080 file_change_cr(v, b, 0); 3081 else if (skip_prefix(command_buf.buf, "N ", &v)) 3082 note_change_n(state, v, b, &prev_fanout); 3083 else if (!strcmp("deleteall", command_buf.buf)) 3084 file_change_deleteall(b); 3085 else if (skip_prefix(command_buf.buf, "ls ", &v)) 3086 parse_ls(state, v, b); 3087 else if (skip_prefix(command_buf.buf, "cat-blob ", &v)) 3088 parse_cat_blob(state, v); 3089 else { 3090 unread_command_buf = 1; 3091 break; 3092 } 3093 if (read_next_command(state) == EOF) 3094 break; 3095 } 3096 3097 new_fanout = convert_num_notes_to_fanout(b->num_notes); 3098 if (new_fanout != prev_fanout) 3099 b->num_notes = change_note_fanout(&b->branch_tree, new_fanout); 3100 3101 /* build the tree and the commit */ 3102 store_tree(&b->branch_tree); 3103 oidcpy(&b->branch_tree.versions[0].oid, 3104 &b->branch_tree.versions[1].oid); 3105 3106 strbuf_reset(&new_data); 3107 strbuf_addf(&new_data, "tree %s\n", 3108 oid_to_hex(&b->branch_tree.versions[1].oid)); 3109 if (!is_null_oid(&b->oid)) 3110 strbuf_addf(&new_data, "parent %s\n", 3111 oid_to_hex(&b->oid)); 3112 while (merge_list) { 3113 struct hash_list *next = merge_list->next; 3114 strbuf_addf(&new_data, "parent %s\n", 3115 oid_to_hex(&merge_list->oid)); 3116 free(merge_list); 3117 merge_list = next; 3118 } 3119 strbuf_addf(&new_data, 3120 "author %s\n" 3121 "committer %s\n", 3122 author ? author : committer, committer); 3123 if (encoding) 3124 strbuf_addf(&new_data, 3125 "encoding %s\n", 3126 encoding); 3127 3128 if ((signed_commit_mode == SIGN_STRIP_IF_INVALID || 3129 signed_commit_mode == SIGN_SIGN_IF_INVALID || 3130 signed_commit_mode == SIGN_ABORT_IF_INVALID) && 3131 (sig_sha1.hash_algo || sig_sha256.hash_algo)) 3132 handle_signature_if_invalid(&new_data, &sig_sha1, &sig_sha256, 3133 &msg, signed_commit_mode); 3134 else 3135 finalize_commit_buffer(&new_data, &sig_sha1, &sig_sha256, &msg); 3136 3137 free(author); 3138 free(committer); 3139 free(encoding); 3140 3141 if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark)) 3142 b->pack_id = pack_id; 3143 b->last_commit = object_count_by_type[OBJ_COMMIT]; 3144 } 3145 3146 static void handle_tag_signature_if_invalid(struct strbuf *buf, 3147 struct strbuf *msg, 3148 size_t sig_offset) 3149 { 3150 struct strbuf signature = STRBUF_INIT; 3151 struct strbuf payload = STRBUF_INIT; 3152 struct signature_check sigc = { 0 }; 3153 3154 strbuf_addbuf(&payload, buf); 3155 strbuf_addch(&payload, '\n'); 3156 strbuf_add(&payload, msg->buf, sig_offset); 3157 strbuf_add(&signature, msg->buf + sig_offset, msg->len - sig_offset); 3158 3159 sigc.payload_type = SIGNATURE_PAYLOAD_TAG; 3160 sigc.payload = strbuf_detach(&payload, &sigc.payload_len); 3161 3162 if (!check_signature(&sigc, signature.buf, signature.len)) 3163 goto out; 3164 3165 if (signed_tag_mode == SIGN_ABORT_IF_INVALID) 3166 die(_("aborting due to invalid signature")); 3167 3168 strbuf_setlen(msg, sig_offset); 3169 3170 if (signed_tag_mode == SIGN_SIGN_IF_INVALID) { 3171 strbuf_attach(&payload, sigc.payload, sigc.payload_len, 3172 sigc.payload_len + 1); 3173 sigc.payload = NULL; 3174 strbuf_reset(&signature); 3175 3176 if (sign_buffer(&payload, &signature, signed_tag_keyid, 3177 SIGN_BUFFER_USE_DEFAULT_KEY)) 3178 die(_("failed to sign tag object")); 3179 3180 strbuf_addbuf(msg, &signature); 3181 } 3182 3183 out: 3184 signature_check_clear(&sigc); 3185 strbuf_release(&signature); 3186 strbuf_release(&payload); 3187 } 3188 3189 static void handle_tag_signature(struct strbuf *buf, struct strbuf *msg, const char *name) 3190 { 3191 size_t sig_offset = parse_signed_buffer(msg->buf, msg->len); 3192 3193 /* If there is no signature, there is nothing to do. */ 3194 if (sig_offset >= msg->len) 3195 return; 3196 3197 switch (signed_tag_mode) { 3198 3199 /* First, modes that don't change anything */ 3200 case SIGN_WARN_VERBATIM: 3201 warning(_("importing a tag signature verbatim for tag '%s'"), name); 3202 /* fallthru */ 3203 case SIGN_VERBATIM: 3204 /* Nothing to do, the signature will be put into the imported tag. */ 3205 break; 3206 3207 /* Second, modes that remove the signature */ 3208 case SIGN_WARN_STRIP: 3209 warning(_("stripping a tag signature for tag '%s'"), name); 3210 /* fallthru */ 3211 case SIGN_STRIP: 3212 /* Truncate the buffer to remove the signature */ 3213 strbuf_setlen(msg, sig_offset); 3214 break; 3215 case SIGN_ABORT_IF_INVALID: 3216 case SIGN_SIGN_IF_INVALID: 3217 case SIGN_STRIP_IF_INVALID: 3218 handle_tag_signature_if_invalid(buf, msg, sig_offset); 3219 break; 3220 3221 /* Third, aborting modes */ 3222 case SIGN_ABORT: 3223 die(_("encountered signed tag; use " 3224 "--signed-tags= to handle it")); 3225 default: 3226 BUG("invalid signed_tag_mode value %d from tag '%s'", 3227 signed_tag_mode, name); 3228 } 3229 } 3230 3231 static void parse_new_tag(struct fast_import_state *state, const char *arg) 3232 { 3233 static struct strbuf msg = STRBUF_INIT; 3234 const char *from; 3235 char *tagger; 3236 struct branch *s; 3237 struct tag *t; 3238 uintmax_t from_mark = 0; 3239 struct object_id oid; 3240 enum object_type type; 3241 const char *v; 3242 3243 t = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct tag)); 3244 t->name = mem_pool_strdup(&fi_mem_pool, arg); 3245 if (last_tag) 3246 last_tag->next_tag = t; 3247 else 3248 first_tag = t; 3249 last_tag = t; 3250 read_next_command(state); 3251 parse_mark(state); 3252 3253 /* from ... */ 3254 if (!skip_prefix(command_buf.buf, "from ", &from)) 3255 die(_("expected 'from' command, got '%s'"), command_buf.buf); 3256 s = lookup_branch(from); 3257 if (s) { 3258 if (is_null_oid(&s->oid)) 3259 die(_("can't tag an empty branch.")); 3260 oidcpy(&oid, &s->oid); 3261 type = OBJ_COMMIT; 3262 } else if (*from == ':') { 3263 struct object_entry *oe; 3264 from_mark = parse_mark_ref_eol(from); 3265 oe = find_mark(marks, from_mark); 3266 type = oe->type; 3267 oidcpy(&oid, &oe->idx.oid); 3268 } else if (!repo_get_oid(the_repository, from, &oid)) { 3269 struct object_entry *oe = find_object(&oid); 3270 if (!oe) { 3271 type = odb_read_object_info(the_repository->objects, 3272 &oid, NULL); 3273 if (type < 0) 3274 die(_("not a valid object: %s"), from); 3275 } else 3276 type = oe->type; 3277 } else 3278 die(_("invalid ref name or SHA1 expression: %s"), from); 3279 read_next_command(state); 3280 3281 /* original-oid ... */ 3282 parse_original_identifier(state); 3283 3284 /* tagger ... */ 3285 if (skip_prefix(command_buf.buf, "tagger ", &v)) { 3286 tagger = parse_ident(v); 3287 read_next_command(state); 3288 } else 3289 tagger = NULL; 3290 3291 /* tag payload/message */ 3292 parse_data(&msg, 0, NULL); 3293 3294 /* build the tag object */ 3295 strbuf_reset(&new_data); 3296 3297 strbuf_addf(&new_data, 3298 "object %s\n" 3299 "type %s\n" 3300 "tag %s\n", 3301 oid_to_hex(&oid), type_name(type), t->name); 3302 if (tagger) 3303 strbuf_addf(&new_data, 3304 "tagger %s\n", tagger); 3305 3306 handle_tag_signature(&new_data, &msg, t->name); 3307 3308 strbuf_addch(&new_data, '\n'); 3309 strbuf_addbuf(&new_data, &msg); 3310 free(tagger); 3311 3312 if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, next_mark)) 3313 t->pack_id = MAX_PACK_ID; 3314 else 3315 t->pack_id = pack_id; 3316 } 3317 3318 static void parse_reset_branch(struct fast_import_state *state, const char *arg) 3319 { 3320 struct branch *b; 3321 const char *tag_name; 3322 3323 b = lookup_branch(arg); 3324 if (b) { 3325 oidclr(&b->oid, the_repository->hash_algo); 3326 oidclr(&b->branch_tree.versions[0].oid, the_repository->hash_algo); 3327 oidclr(&b->branch_tree.versions[1].oid, the_repository->hash_algo); 3328 if (b->branch_tree.tree) { 3329 release_tree_content_recursive(b->branch_tree.tree); 3330 b->branch_tree.tree = NULL; 3331 } 3332 } 3333 else 3334 b = new_branch(arg); 3335 read_next_command(state); 3336 parse_from(state, b); 3337 if (b->delete && skip_prefix(b->name, "refs/tags/", &tag_name)) { 3338 /* 3339 * Elsewhere, we call dump_branches() before dump_tags(), 3340 * and dump_branches() will handle ref deletions first, so 3341 * in order to make sure the deletion actually takes effect, 3342 * we need to remove the tag from our list of tags to update. 3343 * 3344 * NEEDSWORK: replace list of tags with hashmap for faster 3345 * deletion? 3346 */ 3347 struct tag *t, *prev = NULL; 3348 for (t = first_tag; t; t = t->next_tag) { 3349 if (!strcmp(t->name, tag_name)) 3350 break; 3351 prev = t; 3352 } 3353 if (t) { 3354 if (prev) 3355 prev->next_tag = t->next_tag; 3356 else 3357 first_tag = t->next_tag; 3358 if (!t->next_tag) 3359 last_tag = prev; 3360 /* There is no mem_pool_free(t) function to call. */ 3361 } 3362 } 3363 if (command_buf.len > 0) 3364 unread_command_buf = 1; 3365 } 3366 3367 static void cat_blob_write(const char *buf, unsigned long size) 3368 { 3369 if (write_in_full(cat_blob_fd, buf, size) < 0) 3370 die_errno(_("write to frontend failed")); 3371 } 3372 3373 static void cat_blob(struct object_entry *oe, struct object_id *oid) 3374 { 3375 struct strbuf line = STRBUF_INIT; 3376 struct iovec iov[3]; 3377 unsigned long size; 3378 enum object_type type = 0; 3379 char *buf; 3380 3381 if (!oe || oe->pack_id == MAX_PACK_ID) { 3382 size_t size_st = 0; 3383 buf = odb_read_object(the_repository->objects, oid, &type, 3384 &size_st); 3385 size = cast_size_t_to_ulong(size_st); 3386 } else { 3387 type = oe->type; 3388 buf = gfi_unpack_entry(oe, &size); 3389 } 3390 3391 /* 3392 * Output based on batch_one_object() from cat-file.c. 3393 */ 3394 if (type <= 0) { 3395 strbuf_reset(&line); 3396 strbuf_addf(&line, "%s missing\n", oid_to_hex(oid)); 3397 cat_blob_write(line.buf, line.len); 3398 strbuf_release(&line); 3399 free(buf); 3400 return; 3401 } 3402 if (!buf) 3403 die(_("can't read object %s"), oid_to_hex(oid)); 3404 if (type != OBJ_BLOB) 3405 die(_("object %s is a %s but a blob was expected."), 3406 oid_to_hex(oid), type_name(type)); 3407 strbuf_reset(&line); 3408 strbuf_addf(&line, "%s %s %"PRIuMAX"\n", oid_to_hex(oid), 3409 type_name(type), (uintmax_t)size); 3410 3411 /* 3412 * Write the header, the payload and the trailing newline with a 3413 * single writev(3p) call instead of three separate write(3p) calls. 3414 */ 3415 iov[0].iov_base = line.buf; 3416 iov[0].iov_len = line.len; 3417 iov[1].iov_base = buf; 3418 iov[1].iov_len = size; 3419 iov[2].iov_base = (void *) "\n"; 3420 iov[2].iov_len = 1; 3421 3422 if (writev_in_full(cat_blob_fd, iov, ARRAY_SIZE(iov)) < 0) 3423 die_errno(_("write to frontend failed")); 3424 strbuf_release(&line); 3425 if (oe && oe->pack_id == pack_id) { 3426 last_blob.offset = oe->idx.offset; 3427 strbuf_attach(&last_blob.data, buf, size, size + 1); 3428 last_blob.depth = oe->depth; 3429 } else 3430 free(buf); 3431 } 3432 3433 static void parse_get_mark(struct fast_import_state *state UNUSED, 3434 const char *p) 3435 { 3436 struct object_entry *oe; 3437 char output[GIT_MAX_HEXSZ + 2]; 3438 3439 /* get-mark SP LF */ 3440 if (*p != ':') 3441 die(_("not a mark: %s"), p); 3442 3443 oe = find_mark(marks, parse_mark_ref_eol(p)); 3444 if (!oe) 3445 die(_("unknown mark: %s"), command_buf.buf); 3446 3447 xsnprintf(output, sizeof(output), "%s\n", oid_to_hex(&oe->idx.oid)); 3448 cat_blob_write(output, the_hash_algo->hexsz + 1); 3449 } 3450 3451 static void parse_cat_blob(struct fast_import_state *state UNUSED, 3452 const char *p) 3453 { 3454 struct object_entry *oe; 3455 struct object_id oid; 3456 3457 /* cat-blob SP LF */ 3458 if (*p == ':') { 3459 oe = find_mark(marks, parse_mark_ref_eol(p)); 3460 if (!oe) 3461 die(_("unknown mark: %s"), command_buf.buf); 3462 oidcpy(&oid, &oe->idx.oid); 3463 } else { 3464 if (parse_mapped_oid_hex(p, &oid, &p)) 3465 die(_("invalid dataref: %s"), command_buf.buf); 3466 if (*p) 3467 die(_("garbage after SHA1: %s"), command_buf.buf); 3468 oe = find_object(&oid); 3469 } 3470 3471 cat_blob(oe, &oid); 3472 } 3473 3474 static struct object_entry *dereference(struct object_entry *oe, 3475 struct object_id *oid) 3476 { 3477 unsigned long size; 3478 char *buf = NULL; 3479 const unsigned hexsz = the_hash_algo->hexsz; 3480 3481 if (!oe) { 3482 enum object_type type = odb_read_object_info(the_repository->objects, 3483 oid, NULL); 3484 if (type < 0) 3485 die(_("object not found: %s"), oid_to_hex(oid)); 3486 /* cache it! */ 3487 oe = insert_object(oid); 3488 oe->type = type; 3489 oe->pack_id = MAX_PACK_ID; 3490 oe->idx.offset = 1; 3491 } 3492 switch (oe->type) { 3493 case OBJ_TREE: /* easy case. */ 3494 return oe; 3495 case OBJ_COMMIT: 3496 case OBJ_TAG: 3497 break; 3498 default: 3499 die(_("not a tree-ish: %s"), command_buf.buf); 3500 } 3501 3502 if (oe->pack_id != MAX_PACK_ID) { /* in a pack being written */ 3503 buf = gfi_unpack_entry(oe, &size); 3504 } else { 3505 enum object_type unused; 3506 size_t size_st = 0; 3507 buf = odb_read_object(the_repository->objects, oid, 3508 &unused, &size_st); 3509 size = cast_size_t_to_ulong(size_st); 3510 } 3511 if (!buf) 3512 die(_("can't load object %s"), oid_to_hex(oid)); 3513 3514 /* Peel one layer. */ 3515 switch (oe->type) { 3516 case OBJ_TAG: 3517 if (size < hexsz + strlen("object ") || 3518 get_oid_hex(buf + strlen("object "), oid)) 3519 die(_("invalid SHA1 in tag: %s"), command_buf.buf); 3520 break; 3521 case OBJ_COMMIT: 3522 if (size < hexsz + strlen("tree ") || 3523 get_oid_hex(buf + strlen("tree "), oid)) 3524 die(_("invalid SHA1 in commit: %s"), command_buf.buf); 3525 } 3526 3527 free(buf); 3528 return find_object(oid); 3529 } 3530 3531 static void insert_mapped_mark(uintmax_t mark, void *object, void *cbp) 3532 { 3533 struct object_id *fromoid = object; 3534 struct object_id *tooid = find_mark(cbp, mark); 3535 int ret; 3536 khiter_t it; 3537 3538 it = kh_put_oid_map(sub_oid_map, *fromoid, &ret); 3539 /* We've already seen this object. */ 3540 if (ret == 0) 3541 return; 3542 kh_value(sub_oid_map, it) = tooid; 3543 } 3544 3545 static void build_mark_map_one(struct mark_set *from, struct mark_set *to) 3546 { 3547 for_each_mark(from, 0, insert_mapped_mark, to); 3548 } 3549 3550 static void build_mark_map(struct string_list *from, struct string_list *to) 3551 { 3552 struct string_list_item *fromp, *top; 3553 3554 sub_oid_map = kh_init_oid_map(); 3555 3556 for_each_string_list_item(fromp, from) { 3557 top = string_list_lookup(to, fromp->string); 3558 if (!fromp->util) { 3559 die(_("missing from marks for submodule '%s'"), fromp->string); 3560 } else if (!top || !top->util) { 3561 die(_("missing to marks for submodule '%s'"), fromp->string); 3562 } 3563 build_mark_map_one(fromp->util, top->util); 3564 } 3565 } 3566 3567 static struct object_entry *parse_treeish_dataref(const char **p) 3568 { 3569 struct object_id oid; 3570 struct object_entry *e; 3571 3572 if (**p == ':') { /* */ 3573 e = find_mark(marks, parse_mark_ref_space(p)); 3574 if (!e) 3575 die(_("unknown mark: %s"), command_buf.buf); 3576 oidcpy(&oid, &e->idx.oid); 3577 } else { /* */ 3578 if (parse_mapped_oid_hex(*p, &oid, p)) 3579 die(_("invalid dataref: %s"), command_buf.buf); 3580 e = find_object(&oid); 3581 if (*(*p)++ != ' ') 3582 die(_("missing space after tree-ish: %s"), command_buf.buf); 3583 } 3584 3585 while (!e || e->type != OBJ_TREE) 3586 e = dereference(e, &oid); 3587 return e; 3588 } 3589 3590 static void print_ls(int mode, const unsigned char *hash, const char *path) 3591 { 3592 static struct strbuf line = STRBUF_INIT; 3593 3594 /* See show_tree(). */ 3595 const char *type = 3596 S_ISGITLINK(mode) ? commit_type : 3597 S_ISDIR(mode) ? tree_type : 3598 blob_type; 3599 3600 if (!mode) { 3601 /* missing SP path LF */ 3602 strbuf_reset(&line); 3603 strbuf_addstr(&line, "missing "); 3604 quote_c_style(path, &line, NULL, 0); 3605 strbuf_addch(&line, '\n'); 3606 } else { 3607 /* mode SP type SP object_name TAB path LF */ 3608 strbuf_reset(&line); 3609 strbuf_addf(&line, "%06o %s %s\t", 3610 mode & ~NO_DELTA, type, hash_to_hex(hash)); 3611 quote_c_style(path, &line, NULL, 0); 3612 strbuf_addch(&line, '\n'); 3613 } 3614 cat_blob_write(line.buf, line.len); 3615 } 3616 3617 static void parse_ls(struct fast_import_state *state UNUSED, 3618 const char *p, 3619 struct branch *b) 3620 { 3621 static struct strbuf path = STRBUF_INIT; 3622 struct tree_entry *root = NULL; 3623 struct tree_entry leaf = {NULL}; 3624 3625 /* ls SP ( SP)? */ 3626 if (*p == '"') { 3627 if (!b) 3628 die(_("not in a commit: %s"), command_buf.buf); 3629 root = &b->branch_tree; 3630 } else { 3631 struct object_entry *e = parse_treeish_dataref(&p); 3632 root = new_tree_entry(); 3633 oidcpy(&root->versions[1].oid, &e->idx.oid); 3634 if (!is_null_oid(&root->versions[1].oid)) 3635 root->versions[1].mode = S_IFDIR; 3636 load_tree(root); 3637 } 3638 strbuf_reset(&path); 3639 parse_path_eol(&path, p, "path"); 3640 tree_content_get(root, path.buf, &leaf, 1); 3641 /* 3642 * A directory in preparation would have a sha1 of zero 3643 * until it is saved. Save, for simplicity. 3644 */ 3645 if (S_ISDIR(leaf.versions[1].mode)) 3646 store_tree(&leaf); 3647 3648 print_ls(leaf.versions[1].mode, leaf.versions[1].oid.hash, path.buf); 3649 if (leaf.tree) 3650 release_tree_content_recursive(leaf.tree); 3651 if (!b || root != &b->branch_tree) 3652 release_tree_entry(root); 3653 } 3654 3655 static void checkpoint(void) 3656 { 3657 checkpoint_requested = 0; 3658 if (object_count) { 3659 cycle_packfile(); 3660 } 3661 dump_branches(); 3662 dump_tags(); 3663 dump_marks(); 3664 } 3665 3666 static void parse_checkpoint(struct fast_import_state *state UNUSED) 3667 { 3668 checkpoint_requested = 1; 3669 skip_optional_lf(); 3670 } 3671 3672 static void parse_progress(struct fast_import_state *state UNUSED) 3673 { 3674 fwrite(command_buf.buf, 1, command_buf.len, stdout); 3675 fputc('\n', stdout); 3676 fflush(stdout); 3677 skip_optional_lf(); 3678 } 3679 3680 static void parse_alias(struct fast_import_state *state) 3681 { 3682 struct object_entry *e; 3683 struct branch b; 3684 3685 skip_optional_lf(); 3686 read_next_command(state); 3687 3688 /* mark ... */ 3689 parse_mark(state); 3690 if (!next_mark) 3691 die(_("expected 'mark' command, got %s"), command_buf.buf); 3692 3693 /* to ... */ 3694 memset(&b, 0, sizeof(b)); 3695 if (!parse_objectish_with_prefix(state, &b, "to ")) 3696 die(_("expected 'to' command, got %s"), command_buf.buf); 3697 e = find_object(&b.oid); 3698 assert(e); 3699 insert_mark(&marks, next_mark, e); 3700 } 3701 3702 static char* make_fast_import_path(struct fast_import_state *state, 3703 const char *path) 3704 { 3705 if (!relative_marks_paths || is_absolute_path(path)) 3706 return prefix_filename(state->prefix, path); 3707 return repo_git_path(the_repository, "info/fast-import/%s", path); 3708 } 3709 3710 static void option_import_marks(struct fast_import_state *state, 3711 const char *marks, 3712 int from_stream, 3713 int ignore_missing) 3714 { 3715 if (import_marks_file) { 3716 if (from_stream) 3717 die(_("only one import-marks command allowed per stream")); 3718 3719 /* read previous mark file */ 3720 if(!import_marks_file_from_stream) 3721 read_marks(); 3722 } 3723 3724 free(import_marks_file); 3725 import_marks_file = make_fast_import_path(state, marks); 3726 import_marks_file_from_stream = from_stream; 3727 import_marks_file_ignore_missing = ignore_missing; 3728 } 3729 3730 static void option_date_format(const char *fmt) 3731 { 3732 if (!strcmp(fmt, "raw")) 3733 whenspec = WHENSPEC_RAW; 3734 else if (!strcmp(fmt, "raw-permissive")) 3735 whenspec = WHENSPEC_RAW_PERMISSIVE; 3736 else if (!strcmp(fmt, "rfc2822")) 3737 whenspec = WHENSPEC_RFC2822; 3738 else if (!strcmp(fmt, "now")) 3739 whenspec = WHENSPEC_NOW; 3740 else 3741 die(_("unknown --date-format argument %s"), fmt); 3742 } 3743 3744 static unsigned long ulong_arg(const char *option, const char *arg) 3745 { 3746 char *endptr; 3747 unsigned long rv = strtoul(arg, &endptr, 0); 3748 if (strchr(arg, '-') || endptr == arg || *endptr) 3749 die(_("%s: argument must be a non-negative integer"), option); 3750 return rv; 3751 } 3752 3753 static void option_depth(const char *depth) 3754 { 3755 max_depth = ulong_arg("--depth", depth); 3756 if (max_depth > MAX_DEPTH) 3757 die(_("--depth cannot exceed %u"), MAX_DEPTH); 3758 } 3759 3760 static void option_active_branches(const char *branches) 3761 { 3762 max_active_branches = ulong_arg("--active-branches", branches); 3763 } 3764 3765 static void option_export_marks(struct fast_import_state *state, 3766 const char *marks) 3767 { 3768 free(export_marks_file); 3769 export_marks_file = make_fast_import_path(state, marks); 3770 } 3771 3772 static void option_cat_blob_fd(struct fast_import_state *state UNUSED, 3773 const char *fd) 3774 { 3775 unsigned long n = ulong_arg("--cat-blob-fd", fd); 3776 if (n > (unsigned long) INT_MAX) 3777 die(_("--cat-blob-fd cannot exceed %d"), INT_MAX); 3778 cat_blob_fd = (int) n; 3779 } 3780 3781 static void option_export_pack_edges(struct fast_import_state *state, 3782 const char *edges) 3783 { 3784 char *fn = prefix_filename(state->prefix, edges); 3785 if (pack_edges) 3786 fclose(pack_edges); 3787 pack_edges = xfopen(fn, "a"); 3788 free(fn); 3789 } 3790 3791 static void option_rewrite_submodules(struct fast_import_state *state, 3792 const char *arg, 3793 struct string_list *list) 3794 { 3795 struct mark_set *ms; 3796 FILE *fp; 3797 char *s = xstrdup(arg); 3798 char *f = strchr(s, ':'); 3799 if (!f) 3800 die(_("expected format name:filename for submodule rewrite option")); 3801 *f = '\0'; 3802 f++; 3803 CALLOC_ARRAY(ms, 1); 3804 3805 f = prefix_filename(state->prefix, f); 3806 fp = fopen(f, "r"); 3807 if (!fp) 3808 die_errno(_("cannot read '%s'"), f); 3809 read_mark_file(&ms, fp, insert_oid_entry); 3810 fclose(fp); 3811 free(f); 3812 3813 string_list_insert(list, s)->util = ms; 3814 3815 free(s); 3816 } 3817 3818 static void option_max_pack_size(const char *arg) 3819 { 3820 unsigned long v; 3821 3822 if (!git_parse_ulong(arg, &v)) 3823 die(_("--max-pack-size: argument must be a non-negative integer")); 3824 if (v < 8192) { 3825 warning(_("max-pack-size is now in bytes, assuming --max-pack-size=%lum"), v); 3826 v *= 1024 * 1024; 3827 } else if (v < 1024 * 1024) { 3828 warning(_("minimum max-pack-size is 1 MiB")); 3829 v = 1024 * 1024; 3830 } 3831 max_packsize = v; 3832 } 3833 3834 static void option_big_file_threshold(const char *arg) 3835 { 3836 unsigned long v; 3837 3838 if (!git_parse_ulong(arg, &v)) 3839 die(_("--big-file-threshold: argument must be a non-negative integer")); 3840 repo_settings_set_big_file_threshold(the_repository, v); 3841 } 3842 3843 static void option_signed_commits(const char *arg) 3844 { 3845 if (parse_sign_mode(arg, &signed_commit_mode, &signed_commit_keyid)) 3846 usagef(_("unknown --signed-commits mode '%s'"), arg); 3847 } 3848 3849 static void option_signed_tags(const char *arg) 3850 { 3851 if (parse_sign_mode(arg, &signed_tag_mode, &signed_tag_keyid)) 3852 usagef(_("unknown --signed-tags mode '%s'"), arg); 3853 } 3854 3855 static void option_quiet(void) 3856 { 3857 show_stats = 0; 3858 quiet = 1; 3859 } 3860 3861 static void check_unsafe_option(struct fast_import_state *state, 3862 const char *option) 3863 { 3864 if (!state->allow_unsafe_features) 3865 die(_("option '%s' forbidden in input without --allow-unsafe-features"), 3866 option); 3867 } 3868 3869 static int parse_one_option(struct fast_import_state *state, const char *option) 3870 { 3871 if (skip_prefix(option, "max-pack-size=", &option)) { 3872 option_max_pack_size(option); 3873 } else if (skip_prefix(option, "big-file-threshold=", &option)) { 3874 option_big_file_threshold(option); 3875 } else if (skip_prefix(option, "depth=", &option)) { 3876 option_depth(option); 3877 } else if (skip_prefix(option, "active-branches=", &option)) { 3878 option_active_branches(option); 3879 } else if (skip_prefix(option, "export-pack-edges=", &option)) { 3880 check_unsafe_option(state, "export-pack-edges"); 3881 option_export_pack_edges(state, option); 3882 } else if (skip_prefix(option, "signed-commits=", &option)) { 3883 option_signed_commits(option); 3884 } else if (skip_prefix(option, "signed-tags=", &option)) { 3885 option_signed_tags(option); 3886 } else if (!strcmp(option, "quiet")) { 3887 option_quiet(); 3888 } else if (!strcmp(option, "stats")) { 3889 show_stats = 1; 3890 } else if (!strcmp(option, "allow-unsafe-features")) { 3891 ; /* already handled during early option parsing */ 3892 } else { 3893 return 0; 3894 } 3895 3896 return 1; 3897 } 3898 3899 static void check_unsafe_feature(struct fast_import_state *state, 3900 const char *feature) 3901 { 3902 if (!state->allow_unsafe_features) 3903 die(_("feature '%s' forbidden in input without --allow-unsafe-features"), 3904 feature); 3905 } 3906 3907 static int parse_one_feature(struct fast_import_state *state, 3908 const char *feature) 3909 { 3910 const char *arg; 3911 3912 if (skip_prefix(feature, "date-format=", &arg)) { 3913 option_date_format(arg); 3914 } else if (skip_prefix(feature, "import-marks=", &arg)) { 3915 check_unsafe_feature(state, "import-marks"); 3916 option_import_marks(state, arg, 1, 0); 3917 } else if (skip_prefix(feature, "import-marks-if-exists=", &arg)) { 3918 check_unsafe_feature(state, "import-marks-if-exists"); 3919 option_import_marks(state, arg, 1, 1); 3920 } else if (skip_prefix(feature, "export-marks=", &arg)) { 3921 check_unsafe_feature(state, feature); 3922 option_export_marks(state, arg); 3923 } else if (!strcmp(feature, "alias")) { 3924 ; /* Don't die - this feature is supported */ 3925 } else if (skip_prefix(feature, "rewrite-submodules-to=", &arg)) { 3926 check_unsafe_feature(state, "rewrite-submodules-to"); 3927 option_rewrite_submodules(state, arg, &sub_marks_to); 3928 } else if (skip_prefix(feature, "rewrite-submodules-from=", &arg)) { 3929 check_unsafe_feature(state, "rewrite-submodules-from"); 3930 option_rewrite_submodules(state, arg, &sub_marks_from); 3931 } else if (!strcmp(feature, "get-mark")) { 3932 ; /* Don't die - this feature is supported */ 3933 } else if (!strcmp(feature, "cat-blob")) { 3934 ; /* Don't die - this feature is supported */ 3935 } else if (!strcmp(feature, "relative-marks")) { 3936 relative_marks_paths = 1; 3937 } else if (!strcmp(feature, "no-relative-marks")) { 3938 relative_marks_paths = 0; 3939 } else if (!strcmp(feature, "done")) { 3940 require_explicit_termination = 1; 3941 } else if (!strcmp(feature, "force")) { 3942 force_update = 1; 3943 } else if (!strcmp(feature, "notes") || !strcmp(feature, "ls")) { 3944 ; /* do nothing; we have the feature */ 3945 } else { 3946 return 0; 3947 } 3948 3949 return 1; 3950 } 3951 3952 static void parse_feature(struct fast_import_state *state, const char *feature) 3953 { 3954 if (state->seen_data_command) 3955 die(_("got feature command '%s' after data command"), feature); 3956 3957 if (parse_one_feature(state, feature)) 3958 return; 3959 3960 die(_("this version of fast-import does not support feature %s."), feature); 3961 } 3962 3963 static void parse_option(struct fast_import_state *state, const char *option) 3964 { 3965 if (state->seen_data_command) 3966 die(_("got option command '%s' after data command"), option); 3967 3968 if (parse_one_option(state, option)) 3969 return; 3970 3971 die(_("this version of fast-import does not support option: %s"), option); 3972 } 3973 3974 static void git_pack_config(void) 3975 { 3976 int indexversion_value; 3977 int limit; 3978 unsigned long packsizelimit_value; 3979 3980 if (!repo_config_get_ulong(the_repository, "pack.depth", &max_depth)) { 3981 if (max_depth > MAX_DEPTH) 3982 max_depth = MAX_DEPTH; 3983 } 3984 if (!repo_config_get_int(the_repository, "pack.indexversion", &indexversion_value)) { 3985 pack_idx_opts.version = indexversion_value; 3986 if (pack_idx_opts.version > 2) 3987 git_die_config(the_repository, "pack.indexversion", 3988 "bad pack.indexVersion=%"PRIu32, pack_idx_opts.version); 3989 } 3990 if (!repo_config_get_ulong(the_repository, "pack.packsizelimit", &packsizelimit_value)) 3991 max_packsize = packsizelimit_value; 3992 3993 if (!repo_config_get_int(the_repository, "fastimport.unpacklimit", &limit)) 3994 unpack_limit = limit; 3995 else if (!repo_config_get_int(the_repository, "transfer.unpacklimit", &limit)) 3996 unpack_limit = limit; 3997 3998 repo_config(the_repository, git_default_config, NULL); 3999 } 4000 4001 static const char *const fast_import_usage[] = { 4002 N_("git fast-import []"), 4003 NULL 4004 }; 4005 4006 static void parse_argv(struct fast_import_state *state) 4007 { 4008 int argc = parse_options(state->argc, state->argv, state->prefix, 4009 state->option, fast_import_usage, 4010 PARSE_OPT_KEEP_ARGV0); 4011 4012 if (argc > 1) 4013 usage_with_options(fast_import_usage, state->option); 4014 4015 state->seen_data_command = 1; 4016 if (import_marks_file) 4017 read_marks(); 4018 build_mark_map(&sub_marks_from, &sub_marks_to); 4019 } 4020 4021 static int option_parse_date_format(const struct option *opt UNUSED, 4022 const char *arg, int unset) 4023 { 4024 BUG_ON_OPT_NEG(unset); 4025 option_date_format(arg); 4026 return 0; 4027 } 4028 4029 static int option_parse_export_pack_edges(const struct option *opt, 4030 const char *arg, int unset) 4031 { 4032 BUG_ON_OPT_NEG(unset); 4033 option_export_pack_edges(opt->value, arg); 4034 return 0; 4035 } 4036 4037 static int option_parse_max_pack_size(const struct option *opt UNUSED, 4038 const char *arg, int unset) 4039 { 4040 BUG_ON_OPT_NEG(unset); 4041 option_max_pack_size(arg); 4042 return 0; 4043 } 4044 4045 static int option_parse_big_file_threshold(const struct option *opt UNUSED, 4046 const char *arg, int unset) 4047 { 4048 BUG_ON_OPT_NEG(unset); 4049 option_big_file_threshold(arg); 4050 return 0; 4051 } 4052 4053 static int option_parse_signed_commits(const struct option *opt UNUSED, 4054 const char *arg, int unset) 4055 { 4056 BUG_ON_OPT_NEG(unset); 4057 option_signed_commits(arg); 4058 return 0; 4059 } 4060 4061 static int option_parse_signed_tags(const struct option *opt UNUSED, 4062 const char *arg, int unset) 4063 { 4064 BUG_ON_OPT_NEG(unset); 4065 option_signed_tags(arg); 4066 return 0; 4067 } 4068 4069 static int option_parse_rewrite_submodules_from(const struct option *opt, 4070 const char *arg, int unset) 4071 { 4072 BUG_ON_OPT_NEG(unset); 4073 option_rewrite_submodules(opt->value, arg, &sub_marks_from); 4074 return 0; 4075 } 4076 4077 static int option_parse_rewrite_submodules_to(const struct option *opt, 4078 const char *arg, int unset) 4079 { 4080 BUG_ON_OPT_NEG(unset); 4081 option_rewrite_submodules(opt->value, arg, &sub_marks_to); 4082 return 0; 4083 } 4084 4085 static int option_parse_cat_blob_fd(const struct option *opt, 4086 const char *arg, int unset) 4087 { 4088 BUG_ON_OPT_NEG(unset); 4089 option_cat_blob_fd(opt->value, arg); 4090 return 0; 4091 } 4092 4093 static int option_parse_import_marks(const struct option *opt, 4094 const char *arg, int unset) 4095 { 4096 BUG_ON_OPT_NEG(unset); 4097 option_import_marks(opt->value, arg, 0, 0); 4098 return 0; 4099 } 4100 4101 static int option_parse_import_marks_if_exists(const struct option *opt, 4102 const char *arg, int unset) 4103 { 4104 BUG_ON_OPT_NEG(unset); 4105 option_import_marks(opt->value, arg, 0, 1); 4106 return 0; 4107 } 4108 4109 static int option_parse_export_marks(const struct option *opt, 4110 const char *arg, int unset) 4111 { 4112 BUG_ON_OPT_NEG(unset); 4113 option_export_marks(opt->value, arg); 4114 return 0; 4115 } 4116 4117 static int option_parse_depth(const struct option *opt UNUSED, 4118 const char *arg, int unset) 4119 { 4120 BUG_ON_OPT_NEG(unset); 4121 option_depth(arg); 4122 return 0; 4123 } 4124 4125 static int option_parse_active_branches(const struct option *opt UNUSED, 4126 const char *arg, int unset) 4127 { 4128 BUG_ON_OPT_NEG(unset); 4129 option_active_branches(arg); 4130 return 0; 4131 } 4132 4133 static int option_parse_quiet(const struct option *opt UNUSED, 4134 const char *arg UNUSED, int unset) 4135 { 4136 BUG_ON_OPT_NEG(unset); 4137 option_quiet(); 4138 return 0; 4139 } 4140 4141 int cmd_fast_import(int argc, 4142 const char **argv, 4143 const char *prefix, 4144 struct repository *repo) 4145 { 4146 struct fast_import_state state; 4147 4148 struct option fast_import_options[] = { 4149 OPT_GROUP(N_("Common")), 4150 OPT_CALLBACK_F(0, "date-format", NULL, N_("fmt"), 4151 N_("format of the commit/tag dates"), 4152 PARSE_OPT_NONEG, option_parse_date_format), 4153 OPT_BOOL_F(0, "stats", &show_stats, 4154 N_("display some basic statistics (objects, packfiles and memory)"), 4155 PARSE_OPT_NONEG), 4156 OPT_CALLBACK_F(0, "quiet", NULL, NULL, 4157 N_("disable the output shown by --stats"), 4158 PARSE_OPT_NOARG | PARSE_OPT_NONEG, 4159 option_parse_quiet), 4160 OPT_BOOL_F(0, "force", &force_update, 4161 N_("force updating modified existing branches"), PARSE_OPT_NONEG), 4162 OPT_BOOL_F(0, "done", &require_explicit_termination, 4163 N_("require a terminating 'done' command"), PARSE_OPT_NONEG), 4164 OPT_CALLBACK_F(0, "max-pack-size", NULL, N_("n"), 4165 N_("maximum size of each output pack file"), 4166 PARSE_OPT_NONEG, option_parse_max_pack_size), 4167 OPT_CALLBACK_F(0, "big-file-threshold", NULL, N_("n"), 4168 N_("maximum size of a blob that will be deltified"), 4169 PARSE_OPT_NONEG, option_parse_big_file_threshold), 4170 OPT_CALLBACK_F(0, "depth", NULL, N_("n"), 4171 N_("maximum delta depth"), 4172 PARSE_OPT_NONEG, option_parse_depth), 4173 OPT_CALLBACK_F(0, "active-branches", NULL, N_("n"), 4174 N_("maximum number of branches to maintain active"), 4175 PARSE_OPT_NONEG, option_parse_active_branches), 4176 OPT_GROUP(N_("Marks")), 4177 OPT_CALLBACK_F(0, "import-marks", &state, N_("file"), 4178 N_("import marks from "), 4179 PARSE_OPT_NONEG, option_parse_import_marks), 4180 OPT_CALLBACK_F(0, "import-marks-if-exists", &state, N_("file"), 4181 N_("import marks from if it exists"), 4182 PARSE_OPT_NONEG, option_parse_import_marks_if_exists), 4183 OPT_CALLBACK_F(0, "export-marks", &state, N_("file"), 4184 N_("dump marks to "), 4185 PARSE_OPT_NONEG, option_parse_export_marks), 4186 OPT_BOOL(0, "relative-marks", &relative_marks_paths, 4187 N_("are --(import|export)-marks= paths relative to '.git/info/fast-import'?")), 4188 OPT_GROUP(N_("Submodule rewrite")), 4189 OPT_CALLBACK_F(0, "rewrite-submodules-from", &state, N_("name:filename"), 4190 N_("rewrite object IDs for submodule from "), 4191 PARSE_OPT_NONEG, option_parse_rewrite_submodules_from), 4192 OPT_CALLBACK_F(0, "rewrite-submodules-to", &state, N_("name:filename"), 4193 N_("rewrite object IDs for submodule to "), 4194 PARSE_OPT_NONEG, option_parse_rewrite_submodules_to), 4195 OPT_GROUP(N_("Signing")), 4196 OPT_CALLBACK_F(0, "signed-commits", NULL, N_("mode"), 4197 N_("how to handle signed commits"), 4198 PARSE_OPT_NONEG, option_parse_signed_commits), 4199 OPT_CALLBACK_F(0, "signed-tags", NULL, N_("mode"), 4200 N_("how to handle signed tags"), 4201 PARSE_OPT_NONEG, option_parse_signed_tags), 4202 OPT_HIDDEN_GROUP(N_("Advanced")), 4203 OPT_BOOL_F(0, "allow-unsafe-features", &state.allow_unsafe_features, 4204 N_("allow unsafe mark commands from the stream"), 4205 PARSE_OPT_HIDDEN | PARSE_OPT_NONEG), 4206 OPT_CALLBACK_F(0, "export-pack-edges", &state, N_("file"), 4207 N_("dump edge commits to "), 4208 PARSE_OPT_HIDDEN | PARSE_OPT_NONEG, 4209 option_parse_export_pack_edges), 4210 OPT_CALLBACK_F(0, "cat-blob-fd", &state, N_("fd"), 4211 N_("write some responses to instead of stdout"), 4212 PARSE_OPT_HIDDEN | PARSE_OPT_NONEG, 4213 option_parse_cat_blob_fd), 4214 OPT_END() 4215 }; 4216 4217 show_usage_with_options_if_asked(argc, argv, fast_import_usage, fast_import_options); 4218 4219 fast_import_state_init(&state, argc, argv, prefix, fast_import_options); 4220 4221 reset_pack_idx_option(&pack_idx_opts); 4222 git_pack_config(); 4223 4224 alloc_objects(object_entry_alloc); 4225 strbuf_init(&command_buf, 0); 4226 CALLOC_ARRAY(atom_table, atom_table_sz); 4227 CALLOC_ARRAY(branch_table, branch_table_sz); 4228 CALLOC_ARRAY(avail_tree_table, avail_tree_table_sz); 4229 marks = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set)); 4230 4231 hashmap_init(&object_table, object_entry_hashcmp, NULL, 0); 4232 4233 /* 4234 * We don't parse most options until after we've seen the set of 4235 * "feature" lines at the start of the stream (which allows the command 4236 * line to override stream data). But we must do an early parse of any 4237 * command-line options that impact how we interpret the feature lines. 4238 * 4239 * NEEDSWORK: This scan only matches the exact "--allow-unsafe-features" 4240 * spelling and stops at the first argument that doesn't start with a 4241 * dash. As parse_options() below also accepts unambiguous abbreviations 4242 * and values separated by a space from their option, the two disagree 4243 * for command lines like "--allow-unsafe" or "--depth 5 4244 * --allow-unsafe-features": parse_options() accepts the option, but 4245 * this scan doesn't see it, so unsafe features from the stream are 4246 * still refused. This errs on the safe side, but should be fixed by 4247 * teaching this scan about the options that take a value. 4248 */ 4249 for (int i = 1; i < argc; i++) { 4250 const char *arg = argv[i]; 4251 if (*arg != '-' || !strcmp(arg, "--")) 4252 break; 4253 if (!strcmp(arg, "--allow-unsafe-features")) 4254 state.allow_unsafe_features = 1; 4255 } 4256 4257 rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free)); 4258 for (unsigned int i = 0; i < (cmd_save - 1); i++) 4259 rc_free[i].next = &rc_free[i + 1]; 4260 rc_free[cmd_save - 1].next = NULL; 4261 4262 start_packfile(); 4263 set_die_routine(die_nicely); 4264 set_checkpoint_signal(); 4265 while (read_next_command(&state) != EOF) { 4266 const char *v; 4267 if (!strcmp("blob", command_buf.buf)) 4268 parse_new_blob(&state); 4269 else if (skip_prefix(command_buf.buf, "commit ", &v)) 4270 parse_new_commit(&state, v); 4271 else if (skip_prefix(command_buf.buf, "tag ", &v)) 4272 parse_new_tag(&state, v); 4273 else if (skip_prefix(command_buf.buf, "reset ", &v)) 4274 parse_reset_branch(&state, v); 4275 else if (skip_prefix(command_buf.buf, "ls ", &v)) 4276 parse_ls(&state, v, NULL); 4277 else if (skip_prefix(command_buf.buf, "cat-blob ", &v)) 4278 parse_cat_blob(&state, v); 4279 else if (skip_prefix(command_buf.buf, "get-mark ", &v)) 4280 parse_get_mark(&state, v); 4281 else if (!strcmp("checkpoint", command_buf.buf)) 4282 parse_checkpoint(&state); 4283 else if (!strcmp("done", command_buf.buf)) 4284 break; 4285 else if (!strcmp("alias", command_buf.buf)) 4286 parse_alias(&state); 4287 else if (starts_with(command_buf.buf, "progress ")) 4288 parse_progress(&state); 4289 else if (skip_prefix(command_buf.buf, "feature ", &v)) 4290 parse_feature(&state, v); 4291 else if (skip_prefix(command_buf.buf, "option git ", &v)) 4292 parse_option(&state, v); 4293 else if (starts_with(command_buf.buf, "option ")) 4294 /* ignore non-git options*/; 4295 else 4296 die(_("unsupported command: %s"), command_buf.buf); 4297 4298 if (checkpoint_requested) 4299 checkpoint(); 4300 } 4301 4302 /* argv hasn't been parsed yet, do so */ 4303 if (!state.seen_data_command) 4304 parse_argv(&state); 4305 4306 if (require_explicit_termination && feof(stdin)) 4307 die(_("stream ends early")); 4308 4309 end_packfile(); 4310 4311 dump_branches(); 4312 dump_tags(); 4313 unkeep_all_packs(); 4314 dump_marks(); 4315 4316 if (pack_edges) 4317 fclose(pack_edges); 4318 4319 if (show_stats) { 4320 uintmax_t total_count = 0, duplicate_count = 0; 4321 for (size_t i = 0; i < ARRAY_SIZE(object_count_by_type); i++) 4322 total_count += object_count_by_type[i]; 4323 for (size_t i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++) 4324 duplicate_count += duplicate_count_by_type[i]; 4325 4326 fprintf(stderr, "%s statistics:\n", argv[0]); 4327 fprintf(stderr, "---------------------------------------------------------------------\n"); 4328 fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count); 4329 fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count); 4330 fprintf(stderr, " blobs : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB], delta_count_attempts_by_type[OBJ_BLOB]); 4331 fprintf(stderr, " trees : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE], delta_count_attempts_by_type[OBJ_TREE]); 4332 fprintf(stderr, " commits: %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT], delta_count_attempts_by_type[OBJ_COMMIT]); 4333 fprintf(stderr, " tags : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG], delta_count_attempts_by_type[OBJ_TAG]); 4334 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count); 4335 fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count); 4336 fprintf(stderr, " atoms: %10u\n", atom_cnt); 4337 fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (tree_entry_allocd + fi_mem_pool.pool_alloc + alloc_count*sizeof(struct object_entry))/1024); 4338 fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd + fi_mem_pool.pool_alloc) /1024)); 4339 fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024); 4340 fprintf(stderr, "---------------------------------------------------------------------\n"); 4341 pack_report(repo); 4342 fprintf(stderr, "---------------------------------------------------------------------\n"); 4343 fprintf(stderr, "\n"); 4344 } 4345 4346 return failure ? 1 : 0; 4347 }