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 struct packed_git *p = all_packs[oe->pack_id];
1245 if (p == pack_data && p->pack_size < (pack_size + the_hash_algo->rawsz)) {
1246 /* The object is stored in the packfile we are writing to
1247 * and we have modified it since the last time we scanned
1248 * back to read a previously written object. If an old
1249 * window covered [p->pack_size, p->pack_size + rawsz) its
1250 * data is stale and is not valid. Closing all windows
1251 * and updating the packfile length ensures we can read
1252 * the newly written data.
1253 */
1254 close_pack_windows(p);
1255 hashflush(pack_file);
1256
1257 /* We have to offer rawsz bytes additional on the end of
1258 * the packfile as the core unpacker code assumes the
1259 * footer is present at the file end and must promise
1260 * at least rawsz bytes within any window it maps. But
1261 * we don't actually create the footer here.
1262 */
1263 p->pack_size = pack_size + the_hash_algo->rawsz;
1264 }
1265 return unpack_entry(the_repository, p, oe->idx.offset, &type, sizep);
1266 }
1267
1268 static void load_tree(struct tree_entry *root)
1269 {
1270 struct object_id *oid = &root->versions[1].oid;
1271 struct object_entry *myoe;
1272 struct tree_content *t;
1273 unsigned long size;
1274 char *buf;
1275 const char *c;
1276
1277 root->tree = t = new_tree_content(8);
1278 if (is_null_oid(oid))
1279 return;
1280
1281 myoe = find_object(oid);
1282 if (myoe && myoe->pack_id != MAX_PACK_ID) {
1283 if (myoe->type != OBJ_TREE)
1284 die(_("not a tree: %s"), oid_to_hex(oid));
1285 t->delta_depth = myoe->depth;
1286 buf = gfi_unpack_entry(myoe, &size);
1287 if (!buf)
1288 die(_("can't load tree %s"), oid_to_hex(oid));
1289 } else {
1290 enum object_type type;
1291 buf = odb_read_object(the_repository->objects, oid, &type, &size);
1292 if (!buf || type != OBJ_TREE)
1293 die(_("can't load tree %s"), oid_to_hex(oid));
1294 }
1295
1296 c = buf;
1297 while (c != (buf + size)) {
1298 struct tree_entry *e = new_tree_entry();
1299
1300 if (t->entry_count == t->entry_capacity)
1301 root->tree = t = grow_tree_content(t, t->entry_count);
1302 t->entries[t->entry_count++] = e;
1303
1304 e->tree = NULL;
1305 c = parse_mode(c, &e->versions[1].mode);
1306 if (!c)
1307 die(_("corrupt mode in %s"), oid_to_hex(oid));
1308 e->versions[0].mode = e->versions[1].mode;
1309 e->name = to_atom(c, strlen(c));
1310 c += e->name->str_len + 1;
1311 oidread(&e->versions[0].oid, (unsigned char *)c,
1312 the_repository->hash_algo);
1313 oidread(&e->versions[1].oid, (unsigned char *)c,
1314 the_repository->hash_algo);
1315 c += the_hash_algo->rawsz;
1316 }
1317 free(buf);
1318 }
1319
1320 static int tecmp0 (const void *_a, const void *_b)
1321 {
1322 struct tree_entry *a = *((struct tree_entry**)_a);
1323 struct tree_entry *b = *((struct tree_entry**)_b);
1324 return base_name_compare(
1325 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1326 b->name->str_dat, b->name->str_len, b->versions[0].mode);
1327 }
1328
1329 static int tecmp1 (const void *_a, const void *_b)
1330 {
1331 struct tree_entry *a = *((struct tree_entry**)_a);
1332 struct tree_entry *b = *((struct tree_entry**)_b);
1333 return base_name_compare(
1334 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1335 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1336 }
1337
1338 static void mktree(struct tree_content *t, int v, struct strbuf *b)
1339 {
1340 size_t maxlen = 0;
1341 unsigned int i;
1342
1343 if (!v)
1344 QSORT(t->entries, t->entry_count, tecmp0);
1345 else
1346 QSORT(t->entries, t->entry_count, tecmp1);
1347
1348 for (i = 0; i < t->entry_count; i++) {
1349 if (t->entries[i]->versions[v].mode)
1350 maxlen += t->entries[i]->name->str_len + 34;
1351 }
1352
1353 strbuf_reset(b);
1354 strbuf_grow(b, maxlen);
1355 for (i = 0; i < t->entry_count; i++) {
1356 struct tree_entry *e = t->entries[i];
1357 if (!e->versions[v].mode)
1358 continue;
1359 strbuf_addf(b, "%o %s%c",
1360 (unsigned int)(e->versions[v].mode & ~NO_DELTA),
1361 e->name->str_dat, '\0');
1362 strbuf_add(b, e->versions[v].oid.hash, the_hash_algo->rawsz);
1363 }
1364 }
1365
1366 static void store_tree(struct tree_entry *root)
1367 {
1368 struct tree_content *t;
1369 unsigned int i, j, del;
1370 struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 };
1371 struct object_entry *le = NULL;
1372
1373 if (!is_null_oid(&root->versions[1].oid))
1374 return;
1375
1376 if (!root->tree)
1377 load_tree(root);
1378 t = root->tree;
1379
1380 for (i = 0; i < t->entry_count; i++) {
1381 if (t->entries[i]->tree)
1382 store_tree(t->entries[i]);
1383 }
1384
1385 if (!(root->versions[0].mode & NO_DELTA))
1386 le = find_object(&root->versions[0].oid);
1387 if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) {
1388 mktree(t, 0, &old_tree);
1389 lo.data = old_tree;
1390 lo.offset = le->idx.offset;
1391 lo.depth = t->delta_depth;
1392 }
1393
1394 mktree(t, 1, &new_tree);
1395 store_object(OBJ_TREE, &new_tree, &lo, &root->versions[1].oid, 0);
1396
1397 t->delta_depth = lo.depth;
1398 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1399 struct tree_entry *e = t->entries[i];
1400 if (e->versions[1].mode) {
1401 e->versions[0].mode = e->versions[1].mode;
1402 oidcpy(&e->versions[0].oid, &e->versions[1].oid);
1403 t->entries[j++] = e;
1404 } else {
1405 release_tree_entry(e);
1406 del++;
1407 }
1408 }
1409 t->entry_count -= del;
1410 }
1411
1412 static void tree_content_replace(
1413 struct tree_entry *root,
1414 const struct object_id *oid,
1415 const uint16_t mode,
1416 struct tree_content *newtree)
1417 {
1418 if (!S_ISDIR(mode))
1419 die(_("root cannot be a non-directory"));
1420 oidclr(&root->versions[0].oid, the_repository->hash_algo);
1421 oidcpy(&root->versions[1].oid, oid);
1422 if (root->tree)
1423 release_tree_content_recursive(root->tree);
1424 root->tree = newtree;
1425 }
1426
1427 static int tree_content_set(
1428 struct tree_entry *root,
1429 const char *p,
1430 const struct object_id *oid,
1431 const uint16_t mode,
1432 struct tree_content *subtree)
1433 {
1434 struct tree_content *t;
1435 const char *slash1;
1436 unsigned int i, n;
1437 struct tree_entry *e;
1438
1439 slash1 = strchrnul(p, '/');
1440 n = slash1 - p;
1441 if (!n)
1442 die(_("empty path component found in input"));
1443 if (!*slash1 && !S_ISDIR(mode) && subtree)
1444 die(_("non-directories cannot have subtrees"));
1445
1446 if (!root->tree)
1447 load_tree(root);
1448 t = root->tree;
1449 for (i = 0; i < t->entry_count; i++) {
1450 e = t->entries[i];
1451 if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
1452 if (!*slash1) {
1453 if (!S_ISDIR(mode)
1454 && e->versions[1].mode == mode
1455 && oideq(&e->versions[1].oid, oid))
1456 return 0;
1457 e->versions[1].mode = mode;
1458 oidcpy(&e->versions[1].oid, oid);
1459 if (e->tree)
1460 release_tree_content_recursive(e->tree);
1461 e->tree = subtree;
1462
1463 /*
1464 * We need to leave e->versions[0].sha1 alone
1465 * to avoid modifying the preimage tree used
1466 * when writing out the parent directory.
1467 * But after replacing the subdir with a
1468 * completely different one, it's not a good
1469 * delta base any more, and besides, we've
1470 * thrown away the tree entries needed to
1471 * make a delta against it.
1472 *
1473 * So let's just explicitly disable deltas
1474 * for the subtree.
1475 */
1476 if (S_ISDIR(e->versions[0].mode))
1477 e->versions[0].mode |= NO_DELTA;
1478
1479 oidclr(&root->versions[1].oid, the_repository->hash_algo);
1480 return 1;
1481 }
1482 if (!S_ISDIR(e->versions[1].mode)) {
1483 e->tree = new_tree_content(8);
1484 e->versions[1].mode = S_IFDIR;
1485 }
1486 if (!e->tree)
1487 load_tree(e);
1488 if (tree_content_set(e, slash1 + 1, oid, mode, subtree)) {
1489 oidclr(&root->versions[1].oid, the_repository->hash_algo);
1490 return 1;
1491 }
1492 return 0;
1493 }
1494 }
1495
1496 if (t->entry_count == t->entry_capacity)
1497 root->tree = t = grow_tree_content(t, t->entry_count);
1498 e = new_tree_entry();
1499 e->name = to_atom(p, n);
1500 e->versions[0].mode = 0;
1501 oidclr(&e->versions[0].oid, the_repository->hash_algo);
1502 t->entries[t->entry_count++] = e;
1503 if (*slash1) {
1504 e->tree = new_tree_content(8);
1505 e->versions[1].mode = S_IFDIR;
1506 tree_content_set(e, slash1 + 1, oid, mode, subtree);
1507 } else {
1508 e->tree = subtree;
1509 e->versions[1].mode = mode;
1510 oidcpy(&e->versions[1].oid, oid);
1511 }
1512 oidclr(&root->versions[1].oid, the_repository->hash_algo);
1513 return 1;
1514 }
1515
1516 static int tree_content_remove(
1517 struct tree_entry *root,
1518 const char *p,
1519 struct tree_entry *backup_leaf,
1520 int allow_root)
1521 {
1522 struct tree_content *t;
1523 const char *slash1;
1524 unsigned int i, n;
1525 struct tree_entry *e;
1526
1527 slash1 = strchrnul(p, '/');
1528 n = slash1 - p;
1529
1530 if (!root->tree)
1531 load_tree(root);
1532
1533 if (!*p && allow_root) {
1534 e = root;
1535 goto del_entry;
1536 }
1537
1538 t = root->tree;
1539 for (i = 0; i < t->entry_count; i++) {
1540 e = t->entries[i];
1541 if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
1542 if (*slash1 && !S_ISDIR(e->versions[1].mode))
1543 /*
1544 * If p names a file in some subdirectory, and a
1545 * file or symlink matching the name of the
1546 * parent directory of p exists, then p cannot
1547 * exist and need not be deleted.
1548 */
1549 return 1;
1550 if (!*slash1 || !S_ISDIR(e->versions[1].mode))
1551 goto del_entry;
1552 if (!e->tree)
1553 load_tree(e);
1554 if (tree_content_remove(e, slash1 + 1, backup_leaf, 0)) {
1555 for (n = 0; n < e->tree->entry_count; n++) {
1556 if (e->tree->entries[n]->versions[1].mode) {
1557 oidclr(&root->versions[1].oid,
1558 the_repository->hash_algo);
1559 return 1;
1560 }
1561 }
1562 backup_leaf = NULL;
1563 goto del_entry;
1564 }
1565 return 0;
1566 }
1567 }
1568 return 0;
1569
1570 del_entry:
1571 if (backup_leaf)
1572 memcpy(backup_leaf, e, sizeof(*backup_leaf));
1573 else if (e->tree)
1574 release_tree_content_recursive(e->tree);
1575 e->tree = NULL;
1576 e->versions[1].mode = 0;
1577 oidclr(&e->versions[1].oid, the_repository->hash_algo);
1578 oidclr(&root->versions[1].oid, the_repository->hash_algo);
1579 return 1;
1580 }
1581
1582 static int tree_content_get(
1583 struct tree_entry *root,
1584 const char *p,
1585 struct tree_entry *leaf,
1586 int allow_root)
1587 {
1588 struct tree_content *t;
1589 const char *slash1;
1590 unsigned int i, n;
1591 struct tree_entry *e;
1592
1593 slash1 = strchrnul(p, '/');
1594 n = slash1 - p;
1595 if (!n && !allow_root)
1596 die(_("empty path component found in input"));
1597
1598 if (!root->tree)
1599 load_tree(root);
1600
1601 if (!n) {
1602 e = root;
1603 goto found_entry;
1604 }
1605
1606 t = root->tree;
1607 for (i = 0; i < t->entry_count; i++) {
1608 e = t->entries[i];
1609 if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
1610 if (!*slash1)
1611 goto found_entry;
1612 if (!S_ISDIR(e->versions[1].mode))
1613 return 0;
1614 if (!e->tree)
1615 load_tree(e);
1616 return tree_content_get(e, slash1 + 1, leaf, 0);
1617 }
1618 }
1619 return 0;
1620
1621 found_entry:
1622 memcpy(leaf, e, sizeof(*leaf));
1623 if (e->tree && is_null_oid(&e->versions[1].oid))
1624 leaf->tree = dup_tree_content(e->tree);
1625 else
1626 leaf->tree = NULL;
1627 return 1;
1628 }
1629
1630 static int update_branch(struct branch *b)
1631 {
1632 static const char *msg = "fast-import";
1633 struct ref_transaction *transaction;
1634 struct object_id old_oid;
1635 struct strbuf err = STRBUF_INIT;
1636 static const char *replace_prefix = "refs/replace/";
1637
1638 if (starts_with(b->name, replace_prefix) &&
1639 !strcmp(b->name + strlen(replace_prefix),
1640 oid_to_hex(&b->oid))) {
1641 if (!quiet)
1642 warning(_("dropping %s since it would point to "
1643 "itself (i.e. to %s)"),
1644 b->name, oid_to_hex(&b->oid));
1645 refs_delete_ref(get_main_ref_store(the_repository),
1646 NULL, b->name, NULL, 0);
1647 return 0;
1648 }
1649 if (is_null_oid(&b->oid)) {
1650 if (b->delete)
1651 refs_delete_ref(get_main_ref_store(the_repository),
1652 NULL, b->name, NULL, 0);
1653 return 0;
1654 }
1655 if (refs_read_ref(get_main_ref_store(the_repository), b->name, &old_oid))
1656 oidclr(&old_oid, the_repository->hash_algo);
1657 if (!force_update && !is_null_oid(&old_oid)) {
1658 struct commit *old_cmit, *new_cmit;
1659 int ret;
1660
1661 old_cmit = lookup_commit_reference_gently(the_repository,
1662 &old_oid, 0);
1663 new_cmit = lookup_commit_reference_gently(the_repository,
1664 &b->oid, 0);
1665 if (!old_cmit || !new_cmit)
1666 return error(_("branch %s is missing commits."), b->name);
1667
1668 ret = repo_in_merge_bases(the_repository, old_cmit, new_cmit);
1669 if (ret < 0)
1670 exit(128);
1671 if (!ret) {
1672 warning(_("not updating %s"
1673 " (new tip %s does not contain %s)"),
1674 b->name, oid_to_hex(&b->oid),
1675 oid_to_hex(&old_oid));
1676 return -1;
1677 }
1678 }
1679 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
1680 0, &err);
1681 if (!transaction ||
1682 ref_transaction_update(transaction, b->name, &b->oid, &old_oid,
1683 NULL, NULL, 0, msg, &err) ||
1684 ref_transaction_commit(transaction, &err)) {
1685 ref_transaction_free(transaction);
1686 error("%s", err.buf);
1687 strbuf_release(&err);
1688 return -1;
1689 }
1690 ref_transaction_free(transaction);
1691 strbuf_release(&err);
1692 return 0;
1693 }
1694
1695 static void dump_branches(void)
1696 {
1697 unsigned int i;
1698 struct branch *b;
1699
1700 for (i = 0; i < branch_table_sz; i++) {
1701 for (b = branch_table[i]; b; b = b->table_next_branch)
1702 failure |= update_branch(b);
1703 }
1704 }
1705
1706 static void dump_tags(void)
1707 {
1708 static const char *msg = "fast-import";
1709 struct tag *t;
1710 struct strbuf ref_name = STRBUF_INIT;
1711 struct strbuf err = STRBUF_INIT;
1712 struct ref_transaction *transaction;
1713
1714 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
1715 0, &err);
1716 if (!transaction) {
1717 failure |= error("%s", err.buf);
1718 goto cleanup;
1719 }
1720 for (t = first_tag; t; t = t->next_tag) {
1721 strbuf_reset(&ref_name);
1722 strbuf_addf(&ref_name, "refs/tags/%s", t->name);
1723
1724 if (ref_transaction_update(transaction, ref_name.buf,
1725 &t->oid, NULL, NULL, NULL,
1726 0, msg, &err)) {
1727 failure |= error("%s", err.buf);
1728 goto cleanup;
1729 }
1730 }
1731 if (ref_transaction_commit(transaction, &err))
1732 failure |= error("%s", err.buf);
1733
1734 cleanup:
1735 ref_transaction_free(transaction);
1736 strbuf_release(&ref_name);
1737 strbuf_release(&err);
1738 }
1739
1740 static void dump_marks(void)
1741 {
1742 struct lock_file mark_lock = LOCK_INIT;
1743 FILE *f;
1744
1745 if (!export_marks_file || (import_marks_file && !import_marks_file_done))
1746 return;
1747
1748 if (safe_create_leading_directories_const(the_repository, export_marks_file)) {
1749 failure |= error_errno(_("unable to create leading directories of %s"),
1750 export_marks_file);
1751 return;
1752 }
1753
1754 if (hold_lock_file_for_update(&mark_lock, export_marks_file, 0) < 0) {
1755 failure |= error_errno(_("unable to write marks file %s"),
1756 export_marks_file);
1757 return;
1758 }
1759
1760 f = fdopen_lock_file(&mark_lock, "w");
1761 if (!f) {
1762 int saved_errno = errno;
1763 rollback_lock_file(&mark_lock);
1764 failure |= error(_("unable to write marks file %s: %s"),
1765 export_marks_file, strerror(saved_errno));
1766 return;
1767 }
1768
1769 for_each_mark(marks, 0, dump_marks_fn, f);
1770 if (commit_lock_file(&mark_lock)) {
1771 failure |= error_errno(_("unable to write file %s"),
1772 export_marks_file);
1773 return;
1774 }
1775 }
1776
1777 static void insert_object_entry(struct mark_set **s, struct object_id *oid, uintmax_t mark)
1778 {
1779 struct object_entry *e;
1780 e = find_object(oid);
1781 if (!e) {
1782 enum object_type type = odb_read_object_info(the_repository->objects,
1783 oid, NULL);
1784 if (type < 0)
1785 die(_("object not found: %s"), oid_to_hex(oid));
1786 e = insert_object(oid);
1787 e->type = type;
1788 e->pack_id = MAX_PACK_ID;
1789 e->idx.offset = 1; /* just not zero! */
1790 }
1791 insert_mark(s, mark, e);
1792 }
1793
1794 static void insert_oid_entry(struct mark_set **s, struct object_id *oid, uintmax_t mark)
1795 {
1796 insert_mark(s, mark, xmemdupz(oid, sizeof(*oid)));
1797 }
1798
1799 static void read_mark_file(struct mark_set **s, FILE *f, mark_set_inserter_t inserter)
1800 {
1801 char line[512];
1802 while (fgets(line, sizeof(line), f)) {
1803 uintmax_t mark;
1804 char *end;
1805 struct object_id oid;
1806
1807 /* Ensure SHA-1 objects are padded with zeros. */
1808 memset(oid.hash, 0, sizeof(oid.hash));
1809
1810 end = strchr(line, '\n');
1811 if (line[0] != ':' || !end)
1812 die(_("corrupt mark line: %s"), line);
1813 *end = 0;
1814 mark = strtoumax(line + 1, &end, 10);
1815 if (!mark || end == line + 1
1816 || *end != ' '
1817 || get_oid_hex_any(end + 1, &oid) == GIT_HASH_UNKNOWN)
1818 die(_("corrupt mark line: %s"), line);
1819 inserter(s, &oid, mark);
1820 }
1821 }
1822
1823 static void read_marks(void)
1824 {
1825 FILE *f = fopen(import_marks_file, "r");
1826 if (f)
1827 ;
1828 else if (import_marks_file_ignore_missing && errno == ENOENT)
1829 goto done; /* Marks file does not exist */
1830 else
1831 die_errno(_("cannot read '%s'"), import_marks_file);
1832 read_mark_file(&marks, f, insert_object_entry);
1833 fclose(f);
1834 done:
1835 import_marks_file_done = 1;
1836 }
1837
1838
1839 static int read_next_command(void)
1840 {
1841 static int stdin_eof = 0;
1842
1843 if (stdin_eof) {
1844 unread_command_buf = 0;
1845 return EOF;
1846 }
1847
1848 for (;;) {
1849 if (unread_command_buf) {
1850 unread_command_buf = 0;
1851 } else {
1852 struct recent_command *rc;
1853
1854 stdin_eof = strbuf_getline_lf(&command_buf, stdin);
1855 if (stdin_eof)
1856 return EOF;
1857
1858 if (!seen_data_command
1859 && !starts_with(command_buf.buf, "feature ")
1860 && !starts_with(command_buf.buf, "option ")) {
1861 parse_argv();
1862 }
1863
1864 rc = rc_free;
1865 if (rc)
1866 rc_free = rc->next;
1867 else {
1868 rc = cmd_hist.next;
1869 cmd_hist.next = rc->next;
1870 cmd_hist.next->prev = &cmd_hist;
1871 free(rc->buf);
1872 }
1873
1874 rc->buf = xstrdup(command_buf.buf);
1875 rc->prev = cmd_tail;
1876 rc->next = cmd_hist.prev;
1877 rc->prev->next = rc;
1878 cmd_tail = rc;
1879 }
1880 if (command_buf.buf[0] == '#')
1881 continue;
1882 return 0;
1883 }
1884 }
1885
1886 static void skip_optional_lf(void)
1887 {
1888 int term_char = fgetc(stdin);
1889 if (term_char != '\n' && term_char != EOF)
1890 ungetc(term_char, stdin);
1891 }
1892
1893 static void parse_mark(void)
1894 {
1895 const char *v;
1896 if (skip_prefix(command_buf.buf, "mark :", &v)) {
1897 next_mark = strtoumax(v, NULL, 10);
1898 read_next_command();
1899 }
1900 else
1901 next_mark = 0;
1902 }
1903
1904 static void parse_original_identifier(void)
1905 {
1906 const char *v;
1907 if (skip_prefix(command_buf.buf, "original-oid ", &v))
1908 read_next_command();
1909 }
1910
1911 static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
1912 {
1913 const char *data;
1914 strbuf_reset(sb);
1915
1916 if (!skip_prefix(command_buf.buf, "data ", &data))
1917 die(_("expected 'data n' command, found: %s"), command_buf.buf);
1918
1919 if (skip_prefix(data, "<<", &data)) {
1920 char *term = xstrdup(data);
1921 size_t term_len = command_buf.len - (data - command_buf.buf);
1922
1923 for (;;) {
1924 if (strbuf_getline_lf(&command_buf, stdin) == EOF)
1925 die(_("EOF in data (terminator '%s' not found)"), term);
1926 if (term_len == command_buf.len
1927 && !strcmp(term, command_buf.buf))
1928 break;
1929 strbuf_addbuf(sb, &command_buf);
1930 strbuf_addch(sb, '\n');
1931 }
1932 free(term);
1933 }
1934 else {
1935 uintmax_t len = strtoumax(data, NULL, 10);
1936 size_t n = 0, length = (size_t)len;
1937
1938 if (limit && limit < len) {
1939 *len_res = len;
1940 return 0;
1941 }
1942 if (length < len)
1943 die(_("data is too large to use in this context"));
1944
1945 while (n < length) {
1946 size_t s = strbuf_fread(sb, length - n, stdin);
1947 if (!s && feof(stdin))
1948 die(_("EOF in data (%lu bytes remaining)"),
1949 (unsigned long)(length - n));
1950 n += s;
1951 }
1952 }
1953
1954 skip_optional_lf();
1955 return 1;
1956 }
1957
1958 static int validate_raw_date(const char *src, struct strbuf *result, int strict)
1959 {
1960 const char *orig_src = src;
1961 char *endp;
1962 unsigned long num;
1963
1964 errno = 0;
1965
1966 num = strtoul(src, &endp, 10);
1967 /*
1968 * NEEDSWORK: perhaps check for reasonable values? For example, we
1969 * could error on values representing times more than a
1970 * day in the future.
1971 */
1972 if (errno || endp == src || *endp != ' ')
1973 return -1;
1974
1975 src = endp + 1;
1976 if (*src != '-' && *src != '+')
1977 return -1;
1978
1979 num = strtoul(src + 1, &endp, 10);
1980 /*
1981 * NEEDSWORK: check for brokenness other than num > 1400, such as
1982 * (num % 100) >= 60, or ((num % 100) % 15) != 0 ?
1983 */
1984 if (errno || endp == src + 1 || *endp || /* did not parse */
1985 (strict && (1400 < num)) /* parsed a broken timezone */
1986 )
1987 return -1;
1988
1989 strbuf_addstr(result, orig_src);
1990 return 0;
1991 }
1992
1993 static char *parse_ident(const char *buf)
1994 {
1995 const char *ltgt;
1996 size_t name_len;
1997 struct strbuf ident = STRBUF_INIT;
1998
1999 /* ensure there is a space delimiter even if there is no name */
2000 if (*buf == '<')
2001 --buf;
2002
2003 ltgt = buf + strcspn(buf, "<>");
2004 if (*ltgt != '<')
2005 die(_("missing < in ident string: %s"), buf);
2006 if (ltgt != buf && ltgt[-1] != ' ')
2007 die(_("missing space before < in ident string: %s"), buf);
2008 ltgt = ltgt + 1 + strcspn(ltgt + 1, "<>");
2009 if (*ltgt != '>')
2010 die(_("missing > in ident string: %s"), buf);
2011 ltgt++;
2012 if (*ltgt != ' ')
2013 die(_("missing space after > in ident string: %s"), buf);
2014 ltgt++;
2015 name_len = ltgt - buf;
2016 strbuf_add(&ident, buf, name_len);
2017
2018 switch (whenspec) {
2019 case WHENSPEC_RAW:
2020 if (validate_raw_date(ltgt, &ident, 1) < 0)
2021 die(_("invalid raw date \"%s\" in ident: %s"), ltgt, buf);
2022 break;
2023 case WHENSPEC_RAW_PERMISSIVE:
2024 if (validate_raw_date(ltgt, &ident, 0) < 0)
2025 die(_("invalid raw date \"%s\" in ident: %s"), ltgt, buf);
2026 break;
2027 case WHENSPEC_RFC2822:
2028 if (parse_date(ltgt, &ident) < 0)
2029 die(_("invalid rfc2822 date \"%s\" in ident: %s"), ltgt, buf);
2030 break;
2031 case WHENSPEC_NOW:
2032 if (strcmp("now", ltgt))
2033 die(_("date in ident must be 'now': %s"), buf);
2034 datestamp(&ident);
2035 break;
2036 }
2037
2038 return strbuf_detach(&ident, NULL);
2039 }
2040
2041 static void parse_and_store_blob(
2042 struct last_object *last,
2043 struct object_id *oidout,
2044 uintmax_t mark)
2045 {
2046 static struct strbuf buf = STRBUF_INIT;
2047 uintmax_t len;
2048
2049 if (parse_data(&buf, repo_settings_get_big_file_threshold(the_repository), &len))
2050 store_object(OBJ_BLOB, &buf, last, oidout, mark);
2051 else {
2052 if (last) {
2053 strbuf_release(&last->data);
2054 last->offset = 0;
2055 last->depth = 0;
2056 }
2057 stream_blob(len, oidout, mark);
2058 skip_optional_lf();
2059 }
2060 }
2061
2062 static void parse_new_blob(void)
2063 {
2064 read_next_command();
2065 parse_mark();
2066 parse_original_identifier();
2067 parse_and_store_blob(&last_blob, NULL, next_mark);
2068 }
2069
2070 static void unload_one_branch(void)
2071 {
2072 while (cur_active_branches
2073 && cur_active_branches >= max_active_branches) {
2074 uintmax_t min_commit = ULONG_MAX;
2075 struct branch *e, *l = NULL, *p = NULL;
2076
2077 for (e = active_branches; e; e = e->active_next_branch) {
2078 if (e->last_commit < min_commit) {
2079 p = l;
2080 min_commit = e->last_commit;
2081 }
2082 l = e;
2083 }
2084
2085 if (p) {
2086 e = p->active_next_branch;
2087 p->active_next_branch = e->active_next_branch;
2088 } else {
2089 e = active_branches;
2090 active_branches = e->active_next_branch;
2091 }
2092 e->active = 0;
2093 e->active_next_branch = NULL;
2094 if (e->branch_tree.tree) {
2095 release_tree_content_recursive(e->branch_tree.tree);
2096 e->branch_tree.tree = NULL;
2097 }
2098 cur_active_branches--;
2099 }
2100 }
2101
2102 static void load_branch(struct branch *b)
2103 {
2104 load_tree(&b->branch_tree);
2105 if (!b->active) {
2106 b->active = 1;
2107 b->active_next_branch = active_branches;
2108 active_branches = b;
2109 cur_active_branches++;
2110 branch_load_count++;
2111 }
2112 }
2113
2114 static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes)
2115 {
2116 unsigned char fanout = 0;
2117 while ((num_notes >>= 8))
2118 fanout++;
2119 return fanout;
2120 }
2121
2122 static void construct_path_with_fanout(const char *hex_sha1,
2123 unsigned char fanout, char *path)
2124 {
2125 unsigned int i = 0, j = 0;
2126 if (fanout >= the_hash_algo->rawsz)
2127 die(_("too large fanout (%u)"), fanout);
2128 while (fanout) {
2129 path[i++] = hex_sha1[j++];
2130 path[i++] = hex_sha1[j++];
2131 path[i++] = '/';
2132 fanout--;
2133 }
2134 memcpy(path + i, hex_sha1 + j, the_hash_algo->hexsz - j);
2135 path[i + the_hash_algo->hexsz - j] = '\0';
2136 }
2137
2138 static uintmax_t do_change_note_fanout(
2139 struct tree_entry *orig_root, struct tree_entry *root,
2140 char *hex_oid, unsigned int hex_oid_len,
2141 char *fullpath, unsigned int fullpath_len,
2142 unsigned char fanout)
2143 {
2144 struct tree_content *t;
2145 struct tree_entry *e, leaf;
2146 unsigned int i, tmp_hex_oid_len, tmp_fullpath_len;
2147 uintmax_t num_notes = 0;
2148 struct object_id oid;
2149 /* hex oid + '/' between each pair of hex digits + NUL */
2150 char realpath[GIT_MAX_HEXSZ + ((GIT_MAX_HEXSZ / 2) - 1) + 1];
2151 const unsigned hexsz = the_hash_algo->hexsz;
2152
2153 if (!root->tree)
2154 load_tree(root);
2155 t = root->tree;
2156
2157 for (i = 0; t && i < t->entry_count; i++) {
2158 e = t->entries[i];
2159 tmp_hex_oid_len = hex_oid_len + e->name->str_len;
2160 tmp_fullpath_len = fullpath_len;
2161
2162 /*
2163 * We're interested in EITHER existing note entries (entries
2164 * with exactly 40 hex chars in path, not including directory
2165 * separators), OR directory entries that may contain note
2166 * entries (with < 40 hex chars in path).
2167 * Also, each path component in a note entry must be a multiple
2168 * of 2 chars.
2169 */
2170 if (!e->versions[1].mode ||
2171 tmp_hex_oid_len > hexsz ||
2172 e->name->str_len % 2)
2173 continue;
2174
2175 /* This _may_ be a note entry, or a subdir containing notes */
2176 memcpy(hex_oid + hex_oid_len, e->name->str_dat,
2177 e->name->str_len);
2178 if (tmp_fullpath_len)
2179 fullpath[tmp_fullpath_len++] = '/';
2180 memcpy(fullpath + tmp_fullpath_len, e->name->str_dat,
2181 e->name->str_len);
2182 tmp_fullpath_len += e->name->str_len;
2183 fullpath[tmp_fullpath_len] = '\0';
2184
2185 if (tmp_hex_oid_len == hexsz && !get_oid_hex(hex_oid, &oid)) {
2186 /* This is a note entry */
2187 if (fanout == 0xff) {
2188 /* Counting mode, no rename */
2189 num_notes++;
2190 continue;
2191 }
2192 construct_path_with_fanout(hex_oid, fanout, realpath);
2193 if (!strcmp(fullpath, realpath)) {
2194 /* Note entry is in correct location */
2195 num_notes++;
2196 continue;
2197 }
2198
2199 /* Rename fullpath to realpath */
2200 if (!tree_content_remove(orig_root, fullpath, &leaf, 0))
2201 die(_("failed to remove path %s"), fullpath);
2202 tree_content_set(orig_root, realpath,
2203 &leaf.versions[1].oid,
2204 leaf.versions[1].mode,
2205 leaf.tree);
2206 } else if (S_ISDIR(e->versions[1].mode)) {
2207 /* This is a subdir that may contain note entries */
2208 num_notes += do_change_note_fanout(orig_root, e,
2209 hex_oid, tmp_hex_oid_len,
2210 fullpath, tmp_fullpath_len, fanout);
2211 }
2212
2213 /* The above may have reallocated the current tree_content */
2214 t = root->tree;
2215 }
2216 return num_notes;
2217 }
2218
2219 static uintmax_t change_note_fanout(struct tree_entry *root,
2220 unsigned char fanout)
2221 {
2222 /*
2223 * The size of path is due to one slash between every two hex digits,
2224 * plus the terminating NUL. Note that there is no slash at the end, so
2225 * the number of slashes is one less than half the number of hex
2226 * characters.
2227 */
2228 char hex_oid[GIT_MAX_HEXSZ], path[GIT_MAX_HEXSZ + (GIT_MAX_HEXSZ / 2) - 1 + 1];
2229 return do_change_note_fanout(root, root, hex_oid, 0, path, 0, fanout);
2230 }
2231
2232 static int parse_mapped_oid_hex(const char *hex, struct object_id *oid, const char **end)
2233 {
2234 int algo;
2235 khiter_t it;
2236
2237 /* Make SHA-1 object IDs have all-zero padding. */
2238 memset(oid->hash, 0, sizeof(oid->hash));
2239
2240 algo = parse_oid_hex_any(hex, oid, end);
2241 if (algo == GIT_HASH_UNKNOWN)
2242 return -1;
2243
2244 it = kh_get_oid_map(sub_oid_map, *oid);
2245 /* No such object? */
2246 if (it == kh_end(sub_oid_map)) {
2247 /* If we're using the same algorithm, pass it through. */
2248 if (hash_algos[algo].format_id == the_hash_algo->format_id)
2249 return 0;
2250 return -1;
2251 }
2252 oidcpy(oid, kh_value(sub_oid_map, it));
2253 return 0;
2254 }
2255
2256 /*
2257 * Given a pointer into a string, parse a mark reference:
2258 *
2259 * idnum ::= ':' bigint;
2260 *
2261 * Update *endptr to point to the first character after the value.
2262 *
2263 * Complain if the following character is not what is expected,
2264 * either a space or end of the string.
2265 */
2266 static uintmax_t parse_mark_ref(const char *p, char **endptr)
2267 {
2268 uintmax_t mark;
2269
2270 assert(*p == ':');
2271 p++;
2272 mark = strtoumax(p, endptr, 10);
2273 if (*endptr == p)
2274 die(_("no value after ':' in mark: %s"), command_buf.buf);
2275 return mark;
2276 }
2277
2278 /*
2279 * Parse the mark reference, and complain if this is not the end of
2280 * the string.
2281 */
2282 static uintmax_t parse_mark_ref_eol(const char *p)
2283 {
2284 char *end;
2285 uintmax_t mark;
2286
2287 mark = parse_mark_ref(p, &end);
2288 if (*end != '\0')
2289 die(_("garbage after mark: %s"), command_buf.buf);
2290 return mark;
2291 }
2292
2293 /*
2294 * Parse the mark reference, demanding a trailing space. Update *p to
2295 * point to the first character after the space.
2296 */
2297 static uintmax_t parse_mark_ref_space(const char **p)
2298 {
2299 uintmax_t mark;
2300 char *end;
2301
2302 mark = parse_mark_ref(*p, &end);
2303 if (*end++ != ' ')
2304 die(_("missing space after mark: %s"), command_buf.buf);
2305 *p = end;
2306 return mark;
2307 }
2308
2309 /*
2310 * Parse the path string into the strbuf. The path can either be quoted with
2311 * escape sequences or unquoted without escape sequences. Unquoted strings may
2312 * contain spaces only if `is_last_field` is nonzero; otherwise, it stops
2313 * parsing at the first space.
2314 */
2315 static void parse_path(struct strbuf *sb, const char *p, const char **endp,
2316 int is_last_field, const char *field)
2317 {
2318 if (*p == '"') {
2319 if (unquote_c_style(sb, p, endp))
2320 die(_("invalid %s: %s"), field, command_buf.buf);
2321 if (strlen(sb->buf) != sb->len)
2322 die(_("NUL in %s: %s"), field, command_buf.buf);
2323 } else {
2324 /*
2325 * Unless we are parsing the last field of a line,
2326 * SP is the end of this field.
2327 */
2328 *endp = is_last_field
2329 ? p + strlen(p)
2330 : strchrnul(p, ' ');
2331 strbuf_add(sb, p, *endp - p);
2332 }
2333 }
2334
2335 /*
2336 * Parse the path string into the strbuf, and complain if this is not the end of
2337 * the string. Unquoted strings may contain spaces.
2338 */
2339 static void parse_path_eol(struct strbuf *sb, const char *p, const char *field)
2340 {
2341 const char *end;
2342
2343 parse_path(sb, p, &end, 1, field);
2344 if (*end)
2345 die(_("garbage after %s: %s"), field, command_buf.buf);
2346 }
2347
2348 /*
2349 * Parse the path string into the strbuf, and ensure it is followed by a space.
2350 * Unquoted strings may not contain spaces. Update *endp to point to the first
2351 * character after the space.
2352 */
2353 static void parse_path_space(struct strbuf *sb, const char *p,
2354 const char **endp, const char *field)
2355 {
2356 parse_path(sb, p, endp, 0, field);
2357 if (**endp != ' ')
2358 die(_("missing space after %s: %s"), field, command_buf.buf);
2359 (*endp)++;
2360 }
2361
2362 static void file_change_m(const char *p, struct branch *b)
2363 {
2364 static struct strbuf path = STRBUF_INIT;
2365 struct object_entry *oe;
2366 struct object_id oid;
2367 uint16_t mode, inline_data = 0;
2368
2369 p = parse_mode(p, &mode);
2370 if (!p)
2371 die(_("corrupt mode: %s"), command_buf.buf);
2372 switch (mode) {
2373 case 0644:
2374 case 0755:
2375 mode |= S_IFREG;
2376 case S_IFREG | 0644:
2377 case S_IFREG | 0755:
2378 case S_IFLNK:
2379 case S_IFDIR:
2380 case S_IFGITLINK:
2381 /* ok */
2382 break;
2383 default:
2384 die(_("corrupt mode: %s"), command_buf.buf);
2385 }
2386
2387 if (*p == ':') {
2388 oe = find_mark(marks, parse_mark_ref_space(&p));
2389 oidcpy(&oid, &oe->idx.oid);
2390 } else if (skip_prefix(p, "inline ", &p)) {
2391 inline_data = 1;
2392 oe = NULL; /* not used with inline_data, but makes gcc happy */
2393 } else {
2394 if (parse_mapped_oid_hex(p, &oid, &p))
2395 die(_("invalid dataref: %s"), command_buf.buf);
2396 oe = find_object(&oid);
2397 if (*p++ != ' ')
2398 die(_("missing space after SHA1: %s"), command_buf.buf);
2399 }
2400
2401 strbuf_reset(&path);
2402 parse_path_eol(&path, p, "path");
2403
2404 /* Git does not track empty, non-toplevel directories. */
2405 if (S_ISDIR(mode) &&
2406 is_empty_tree_oid(&oid, the_repository->hash_algo) &&
2407 *path.buf) {
2408 tree_content_remove(&b->branch_tree, path.buf, NULL, 0);
2409 return;
2410 }
2411
2412 if (S_ISGITLINK(mode)) {
2413 if (inline_data)
2414 die(_("Git links cannot be specified 'inline': %s"),
2415 command_buf.buf);
2416 else if (oe) {
2417 if (oe->type != OBJ_COMMIT)
2418 die(_("not a commit (actually a %s): %s"),
2419 type_name(oe->type), command_buf.buf);
2420 }
2421 /*
2422 * Accept the sha1 without checking; it expected to be in
2423 * another repository.
2424 */
2425 } else if (inline_data) {
2426 if (S_ISDIR(mode))
2427 die(_("directories cannot be specified 'inline': %s"),
2428 command_buf.buf);
2429 while (read_next_command() != EOF) {
2430 const char *v;
2431 if (skip_prefix(command_buf.buf, "cat-blob ", &v))
2432 parse_cat_blob(v);
2433 else {
2434 parse_and_store_blob(&last_blob, &oid, 0);
2435 break;
2436 }
2437 }
2438 } else {
2439 enum object_type expected = S_ISDIR(mode) ?
2440 OBJ_TREE: OBJ_BLOB;
2441 enum object_type type = oe ? oe->type :
2442 odb_read_object_info(the_repository->objects,
2443 &oid, NULL);
2444 if (type < 0)
2445 die(_("%s not found: %s"),
2446 S_ISDIR(mode) ? _("tree") : _("blob"),
2447 command_buf.buf);
2448 if (type != expected)
2449 die(_("not a %s (actually a %s): %s"),
2450 type_name(expected), type_name(type),
2451 command_buf.buf);
2452 }
2453
2454 if (!*path.buf) {
2455 tree_content_replace(&b->branch_tree, &oid, mode, NULL);
2456 return;
2457 }
2458
2459 if (!verify_path(path.buf, mode))
2460 die(_("invalid path '%s'"), path.buf);
2461 tree_content_set(&b->branch_tree, path.buf, &oid, mode, NULL);
2462 }
2463
2464 static void file_change_d(const char *p, struct branch *b)
2465 {
2466 static struct strbuf path = STRBUF_INIT;
2467
2468 strbuf_reset(&path);
2469 parse_path_eol(&path, p, "path");
2470 tree_content_remove(&b->branch_tree, path.buf, NULL, 1);
2471 }
2472
2473 static void file_change_cr(const char *p, struct branch *b, int rename)
2474 {
2475 static struct strbuf source = STRBUF_INIT;
2476 static struct strbuf dest = STRBUF_INIT;
2477 struct tree_entry leaf;
2478
2479 strbuf_reset(&source);
2480 parse_path_space(&source, p, &p, "source");
2481 strbuf_reset(&dest);
2482 parse_path_eol(&dest, p, "dest");
2483
2484 memset(&leaf, 0, sizeof(leaf));
2485 if (rename)
2486 tree_content_remove(&b->branch_tree, source.buf, &leaf, 1);
2487 else
2488 tree_content_get(&b->branch_tree, source.buf, &leaf, 1);
2489 if (!leaf.versions[1].mode)
2490 die(_("path %s not in branch"), source.buf);
2491 if (!*dest.buf) { /* C "path/to/subdir" "" */
2492 tree_content_replace(&b->branch_tree,
2493 &leaf.versions[1].oid,
2494 leaf.versions[1].mode,
2495 leaf.tree);
2496 return;
2497 }
2498 if (!verify_path(dest.buf, leaf.versions[1].mode))
2499 die(_("invalid path '%s'"), dest.buf);
2500 tree_content_set(&b->branch_tree, dest.buf,
2501 &leaf.versions[1].oid,
2502 leaf.versions[1].mode,
2503 leaf.tree);
2504 }
2505
2506 static void note_change_n(const char *p, struct branch *b, unsigned char *old_fanout)
2507 {
2508 struct object_entry *oe;
2509 struct branch *s;
2510 struct object_id oid, commit_oid;
2511 char path[GIT_MAX_RAWSZ * 3];
2512 uint16_t inline_data = 0;
2513 unsigned char new_fanout;
2514
2515 /*
2516 * When loading a branch, we don't traverse its tree to count the real
2517 * number of notes (too expensive to do this for all non-note refs).
2518 * This means that recently loaded notes refs might incorrectly have
2519 * b->num_notes == 0, and consequently, old_fanout might be wrong.
2520 *
2521 * Fix this by traversing the tree and counting the number of notes
2522 * when b->num_notes == 0. If the notes tree is truly empty, the
2523 * calculation should not take long.
2524 */
2525 if (b->num_notes == 0 && *old_fanout == 0) {
2526 /* Invoke change_note_fanout() in "counting mode". */
2527 b->num_notes = change_note_fanout(&b->branch_tree, 0xff);
2528 *old_fanout = convert_num_notes_to_fanout(b->num_notes);
2529 }
2530
2531 /* Now parse the notemodify command. */
2532 /* <dataref> or 'inline' */
2533 if (*p == ':') {
2534 oe = find_mark(marks, parse_mark_ref_space(&p));
2535 oidcpy(&oid, &oe->idx.oid);
2536 } else if (skip_prefix(p, "inline ", &p)) {
2537 inline_data = 1;
2538 oe = NULL; /* not used with inline_data, but makes gcc happy */
2539 } else {
2540 if (parse_mapped_oid_hex(p, &oid, &p))
2541 die(_("invalid dataref: %s"), command_buf.buf);
2542 oe = find_object(&oid);
2543 if (*p++ != ' ')
2544 die(_("missing space after SHA1: %s"), command_buf.buf);
2545 }
2546
2547 /* <commit-ish> */
2548 s = lookup_branch(p);
2549 if (s) {
2550 if (is_null_oid(&s->oid))
2551 die(_("can't add a note on empty branch."));
2552 oidcpy(&commit_oid, &s->oid);
2553 } else if (*p == ':') {
2554 uintmax_t commit_mark = parse_mark_ref_eol(p);
2555 struct object_entry *commit_oe = find_mark(marks, commit_mark);
2556 if (commit_oe->type != OBJ_COMMIT)
2557 die(_("mark :%" PRIuMAX " not a commit"), commit_mark);
2558 oidcpy(&commit_oid, &commit_oe->idx.oid);
2559 } else if (!repo_get_oid(the_repository, p, &commit_oid)) {
2560 unsigned long size;
2561 char *buf = odb_read_object_peeled(the_repository->objects,
2562 &commit_oid, OBJ_COMMIT, &size,
2563 &commit_oid);
2564 if (!buf || size < the_hash_algo->hexsz + 6)
2565 die(_("not a valid commit: %s"), p);
2566 free(buf);
2567 } else
2568 die(_("invalid ref name or SHA1 expression: %s"), p);
2569
2570 if (inline_data) {
2571 read_next_command();
2572 parse_and_store_blob(&last_blob, &oid, 0);
2573 } else if (oe) {
2574 if (oe->type != OBJ_BLOB)
2575 die(_("not a blob (actually a %s): %s"),
2576 type_name(oe->type), command_buf.buf);
2577 } else if (!is_null_oid(&oid)) {
2578 enum object_type type = odb_read_object_info(the_repository->objects, &oid,
2579 NULL);
2580 if (type < 0)
2581 die(_("blob not found: %s"), command_buf.buf);
2582 if (type != OBJ_BLOB)
2583 die(_("not a blob (actually a %s): %s"),
2584 type_name(type), command_buf.buf);
2585 }
2586
2587 construct_path_with_fanout(oid_to_hex(&commit_oid), *old_fanout, path);
2588 if (tree_content_remove(&b->branch_tree, path, NULL, 0))
2589 b->num_notes--;
2590
2591 if (is_null_oid(&oid))
2592 return; /* nothing to insert */
2593
2594 b->num_notes++;
2595 new_fanout = convert_num_notes_to_fanout(b->num_notes);
2596 construct_path_with_fanout(oid_to_hex(&commit_oid), new_fanout, path);
2597 tree_content_set(&b->branch_tree, path, &oid, S_IFREG | 0644, NULL);
2598 }
2599
2600 static void file_change_deleteall(struct branch *b)
2601 {
2602 release_tree_content_recursive(b->branch_tree.tree);
2603 oidclr(&b->branch_tree.versions[0].oid, the_repository->hash_algo);
2604 oidclr(&b->branch_tree.versions[1].oid, the_repository->hash_algo);
2605 load_tree(&b->branch_tree);
2606 b->num_notes = 0;
2607 }
2608
2609 static void parse_from_commit(struct branch *b, char *buf, unsigned long size)
2610 {
2611 if (!buf || size < the_hash_algo->hexsz + 6)
2612 die(_("not a valid commit: %s"), oid_to_hex(&b->oid));
2613 if (memcmp("tree ", buf, 5)
2614 || get_oid_hex(buf + 5, &b->branch_tree.versions[1].oid))
2615 die(_("the commit %s is corrupt"), oid_to_hex(&b->oid));
2616 oidcpy(&b->branch_tree.versions[0].oid,
2617 &b->branch_tree.versions[1].oid);
2618 }
2619
2620 static void parse_from_existing(struct branch *b)
2621 {
2622 if (is_null_oid(&b->oid)) {
2623 oidclr(&b->branch_tree.versions[0].oid, the_repository->hash_algo);
2624 oidclr(&b->branch_tree.versions[1].oid, the_repository->hash_algo);
2625 } else {
2626 unsigned long size;
2627 char *buf;
2628
2629 buf = odb_read_object_peeled(the_repository->objects, &b->oid,
2630 OBJ_COMMIT, &size, &b->oid);
2631 parse_from_commit(b, buf, size);
2632 free(buf);
2633 }
2634 }
2635
2636 static int parse_objectish(struct branch *b, const char *objectish)
2637 {
2638 struct branch *s;
2639 struct object_id oid;
2640
2641 oidcpy(&oid, &b->branch_tree.versions[1].oid);
2642
2643 s = lookup_branch(objectish);
2644 if (b == s)
2645 die(_("can't create a branch from itself: %s"), b->name);
2646 else if (s) {
2647 struct object_id *t = &s->branch_tree.versions[1].oid;
2648 oidcpy(&b->oid, &s->oid);
2649 oidcpy(&b->branch_tree.versions[0].oid, t);
2650 oidcpy(&b->branch_tree.versions[1].oid, t);
2651 } else if (*objectish == ':') {
2652 uintmax_t idnum = parse_mark_ref_eol(objectish);
2653 struct object_entry *oe = find_mark(marks, idnum);
2654 if (oe->type != OBJ_COMMIT)
2655 die(_("mark :%" PRIuMAX " not a commit"), idnum);
2656 if (!oideq(&b->oid, &oe->idx.oid)) {
2657 oidcpy(&b->oid, &oe->idx.oid);
2658 if (oe->pack_id != MAX_PACK_ID) {
2659 unsigned long size;
2660 char *buf = gfi_unpack_entry(oe, &size);
2661 parse_from_commit(b, buf, size);
2662 free(buf);
2663 } else
2664 parse_from_existing(b);
2665 }
2666 } else if (!repo_get_oid(the_repository, objectish, &b->oid)) {
2667 parse_from_existing(b);
2668 if (is_null_oid(&b->oid))
2669 b->delete = 1;
2670 }
2671 else
2672 die(_("invalid ref name or SHA1 expression: %s"), objectish);
2673
2674 if (b->branch_tree.tree && !oideq(&oid, &b->branch_tree.versions[1].oid)) {
2675 release_tree_content_recursive(b->branch_tree.tree);
2676 b->branch_tree.tree = NULL;
2677 }
2678
2679 read_next_command();
2680 return 1;
2681 }
2682
2683 static int parse_from(struct branch *b)
2684 {
2685 const char *from;
2686
2687 if (!skip_prefix(command_buf.buf, "from ", &from))
2688 return 0;
2689
2690 return parse_objectish(b, from);
2691 }
2692
2693 static int parse_objectish_with_prefix(struct branch *b, const char *prefix)
2694 {
2695 const char *base;
2696
2697 if (!skip_prefix(command_buf.buf, prefix, &base))
2698 return 0;
2699
2700 return parse_objectish(b, base);
2701 }
2702
2703 static struct hash_list *parse_merge(unsigned int *count)
2704 {
2705 struct hash_list *list = NULL, **tail = &list, *n;
2706 const char *from;
2707 struct branch *s;
2708
2709 *count = 0;
2710 while (skip_prefix(command_buf.buf, "merge ", &from)) {
2711 n = xmalloc(sizeof(*n));
2712 s = lookup_branch(from);
2713 if (s)
2714 oidcpy(&n->oid, &s->oid);
2715 else if (*from == ':') {
2716 uintmax_t idnum = parse_mark_ref_eol(from);
2717 struct object_entry *oe = find_mark(marks, idnum);
2718 if (oe->type != OBJ_COMMIT)
2719 die(_("mark :%" PRIuMAX " not a commit"), idnum);
2720 oidcpy(&n->oid, &oe->idx.oid);
2721 } else if (!repo_get_oid(the_repository, from, &n->oid)) {
2722 unsigned long size;
2723 char *buf = odb_read_object_peeled(the_repository->objects,
2724 &n->oid, OBJ_COMMIT,
2725 &size, &n->oid);
2726 if (!buf || size < the_hash_algo->hexsz + 6)
2727 die(_("not a valid commit: %s"), from);
2728 free(buf);
2729 } else
2730 die(_("invalid ref name or SHA1 expression: %s"), from);
2731
2732 n->next = NULL;
2733 *tail = n;
2734 tail = &n->next;
2735
2736 (*count)++;
2737 read_next_command();
2738 }
2739 return list;
2740 }
2741
2742 struct signature_data {
2743 char *hash_algo; /* "sha1" or "sha256" */
2744 char *sig_format; /* "openpgp", "x509", "ssh", or "unknown" */
2745 struct strbuf data; /* The actual signature data */
2746 };
2747
2748 static void parse_one_signature(struct signature_data *sig, const char *v)
2749 {
2750 char *args = xstrdup(v); /* Will be freed when sig->hash_algo is freed */
2751 char *space = strchr(args, ' ');
2752
2753 if (!space)
2754 die(_("expected gpgsig format: 'gpgsig <hash-algo> <signature-format>', "
2755 "got 'gpgsig %s'"), args);
2756 *space = '\0';
2757
2758 sig->hash_algo = args;
2759 sig->sig_format = space + 1;
2760
2761 /* Validate hash algorithm */
2762 if (strcmp(sig->hash_algo, "sha1") &&
2763 strcmp(sig->hash_algo, "sha256"))
2764 die(_("unknown git hash algorithm in gpgsig: '%s'"), sig->hash_algo);
2765
2766 /* Validate signature format */
2767 if (!valid_signature_format(sig->sig_format))
2768 die(_("invalid signature format in gpgsig: '%s'"), sig->sig_format);
2769 if (!strcmp(sig->sig_format, "unknown"))
2770 warning(_("'unknown' signature format in gpgsig"));
2771
2772 /* Read signature data */
2773 read_next_command();
2774 parse_data(&sig->data, 0, NULL);
2775 }
2776
2777 static void discard_one_signature(void)
2778 {
2779 struct strbuf data = STRBUF_INIT;
2780
2781 read_next_command();
2782 parse_data(&data, 0, NULL);
2783 strbuf_release(&data);
2784 }
2785
2786 static void add_gpgsig_to_commit(struct strbuf *commit_data,
2787 const char *header,
2788 struct signature_data *sig)
2789 {
2790 struct string_list siglines = STRING_LIST_INIT_NODUP;
2791
2792 if (!sig || !sig->hash_algo)
2793 return;
2794
2795 strbuf_addstr(commit_data, header);
2796 string_list_split_in_place(&siglines, sig->data.buf, "\n", -1);
2797 strbuf_add_separated_string_list(commit_data, "\n ", &siglines);
2798 strbuf_addch(commit_data, '\n');
2799 string_list_clear(&siglines, 1);
2800 strbuf_release(&sig->data);
2801 free(sig->hash_algo);
2802 }
2803
2804 static void store_signature(struct signature_data *stored_sig,
2805 struct signature_data *new_sig,
2806 const char *hash_type)
2807 {
2808 if (stored_sig->hash_algo) {
2809 warning(_("multiple %s signatures found, "
2810 "ignoring additional signature"),
2811 hash_type);
2812 strbuf_release(&new_sig->data);
2813 free(new_sig->hash_algo);
2814 } else {
2815 *stored_sig = *new_sig;
2816 }
2817 }
2818
2819 static void import_one_signature(struct signature_data *sig_sha1,
2820 struct signature_data *sig_sha256,
2821 const char *v)
2822 {
2823 struct signature_data sig = { NULL, NULL, STRBUF_INIT };
2824
2825 parse_one_signature(&sig, v);
2826
2827 if (!strcmp(sig.hash_algo, "sha1"))
2828 store_signature(sig_sha1, &sig, "SHA-1");
2829 else if (!strcmp(sig.hash_algo, "sha256"))
2830 store_signature(sig_sha256, &sig, "SHA-256");
2831 else
2832 die(_("parse_one_signature() returned unknown hash algo"));
2833 }
2834
2835 static void finalize_commit_buffer(struct strbuf *new_data,
2836 struct signature_data *sig_sha1,
2837 struct signature_data *sig_sha256,
2838 struct strbuf *msg)
2839 {
2840 add_gpgsig_to_commit(new_data, "gpgsig ", sig_sha1);
2841 add_gpgsig_to_commit(new_data, "gpgsig-sha256 ", sig_sha256);
2842
2843 strbuf_addch(new_data, '\n');
2844 strbuf_addbuf(new_data, msg);
2845 }
2846
2847 static void warn_invalid_signature(struct signature_check *check,
2848 const char *msg, enum sign_mode mode)
2849 {
2850 const char *signer = check->signer ? check->signer : _("unknown");
2851 const char *subject;
2852 int subject_len = find_commit_subject(msg, &subject);
2853
2854 switch (mode) {
2855 case SIGN_STRIP_IF_INVALID:
2856 if (subject_len > 100)
2857 warning(_("stripping invalid signature for commit '%.100s...'\n"
2858 " allegedly by %s"), subject, signer);
2859 else if (subject_len > 0)
2860 warning(_("stripping invalid signature for commit '%.*s'\n"
2861 " allegedly by %s"), subject_len, subject, signer);
2862 else
2863 warning(_("stripping invalid signature for commit\n"
2864 " allegedly by %s"), signer);
2865 break;
2866 case SIGN_SIGN_IF_INVALID:
2867 if (subject_len > 100)
2868 warning(_("replacing invalid signature for commit '%.100s...'\n"
2869 " allegedly by %s"), subject, signer);
2870 else if (subject_len > 0)
2871 warning(_("replacing invalid signature for commit '%.*s'\n"
2872 " allegedly by %s"), subject_len, subject, signer);
2873 else
2874 warning(_("replacing invalid signature for commit\n"
2875 " allegedly by %s"), signer);
2876 break;
2877 default:
2878 BUG("unsupported signing mode");
2879 }
2880 }
2881
2882 static void handle_signature_if_invalid(struct strbuf *new_data,
2883 struct signature_data *sig_sha1,
2884 struct signature_data *sig_sha256,
2885 struct strbuf *msg,
2886 enum sign_mode mode)
2887 {
2888 struct strbuf tmp_buf = STRBUF_INIT;
2889 struct signature_check signature_check = { 0 };
2890 int ret;
2891
2892 /* Check signature in a temporary commit buffer */
2893 strbuf_addbuf(&tmp_buf, new_data);
2894 finalize_commit_buffer(&tmp_buf, sig_sha1, sig_sha256, msg);
2895 ret = verify_commit_buffer(tmp_buf.buf, tmp_buf.len, &signature_check);
2896
2897 if (ret) {
2898 if (mode == SIGN_ABORT_IF_INVALID)
2899 die(_("aborting due to invalid signature"));
2900
2901 warn_invalid_signature(&signature_check, msg->buf, mode);
2902
2903 if (mode == SIGN_SIGN_IF_INVALID) {
2904 struct strbuf signature = STRBUF_INIT;
2905 struct strbuf payload = STRBUF_INIT;
2906
2907 /*
2908 * NEEDSWORK: To properly support interoperability mode
2909 * when signing commit signatures, the commit buffer
2910 * must be provided in both the repository and
2911 * compatibility object formats. As currently
2912 * implemented, only the repository object format is
2913 * considered meaning compatibility signatures cannot be
2914 * generated. Thus, attempting to sign commit signatures
2915 * in interoperability mode is currently unsupported.
2916 */
2917 if (the_repository->compat_hash_algo)
2918 die(_("signing commits in interoperability mode is unsupported"));
2919
2920 strbuf_addstr(&payload, signature_check.payload);
2921 if (sign_buffer(&payload, &signature, signed_commit_keyid,
2922 SIGN_BUFFER_USE_DEFAULT_KEY))
2923 die(_("failed to sign commit object"));
2924 add_header_signature(new_data, &signature, the_hash_algo);
2925
2926 strbuf_release(&signature);
2927 strbuf_release(&payload);
2928 }
2929
2930 finalize_commit_buffer(new_data, NULL, NULL, msg);
2931 } else {
2932 strbuf_swap(new_data, &tmp_buf);
2933 }
2934
2935 signature_check_clear(&signature_check);
2936 strbuf_release(&tmp_buf);
2937 }
2938
2939 static void parse_new_commit(const char *arg)
2940 {
2941 static struct strbuf msg = STRBUF_INIT;
2942 struct signature_data sig_sha1 = { NULL, NULL, STRBUF_INIT };
2943 struct signature_data sig_sha256 = { NULL, NULL, STRBUF_INIT };
2944 struct branch *b;
2945 char *author = NULL;
2946 char *committer = NULL;
2947 char *encoding = NULL;
2948 struct hash_list *merge_list = NULL;
2949 unsigned int merge_count;
2950 unsigned char prev_fanout, new_fanout;
2951 const char *v;
2952
2953 b = lookup_branch(arg);
2954 if (!b)
2955 b = new_branch(arg);
2956
2957 read_next_command();
2958 parse_mark();
2959 parse_original_identifier();
2960 if (skip_prefix(command_buf.buf, "author ", &v)) {
2961 author = parse_ident(v);
2962 read_next_command();
2963 }
2964 if (skip_prefix(command_buf.buf, "committer ", &v)) {
2965 committer = parse_ident(v);
2966 read_next_command();
2967 }
2968 if (!committer)
2969 die(_("expected committer but didn't get one"));
2970
2971 while (skip_prefix(command_buf.buf, "gpgsig ", &v)) {
2972 switch (signed_commit_mode) {
2973
2974 /* First, modes that don't need the signature to be parsed */
2975 case SIGN_ABORT:
2976 die(_("encountered signed commit; use "
2977 "--signed-commits=<mode> to handle it"));
2978 case SIGN_WARN_STRIP:
2979 warning(_("stripping a commit signature"));
2980 /* fallthru */
2981 case SIGN_STRIP:
2982 discard_one_signature();
2983 break;
2984
2985 /* Second, modes that parse the signature */
2986 case SIGN_WARN_VERBATIM:
2987 warning(_("importing a commit signature verbatim"));
2988 /* fallthru */
2989 case SIGN_VERBATIM:
2990 case SIGN_STRIP_IF_INVALID:
2991 case SIGN_SIGN_IF_INVALID:
2992 case SIGN_ABORT_IF_INVALID:
2993 import_one_signature(&sig_sha1, &sig_sha256, v);
2994 break;
2995
2996 /* Third, BUG */
2997 default:
2998 BUG("invalid signed_commit_mode value %d", signed_commit_mode);
2999 }
3000 read_next_command();
3001 }
3002
3003 if (skip_prefix(command_buf.buf, "encoding ", &v)) {
3004 encoding = xstrdup(v);
3005 read_next_command();
3006 }
3007 parse_data(&msg, 0, NULL);
3008 read_next_command();
3009 parse_from(b);
3010 merge_list = parse_merge(&merge_count);
3011
3012 /* ensure the branch is active/loaded */
3013 if (!b->branch_tree.tree || !max_active_branches) {
3014 unload_one_branch();
3015 load_branch(b);
3016 }
3017
3018 prev_fanout = convert_num_notes_to_fanout(b->num_notes);
3019
3020 /* file_change* */
3021 while (command_buf.len > 0) {
3022 if (skip_prefix(command_buf.buf, "M ", &v))
3023 file_change_m(v, b);
3024 else if (skip_prefix(command_buf.buf, "D ", &v))
3025 file_change_d(v, b);
3026 else if (skip_prefix(command_buf.buf, "R ", &v))
3027 file_change_cr(v, b, 1);
3028 else if (skip_prefix(command_buf.buf, "C ", &v))
3029 file_change_cr(v, b, 0);
3030 else if (skip_prefix(command_buf.buf, "N ", &v))
3031 note_change_n(v, b, &prev_fanout);
3032 else if (!strcmp("deleteall", command_buf.buf))
3033 file_change_deleteall(b);
3034 else if (skip_prefix(command_buf.buf, "ls ", &v))
3035 parse_ls(v, b);
3036 else if (skip_prefix(command_buf.buf, "cat-blob ", &v))
3037 parse_cat_blob(v);
3038 else {
3039 unread_command_buf = 1;
3040 break;
3041 }
3042 if (read_next_command() == EOF)
3043 break;
3044 }
3045
3046 new_fanout = convert_num_notes_to_fanout(b->num_notes);
3047 if (new_fanout != prev_fanout)
3048 b->num_notes = change_note_fanout(&b->branch_tree, new_fanout);
3049
3050 /* build the tree and the commit */
3051 store_tree(&b->branch_tree);
3052 oidcpy(&b->branch_tree.versions[0].oid,
3053 &b->branch_tree.versions[1].oid);
3054
3055 strbuf_reset(&new_data);
3056 strbuf_addf(&new_data, "tree %s\n",
3057 oid_to_hex(&b->branch_tree.versions[1].oid));
3058 if (!is_null_oid(&b->oid))
3059 strbuf_addf(&new_data, "parent %s\n",
3060 oid_to_hex(&b->oid));
3061 while (merge_list) {
3062 struct hash_list *next = merge_list->next;
3063 strbuf_addf(&new_data, "parent %s\n",
3064 oid_to_hex(&merge_list->oid));
3065 free(merge_list);
3066 merge_list = next;
3067 }
3068 strbuf_addf(&new_data,
3069 "author %s\n"
3070 "committer %s\n",
3071 author ? author : committer, committer);
3072 if (encoding)
3073 strbuf_addf(&new_data,
3074 "encoding %s\n",
3075 encoding);
3076
3077 if ((signed_commit_mode == SIGN_STRIP_IF_INVALID ||
3078 signed_commit_mode == SIGN_SIGN_IF_INVALID ||
3079 signed_commit_mode == SIGN_ABORT_IF_INVALID) &&
3080 (sig_sha1.hash_algo || sig_sha256.hash_algo))
3081 handle_signature_if_invalid(&new_data, &sig_sha1, &sig_sha256,
3082 &msg, signed_commit_mode);
3083 else
3084 finalize_commit_buffer(&new_data, &sig_sha1, &sig_sha256, &msg);
3085
3086 free(author);
3087 free(committer);
3088 free(encoding);
3089
3090 if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark))
3091 b->pack_id = pack_id;
3092 b->last_commit = object_count_by_type[OBJ_COMMIT];
3093 }
3094
3095 static void handle_tag_signature_if_invalid(struct strbuf *buf,
3096 struct strbuf *msg,
3097 size_t sig_offset)
3098 {
3099 struct strbuf signature = STRBUF_INIT;
3100 struct strbuf payload = STRBUF_INIT;
3101 struct signature_check sigc = { 0 };
3102
3103 strbuf_addbuf(&payload, buf);
3104 strbuf_addch(&payload, '\n');
3105 strbuf_add(&payload, msg->buf, sig_offset);
3106 strbuf_add(&signature, msg->buf + sig_offset, msg->len - sig_offset);
3107
3108 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
3109 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
3110
3111 if (!check_signature(&sigc, signature.buf, signature.len))
3112 goto out;
3113
3114 if (signed_tag_mode == SIGN_ABORT_IF_INVALID)
3115 die(_("aborting due to invalid signature"));
3116
3117 strbuf_setlen(msg, sig_offset);
3118
3119 if (signed_tag_mode == SIGN_SIGN_IF_INVALID) {
3120 strbuf_attach(&payload, sigc.payload, sigc.payload_len,
3121 sigc.payload_len + 1);
3122 sigc.payload = NULL;
3123 strbuf_reset(&signature);
3124
3125 if (sign_buffer(&payload, &signature, signed_tag_keyid,
3126 SIGN_BUFFER_USE_DEFAULT_KEY))
3127 die(_("failed to sign tag object"));
3128
3129 strbuf_addbuf(msg, &signature);
3130 }
3131
3132 out:
3133 signature_check_clear(&sigc);
3134 strbuf_release(&signature);
3135 strbuf_release(&payload);
3136 }
3137
3138 static void handle_tag_signature(struct strbuf *buf, struct strbuf *msg, const char *name)
3139 {
3140 size_t sig_offset = parse_signed_buffer(msg->buf, msg->len);
3141
3142 /* If there is no signature, there is nothing to do. */
3143 if (sig_offset >= msg->len)
3144 return;
3145
3146 switch (signed_tag_mode) {
3147
3148 /* First, modes that don't change anything */
3149 case SIGN_WARN_VERBATIM:
3150 warning(_("importing a tag signature verbatim for tag '%s'"), name);
3151 /* fallthru */
3152 case SIGN_VERBATIM:
3153 /* Nothing to do, the signature will be put into the imported tag. */
3154 break;
3155
3156 /* Second, modes that remove the signature */
3157 case SIGN_WARN_STRIP:
3158 warning(_("stripping a tag signature for tag '%s'"), name);
3159 /* fallthru */
3160 case SIGN_STRIP:
3161 /* Truncate the buffer to remove the signature */
3162 strbuf_setlen(msg, sig_offset);
3163 break;
3164 case SIGN_ABORT_IF_INVALID:
3165 case SIGN_SIGN_IF_INVALID:
3166 case SIGN_STRIP_IF_INVALID:
3167 handle_tag_signature_if_invalid(buf, msg, sig_offset);
3168 break;
3169
3170 /* Third, aborting modes */
3171 case SIGN_ABORT:
3172 die(_("encountered signed tag; use "
3173 "--signed-tags=<mode> to handle it"));
3174 default:
3175 BUG("invalid signed_tag_mode value %d from tag '%s'",
3176 signed_tag_mode, name);
3177 }
3178 }
3179
3180 static void parse_new_tag(const char *arg)
3181 {
3182 static struct strbuf msg = STRBUF_INIT;
3183 const char *from;
3184 char *tagger;
3185 struct branch *s;
3186 struct tag *t;
3187 uintmax_t from_mark = 0;
3188 struct object_id oid;
3189 enum object_type type;
3190 const char *v;
3191
3192 t = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct tag));
3193 t->name = mem_pool_strdup(&fi_mem_pool, arg);
3194 if (last_tag)
3195 last_tag->next_tag = t;
3196 else
3197 first_tag = t;
3198 last_tag = t;
3199 read_next_command();
3200 parse_mark();
3201
3202 /* from ... */
3203 if (!skip_prefix(command_buf.buf, "from ", &from))
3204 die(_("expected 'from' command, got '%s'"), command_buf.buf);
3205 s = lookup_branch(from);
3206 if (s) {
3207 if (is_null_oid(&s->oid))
3208 die(_("can't tag an empty branch."));
3209 oidcpy(&oid, &s->oid);
3210 type = OBJ_COMMIT;
3211 } else if (*from == ':') {
3212 struct object_entry *oe;
3213 from_mark = parse_mark_ref_eol(from);
3214 oe = find_mark(marks, from_mark);
3215 type = oe->type;
3216 oidcpy(&oid, &oe->idx.oid);
3217 } else if (!repo_get_oid(the_repository, from, &oid)) {
3218 struct object_entry *oe = find_object(&oid);
3219 if (!oe) {
3220 type = odb_read_object_info(the_repository->objects,
3221 &oid, NULL);
3222 if (type < 0)
3223 die(_("not a valid object: %s"), from);
3224 } else
3225 type = oe->type;
3226 } else
3227 die(_("invalid ref name or SHA1 expression: %s"), from);
3228 read_next_command();
3229
3230 /* original-oid ... */
3231 parse_original_identifier();
3232
3233 /* tagger ... */
3234 if (skip_prefix(command_buf.buf, "tagger ", &v)) {
3235 tagger = parse_ident(v);
3236 read_next_command();
3237 } else
3238 tagger = NULL;
3239
3240 /* tag payload/message */
3241 parse_data(&msg, 0, NULL);
3242
3243 /* build the tag object */
3244 strbuf_reset(&new_data);
3245
3246 strbuf_addf(&new_data,
3247 "object %s\n"
3248 "type %s\n"
3249 "tag %s\n",
3250 oid_to_hex(&oid), type_name(type), t->name);
3251 if (tagger)
3252 strbuf_addf(&new_data,
3253 "tagger %s\n", tagger);
3254
3255 handle_tag_signature(&new_data, &msg, t->name);
3256
3257 strbuf_addch(&new_data, '\n');
3258 strbuf_addbuf(&new_data, &msg);
3259 free(tagger);
3260
3261 if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, next_mark))
3262 t->pack_id = MAX_PACK_ID;
3263 else
3264 t->pack_id = pack_id;
3265 }
3266
3267 static void parse_reset_branch(const char *arg)
3268 {
3269 struct branch *b;
3270 const char *tag_name;
3271
3272 b = lookup_branch(arg);
3273 if (b) {
3274 oidclr(&b->oid, the_repository->hash_algo);
3275 oidclr(&b->branch_tree.versions[0].oid, the_repository->hash_algo);
3276 oidclr(&b->branch_tree.versions[1].oid, the_repository->hash_algo);
3277 if (b->branch_tree.tree) {
3278 release_tree_content_recursive(b->branch_tree.tree);
3279 b->branch_tree.tree = NULL;
3280 }
3281 }
3282 else
3283 b = new_branch(arg);
3284 read_next_command();
3285 parse_from(b);
3286 if (b->delete && skip_prefix(b->name, "refs/tags/", &tag_name)) {
3287 /*
3288 * Elsewhere, we call dump_branches() before dump_tags(),
3289 * and dump_branches() will handle ref deletions first, so
3290 * in order to make sure the deletion actually takes effect,
3291 * we need to remove the tag from our list of tags to update.
3292 *
3293 * NEEDSWORK: replace list of tags with hashmap for faster
3294 * deletion?
3295 */
3296 struct tag *t, *prev = NULL;
3297 for (t = first_tag; t; t = t->next_tag) {
3298 if (!strcmp(t->name, tag_name))
3299 break;
3300 prev = t;
3301 }
3302 if (t) {
3303 if (prev)
3304 prev->next_tag = t->next_tag;
3305 else
3306 first_tag = t->next_tag;
3307 if (!t->next_tag)
3308 last_tag = prev;
3309 /* There is no mem_pool_free(t) function to call. */
3310 }
3311 }
3312 if (command_buf.len > 0)
3313 unread_command_buf = 1;
3314 }
3315
3316 static void cat_blob_write(const char *buf, unsigned long size)
3317 {
3318 if (write_in_full(cat_blob_fd, buf, size) < 0)
3319 die_errno(_("write to frontend failed"));
3320 }
3321
3322 static void cat_blob(struct object_entry *oe, struct object_id *oid)
3323 {
3324 struct strbuf line = STRBUF_INIT;
3325 unsigned long size;
3326 enum object_type type = 0;
3327 char *buf;
3328
3329 if (!oe || oe->pack_id == MAX_PACK_ID) {
3330 buf = odb_read_object(the_repository->objects, oid, &type, &size);
3331 } else {
3332 type = oe->type;
3333 buf = gfi_unpack_entry(oe, &size);
3334 }
3335
3336 /*
3337 * Output based on batch_one_object() from cat-file.c.
3338 */
3339 if (type <= 0) {
3340 strbuf_reset(&line);
3341 strbuf_addf(&line, "%s missing\n", oid_to_hex(oid));
3342 cat_blob_write(line.buf, line.len);
3343 strbuf_release(&line);
3344 free(buf);
3345 return;
3346 }
3347 if (!buf)
3348 die(_("can't read object %s"), oid_to_hex(oid));
3349 if (type != OBJ_BLOB)
3350 die(_("object %s is a %s but a blob was expected."),
3351 oid_to_hex(oid), type_name(type));
3352 strbuf_reset(&line);
3353 strbuf_addf(&line, "%s %s %"PRIuMAX"\n", oid_to_hex(oid),
3354 type_name(type), (uintmax_t)size);
3355 cat_blob_write(line.buf, line.len);
3356 strbuf_release(&line);
3357 cat_blob_write(buf, size);
3358 cat_blob_write("\n", 1);
3359 if (oe && oe->pack_id == pack_id) {
3360 last_blob.offset = oe->idx.offset;
3361 strbuf_attach(&last_blob.data, buf, size, size + 1);
3362 last_blob.depth = oe->depth;
3363 } else
3364 free(buf);
3365 }
3366
3367 static void parse_get_mark(const char *p)
3368 {
3369 struct object_entry *oe;
3370 char output[GIT_MAX_HEXSZ + 2];
3371
3372 /* get-mark SP <object> LF */
3373 if (*p != ':')
3374 die(_("not a mark: %s"), p);
3375
3376 oe = find_mark(marks, parse_mark_ref_eol(p));
3377 if (!oe)
3378 die(_("unknown mark: %s"), command_buf.buf);
3379
3380 xsnprintf(output, sizeof(output), "%s\n", oid_to_hex(&oe->idx.oid));
3381 cat_blob_write(output, the_hash_algo->hexsz + 1);
3382 }
3383
3384 static void parse_cat_blob(const char *p)
3385 {
3386 struct object_entry *oe;
3387 struct object_id oid;
3388
3389 /* cat-blob SP <object> LF */
3390 if (*p == ':') {
3391 oe = find_mark(marks, parse_mark_ref_eol(p));
3392 if (!oe)
3393 die(_("unknown mark: %s"), command_buf.buf);
3394 oidcpy(&oid, &oe->idx.oid);
3395 } else {
3396 if (parse_mapped_oid_hex(p, &oid, &p))
3397 die(_("invalid dataref: %s"), command_buf.buf);
3398 if (*p)
3399 die(_("garbage after SHA1: %s"), command_buf.buf);
3400 oe = find_object(&oid);
3401 }
3402
3403 cat_blob(oe, &oid);
3404 }
3405
3406 static struct object_entry *dereference(struct object_entry *oe,
3407 struct object_id *oid)
3408 {
3409 unsigned long size;
3410 char *buf = NULL;
3411 const unsigned hexsz = the_hash_algo->hexsz;
3412
3413 if (!oe) {
3414 enum object_type type = odb_read_object_info(the_repository->objects,
3415 oid, NULL);
3416 if (type < 0)
3417 die(_("object not found: %s"), oid_to_hex(oid));
3418 /* cache it! */
3419 oe = insert_object(oid);
3420 oe->type = type;
3421 oe->pack_id = MAX_PACK_ID;
3422 oe->idx.offset = 1;
3423 }
3424 switch (oe->type) {
3425 case OBJ_TREE: /* easy case. */
3426 return oe;
3427 case OBJ_COMMIT:
3428 case OBJ_TAG:
3429 break;
3430 default:
3431 die(_("not a tree-ish: %s"), command_buf.buf);
3432 }
3433
3434 if (oe->pack_id != MAX_PACK_ID) { /* in a pack being written */
3435 buf = gfi_unpack_entry(oe, &size);
3436 } else {
3437 enum object_type unused;
3438 buf = odb_read_object(the_repository->objects, oid,
3439 &unused, &size);
3440 }
3441 if (!buf)
3442 die(_("can't load object %s"), oid_to_hex(oid));
3443
3444 /* Peel one layer. */
3445 switch (oe->type) {
3446 case OBJ_TAG:
3447 if (size < hexsz + strlen("object ") ||
3448 get_oid_hex(buf + strlen("object "), oid))
3449 die(_("invalid SHA1 in tag: %s"), command_buf.buf);
3450 break;
3451 case OBJ_COMMIT:
3452 if (size < hexsz + strlen("tree ") ||
3453 get_oid_hex(buf + strlen("tree "), oid))
3454 die(_("invalid SHA1 in commit: %s"), command_buf.buf);
3455 }
3456
3457 free(buf);
3458 return find_object(oid);
3459 }
3460
3461 static void insert_mapped_mark(uintmax_t mark, void *object, void *cbp)
3462 {
3463 struct object_id *fromoid = object;
3464 struct object_id *tooid = find_mark(cbp, mark);
3465 int ret;
3466 khiter_t it;
3467
3468 it = kh_put_oid_map(sub_oid_map, *fromoid, &ret);
3469 /* We've already seen this object. */
3470 if (ret == 0)
3471 return;
3472 kh_value(sub_oid_map, it) = tooid;
3473 }
3474
3475 static void build_mark_map_one(struct mark_set *from, struct mark_set *to)
3476 {
3477 for_each_mark(from, 0, insert_mapped_mark, to);
3478 }
3479
3480 static void build_mark_map(struct string_list *from, struct string_list *to)
3481 {
3482 struct string_list_item *fromp, *top;
3483
3484 sub_oid_map = kh_init_oid_map();
3485
3486 for_each_string_list_item(fromp, from) {
3487 top = string_list_lookup(to, fromp->string);
3488 if (!fromp->util) {
3489 die(_("missing from marks for submodule '%s'"), fromp->string);
3490 } else if (!top || !top->util) {
3491 die(_("missing to marks for submodule '%s'"), fromp->string);
3492 }
3493 build_mark_map_one(fromp->util, top->util);
3494 }
3495 }
3496
3497 static struct object_entry *parse_treeish_dataref(const char **p)
3498 {
3499 struct object_id oid;
3500 struct object_entry *e;
3501
3502 if (**p == ':') { /* <mark> */
3503 e = find_mark(marks, parse_mark_ref_space(p));
3504 if (!e)
3505 die(_("unknown mark: %s"), command_buf.buf);
3506 oidcpy(&oid, &e->idx.oid);
3507 } else { /* <sha1> */
3508 if (parse_mapped_oid_hex(*p, &oid, p))
3509 die(_("invalid dataref: %s"), command_buf.buf);
3510 e = find_object(&oid);
3511 if (*(*p)++ != ' ')
3512 die(_("missing space after tree-ish: %s"), command_buf.buf);
3513 }
3514
3515 while (!e || e->type != OBJ_TREE)
3516 e = dereference(e, &oid);
3517 return e;
3518 }
3519
3520 static void print_ls(int mode, const unsigned char *hash, const char *path)
3521 {
3522 static struct strbuf line = STRBUF_INIT;
3523
3524 /* See show_tree(). */
3525 const char *type =
3526 S_ISGITLINK(mode) ? commit_type :
3527 S_ISDIR(mode) ? tree_type :
3528 blob_type;
3529
3530 if (!mode) {
3531 /* missing SP path LF */
3532 strbuf_reset(&line);
3533 strbuf_addstr(&line, "missing ");
3534 quote_c_style(path, &line, NULL, 0);
3535 strbuf_addch(&line, '\n');
3536 } else {
3537 /* mode SP type SP object_name TAB path LF */
3538 strbuf_reset(&line);
3539 strbuf_addf(&line, "%06o %s %s\t",
3540 mode & ~NO_DELTA, type, hash_to_hex(hash));
3541 quote_c_style(path, &line, NULL, 0);
3542 strbuf_addch(&line, '\n');
3543 }
3544 cat_blob_write(line.buf, line.len);
3545 }
3546
3547 static void parse_ls(const char *p, struct branch *b)
3548 {
3549 static struct strbuf path = STRBUF_INIT;
3550 struct tree_entry *root = NULL;
3551 struct tree_entry leaf = {NULL};
3552
3553 /* ls SP (<tree-ish> SP)? <path> */
3554 if (*p == '"') {
3555 if (!b)
3556 die(_("not in a commit: %s"), command_buf.buf);
3557 root = &b->branch_tree;
3558 } else {
3559 struct object_entry *e = parse_treeish_dataref(&p);
3560 root = new_tree_entry();
3561 oidcpy(&root->versions[1].oid, &e->idx.oid);
3562 if (!is_null_oid(&root->versions[1].oid))
3563 root->versions[1].mode = S_IFDIR;
3564 load_tree(root);
3565 }
3566 strbuf_reset(&path);
3567 parse_path_eol(&path, p, "path");
3568 tree_content_get(root, path.buf, &leaf, 1);
3569 /*
3570 * A directory in preparation would have a sha1 of zero
3571 * until it is saved. Save, for simplicity.
3572 */
3573 if (S_ISDIR(leaf.versions[1].mode))
3574 store_tree(&leaf);
3575
3576 print_ls(leaf.versions[1].mode, leaf.versions[1].oid.hash, path.buf);
3577 if (leaf.tree)
3578 release_tree_content_recursive(leaf.tree);
3579 if (!b || root != &b->branch_tree)
3580 release_tree_entry(root);
3581 }
3582
3583 static void checkpoint(void)
3584 {
3585 checkpoint_requested = 0;
3586 if (object_count) {
3587 cycle_packfile();
3588 }
3589 dump_branches();
3590 dump_tags();
3591 dump_marks();
3592 }
3593
3594 static void parse_checkpoint(void)
3595 {
3596 checkpoint_requested = 1;
3597 skip_optional_lf();
3598 }
3599
3600 static void parse_progress(void)
3601 {
3602 fwrite(command_buf.buf, 1, command_buf.len, stdout);
3603 fputc('\n', stdout);
3604 fflush(stdout);
3605 skip_optional_lf();
3606 }
3607
3608 static void parse_alias(void)
3609 {
3610 struct object_entry *e;
3611 struct branch b;
3612
3613 skip_optional_lf();
3614 read_next_command();
3615
3616 /* mark ... */
3617 parse_mark();
3618 if (!next_mark)
3619 die(_("expected 'mark' command, got %s"), command_buf.buf);
3620
3621 /* to ... */
3622 memset(&b, 0, sizeof(b));
3623 if (!parse_objectish_with_prefix(&b, "to "))
3624 die(_("expected 'to' command, got %s"), command_buf.buf);
3625 e = find_object(&b.oid);
3626 assert(e);
3627 insert_mark(&marks, next_mark, e);
3628 }
3629
3630 static char* make_fast_import_path(const char *path)
3631 {
3632 if (!relative_marks_paths || is_absolute_path(path))
3633 return prefix_filename(global_prefix, path);
3634 return repo_git_path(the_repository, "info/fast-import/%s", path);
3635 }
3636
3637 static void option_import_marks(const char *marks,
3638 int from_stream, int ignore_missing)
3639 {
3640 if (import_marks_file) {
3641 if (from_stream)
3642 die(_("only one import-marks command allowed per stream"));
3643
3644 /* read previous mark file */
3645 if(!import_marks_file_from_stream)
3646 read_marks();
3647 }
3648
3649 free(import_marks_file);
3650 import_marks_file = make_fast_import_path(marks);
3651 import_marks_file_from_stream = from_stream;
3652 import_marks_file_ignore_missing = ignore_missing;
3653 }
3654
3655 static void option_date_format(const char *fmt)
3656 {
3657 if (!strcmp(fmt, "raw"))
3658 whenspec = WHENSPEC_RAW;
3659 else if (!strcmp(fmt, "raw-permissive"))
3660 whenspec = WHENSPEC_RAW_PERMISSIVE;
3661 else if (!strcmp(fmt, "rfc2822"))
3662 whenspec = WHENSPEC_RFC2822;
3663 else if (!strcmp(fmt, "now"))
3664 whenspec = WHENSPEC_NOW;
3665 else
3666 die(_("unknown --date-format argument %s"), fmt);
3667 }
3668
3669 static unsigned long ulong_arg(const char *option, const char *arg)
3670 {
3671 char *endptr;
3672 unsigned long rv = strtoul(arg, &endptr, 0);
3673 if (strchr(arg, '-') || endptr == arg || *endptr)
3674 die(_("%s: argument must be a non-negative integer"), option);
3675 return rv;
3676 }
3677
3678 static void option_depth(const char *depth)
3679 {
3680 max_depth = ulong_arg("--depth", depth);
3681 if (max_depth > MAX_DEPTH)
3682 die(_("--depth cannot exceed %u"), MAX_DEPTH);
3683 }
3684
3685 static void option_active_branches(const char *branches)
3686 {
3687 max_active_branches = ulong_arg("--active-branches", branches);
3688 }
3689
3690 static void option_export_marks(const char *marks)
3691 {
3692 free(export_marks_file);
3693 export_marks_file = make_fast_import_path(marks);
3694 }
3695
3696 static void option_cat_blob_fd(const char *fd)
3697 {
3698 unsigned long n = ulong_arg("--cat-blob-fd", fd);
3699 if (n > (unsigned long) INT_MAX)
3700 die(_("--cat-blob-fd cannot exceed %d"), INT_MAX);
3701 cat_blob_fd = (int) n;
3702 }
3703
3704 static void option_export_pack_edges(const char *edges)
3705 {
3706 char *fn = prefix_filename(global_prefix, edges);
3707 if (pack_edges)
3708 fclose(pack_edges);
3709 pack_edges = xfopen(fn, "a");
3710 free(fn);
3711 }
3712
3713 static void option_rewrite_submodules(const char *arg, struct string_list *list)
3714 {
3715 struct mark_set *ms;
3716 FILE *fp;
3717 char *s = xstrdup(arg);
3718 char *f = strchr(s, ':');
3719 if (!f)
3720 die(_("expected format name:filename for submodule rewrite option"));
3721 *f = '\0';
3722 f++;
3723 CALLOC_ARRAY(ms, 1);
3724
3725 f = prefix_filename(global_prefix, f);
3726 fp = fopen(f, "r");
3727 if (!fp)
3728 die_errno(_("cannot read '%s'"), f);
3729 read_mark_file(&ms, fp, insert_oid_entry);
3730 fclose(fp);
3731 free(f);
3732
3733 string_list_insert(list, s)->util = ms;
3734
3735 free(s);
3736 }
3737
3738 static int parse_one_option(const char *option)
3739 {
3740 if (skip_prefix(option, "max-pack-size=", &option)) {
3741 unsigned long v;
3742 if (!git_parse_ulong(option, &v))
3743 return 0;
3744 if (v < 8192) {
3745 warning(_("max-pack-size is now in bytes, assuming --max-pack-size=%lum"), v);
3746 v *= 1024 * 1024;
3747 } else if (v < 1024 * 1024) {
3748 warning(_("minimum max-pack-size is 1 MiB"));
3749 v = 1024 * 1024;
3750 }
3751 max_packsize = v;
3752 } else if (skip_prefix(option, "big-file-threshold=", &option)) {
3753 unsigned long v;
3754 if (!git_parse_ulong(option, &v))
3755 return 0;
3756 repo_settings_set_big_file_threshold(the_repository, v);
3757 } else if (skip_prefix(option, "depth=", &option)) {
3758 option_depth(option);
3759 } else if (skip_prefix(option, "active-branches=", &option)) {
3760 option_active_branches(option);
3761 } else if (skip_prefix(option, "export-pack-edges=", &option)) {
3762 option_export_pack_edges(option);
3763 } else if (skip_prefix(option, "signed-commits=", &option)) {
3764 if (parse_sign_mode(option, &signed_commit_mode, &signed_commit_keyid))
3765 usagef(_("unknown --signed-commits mode '%s'"), option);
3766 } else if (skip_prefix(option, "signed-tags=", &option)) {
3767 if (parse_sign_mode(option, &signed_tag_mode, &signed_tag_keyid))
3768 usagef(_("unknown --signed-tags mode '%s'"), option);
3769 } else if (!strcmp(option, "quiet")) {
3770 show_stats = 0;
3771 quiet = 1;
3772 } else if (!strcmp(option, "stats")) {
3773 show_stats = 1;
3774 } else if (!strcmp(option, "allow-unsafe-features")) {
3775 ; /* already handled during early option parsing */
3776 } else {
3777 return 0;
3778 }
3779
3780 return 1;
3781 }
3782
3783 static void check_unsafe_feature(const char *feature, int from_stream)
3784 {
3785 if (from_stream && !allow_unsafe_features)
3786 die(_("feature '%s' forbidden in input without --allow-unsafe-features"),
3787 feature);
3788 }
3789
3790 static int parse_one_feature(const char *feature, int from_stream)
3791 {
3792 const char *arg;
3793
3794 if (skip_prefix(feature, "date-format=", &arg)) {
3795 option_date_format(arg);
3796 } else if (skip_prefix(feature, "import-marks=", &arg)) {
3797 check_unsafe_feature("import-marks", from_stream);
3798 option_import_marks(arg, from_stream, 0);
3799 } else if (skip_prefix(feature, "import-marks-if-exists=", &arg)) {
3800 check_unsafe_feature("import-marks-if-exists", from_stream);
3801 option_import_marks(arg, from_stream, 1);
3802 } else if (skip_prefix(feature, "export-marks=", &arg)) {
3803 check_unsafe_feature(feature, from_stream);
3804 option_export_marks(arg);
3805 } else if (!strcmp(feature, "alias")) {
3806 ; /* Don't die - this feature is supported */
3807 } else if (skip_prefix(feature, "rewrite-submodules-to=", &arg)) {
3808 option_rewrite_submodules(arg, &sub_marks_to);
3809 } else if (skip_prefix(feature, "rewrite-submodules-from=", &arg)) {
3810 option_rewrite_submodules(arg, &sub_marks_from);
3811 } else if (!strcmp(feature, "get-mark")) {
3812 ; /* Don't die - this feature is supported */
3813 } else if (!strcmp(feature, "cat-blob")) {
3814 ; /* Don't die - this feature is supported */
3815 } else if (!strcmp(feature, "relative-marks")) {
3816 relative_marks_paths = 1;
3817 } else if (!strcmp(feature, "no-relative-marks")) {
3818 relative_marks_paths = 0;
3819 } else if (!strcmp(feature, "done")) {
3820 require_explicit_termination = 1;
3821 } else if (!strcmp(feature, "force")) {
3822 force_update = 1;
3823 } else if (!strcmp(feature, "notes") || !strcmp(feature, "ls")) {
3824 ; /* do nothing; we have the feature */
3825 } else {
3826 return 0;
3827 }
3828
3829 return 1;
3830 }
3831
3832 static void parse_feature(const char *feature)
3833 {
3834 if (seen_data_command)
3835 die(_("got feature command '%s' after data command"), feature);
3836
3837 if (parse_one_feature(feature, 1))
3838 return;
3839
3840 die(_("this version of fast-import does not support feature %s."), feature);
3841 }
3842
3843 static void parse_option(const char *option)
3844 {
3845 if (seen_data_command)
3846 die(_("got option command '%s' after data command"), option);
3847
3848 if (parse_one_option(option))
3849 return;
3850
3851 die(_("this version of fast-import does not support option: %s"), option);
3852 }
3853
3854 static void git_pack_config(void)
3855 {
3856 int indexversion_value;
3857 int limit;
3858 unsigned long packsizelimit_value;
3859
3860 if (!repo_config_get_ulong(the_repository, "pack.depth", &max_depth)) {
3861 if (max_depth > MAX_DEPTH)
3862 max_depth = MAX_DEPTH;
3863 }
3864 if (!repo_config_get_int(the_repository, "pack.indexversion", &indexversion_value)) {
3865 pack_idx_opts.version = indexversion_value;
3866 if (pack_idx_opts.version > 2)
3867 git_die_config(the_repository, "pack.indexversion",
3868 "bad pack.indexVersion=%"PRIu32, pack_idx_opts.version);
3869 }
3870 if (!repo_config_get_ulong(the_repository, "pack.packsizelimit", &packsizelimit_value))
3871 max_packsize = packsizelimit_value;
3872
3873 if (!repo_config_get_int(the_repository, "fastimport.unpacklimit", &limit))
3874 unpack_limit = limit;
3875 else if (!repo_config_get_int(the_repository, "transfer.unpacklimit", &limit))
3876 unpack_limit = limit;
3877
3878 repo_config(the_repository, git_default_config, NULL);
3879 }
3880
3881 static const char fast_import_usage[] =
3882 "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
3883
3884 static void parse_argv(void)
3885 {
3886 unsigned int i;
3887
3888 for (i = 1; i < global_argc; i++) {
3889 const char *a = global_argv[i];
3890
3891 if (*a != '-' || !strcmp(a, "--"))
3892 break;
3893
3894 if (!skip_prefix(a, "--", &a))
3895 die(_("unknown option %s"), a);
3896
3897 if (parse_one_option(a))
3898 continue;
3899
3900 if (parse_one_feature(a, 0))
3901 continue;
3902
3903 if (skip_prefix(a, "cat-blob-fd=", &a)) {
3904 option_cat_blob_fd(a);
3905 continue;
3906 }
3907
3908 die(_("unknown option --%s"), a);
3909 }
3910 if (i != global_argc)
3911 usage(fast_import_usage);
3912
3913 seen_data_command = 1;
3914 if (import_marks_file)
3915 read_marks();
3916 build_mark_map(&sub_marks_from, &sub_marks_to);
3917 }
3918
3919 int cmd_fast_import(int argc,
3920 const char **argv,
3921 const char *prefix,
3922 struct repository *repo)
3923 {
3924 unsigned int i;
3925
3926 show_usage_if_asked(argc, argv, fast_import_usage);
3927
3928 reset_pack_idx_option(&pack_idx_opts);
3929 git_pack_config();
3930
3931 alloc_objects(object_entry_alloc);
3932 strbuf_init(&command_buf, 0);
3933 CALLOC_ARRAY(atom_table, atom_table_sz);
3934 CALLOC_ARRAY(branch_table, branch_table_sz);
3935 CALLOC_ARRAY(avail_tree_table, avail_tree_table_sz);
3936 marks = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
3937
3938 hashmap_init(&object_table, object_entry_hashcmp, NULL, 0);
3939
3940 /*
3941 * We don't parse most options until after we've seen the set of
3942 * "feature" lines at the start of the stream (which allows the command
3943 * line to override stream data). But we must do an early parse of any
3944 * command-line options that impact how we interpret the feature lines.
3945 */
3946 for (i = 1; i < argc; i++) {
3947 const char *arg = argv[i];
3948 if (*arg != '-' || !strcmp(arg, "--"))
3949 break;
3950 if (!strcmp(arg, "--allow-unsafe-features"))
3951 allow_unsafe_features = 1;
3952 }
3953
3954 global_argc = argc;
3955 global_argv = argv;
3956 global_prefix = prefix;
3957
3958 rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free));
3959 for (i = 0; i < (cmd_save - 1); i++)
3960 rc_free[i].next = &rc_free[i + 1];
3961 rc_free[cmd_save - 1].next = NULL;
3962
3963 start_packfile();
3964 set_die_routine(die_nicely);
3965 set_checkpoint_signal();
3966 while (read_next_command() != EOF) {
3967 const char *v;
3968 if (!strcmp("blob", command_buf.buf))
3969 parse_new_blob();
3970 else if (skip_prefix(command_buf.buf, "commit ", &v))
3971 parse_new_commit(v);
3972 else if (skip_prefix(command_buf.buf, "tag ", &v))
3973 parse_new_tag(v);
3974 else if (skip_prefix(command_buf.buf, "reset ", &v))
3975 parse_reset_branch(v);
3976 else if (skip_prefix(command_buf.buf, "ls ", &v))
3977 parse_ls(v, NULL);
3978 else if (skip_prefix(command_buf.buf, "cat-blob ", &v))
3979 parse_cat_blob(v);
3980 else if (skip_prefix(command_buf.buf, "get-mark ", &v))
3981 parse_get_mark(v);
3982 else if (!strcmp("checkpoint", command_buf.buf))
3983 parse_checkpoint();
3984 else if (!strcmp("done", command_buf.buf))
3985 break;
3986 else if (!strcmp("alias", command_buf.buf))
3987 parse_alias();
3988 else if (starts_with(command_buf.buf, "progress "))
3989 parse_progress();
3990 else if (skip_prefix(command_buf.buf, "feature ", &v))
3991 parse_feature(v);
3992 else if (skip_prefix(command_buf.buf, "option git ", &v))
3993 parse_option(v);
3994 else if (starts_with(command_buf.buf, "option "))
3995 /* ignore non-git options*/;
3996 else
3997 die(_("unsupported command: %s"), command_buf.buf);
3998
3999 if (checkpoint_requested)
4000 checkpoint();
4001 }
4002
4003 /* argv hasn't been parsed yet, do so */
4004 if (!seen_data_command)
4005 parse_argv();
4006
4007 if (require_explicit_termination && feof(stdin))
4008 die(_("stream ends early"));
4009
4010 end_packfile();
4011
4012 dump_branches();
4013 dump_tags();
4014 unkeep_all_packs();
4015 dump_marks();
4016
4017 if (pack_edges)
4018 fclose(pack_edges);
4019
4020 if (show_stats) {
4021 uintmax_t total_count = 0, duplicate_count = 0;
4022 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
4023 total_count += object_count_by_type[i];
4024 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
4025 duplicate_count += duplicate_count_by_type[i];
4026
4027 fprintf(stderr, "%s statistics:\n", argv[0]);
4028 fprintf(stderr, "---------------------------------------------------------------------\n");
4029 fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
4030 fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
4031 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]);
4032 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]);
4033 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]);
4034 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]);
4035 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
4036 fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
4037 fprintf(stderr, " atoms: %10u\n", atom_cnt);
4038 fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (tree_entry_allocd + fi_mem_pool.pool_alloc + alloc_count*sizeof(struct object_entry))/1024);
4039 fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd + fi_mem_pool.pool_alloc) /1024));
4040 fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
4041 fprintf(stderr, "---------------------------------------------------------------------\n");
4042 pack_report(repo);
4043 fprintf(stderr, "---------------------------------------------------------------------\n");
4044 fprintf(stderr, "\n");
4045 }
4046
4047 return failure ? 1 : 0;
4048 }