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