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