]> git.ipfire.org Git - thirdparty/git.git/blame - fast-import.c
path.c: don't call the match function without value in trie_find()
[thirdparty/git.git] / fast-import.c
CommitLineData
db5e523f
SP
1#include "builtin.h"
2#include "cache.h"
a80d72db 3#include "repository.h"
b2141fc1 4#include "config.h"
697cc8ef 5#include "lockfile.h"
db5e523f
SP
6#include "object.h"
7#include "blob.h"
463acbe1 8#include "tree.h"
7073e69e 9#include "commit.h"
db5e523f
SP
10#include "delta.h"
11#include "pack.h"
463acbe1 12#include "refs.h"
db5e523f 13#include "csum-file.h"
c44cdc7e 14#include "quote.h"
50906e04 15#include "dir.h"
d9545c7f 16#include "run-command.h"
4f39cd82 17#include "packfile.h"
a80d72db 18#include "object-store.h"
065feab4 19#include "mem-pool.h"
64043556 20#include "commit-reach.h"
db5e523f 21
69e74e74
SP
22#define PACK_ID_BITS 16
23#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
436e7a74
SP
24#define DEPTH_BITS 13
25#define MAX_DEPTH ((1<<DEPTH_BITS)-1)
69e74e74 26
8fb3ad76
DI
27/*
28 * We abuse the setuid bit on directories to mean "do not delta".
29 */
30#define NO_DELTA S_ISUID
31
28d055bd 32/*
33 * The amount of additional space required in order to write an object into the
34 * current pack. This is the hash lengths at the end of the pack, plus the
35 * length of one object ID.
36 */
37#define PACK_SIZE_THRESHOLD (the_hash_algo->rawsz * 3)
38
9cba13ca 39struct object_entry {
3fc366bd 40 struct pack_idx_entry idx;
27d6d290 41 struct object_entry *next;
436e7a74
SP
42 uint32_t type : TYPE_BITS,
43 pack_id : PACK_ID_BITS,
44 depth : DEPTH_BITS;
27d6d290
SP
45};
46
9cba13ca 47struct object_entry_pool {
463acbe1 48 struct object_entry_pool *next_pool;
27d6d290
SP
49 struct object_entry *next_free;
50 struct object_entry *end;
ac47a738 51 struct object_entry entries[FLEX_ARRAY]; /* more */
27d6d290
SP
52};
53
9cba13ca 54struct mark_set {
d8397168
SP
55 union {
56 struct object_entry *marked[1024];
57 struct mark_set *sets[1024];
58 } data;
6f64f6d9 59 unsigned int shift;
d8397168
SP
60};
61
9cba13ca 62struct last_object {
05576569 63 struct strbuf data;
89e0a3a1 64 off_t offset;
6bb5b329 65 unsigned int depth;
05576569 66 unsigned no_swap : 1;
ac47a738
SP
67};
68
9cba13ca 69struct atom_str {
463acbe1 70 struct atom_str *next_atom;
10831c55 71 unsigned short str_len;
463acbe1
SP
72 char str_dat[FLEX_ARRAY]; /* more */
73};
74
75struct tree_content;
9cba13ca 76struct tree_entry {
463acbe1 77 struct tree_content *tree;
4b25d091 78 struct atom_str *name;
9cba13ca 79 struct tree_entry_ms {
10831c55 80 uint16_t mode;
d7e6b6a8 81 struct object_id oid;
4cabf858 82 } versions[2];
6bb5b329
SP
83};
84
9cba13ca 85struct tree_content {
463acbe1
SP
86 unsigned int entry_capacity; /* must match avail_tree_content */
87 unsigned int entry_count;
4cabf858 88 unsigned int delta_depth;
463acbe1
SP
89 struct tree_entry *entries[FLEX_ARRAY]; /* more */
90};
91
9cba13ca 92struct avail_tree_content {
463acbe1
SP
93 unsigned int entry_capacity; /* must match tree_content */
94 struct avail_tree_content *next_avail;
6bb5b329
SP
95};
96
9cba13ca 97struct branch {
463acbe1
SP
98 struct branch *table_next_branch;
99 struct branch *active_next_branch;
6bb5b329 100 const char *name;
463acbe1 101 struct tree_entry branch_tree;
69e74e74 102 uintmax_t last_commit;
2a113aee 103 uintmax_t num_notes;
734c91f9 104 unsigned active : 1;
4ee1b225 105 unsigned delete : 1;
734c91f9 106 unsigned pack_id : PACK_ID_BITS;
d7e6b6a8 107 struct object_id oid;
6bb5b329
SP
108};
109
9cba13ca 110struct tag {
72303d44
SP
111 struct tag *next_tag;
112 const char *name;
2369ed79 113 unsigned int pack_id;
d7e6b6a8 114 struct object_id oid;
72303d44
SP
115};
116
9cba13ca 117struct hash_list {
62b6f483 118 struct hash_list *next;
d7e6b6a8 119 struct object_id oid;
62b6f483 120};
463acbe1 121
63e0c8b3
SP
122typedef enum {
123 WHENSPEC_RAW = 1,
124 WHENSPEC_RFC2822,
4b05548f 125 WHENSPEC_NOW
63e0c8b3
SP
126} whenspec_type;
127
9cba13ca 128struct recent_command {
904b1941
SP
129 struct recent_command *prev;
130 struct recent_command *next;
131 char *buf;
132};
133
0ea9f045 134/* Configured limits on output */
4f2220e6 135static unsigned long max_depth = 50;
89e0a3a1 136static off_t max_packsize;
d9545c7f 137static int unpack_limit = 100;
7073e69e 138static int force_update;
0ea9f045
SP
139
140/* Stats and misc. counters */
141static uintmax_t alloc_count;
0ea9f045
SP
142static uintmax_t marks_set_count;
143static uintmax_t object_count_by_type[1 << TYPE_BITS];
144static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
145static uintmax_t delta_count_by_type[1 << TYPE_BITS];
94c3b482 146static uintmax_t delta_count_attempts_by_type[1 << TYPE_BITS];
a7ddc487 147static unsigned long object_count;
6bb5b329 148static unsigned long branch_count;
d6c7eb2c 149static unsigned long branch_load_count;
7073e69e 150static int failure;
bdf1c06d 151static FILE *pack_edges;
0f6927c2 152static unsigned int show_stats = 1;
9c8398f0 153static int global_argc;
3f2e2297 154static const char **global_argv;
ac47a738 155
463acbe1 156/* Memory pools */
96c47d14
JM
157static struct mem_pool fi_mem_pool = {NULL, 2*1024*1024 -
158 sizeof(struct mp_block), 0 };
463acbe1 159
c44cdc7e 160/* Atom management */
463acbe1
SP
161static unsigned int atom_table_sz = 4451;
162static unsigned int atom_cnt;
163static struct atom_str **atom_table;
164
165/* The .pack file being generated */
ebcfb379 166static struct pack_idx_option pack_idx_opts;
7bfe6e26 167static unsigned int pack_id;
98a3beab 168static struct hashfile *pack_file;
d489bc14 169static struct packed_git *pack_data;
7bfe6e26 170static struct packed_git **all_packs;
89e0a3a1 171static off_t pack_size;
ac47a738
SP
172
173/* Table of objects we've written. */
4cabf858 174static unsigned int object_entry_alloc = 5000;
463acbe1
SP
175static struct object_entry_pool *blocks;
176static struct object_entry *object_table[1 << 16];
d8397168 177static struct mark_set *marks;
07cd9328
SR
178static const char *export_marks_file;
179static const char *import_marks_file;
081751c8 180static int import_marks_file_from_stream;
dded4f12 181static int import_marks_file_ignore_missing;
f4beed60 182static int import_marks_file_done;
bc3c79ae 183static int relative_marks_paths;
ac47a738
SP
184
185/* Our last blob */
05576569 186static struct last_object last_blob = { STRBUF_INIT, 0, 0, 0 };
463acbe1
SP
187
188/* Tree management */
189static unsigned int tree_entry_alloc = 1000;
190static void *avail_tree_entry;
191static unsigned int avail_tree_table_sz = 100;
192static struct avail_tree_content **avail_tree_table;
96c47d14 193static size_t tree_entry_allocd;
eec813cf
PH
194static struct strbuf old_tree = STRBUF_INIT;
195static struct strbuf new_tree = STRBUF_INIT;
8bcce301 196
6bb5b329 197/* Branch data */
d5c57b28
SP
198static unsigned long max_active_branches = 5;
199static unsigned long cur_active_branches;
200static unsigned long branch_table_sz = 1039;
463acbe1
SP
201static struct branch **branch_table;
202static struct branch *active_branches;
203
72303d44
SP
204/* Tag data */
205static struct tag *first_tag;
206static struct tag *last_tag;
207
c44cdc7e 208/* Input stream parsing */
63e0c8b3 209static whenspec_type whenspec = WHENSPEC_RAW;
4a241d79 210static struct strbuf command_buf = STRBUF_INIT;
1fdb649c 211static int unread_command_buf;
904b1941
SP
212static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
213static struct recent_command *cmd_tail = &cmd_hist;
214static struct recent_command *rc_free;
215static unsigned int cmd_save = 100;
0ea9f045 216static uintmax_t next_mark;
eec813cf 217static struct strbuf new_data = STRBUF_INIT;
f963bd5d 218static int seen_data_command;
be56862f 219static int require_explicit_termination;
c44cdc7e 220
dc01f59d
JN
221/* Signal handling */
222static volatile sig_atomic_t checkpoint_requested;
223
85c62395
DB
224/* Where to write output of cat-blob commands */
225static int cat_blob_fd = STDOUT_FILENO;
226
9c8398f0 227static void parse_argv(void);
28c7b1f7 228static void parse_get_mark(const char *p);
97313bef
JK
229static void parse_cat_blob(const char *p);
230static void parse_ls(const char *p, struct branch *b);
c44cdc7e 231
8acb3297
SP
232static void write_branch_report(FILE *rpt, struct branch *b)
233{
234 fprintf(rpt, "%s:\n", b->name);
235
236 fprintf(rpt, " status :");
237 if (b->active)
238 fputs(" active", rpt);
239 if (b->branch_tree.tree)
240 fputs(" loaded", rpt);
d7e6b6a8 241 if (is_null_oid(&b->branch_tree.versions[1].oid))
8acb3297
SP
242 fputs(" dirty", rpt);
243 fputc('\n', rpt);
244
d7e6b6a8 245 fprintf(rpt, " tip commit : %s\n", oid_to_hex(&b->oid));
246 fprintf(rpt, " old tree : %s\n",
247 oid_to_hex(&b->branch_tree.versions[0].oid));
248 fprintf(rpt, " cur tree : %s\n",
249 oid_to_hex(&b->branch_tree.versions[1].oid));
8acb3297
SP
250 fprintf(rpt, " commit clock: %" PRIuMAX "\n", b->last_commit);
251
252 fputs(" last pack : ", rpt);
253 if (b->pack_id < MAX_PACK_ID)
254 fprintf(rpt, "%u", b->pack_id);
255 fputc('\n', rpt);
256
257 fputc('\n', rpt);
258}
259
3b08e5b8
SP
260static void dump_marks_helper(FILE *, uintmax_t, struct mark_set *);
261
4bf53833 262static void write_crash_report(const char *err)
8acb3297 263{
fcd12db6 264 char *loc = git_pathdup("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
8acb3297
SP
265 FILE *rpt = fopen(loc, "w");
266 struct branch *b;
267 unsigned long lu;
904b1941 268 struct recent_command *rc;
8acb3297
SP
269
270 if (!rpt) {
6c223e49 271 error_errno("can't write crash report %s", loc);
fcd12db6 272 free(loc);
8acb3297
SP
273 return;
274 }
275
276 fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
277
278 fprintf(rpt, "fast-import crash report:\n");
85e72830
DSP
279 fprintf(rpt, " fast-import process: %"PRIuMAX"\n", (uintmax_t) getpid());
280 fprintf(rpt, " parent process : %"PRIuMAX"\n", (uintmax_t) getppid());
547ed716 281 fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_MODE(ISO8601)));
8acb3297
SP
282 fputc('\n', rpt);
283
284 fputs("fatal: ", rpt);
4bf53833 285 fputs(err, rpt);
8acb3297
SP
286 fputc('\n', rpt);
287
904b1941
SP
288 fputc('\n', rpt);
289 fputs("Most Recent Commands Before Crash\n", rpt);
290 fputs("---------------------------------\n", rpt);
291 for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
292 if (rc->next == &cmd_hist)
293 fputs("* ", rpt);
294 else
295 fputs(" ", rpt);
296 fputs(rc->buf, rpt);
297 fputc('\n', rpt);
298 }
299
8acb3297
SP
300 fputc('\n', rpt);
301 fputs("Active Branch LRU\n", rpt);
302 fputs("-----------------\n", rpt);
303 fprintf(rpt, " active_branches = %lu cur, %lu max\n",
304 cur_active_branches,
305 max_active_branches);
306 fputc('\n', rpt);
307 fputs(" pos clock name\n", rpt);
308 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt);
309 for (b = active_branches, lu = 0; b; b = b->active_next_branch)
310 fprintf(rpt, " %2lu) %6" PRIuMAX" %s\n",
311 ++lu, b->last_commit, b->name);
312
313 fputc('\n', rpt);
314 fputs("Inactive Branches\n", rpt);
315 fputs("-----------------\n", rpt);
316 for (lu = 0; lu < branch_table_sz; lu++) {
317 for (b = branch_table[lu]; b; b = b->table_next_branch)
318 write_branch_report(rpt, b);
319 }
320
fbc63ea6
SP
321 if (first_tag) {
322 struct tag *tg;
323 fputc('\n', rpt);
324 fputs("Annotated Tags\n", rpt);
325 fputs("--------------\n", rpt);
326 for (tg = first_tag; tg; tg = tg->next_tag) {
d7e6b6a8 327 fputs(oid_to_hex(&tg->oid), rpt);
fbc63ea6
SP
328 fputc(' ', rpt);
329 fputs(tg->name, rpt);
330 fputc('\n', rpt);
331 }
332 }
333
3b08e5b8
SP
334 fputc('\n', rpt);
335 fputs("Marks\n", rpt);
336 fputs("-----\n", rpt);
07cd9328
SR
337 if (export_marks_file)
338 fprintf(rpt, " exported to %s\n", export_marks_file);
3b08e5b8
SP
339 else
340 dump_marks_helper(rpt, 0, marks);
341
8acb3297
SP
342 fputc('\n', rpt);
343 fputs("-------------------\n", rpt);
344 fputs("END OF CRASH REPORT\n", rpt);
345 fclose(rpt);
fcd12db6 346 free(loc);
8acb3297
SP
347}
348
118805b9
SP
349static void end_packfile(void);
350static void unkeep_all_packs(void);
351static void dump_marks(void);
352
8acb3297
SP
353static NORETURN void die_nicely(const char *err, va_list params)
354{
355 static int zombie;
4bf53833 356 char message[2 * PATH_MAX];
8acb3297 357
4bf53833 358 vsnprintf(message, sizeof(message), err, params);
8acb3297 359 fputs("fatal: ", stderr);
4bf53833 360 fputs(message, stderr);
8acb3297
SP
361 fputc('\n', stderr);
362
363 if (!zombie) {
364 zombie = 1;
4bf53833 365 write_crash_report(message);
118805b9
SP
366 end_packfile();
367 unkeep_all_packs();
368 dump_marks();
8acb3297 369 }
8acb3297
SP
370 exit(128);
371}
6bb5b329 372
dc01f59d
JN
373#ifndef SIGUSR1 /* Windows, for example */
374
375static void set_checkpoint_signal(void)
376{
377}
378
379#else
380
381static void checkpoint_signal(int signo)
382{
383 checkpoint_requested = 1;
384}
385
386static void set_checkpoint_signal(void)
387{
388 struct sigaction sa;
389
390 memset(&sa, 0, sizeof(sa));
391 sa.sa_handler = checkpoint_signal;
392 sigemptyset(&sa.sa_mask);
393 sa.sa_flags = SA_RESTART;
394 sigaction(SIGUSR1, &sa, NULL);
395}
396
397#endif
398
03842d8e 399static void alloc_objects(unsigned int cnt)
8bcce301 400{
463acbe1 401 struct object_entry_pool *b;
27d6d290 402
463acbe1 403 b = xmalloc(sizeof(struct object_entry_pool)
27d6d290 404 + cnt * sizeof(struct object_entry));
463acbe1 405 b->next_pool = blocks;
27d6d290
SP
406 b->next_free = b->entries;
407 b->end = b->entries + cnt;
408 blocks = b;
409 alloc_count += cnt;
410}
8bcce301 411
912c13d5 412static struct object_entry *new_object(struct object_id *oid)
8bcce301 413{
27d6d290 414 struct object_entry *e;
8bcce301 415
27d6d290 416 if (blocks->next_free == blocks->end)
463acbe1 417 alloc_objects(object_entry_alloc);
8bcce301 418
27d6d290 419 e = blocks->next_free++;
e6a492b7 420 oidcpy(&e->idx.oid, oid);
27d6d290 421 return e;
8bcce301
SP
422}
423
912c13d5 424static struct object_entry *find_object(struct object_id *oid)
463acbe1 425{
912c13d5 426 unsigned int h = oid->hash[0] << 8 | oid->hash[1];
463acbe1
SP
427 struct object_entry *e;
428 for (e = object_table[h]; e; e = e->next)
4a7e27e9 429 if (oideq(oid, &e->idx.oid))
463acbe1
SP
430 return e;
431 return NULL;
432}
433
912c13d5 434static struct object_entry *insert_object(struct object_id *oid)
8bcce301 435{
912c13d5 436 unsigned int h = oid->hash[0] << 8 | oid->hash[1];
27d6d290 437 struct object_entry *e = object_table[h];
8bcce301
SP
438
439 while (e) {
4a7e27e9 440 if (oideq(oid, &e->idx.oid))
8bcce301 441 return e;
8bcce301
SP
442 e = e->next;
443 }
444
912c13d5 445 e = new_object(oid);
b7c1ce4f 446 e->next = object_table[h];
3fc366bd 447 e->idx.offset = 0;
b7c1ce4f 448 object_table[h] = e;
8bcce301
SP
449 return e;
450}
db5e523f 451
d2986d0f
EW
452static void invalidate_pack_id(unsigned int id)
453{
454 unsigned int h;
455 unsigned long lu;
456 struct tag *t;
457
458 for (h = 0; h < ARRAY_SIZE(object_table); h++) {
459 struct object_entry *e;
460
461 for (e = object_table[h]; e; e = e->next)
462 if (e->pack_id == id)
463 e->pack_id = MAX_PACK_ID;
464 }
465
466 for (lu = 0; lu < branch_table_sz; lu++) {
467 struct branch *b;
468
469 for (b = branch_table[lu]; b; b = b->table_next_branch)
470 if (b->pack_id == id)
471 b->pack_id = MAX_PACK_ID;
472 }
473
474 for (t = first_tag; t; t = t->next_tag)
475 if (t->pack_id == id)
476 t->pack_id = MAX_PACK_ID;
477}
478
463acbe1
SP
479static unsigned int hc_str(const char *s, size_t len)
480{
481 unsigned int r = 0;
482 while (len-- > 0)
483 r = r * 31 + *s++;
484 return r;
485}
486
e5b1444b 487static char *pool_strdup(const char *s)
463acbe1 488{
34fa79a6 489 size_t len = strlen(s) + 1;
96c47d14 490 char *r = mem_pool_alloc(&fi_mem_pool, len);
34fa79a6 491 memcpy(r, s, len);
463acbe1
SP
492 return r;
493}
494
0ea9f045 495static void insert_mark(uintmax_t idnum, struct object_entry *oe)
d8397168
SP
496{
497 struct mark_set *s = marks;
498 while ((idnum >> s->shift) >= 1024) {
96c47d14 499 s = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
d8397168
SP
500 s->shift = marks->shift + 10;
501 s->data.sets[0] = marks;
502 marks = s;
503 }
504 while (s->shift) {
0ea9f045 505 uintmax_t i = idnum >> s->shift;
d8397168
SP
506 idnum -= i << s->shift;
507 if (!s->data.sets[i]) {
96c47d14 508 s->data.sets[i] = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
d8397168
SP
509 s->data.sets[i]->shift = s->shift - 10;
510 }
511 s = s->data.sets[i];
512 }
513 if (!s->data.marked[idnum])
514 marks_set_count++;
515 s->data.marked[idnum] = oe;
516}
517
e5b1444b 518static struct object_entry *find_mark(uintmax_t idnum)
d8397168 519{
0ea9f045 520 uintmax_t orig_idnum = idnum;
d8397168
SP
521 struct mark_set *s = marks;
522 struct object_entry *oe = NULL;
523 if ((idnum >> s->shift) < 1024) {
524 while (s && s->shift) {
0ea9f045 525 uintmax_t i = idnum >> s->shift;
d8397168
SP
526 idnum -= i << s->shift;
527 s = s->data.sets[i];
528 }
529 if (s)
530 oe = s->data.marked[idnum];
531 }
532 if (!oe)
3efb1f34 533 die("mark :%" PRIuMAX " not declared", orig_idnum);
d8397168
SP
534 return oe;
535}
536
e5b1444b 537static struct atom_str *to_atom(const char *s, unsigned short len)
463acbe1
SP
538{
539 unsigned int hc = hc_str(s, len) % atom_table_sz;
540 struct atom_str *c;
541
542 for (c = atom_table[hc]; c; c = c->next_atom)
543 if (c->str_len == len && !strncmp(s, c->str_dat, len))
544 return c;
545
96c47d14 546 c = mem_pool_alloc(&fi_mem_pool, sizeof(struct atom_str) + len + 1);
463acbe1 547 c->str_len = len;
eddda371 548 memcpy(c->str_dat, s, len);
463acbe1
SP
549 c->str_dat[len] = 0;
550 c->next_atom = atom_table[hc];
551 atom_table[hc] = c;
552 atom_cnt++;
553 return c;
554}
555
e5b1444b 556static struct branch *lookup_branch(const char *name)
463acbe1
SP
557{
558 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
559 struct branch *b;
560
561 for (b = branch_table[hc]; b; b = b->table_next_branch)
562 if (!strcmp(name, b->name))
563 return b;
564 return NULL;
565}
566
e5b1444b 567static struct branch *new_branch(const char *name)
463acbe1
SP
568{
569 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
4b25d091 570 struct branch *b = lookup_branch(name);
463acbe1
SP
571
572 if (b)
573 die("Invalid attempt to create duplicate branch: %s", name);
8d9c5010 574 if (check_refname_format(name, REFNAME_ALLOW_ONELEVEL))
c44cdc7e 575 die("Branch name doesn't conform to GIT standards: %s", name);
463acbe1 576
96c47d14 577 b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch));
463acbe1
SP
578 b->name = pool_strdup(name);
579 b->table_next_branch = branch_table[hc];
8a8c55ea
SP
580 b->branch_tree.versions[0].mode = S_IFDIR;
581 b->branch_tree.versions[1].mode = S_IFDIR;
2a113aee 582 b->num_notes = 0;
734c91f9 583 b->active = 0;
69e74e74 584 b->pack_id = MAX_PACK_ID;
463acbe1
SP
585 branch_table[hc] = b;
586 branch_count++;
587 return b;
588}
589
590static unsigned int hc_entries(unsigned int cnt)
591{
592 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
593 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
594}
595
e5b1444b 596static struct tree_content *new_tree_content(unsigned int cnt)
463acbe1
SP
597{
598 struct avail_tree_content *f, *l = NULL;
599 struct tree_content *t;
600 unsigned int hc = hc_entries(cnt);
601
602 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
603 if (f->entry_capacity >= cnt)
604 break;
605
606 if (f) {
607 if (l)
608 l->next_avail = f->next_avail;
609 else
610 avail_tree_table[hc] = f->next_avail;
611 } else {
612 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
96c47d14 613 f = mem_pool_alloc(&fi_mem_pool, sizeof(*t) + sizeof(t->entries[0]) * cnt);
463acbe1
SP
614 f->entry_capacity = cnt;
615 }
616
617 t = (struct tree_content*)f;
618 t->entry_count = 0;
4cabf858 619 t->delta_depth = 0;
463acbe1
SP
620 return t;
621}
622
623static void release_tree_entry(struct tree_entry *e);
624static void release_tree_content(struct tree_content *t)
625{
626 struct avail_tree_content *f = (struct avail_tree_content*)t;
627 unsigned int hc = hc_entries(f->entry_capacity);
afde8dd9
SP
628 f->next_avail = avail_tree_table[hc];
629 avail_tree_table[hc] = f;
630}
631
632static void release_tree_content_recursive(struct tree_content *t)
633{
463acbe1
SP
634 unsigned int i;
635 for (i = 0; i < t->entry_count; i++)
636 release_tree_entry(t->entries[i]);
afde8dd9 637 release_tree_content(t);
463acbe1
SP
638}
639
e5b1444b 640static struct tree_content *grow_tree_content(
463acbe1
SP
641 struct tree_content *t,
642 int amt)
643{
644 struct tree_content *r = new_tree_content(t->entry_count + amt);
645 r->entry_count = t->entry_count;
4cabf858 646 r->delta_depth = t->delta_depth;
921d49be 647 COPY_ARRAY(r->entries, t->entries, t->entry_count);
463acbe1
SP
648 release_tree_content(t);
649 return r;
650}
651
e5b1444b 652static struct tree_entry *new_tree_entry(void)
463acbe1
SP
653{
654 struct tree_entry *e;
655
656 if (!avail_tree_entry) {
657 unsigned int n = tree_entry_alloc;
96c47d14 658 tree_entry_allocd += n * sizeof(struct tree_entry);
b32fa95f
JK
659 ALLOC_ARRAY(e, n);
660 avail_tree_entry = e;
2eb26d84 661 while (n-- > 1) {
463acbe1
SP
662 *((void**)e) = e + 1;
663 e++;
664 }
35ef237c 665 *((void**)e) = NULL;
463acbe1
SP
666 }
667
668 e = avail_tree_entry;
669 avail_tree_entry = *((void**)e);
670 return e;
671}
672
673static void release_tree_entry(struct tree_entry *e)
674{
675 if (e->tree)
afde8dd9 676 release_tree_content_recursive(e->tree);
463acbe1
SP
677 *((void**)e) = avail_tree_entry;
678 avail_tree_entry = e;
679}
680
b6f3481b
SP
681static struct tree_content *dup_tree_content(struct tree_content *s)
682{
683 struct tree_content *d;
684 struct tree_entry *a, *b;
685 unsigned int i;
686
687 if (!s)
688 return NULL;
689 d = new_tree_content(s->entry_count);
690 for (i = 0; i < s->entry_count; i++) {
691 a = s->entries[i];
692 b = new_tree_entry();
693 memcpy(b, a, sizeof(*a));
d7e6b6a8 694 if (a->tree && is_null_oid(&b->versions[1].oid))
b6f3481b
SP
695 b->tree = dup_tree_content(a->tree);
696 else
697 b->tree = NULL;
698 d->entries[i] = b;
699 }
700 d->entry_count = s->entry_count;
701 d->delta_depth = s->delta_depth;
702
703 return d;
704}
705
fd99224e 706static void start_packfile(void)
f70b6534 707{
594fa999 708 struct strbuf tmp_file = STRBUF_INIT;
7bfe6e26 709 struct packed_git *p;
f70b6534 710 struct pack_header hdr;
0fcbcae7 711 int pack_fd;
f70b6534 712
594fa999
JK
713 pack_fd = odb_mkstemp(&tmp_file, "pack/tmp_pack_XXXXXX");
714 FLEX_ALLOC_STR(p, pack_name, tmp_file.buf);
715 strbuf_release(&tmp_file);
716
7bfe6e26 717 p->pack_fd = pack_fd;
d131b7af 718 p->do_not_close = 1;
98a3beab 719 pack_file = hashfd(pack_fd, p->pack_name);
f70b6534
SP
720
721 hdr.hdr_signature = htonl(PACK_SIGNATURE);
722 hdr.hdr_version = htonl(2);
723 hdr.hdr_entries = 0;
98a3beab 724 hashwrite(pack_file, &hdr, sizeof(hdr));
7bfe6e26
SP
725
726 pack_data = p;
f70b6534
SP
727 pack_size = sizeof(hdr);
728 object_count = 0;
7bfe6e26 729
2756ca43 730 REALLOC_ARRAY(all_packs, pack_id + 1);
7bfe6e26 731 all_packs[pack_id] = p;
f70b6534
SP
732}
733
427cb22c 734static const char *create_index(void)
f70b6534 735{
427cb22c
NP
736 const char *tmpfile;
737 struct pack_idx_entry **idx, **c, **last;
738 struct object_entry *e;
f70b6534 739 struct object_entry_pool *o;
f70b6534 740
427cb22c 741 /* Build the table of object IDs. */
b32fa95f 742 ALLOC_ARRAY(idx, object_count);
f70b6534
SP
743 c = idx;
744 for (o = blocks; o; o = o->next_pool)
d9ee53ce
SP
745 for (e = o->next_free; e-- != o->entries;)
746 if (pack_id == e->pack_id)
427cb22c 747 *c++ = &e->idx;
f70b6534 748 last = idx + object_count;
2fce1f3c
SP
749 if (c != last)
750 die("internal consistency error creating the index");
f70b6534 751
538b1523 752 tmpfile = write_idx_file(NULL, idx, object_count, &pack_idx_opts,
753 pack_data->hash);
f70b6534 754 free(idx);
8455e484
SP
755 return tmpfile;
756}
757
427cb22c 758static char *keep_pack(const char *curr_index_name)
8455e484 759{
3a55602e 760 static const char *keep_msg = "fast-import";
ba47a308 761 struct strbuf name = STRBUF_INIT;
8455e484
SP
762 int keep_fd;
763
538b1523 764 odb_pack_name(&name, pack_data->hash, "keep");
ba47a308 765 keep_fd = odb_pack_keep(name.buf);
8455e484 766 if (keep_fd < 0)
0721c314 767 die_errno("cannot create keep file");
95693d45
JM
768 write_or_die(keep_fd, keep_msg, strlen(keep_msg));
769 if (close(keep_fd))
0721c314 770 die_errno("failed to write keep file");
8455e484 771
538b1523 772 odb_pack_name(&name, pack_data->hash, "pack");
ba47a308 773 if (finalize_object_file(pack_data->pack_name, name.buf))
8455e484 774 die("cannot store pack file");
8455e484 775
538b1523 776 odb_pack_name(&name, pack_data->hash, "idx");
ba47a308 777 if (finalize_object_file(curr_index_name, name.buf))
8455e484 778 die("cannot store index file");
427cb22c 779 free((void *)curr_index_name);
ba47a308 780 return strbuf_detach(&name, NULL);
8455e484
SP
781}
782
fd99224e 783static void unkeep_all_packs(void)
8455e484 784{
ba47a308 785 struct strbuf name = STRBUF_INIT;
8455e484
SP
786 int k;
787
788 for (k = 0; k < pack_id; k++) {
789 struct packed_git *p = all_packs[k];
538b1523 790 odb_pack_name(&name, p->hash, "keep");
ba47a308 791 unlink_or_warn(name.buf);
8455e484 792 }
ba47a308 793 strbuf_release(&name);
f70b6534
SP
794}
795
d9545c7f
EW
796static int loosen_small_pack(const struct packed_git *p)
797{
798 struct child_process unpack = CHILD_PROCESS_INIT;
799
800 if (lseek(p->pack_fd, 0, SEEK_SET) < 0)
801 die_errno("Failed seeking to start of '%s'", p->pack_name);
802
803 unpack.in = p->pack_fd;
804 unpack.git_cmd = 1;
805 unpack.stdout_to_stderr = 1;
806 argv_array_push(&unpack.args, "unpack-objects");
807 if (!show_stats)
808 argv_array_push(&unpack.args, "-q");
809
810 return run_command(&unpack);
811}
812
fd99224e 813static void end_packfile(void)
f70b6534 814{
5e915f30
JK
815 static int running;
816
817 if (running || !pack_data)
3c078b9c 818 return;
7bfe6e26 819
5e915f30 820 running = 1;
3d20c636 821 clear_delta_base_cache();
3e005baf 822 if (object_count) {
3c078b9c 823 struct packed_git *new_p;
912c13d5 824 struct object_id cur_pack_oid;
8455e484 825 char *idx_name;
2369ed79
SP
826 int i;
827 struct branch *b;
828 struct tag *t;
8455e484 829
c9ced051 830 close_pack_windows(pack_data);
f2af9f5e 831 finalize_hashfile(pack_file, cur_pack_oid.hash, 0);
538b1523 832 fixup_pack_header_footer(pack_data->pack_fd, pack_data->hash,
833 pack_data->pack_name, object_count,
834 cur_pack_oid.hash, pack_size);
d9545c7f
EW
835
836 if (object_count <= unpack_limit) {
d2986d0f
EW
837 if (!loosen_small_pack(pack_data)) {
838 invalidate_pack_id(pack_id);
d9545c7f 839 goto discard_pack;
d2986d0f 840 }
d9545c7f
EW
841 }
842
8b0eca7c 843 close(pack_data->pack_fd);
8455e484 844 idx_name = keep_pack(create_index());
3e005baf 845
3ea3c215 846 /* Register the packfile with core git's machinery. */
3e005baf
SP
847 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
848 if (!new_p)
849 die("core git rejected index %s", idx_name);
2369ed79 850 all_packs[pack_id] = new_p;
5babff16 851 install_packed_git(the_repository, new_p);
ba47a308 852 free(idx_name);
2369ed79
SP
853
854 /* Print the boundary */
bdf1c06d
SP
855 if (pack_edges) {
856 fprintf(pack_edges, "%s:", new_p->pack_name);
857 for (i = 0; i < branch_table_sz; i++) {
858 for (b = branch_table[i]; b; b = b->table_next_branch) {
859 if (b->pack_id == pack_id)
d7e6b6a8 860 fprintf(pack_edges, " %s",
861 oid_to_hex(&b->oid));
bdf1c06d 862 }
2369ed79 863 }
bdf1c06d
SP
864 for (t = first_tag; t; t = t->next_tag) {
865 if (t->pack_id == pack_id)
d7e6b6a8 866 fprintf(pack_edges, " %s",
867 oid_to_hex(&t->oid));
bdf1c06d
SP
868 }
869 fputc('\n', pack_edges);
870 fflush(pack_edges);
2369ed79 871 }
2369ed79
SP
872
873 pack_id++;
3e005baf 874 }
87c8a56e 875 else {
d9545c7f 876discard_pack:
3c078b9c
JK
877 close(pack_data->pack_fd);
878 unlink_or_warn(pack_data->pack_name);
87c8a56e 879 }
6a83d902 880 FREE_AND_NULL(pack_data);
5e915f30 881 running = 0;
7bfe6e26
SP
882
883 /* We can't carry a delta across packfiles. */
05576569 884 strbuf_release(&last_blob.data);
7bfe6e26
SP
885 last_blob.offset = 0;
886 last_blob.depth = 0;
f70b6534
SP
887}
888
820b9310 889static void cycle_packfile(void)
d9ee53ce
SP
890{
891 end_packfile();
892 start_packfile();
893}
894
ac47a738
SP
895static int store_object(
896 enum object_type type,
eec813cf 897 struct strbuf *dat,
6bb5b329 898 struct last_object *last,
912c13d5 899 struct object_id *oidout,
0ea9f045 900 uintmax_t mark)
db5e523f 901{
db5e523f 902 void *out, *delta;
ac47a738
SP
903 struct object_entry *e;
904 unsigned char hdr[96];
912c13d5 905 struct object_id oid;
db5e523f 906 unsigned long hdrlen, deltalen;
7f89428d 907 git_hash_ctx c;
ef49a7a0 908 git_zstream s;
ac47a738 909
ef1286d3 910 hdrlen = xsnprintf((char *)hdr, sizeof(hdr), "%s %lu",
debca9d2 911 type_name(type), (unsigned long)dat->len) + 1;
7f89428d 912 the_hash_algo->init_fn(&c);
913 the_hash_algo->update_fn(&c, hdr, hdrlen);
914 the_hash_algo->update_fn(&c, dat->buf, dat->len);
915 the_hash_algo->final_fn(oid.hash, &c);
912c13d5 916 if (oidout)
917 oidcpy(oidout, &oid);
ac47a738 918
912c13d5 919 e = insert_object(&oid);
d8397168
SP
920 if (mark)
921 insert_mark(mark, e);
3fc366bd 922 if (e->idx.offset) {
6143f064 923 duplicate_count_by_type[type]++;
463acbe1 924 return 1;
a80d72db 925 } else if (find_sha1_pack(oid.hash,
454ea2e4 926 get_all_packs(the_repository))) {
a5c1780a
SP
927 e->type = type;
928 e->pack_id = MAX_PACK_ID;
3fc366bd 929 e->idx.offset = 1; /* just not zero! */
a5c1780a
SP
930 duplicate_count_by_type[type]++;
931 return 1;
ac47a738 932 }
db5e523f 933
9d14ecf3 934 if (last && last->data.len && last->data.buf && last->depth < max_depth
7f89428d 935 && dat->len > the_hash_algo->rawsz) {
936
94c3b482 937 delta_count_attempts_by_type[type]++;
05576569 938 delta = diff_delta(last->data.buf, last->data.len,
eec813cf 939 dat->buf, dat->len,
7f89428d 940 &deltalen, dat->len - the_hash_algo->rawsz);
d9ee53ce
SP
941 } else
942 delta = NULL;
db5e523f 943
55bb5c91 944 git_deflate_init(&s, pack_compression_level);
d9ee53ce
SP
945 if (delta) {
946 s.next_in = delta;
947 s.avail_in = deltalen;
948 } else {
eec813cf
PH
949 s.next_in = (void *)dat->buf;
950 s.avail_in = dat->len;
d9ee53ce 951 }
225a6f10 952 s.avail_out = git_deflate_bound(&s, s.avail_in);
d9ee53ce 953 s.next_out = out = xmalloc(s.avail_out);
55bb5c91
JH
954 while (git_deflate(&s, Z_FINISH) == Z_OK)
955 ; /* nothing */
956 git_deflate_end(&s);
d9ee53ce
SP
957
958 /* Determine if we should auto-checkpoint. */
28d055bd 959 if ((max_packsize
960 && (pack_size + PACK_SIZE_THRESHOLD + s.total_out) > max_packsize)
961 || (pack_size + PACK_SIZE_THRESHOLD + s.total_out) < pack_size) {
d9ee53ce
SP
962
963 /* This new object needs to *not* have the current pack_id. */
964 e->pack_id = pack_id + 1;
820b9310 965 cycle_packfile();
d9ee53ce
SP
966
967 /* We cannot carry a delta into the new pack. */
968 if (delta) {
6a83d902 969 FREE_AND_NULL(delta);
5d6f3ef6 970
55bb5c91 971 git_deflate_init(&s, pack_compression_level);
eec813cf
PH
972 s.next_in = (void *)dat->buf;
973 s.avail_in = dat->len;
225a6f10 974 s.avail_out = git_deflate_bound(&s, s.avail_in);
5d6f3ef6 975 s.next_out = out = xrealloc(out, s.avail_out);
55bb5c91
JH
976 while (git_deflate(&s, Z_FINISH) == Z_OK)
977 ; /* nothing */
978 git_deflate_end(&s);
d9ee53ce 979 }
d9ee53ce
SP
980 }
981
982 e->type = type;
983 e->pack_id = pack_id;
3fc366bd 984 e->idx.offset = pack_size;
d9ee53ce
SP
985 object_count++;
986 object_count_by_type[type]++;
db5e523f 987
427cb22c
NP
988 crc32_begin(pack_file);
989
db5e523f 990 if (delta) {
89e0a3a1 991 off_t ofs = e->idx.offset - last->offset;
d489bc14
SP
992 unsigned pos = sizeof(hdr) - 1;
993
4cabf858 994 delta_count_by_type[type]++;
436e7a74 995 e->depth = last->depth + 1;
d489bc14 996
7202a6fa
JK
997 hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr),
998 OBJ_OFS_DELTA, deltalen);
98a3beab 999 hashwrite(pack_file, hdr, hdrlen);
d489bc14
SP
1000 pack_size += hdrlen;
1001
1002 hdr[pos] = ofs & 127;
1003 while (ofs >>= 7)
1004 hdr[--pos] = 128 | (--ofs & 127);
98a3beab 1005 hashwrite(pack_file, hdr + pos, sizeof(hdr) - pos);
d489bc14 1006 pack_size += sizeof(hdr) - pos;
db5e523f 1007 } else {
436e7a74 1008 e->depth = 0;
7202a6fa
JK
1009 hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr),
1010 type, dat->len);
98a3beab 1011 hashwrite(pack_file, hdr, hdrlen);
41e5257f 1012 pack_size += hdrlen;
db5e523f
SP
1013 }
1014
98a3beab 1015 hashwrite(pack_file, out, s.total_out);
41e5257f 1016 pack_size += s.total_out;
db5e523f 1017
427cb22c
NP
1018 e->idx.crc32 = crc32_end(pack_file);
1019
db5e523f 1020 free(out);
e7d06a4b 1021 free(delta);
463acbe1 1022 if (last) {
05576569
PH
1023 if (last->no_swap) {
1024 last->data = *dat;
1025 } else {
c76689df 1026 strbuf_swap(&last->data, dat);
05576569 1027 }
3fc366bd 1028 last->offset = e->idx.offset;
436e7a74 1029 last->depth = e->depth;
463acbe1
SP
1030 }
1031 return 0;
1032}
1033
98a3beab 1034static void truncate_pack(struct hashfile_checkpoint *checkpoint)
5eef828b 1035{
98a3beab 1036 if (hashfile_truncate(pack_file, checkpoint))
5eef828b 1037 die_errno("cannot truncate pack to skip duplicate");
6c526148 1038 pack_size = checkpoint->offset;
5eef828b
SP
1039}
1040
912c13d5 1041static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
5eef828b
SP
1042{
1043 size_t in_sz = 64 * 1024, out_sz = 64 * 1024;
1044 unsigned char *in_buf = xmalloc(in_sz);
1045 unsigned char *out_buf = xmalloc(out_sz);
1046 struct object_entry *e;
912c13d5 1047 struct object_id oid;
5eef828b
SP
1048 unsigned long hdrlen;
1049 off_t offset;
7f89428d 1050 git_hash_ctx c;
ef49a7a0 1051 git_zstream s;
98a3beab 1052 struct hashfile_checkpoint checkpoint;
5eef828b
SP
1053 int status = Z_OK;
1054
1055 /* Determine if we should auto-checkpoint. */
28d055bd 1056 if ((max_packsize
1057 && (pack_size + PACK_SIZE_THRESHOLD + len) > max_packsize)
1058 || (pack_size + PACK_SIZE_THRESHOLD + len) < pack_size)
5eef828b
SP
1059 cycle_packfile();
1060
98a3beab 1061 hashfile_checkpoint(pack_file, &checkpoint);
6c526148 1062 offset = checkpoint.offset;
21281816 1063
98718242 1064 hdrlen = xsnprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1;
5eef828b 1065
7f89428d 1066 the_hash_algo->init_fn(&c);
1067 the_hash_algo->update_fn(&c, out_buf, hdrlen);
5eef828b 1068
427cb22c
NP
1069 crc32_begin(pack_file);
1070
55bb5c91 1071 git_deflate_init(&s, pack_compression_level);
5eef828b 1072
7202a6fa 1073 hdrlen = encode_in_pack_object_header(out_buf, out_sz, OBJ_BLOB, len);
5eef828b
SP
1074
1075 s.next_out = out_buf + hdrlen;
1076 s.avail_out = out_sz - hdrlen;
1077
1078 while (status != Z_STREAM_END) {
1079 if (0 < len && !s.avail_in) {
1080 size_t cnt = in_sz < len ? in_sz : (size_t)len;
1081 size_t n = fread(in_buf, 1, cnt, stdin);
1082 if (!n && feof(stdin))
1083 die("EOF in data (%" PRIuMAX " bytes remaining)", len);
1084
7f89428d 1085 the_hash_algo->update_fn(&c, in_buf, n);
5eef828b
SP
1086 s.next_in = in_buf;
1087 s.avail_in = n;
1088 len -= n;
1089 }
1090
55bb5c91 1091 status = git_deflate(&s, len ? 0 : Z_FINISH);
5eef828b
SP
1092
1093 if (!s.avail_out || status == Z_STREAM_END) {
1094 size_t n = s.next_out - out_buf;
98a3beab 1095 hashwrite(pack_file, out_buf, n);
5eef828b
SP
1096 pack_size += n;
1097 s.next_out = out_buf;
1098 s.avail_out = out_sz;
1099 }
1100
1101 switch (status) {
1102 case Z_OK:
1103 case Z_BUF_ERROR:
1104 case Z_STREAM_END:
1105 continue;
1106 default:
1107 die("unexpected deflate failure: %d", status);
1108 }
1109 }
55bb5c91 1110 git_deflate_end(&s);
7f89428d 1111 the_hash_algo->final_fn(oid.hash, &c);
5eef828b 1112
912c13d5 1113 if (oidout)
1114 oidcpy(oidout, &oid);
5eef828b 1115
912c13d5 1116 e = insert_object(&oid);
5eef828b
SP
1117
1118 if (mark)
1119 insert_mark(mark, e);
1120
3fc366bd 1121 if (e->idx.offset) {
5eef828b 1122 duplicate_count_by_type[OBJ_BLOB]++;
6c526148 1123 truncate_pack(&checkpoint);
5eef828b 1124
a80d72db 1125 } else if (find_sha1_pack(oid.hash,
454ea2e4 1126 get_all_packs(the_repository))) {
5eef828b
SP
1127 e->type = OBJ_BLOB;
1128 e->pack_id = MAX_PACK_ID;
3fc366bd 1129 e->idx.offset = 1; /* just not zero! */
5eef828b 1130 duplicate_count_by_type[OBJ_BLOB]++;
6c526148 1131 truncate_pack(&checkpoint);
5eef828b
SP
1132
1133 } else {
1134 e->depth = 0;
1135 e->type = OBJ_BLOB;
1136 e->pack_id = pack_id;
3fc366bd 1137 e->idx.offset = offset;
427cb22c 1138 e->idx.crc32 = crc32_end(pack_file);
5eef828b
SP
1139 object_count++;
1140 object_count_by_type[OBJ_BLOB]++;
1141 }
1142
1143 free(in_buf);
1144 free(out_buf);
1145}
1146
7422bac4
SP
1147/* All calls must be guarded by find_object() or find_mark() to
1148 * ensure the 'struct object_entry' passed was written by this
1149 * process instance. We unpack the entry by the offset, avoiding
1150 * the need for the corresponding .idx file. This unpacking rule
1151 * works because we only use OBJ_REF_DELTA within the packfiles
1152 * created by fast-import.
1153 *
1154 * oe must not be NULL. Such an oe usually comes from giving
1155 * an unknown SHA-1 to find_object() or an undefined mark to
1156 * find_mark(). Callers must test for this condition and use
1157 * the standard read_sha1_file() when it happens.
1158 *
1159 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1160 * find_mark(), where the mark was reloaded from an existing marks
1161 * file and is referencing an object that this fast-import process
1162 * instance did not write out to a packfile. Callers must test for
1163 * this condition and use read_sha1_file() instead.
1164 */
7bfe6e26
SP
1165static void *gfi_unpack_entry(
1166 struct object_entry *oe,
1167 unsigned long *sizep)
41e5257f 1168{
21666f1a 1169 enum object_type type;
7bfe6e26 1170 struct packed_git *p = all_packs[oe->pack_id];
7f89428d 1171 if (p == pack_data && p->pack_size < (pack_size + the_hash_algo->rawsz)) {
7422bac4
SP
1172 /* The object is stored in the packfile we are writing to
1173 * and we have modified it since the last time we scanned
1174 * back to read a previously written object. If an old
7f89428d 1175 * window covered [p->pack_size, p->pack_size + rawsz) its
7422bac4
SP
1176 * data is stale and is not valid. Closing all windows
1177 * and updating the packfile length ensures we can read
1178 * the newly written data.
1179 */
c9ced051 1180 close_pack_windows(p);
98a3beab 1181 hashflush(pack_file);
7422bac4 1182
7f89428d 1183 /* We have to offer rawsz bytes additional on the end of
7422bac4
SP
1184 * the packfile as the core unpacker code assumes the
1185 * footer is present at the file end and must promise
7f89428d 1186 * at least rawsz bytes within any window it maps. But
7422bac4
SP
1187 * we don't actually create the footer here.
1188 */
7f89428d 1189 p->pack_size = pack_size + the_hash_algo->rawsz;
c9ced051 1190 }
57a6a500 1191 return unpack_entry(the_repository, p, oe->idx.offset, &type, sizep);
41e5257f
SP
1192}
1193
10831c55 1194static const char *get_mode(const char *str, uint16_t *modep)
463acbe1
SP
1195{
1196 unsigned char c;
10831c55 1197 uint16_t mode = 0;
463acbe1
SP
1198
1199 while ((c = *str++) != ' ') {
1200 if (c < '0' || c > '7')
1201 return NULL;
1202 mode = (mode << 3) + (c - '0');
1203 }
1204 *modep = mode;
1205 return str;
1206}
1207
1208static void load_tree(struct tree_entry *root)
1209{
912c13d5 1210 struct object_id *oid = &root->versions[1].oid;
463acbe1
SP
1211 struct object_entry *myoe;
1212 struct tree_content *t;
1213 unsigned long size;
1214 char *buf;
1215 const char *c;
463acbe1
SP
1216
1217 root->tree = t = new_tree_content(8);
912c13d5 1218 if (is_null_oid(oid))
463acbe1
SP
1219 return;
1220
912c13d5 1221 myoe = find_object(oid);
20f546a8 1222 if (myoe && myoe->pack_id != MAX_PACK_ID) {
41e5257f 1223 if (myoe->type != OBJ_TREE)
912c13d5 1224 die("Not a tree: %s", oid_to_hex(oid));
436e7a74 1225 t->delta_depth = myoe->depth;
7bfe6e26 1226 buf = gfi_unpack_entry(myoe, &size);
e8b32e06 1227 if (!buf)
912c13d5 1228 die("Can't load tree %s", oid_to_hex(oid));
463acbe1 1229 } else {
21666f1a 1230 enum object_type type;
b4f5aca4 1231 buf = read_object_file(oid, &type, &size);
21666f1a 1232 if (!buf || type != OBJ_TREE)
912c13d5 1233 die("Can't load tree %s", oid_to_hex(oid));
463acbe1
SP
1234 }
1235
1236 c = buf;
1237 while (c != (buf + size)) {
1238 struct tree_entry *e = new_tree_entry();
1239
1240 if (t->entry_count == t->entry_capacity)
f022f85f 1241 root->tree = t = grow_tree_content(t, t->entry_count);
463acbe1
SP
1242 t->entries[t->entry_count++] = e;
1243
1244 e->tree = NULL;
4cabf858 1245 c = get_mode(c, &e->versions[1].mode);
463acbe1 1246 if (!c)
912c13d5 1247 die("Corrupt mode in %s", oid_to_hex(oid));
4cabf858 1248 e->versions[0].mode = e->versions[1].mode;
061e35c5 1249 e->name = to_atom(c, strlen(c));
463acbe1 1250 c += e->name->str_len + 1;
d7e6b6a8 1251 hashcpy(e->versions[0].oid.hash, (unsigned char *)c);
1252 hashcpy(e->versions[1].oid.hash, (unsigned char *)c);
28d055bd 1253 c += the_hash_algo->rawsz;
463acbe1
SP
1254 }
1255 free(buf);
1256}
1257
4cabf858 1258static int tecmp0 (const void *_a, const void *_b)
463acbe1
SP
1259{
1260 struct tree_entry *a = *((struct tree_entry**)_a);
1261 struct tree_entry *b = *((struct tree_entry**)_b);
1262 return base_name_compare(
4cabf858
SP
1263 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1264 b->name->str_dat, b->name->str_len, b->versions[0].mode);
463acbe1
SP
1265}
1266
4cabf858 1267static int tecmp1 (const void *_a, const void *_b)
463acbe1 1268{
4cabf858
SP
1269 struct tree_entry *a = *((struct tree_entry**)_a);
1270 struct tree_entry *b = *((struct tree_entry**)_b);
1271 return base_name_compare(
1272 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1273 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1274}
1275
eec813cf 1276static void mktree(struct tree_content *t, int v, struct strbuf *b)
4cabf858
SP
1277{
1278 size_t maxlen = 0;
463acbe1 1279 unsigned int i;
463acbe1 1280
4cabf858 1281 if (!v)
9ed0d8d6 1282 QSORT(t->entries, t->entry_count, tecmp0);
4cabf858 1283 else
9ed0d8d6 1284 QSORT(t->entries, t->entry_count, tecmp1);
463acbe1 1285
463acbe1 1286 for (i = 0; i < t->entry_count; i++) {
4cabf858
SP
1287 if (t->entries[i]->versions[v].mode)
1288 maxlen += t->entries[i]->name->str_len + 34;
463acbe1
SP
1289 }
1290
eec813cf
PH
1291 strbuf_reset(b);
1292 strbuf_grow(b, maxlen);
463acbe1
SP
1293 for (i = 0; i < t->entry_count; i++) {
1294 struct tree_entry *e = t->entries[i];
4cabf858
SP
1295 if (!e->versions[v].mode)
1296 continue;
8fb3ad76
DI
1297 strbuf_addf(b, "%o %s%c",
1298 (unsigned int)(e->versions[v].mode & ~NO_DELTA),
1299 e->name->str_dat, '\0');
28d055bd 1300 strbuf_add(b, e->versions[v].oid.hash, the_hash_algo->rawsz);
463acbe1 1301 }
4cabf858
SP
1302}
1303
1304static void store_tree(struct tree_entry *root)
1305{
2668d692 1306 struct tree_content *t;
4cabf858 1307 unsigned int i, j, del;
05576569 1308 struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 };
8fb3ad76 1309 struct object_entry *le = NULL;
4cabf858 1310
d7e6b6a8 1311 if (!is_null_oid(&root->versions[1].oid))
4cabf858
SP
1312 return;
1313
2668d692
MB
1314 if (!root->tree)
1315 load_tree(root);
1316 t = root->tree;
1317
4cabf858
SP
1318 for (i = 0; i < t->entry_count; i++) {
1319 if (t->entries[i]->tree)
1320 store_tree(t->entries[i]);
1321 }
1322
8fb3ad76 1323 if (!(root->versions[0].mode & NO_DELTA))
912c13d5 1324 le = find_object(&root->versions[0].oid);
05576569 1325 if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) {
eec813cf 1326 mktree(t, 0, &old_tree);
05576569 1327 lo.data = old_tree;
3fc366bd 1328 lo.offset = le->idx.offset;
4cabf858 1329 lo.depth = t->delta_depth;
4cabf858 1330 }
4cabf858 1331
eec813cf 1332 mktree(t, 1, &new_tree);
912c13d5 1333 store_object(OBJ_TREE, &new_tree, &lo, &root->versions[1].oid, 0);
4cabf858
SP
1334
1335 t->delta_depth = lo.depth;
4cabf858
SP
1336 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1337 struct tree_entry *e = t->entries[i];
1338 if (e->versions[1].mode) {
1339 e->versions[0].mode = e->versions[1].mode;
d7e6b6a8 1340 oidcpy(&e->versions[0].oid, &e->versions[1].oid);
4cabf858
SP
1341 t->entries[j++] = e;
1342 } else {
1343 release_tree_entry(e);
1344 del++;
1345 }
1346 }
1347 t->entry_count -= del;
463acbe1
SP
1348}
1349
34215783
JN
1350static void tree_content_replace(
1351 struct tree_entry *root,
912c13d5 1352 const struct object_id *oid,
34215783
JN
1353 const uint16_t mode,
1354 struct tree_content *newtree)
1355{
1356 if (!S_ISDIR(mode))
1357 die("Root cannot be a non-directory");
d7e6b6a8 1358 oidclr(&root->versions[0].oid);
912c13d5 1359 oidcpy(&root->versions[1].oid, oid);
34215783
JN
1360 if (root->tree)
1361 release_tree_content_recursive(root->tree);
1362 root->tree = newtree;
1363}
1364
463acbe1
SP
1365static int tree_content_set(
1366 struct tree_entry *root,
1367 const char *p,
912c13d5 1368 const struct object_id *oid,
f39a946a
SP
1369 const uint16_t mode,
1370 struct tree_content *subtree)
463acbe1 1371{
5edde510 1372 struct tree_content *t;
463acbe1
SP
1373 const char *slash1;
1374 unsigned int i, n;
1375 struct tree_entry *e;
1376
2c5495f7
RM
1377 slash1 = strchrnul(p, '/');
1378 n = slash1 - p;
475d1b33
SP
1379 if (!n)
1380 die("Empty path component found in input");
2c5495f7 1381 if (!*slash1 && !S_ISDIR(mode) && subtree)
f39a946a 1382 die("Non-directories cannot have subtrees");
463acbe1 1383
5edde510
JN
1384 if (!root->tree)
1385 load_tree(root);
1386 t = root->tree;
463acbe1
SP
1387 for (i = 0; i < t->entry_count; i++) {
1388 e = t->entries[i];
ba0897e6 1389 if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
2c5495f7 1390 if (!*slash1) {
f39a946a
SP
1391 if (!S_ISDIR(mode)
1392 && e->versions[1].mode == mode
4a7e27e9 1393 && oideq(&e->versions[1].oid, oid))
463acbe1 1394 return 0;
4cabf858 1395 e->versions[1].mode = mode;
912c13d5 1396 oidcpy(&e->versions[1].oid, oid);
f39a946a 1397 if (e->tree)
afde8dd9 1398 release_tree_content_recursive(e->tree);
f39a946a 1399 e->tree = subtree;
8fb3ad76
DI
1400
1401 /*
1402 * We need to leave e->versions[0].sha1 alone
1403 * to avoid modifying the preimage tree used
1404 * when writing out the parent directory.
1405 * But after replacing the subdir with a
1406 * completely different one, it's not a good
1407 * delta base any more, and besides, we've
1408 * thrown away the tree entries needed to
1409 * make a delta against it.
1410 *
1411 * So let's just explicitly disable deltas
1412 * for the subtree.
1413 */
1414 if (S_ISDIR(e->versions[0].mode))
1415 e->versions[0].mode |= NO_DELTA;
1416
d7e6b6a8 1417 oidclr(&root->versions[1].oid);
463acbe1
SP
1418 return 1;
1419 }
4cabf858 1420 if (!S_ISDIR(e->versions[1].mode)) {
463acbe1 1421 e->tree = new_tree_content(8);
4cabf858 1422 e->versions[1].mode = S_IFDIR;
463acbe1
SP
1423 }
1424 if (!e->tree)
1425 load_tree(e);
912c13d5 1426 if (tree_content_set(e, slash1 + 1, oid, mode, subtree)) {
d7e6b6a8 1427 oidclr(&root->versions[1].oid);
463acbe1
SP
1428 return 1;
1429 }
1430 return 0;
1431 }
1432 }
1433
1434 if (t->entry_count == t->entry_capacity)
f022f85f 1435 root->tree = t = grow_tree_content(t, t->entry_count);
463acbe1 1436 e = new_tree_entry();
061e35c5 1437 e->name = to_atom(p, n);
4cabf858 1438 e->versions[0].mode = 0;
d7e6b6a8 1439 oidclr(&e->versions[0].oid);
463acbe1 1440 t->entries[t->entry_count++] = e;
2c5495f7 1441 if (*slash1) {
463acbe1 1442 e->tree = new_tree_content(8);
4cabf858 1443 e->versions[1].mode = S_IFDIR;
912c13d5 1444 tree_content_set(e, slash1 + 1, oid, mode, subtree);
463acbe1 1445 } else {
f39a946a 1446 e->tree = subtree;
4cabf858 1447 e->versions[1].mode = mode;
912c13d5 1448 oidcpy(&e->versions[1].oid, oid);
463acbe1 1449 }
d7e6b6a8 1450 oidclr(&root->versions[1].oid);
463acbe1
SP
1451 return 1;
1452}
1453
f39a946a
SP
1454static int tree_content_remove(
1455 struct tree_entry *root,
1456 const char *p,
62bfa11c
JK
1457 struct tree_entry *backup_leaf,
1458 int allow_root)
463acbe1 1459{
5edde510 1460 struct tree_content *t;
463acbe1
SP
1461 const char *slash1;
1462 unsigned int i, n;
1463 struct tree_entry *e;
1464
2c5495f7
RM
1465 slash1 = strchrnul(p, '/');
1466 n = slash1 - p;
463acbe1 1467
5edde510
JN
1468 if (!root->tree)
1469 load_tree(root);
62bfa11c
JK
1470
1471 if (!*p && allow_root) {
1472 e = root;
1473 goto del_entry;
1474 }
1475
5edde510 1476 t = root->tree;
463acbe1
SP
1477 for (i = 0; i < t->entry_count; i++) {
1478 e = t->entries[i];
ba0897e6 1479 if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
2c5495f7 1480 if (*slash1 && !S_ISDIR(e->versions[1].mode))
253fb5f8
EN
1481 /*
1482 * If p names a file in some subdirectory, and a
1483 * file or symlink matching the name of the
1484 * parent directory of p exists, then p cannot
1485 * exist and need not be deleted.
1486 */
1487 return 1;
2c5495f7 1488 if (!*slash1 || !S_ISDIR(e->versions[1].mode))
463acbe1
SP
1489 goto del_entry;
1490 if (!e->tree)
1491 load_tree(e);
62bfa11c 1492 if (tree_content_remove(e, slash1 + 1, backup_leaf, 0)) {
b54d6422
SP
1493 for (n = 0; n < e->tree->entry_count; n++) {
1494 if (e->tree->entries[n]->versions[1].mode) {
d7e6b6a8 1495 oidclr(&root->versions[1].oid);
b54d6422
SP
1496 return 1;
1497 }
1498 }
f39a946a 1499 backup_leaf = NULL;
b54d6422 1500 goto del_entry;
463acbe1
SP
1501 }
1502 return 0;
1503 }
1504 }
1505 return 0;
1506
1507del_entry:
f39a946a
SP
1508 if (backup_leaf)
1509 memcpy(backup_leaf, e, sizeof(*backup_leaf));
1510 else if (e->tree)
4cabf858 1511 release_tree_content_recursive(e->tree);
f39a946a 1512 e->tree = NULL;
4cabf858 1513 e->versions[1].mode = 0;
d7e6b6a8 1514 oidclr(&e->versions[1].oid);
1515 oidclr(&root->versions[1].oid);
ac47a738 1516 return 1;
db5e523f
SP
1517}
1518
b6f3481b
SP
1519static int tree_content_get(
1520 struct tree_entry *root,
1521 const char *p,
e0eb6b97
JK
1522 struct tree_entry *leaf,
1523 int allow_root)
b6f3481b 1524{
5edde510 1525 struct tree_content *t;
b6f3481b
SP
1526 const char *slash1;
1527 unsigned int i, n;
1528 struct tree_entry *e;
1529
2c5495f7
RM
1530 slash1 = strchrnul(p, '/');
1531 n = slash1 - p;
e0eb6b97 1532 if (!n && !allow_root)
178e1dea 1533 die("Empty path component found in input");
b6f3481b 1534
5edde510
JN
1535 if (!root->tree)
1536 load_tree(root);
e0eb6b97
JK
1537
1538 if (!n) {
1539 e = root;
1540 goto found_entry;
1541 }
1542
5edde510 1543 t = root->tree;
b6f3481b
SP
1544 for (i = 0; i < t->entry_count; i++) {
1545 e = t->entries[i];
ba0897e6 1546 if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
2c5495f7 1547 if (!*slash1)
e0eb6b97 1548 goto found_entry;
b6f3481b
SP
1549 if (!S_ISDIR(e->versions[1].mode))
1550 return 0;
1551 if (!e->tree)
1552 load_tree(e);
e0eb6b97 1553 return tree_content_get(e, slash1 + 1, leaf, 0);
b6f3481b
SP
1554 }
1555 }
1556 return 0;
e0eb6b97
JK
1557
1558found_entry:
1559 memcpy(leaf, e, sizeof(*leaf));
d7e6b6a8 1560 if (e->tree && is_null_oid(&e->versions[1].oid))
e0eb6b97
JK
1561 leaf->tree = dup_tree_content(e->tree);
1562 else
1563 leaf->tree = NULL;
1564 return 1;
b6f3481b
SP
1565}
1566
7073e69e 1567static int update_branch(struct branch *b)
463acbe1
SP
1568{
1569 static const char *msg = "fast-import";
de7e86f5 1570 struct ref_transaction *transaction;
912c13d5 1571 struct object_id old_oid;
de7e86f5 1572 struct strbuf err = STRBUF_INIT;
7073e69e 1573
d7e6b6a8 1574 if (is_null_oid(&b->oid)) {
4ee1b225 1575 if (b->delete)
755b49ae 1576 delete_ref(NULL, b->name, NULL, 0);
4ee1b225
FC
1577 return 0;
1578 }
34c290a6 1579 if (read_ref(b->name, &old_oid))
912c13d5 1580 oidclr(&old_oid);
1581 if (!force_update && !is_null_oid(&old_oid)) {
7073e69e
SP
1582 struct commit *old_cmit, *new_cmit;
1583
21e1ee8f
SB
1584 old_cmit = lookup_commit_reference_gently(the_repository,
1585 &old_oid, 0);
1586 new_cmit = lookup_commit_reference_gently(the_repository,
1587 &b->oid, 0);
de7e86f5 1588 if (!old_cmit || !new_cmit)
7073e69e 1589 return error("Branch %s is missing commits.", b->name);
7073e69e 1590
a20efee9 1591 if (!in_merge_bases(old_cmit, new_cmit)) {
46efd2d9 1592 warning("Not updating %s"
7073e69e 1593 " (new tip %s does not contain %s)",
d7e6b6a8 1594 b->name, oid_to_hex(&b->oid),
912c13d5 1595 oid_to_hex(&old_oid));
7073e69e
SP
1596 return -1;
1597 }
1598 }
de7e86f5
RS
1599 transaction = ref_transaction_begin(&err);
1600 if (!transaction ||
89f3bbdd 1601 ref_transaction_update(transaction, b->name, &b->oid, &old_oid,
1d147bdf 1602 0, msg, &err) ||
db7516ab 1603 ref_transaction_commit(transaction, &err)) {
de7e86f5
RS
1604 ref_transaction_free(transaction);
1605 error("%s", err.buf);
1606 strbuf_release(&err);
1607 return -1;
1608 }
1609 ref_transaction_free(transaction);
1610 strbuf_release(&err);
7073e69e
SP
1611 return 0;
1612}
1613
1614static void dump_branches(void)
1615{
463acbe1
SP
1616 unsigned int i;
1617 struct branch *b;
463acbe1
SP
1618
1619 for (i = 0; i < branch_table_sz; i++) {
7073e69e
SP
1620 for (b = branch_table[i]; b; b = b->table_next_branch)
1621 failure |= update_branch(b);
463acbe1
SP
1622 }
1623}
1624
fd99224e 1625static void dump_tags(void)
72303d44
SP
1626{
1627 static const char *msg = "fast-import";
1628 struct tag *t;
3f09ba75
RS
1629 struct strbuf ref_name = STRBUF_INIT;
1630 struct strbuf err = STRBUF_INIT;
1631 struct ref_transaction *transaction;
72303d44 1632
3f09ba75
RS
1633 transaction = ref_transaction_begin(&err);
1634 if (!transaction) {
1635 failure |= error("%s", err.buf);
1636 goto cleanup;
1637 }
72303d44 1638 for (t = first_tag; t; t = t->next_tag) {
3f09ba75
RS
1639 strbuf_reset(&ref_name);
1640 strbuf_addf(&ref_name, "refs/tags/%s", t->name);
1641
1d147bdf 1642 if (ref_transaction_update(transaction, ref_name.buf,
89f3bbdd 1643 &t->oid, NULL, 0, msg, &err)) {
3f09ba75
RS
1644 failure |= error("%s", err.buf);
1645 goto cleanup;
1646 }
72303d44 1647 }
db7516ab 1648 if (ref_transaction_commit(transaction, &err))
3f09ba75
RS
1649 failure |= error("%s", err.buf);
1650
1651 cleanup:
1652 ref_transaction_free(transaction);
1653 strbuf_release(&ref_name);
1654 strbuf_release(&err);
72303d44
SP
1655}
1656
a6a1a831 1657static void dump_marks_helper(FILE *f,
0ea9f045 1658 uintmax_t base,
a6a1a831
SP
1659 struct mark_set *m)
1660{
0ea9f045 1661 uintmax_t k;
a6a1a831
SP
1662 if (m->shift) {
1663 for (k = 0; k < 1024; k++) {
1664 if (m->data.sets[k])
7e7db5e4 1665 dump_marks_helper(f, base + (k << m->shift),
a6a1a831
SP
1666 m->data.sets[k]);
1667 }
1668 } else {
1669 for (k = 0; k < 1024; k++) {
1670 if (m->data.marked[k])
3efb1f34 1671 fprintf(f, ":%" PRIuMAX " %s\n", base + k,
e6a492b7 1672 oid_to_hex(&m->data.marked[k]->idx.oid));
a6a1a831
SP
1673 }
1674 }
1675}
1676
fd99224e 1677static void dump_marks(void)
a6a1a831 1678{
b2275868 1679 struct lock_file mark_lock = LOCK_INIT;
60b9004c
SP
1680 FILE *f;
1681
f4beed60 1682 if (!export_marks_file || (import_marks_file && !import_marks_file_done))
60b9004c
SP
1683 return;
1684
f70f0565 1685 if (hold_lock_file_for_update(&mark_lock, export_marks_file, 0) < 0) {
6c223e49
NTND
1686 failure |= error_errno("Unable to write marks file %s",
1687 export_marks_file);
60b9004c 1688 return;
a6a1a831 1689 }
60b9004c 1690
f70f0565 1691 f = fdopen_lock_file(&mark_lock, "w");
60b9004c 1692 if (!f) {
5a7b1b57 1693 int saved_errno = errno;
60b9004c
SP
1694 rollback_lock_file(&mark_lock);
1695 failure |= error("Unable to write marks file %s: %s",
07cd9328 1696 export_marks_file, strerror(saved_errno));
60b9004c 1697 return;
a6a1a831 1698 }
60b9004c 1699
fb54abd6 1700 dump_marks_helper(f, 0, marks);
fb54abd6 1701 if (commit_lock_file(&mark_lock)) {
6c223e49
NTND
1702 failure |= error_errno("Unable to write file %s",
1703 export_marks_file);
fb54abd6
BC
1704 return;
1705 }
a6a1a831
SP
1706}
1707
07cd9328
SR
1708static void read_marks(void)
1709{
1710 char line[512];
1711 FILE *f = fopen(import_marks_file, "r");
dded4f12
RR
1712 if (f)
1713 ;
1714 else if (import_marks_file_ignore_missing && errno == ENOENT)
f4beed60 1715 goto done; /* Marks file does not exist */
dded4f12 1716 else
07cd9328
SR
1717 die_errno("cannot read '%s'", import_marks_file);
1718 while (fgets(line, sizeof(line), f)) {
1719 uintmax_t mark;
1720 char *end;
912c13d5 1721 struct object_id oid;
07cd9328
SR
1722 struct object_entry *e;
1723
1724 end = strchr(line, '\n');
1725 if (line[0] != ':' || !end)
1726 die("corrupt mark line: %s", line);
1727 *end = 0;
1728 mark = strtoumax(line + 1, &end, 10);
1729 if (!mark || end == line + 1
912c13d5 1730 || *end != ' ' || get_oid_hex(end + 1, &oid))
07cd9328 1731 die("corrupt mark line: %s", line);
912c13d5 1732 e = find_object(&oid);
07cd9328 1733 if (!e) {
0df8e965
SB
1734 enum object_type type = oid_object_info(the_repository,
1735 &oid, NULL);
07cd9328 1736 if (type < 0)
912c13d5 1737 die("object not found: %s", oid_to_hex(&oid));
1738 e = insert_object(&oid);
07cd9328
SR
1739 e->type = type;
1740 e->pack_id = MAX_PACK_ID;
3fc366bd 1741 e->idx.offset = 1; /* just not zero! */
07cd9328
SR
1742 }
1743 insert_mark(mark, e);
1744 }
1745 fclose(f);
f4beed60
FC
1746done:
1747 import_marks_file_done = 1;
07cd9328
SR
1748}
1749
1750
e6c019d0 1751static int read_next_command(void)
c44cdc7e 1752{
e6c019d0
PH
1753 static int stdin_eof = 0;
1754
1755 if (stdin_eof) {
1756 unread_command_buf = 0;
1757 return EOF;
1758 }
1759
777f80d7 1760 for (;;) {
904b1941 1761 if (unread_command_buf) {
1fdb649c 1762 unread_command_buf = 0;
904b1941
SP
1763 } else {
1764 struct recent_command *rc;
1765
b315c5c0 1766 strbuf_detach(&command_buf, NULL);
8f309aeb 1767 stdin_eof = strbuf_getline_lf(&command_buf, stdin);
e6c019d0
PH
1768 if (stdin_eof)
1769 return EOF;
904b1941 1770
f963bd5d 1771 if (!seen_data_command
59556548
CC
1772 && !starts_with(command_buf.buf, "feature ")
1773 && !starts_with(command_buf.buf, "option ")) {
9c8398f0 1774 parse_argv();
f963bd5d
SR
1775 }
1776
904b1941
SP
1777 rc = rc_free;
1778 if (rc)
1779 rc_free = rc->next;
1780 else {
1781 rc = cmd_hist.next;
1782 cmd_hist.next = rc->next;
1783 cmd_hist.next->prev = &cmd_hist;
1784 free(rc->buf);
1785 }
1786
1787 rc->buf = command_buf.buf;
1788 rc->prev = cmd_tail;
1789 rc->next = cmd_hist.prev;
1790 rc->prev->next = rc;
1791 cmd_tail = rc;
1792 }
777f80d7
JN
1793 if (command_buf.buf[0] == '#')
1794 continue;
1795 return 0;
1796 }
c44cdc7e
SP
1797}
1798
7e5dcea8 1799static void skip_optional_lf(void)
2c570cde
SP
1800{
1801 int term_char = fgetc(stdin);
1802 if (term_char != '\n' && term_char != EOF)
1803 ungetc(term_char, stdin);
1804}
1805
b3031781 1806static void parse_mark(void)
c44cdc7e 1807{
ae021d87
JK
1808 const char *v;
1809 if (skip_prefix(command_buf.buf, "mark :", &v)) {
1810 next_mark = strtoumax(v, NULL, 10);
c44cdc7e
SP
1811 read_next_command();
1812 }
1813 else
d8397168 1814 next_mark = 0;
c44cdc7e
SP
1815}
1816
a965bb31
EN
1817static void parse_original_identifier(void)
1818{
1819 const char *v;
1820 if (skip_prefix(command_buf.buf, "original-oid ", &v))
1821 read_next_command();
1822}
1823
5eef828b 1824static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
c44cdc7e 1825{
ae021d87 1826 const char *data;
eec813cf 1827 strbuf_reset(sb);
c44cdc7e 1828
ae021d87 1829 if (!skip_prefix(command_buf.buf, "data ", &data))
c44cdc7e
SP
1830 die("Expected 'data n' command, found: %s", command_buf.buf);
1831
ae021d87
JK
1832 if (skip_prefix(data, "<<", &data)) {
1833 char *term = xstrdup(data);
1834 size_t term_len = command_buf.len - (data - command_buf.buf);
4a241d79 1835
c2e6b6d0 1836 strbuf_detach(&command_buf, NULL);
3b4dce02 1837 for (;;) {
8f309aeb 1838 if (strbuf_getline_lf(&command_buf, stdin) == EOF)
3b4dce02
SP
1839 die("EOF in data (terminator '%s' not found)", term);
1840 if (term_len == command_buf.len
1841 && !strcmp(term, command_buf.buf))
1842 break;
eec813cf
PH
1843 strbuf_addbuf(sb, &command_buf);
1844 strbuf_addch(sb, '\n');
3b4dce02
SP
1845 }
1846 free(term);
1847 }
1848 else {
ae021d87 1849 uintmax_t len = strtoumax(data, NULL, 10);
5eef828b 1850 size_t n = 0, length = (size_t)len;
4a241d79 1851
5eef828b
SP
1852 if (limit && limit < len) {
1853 *len_res = len;
1854 return 0;
1855 }
1856 if (length < len)
1857 die("data is too large to use in this context");
4a241d79 1858
3b4dce02 1859 while (n < length) {
eec813cf 1860 size_t s = strbuf_fread(sb, length - n, stdin);
3b4dce02 1861 if (!s && feof(stdin))
40db58b8
JS
1862 die("EOF in data (%lu bytes remaining)",
1863 (unsigned long)(length - n));
3b4dce02
SP
1864 n += s;
1865 }
c44cdc7e
SP
1866 }
1867
2c570cde 1868 skip_optional_lf();
5eef828b 1869 return 1;
c44cdc7e
SP
1870}
1871
c33ddc2e 1872static int validate_raw_date(const char *src, struct strbuf *result)
63e0c8b3
SP
1873{
1874 const char *orig_src = src;
eb3a9dd3 1875 char *endp;
1cd749cc 1876 unsigned long num;
63e0c8b3 1877
c55fae43
RS
1878 errno = 0;
1879
1cd749cc
JH
1880 num = strtoul(src, &endp, 10);
1881 /* NEEDSWORK: perhaps check for reasonable values? */
c55fae43 1882 if (errno || endp == src || *endp != ' ')
63e0c8b3
SP
1883 return -1;
1884
1885 src = endp + 1;
1886 if (*src != '-' && *src != '+')
1887 return -1;
63e0c8b3 1888
1cd749cc 1889 num = strtoul(src + 1, &endp, 10);
c33ddc2e 1890 if (errno || endp == src + 1 || *endp || 1400 < num)
63e0c8b3
SP
1891 return -1;
1892
c33ddc2e 1893 strbuf_addstr(result, orig_src);
63e0c8b3
SP
1894 return 0;
1895}
1896
1897static char *parse_ident(const char *buf)
1898{
4b4963c0 1899 const char *ltgt;
63e0c8b3 1900 size_t name_len;
c33ddc2e 1901 struct strbuf ident = STRBUF_INIT;
63e0c8b3 1902
17fb0072
DI
1903 /* ensure there is a space delimiter even if there is no name */
1904 if (*buf == '<')
1905 --buf;
1906
4b4963c0
DI
1907 ltgt = buf + strcspn(buf, "<>");
1908 if (*ltgt != '<')
1909 die("Missing < in ident string: %s", buf);
1910 if (ltgt != buf && ltgt[-1] != ' ')
1911 die("Missing space before < in ident string: %s", buf);
1912 ltgt = ltgt + 1 + strcspn(ltgt + 1, "<>");
1913 if (*ltgt != '>')
63e0c8b3 1914 die("Missing > in ident string: %s", buf);
4b4963c0
DI
1915 ltgt++;
1916 if (*ltgt != ' ')
63e0c8b3 1917 die("Missing space after > in ident string: %s", buf);
4b4963c0
DI
1918 ltgt++;
1919 name_len = ltgt - buf;
c33ddc2e 1920 strbuf_add(&ident, buf, name_len);
63e0c8b3
SP
1921
1922 switch (whenspec) {
1923 case WHENSPEC_RAW:
c33ddc2e 1924 if (validate_raw_date(ltgt, &ident) < 0)
4b4963c0 1925 die("Invalid raw date \"%s\" in ident: %s", ltgt, buf);
63e0c8b3
SP
1926 break;
1927 case WHENSPEC_RFC2822:
c33ddc2e 1928 if (parse_date(ltgt, &ident) < 0)
4b4963c0 1929 die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt, buf);
63e0c8b3
SP
1930 break;
1931 case WHENSPEC_NOW:
4b4963c0 1932 if (strcmp("now", ltgt))
63e0c8b3 1933 die("Date in ident must be 'now': %s", buf);
c33ddc2e 1934 datestamp(&ident);
63e0c8b3
SP
1935 break;
1936 }
1937
c33ddc2e 1938 return strbuf_detach(&ident, NULL);
63e0c8b3
SP
1939}
1940
5eef828b
SP
1941static void parse_and_store_blob(
1942 struct last_object *last,
912c13d5 1943 struct object_id *oidout,
5eef828b 1944 uintmax_t mark)
6143f064 1945{
05576569 1946 static struct strbuf buf = STRBUF_INIT;
5eef828b 1947 uintmax_t len;
c44cdc7e 1948
5eef828b 1949 if (parse_data(&buf, big_file_threshold, &len))
912c13d5 1950 store_object(OBJ_BLOB, &buf, last, oidout, mark);
5eef828b
SP
1951 else {
1952 if (last) {
1953 strbuf_release(&last->data);
1954 last->offset = 0;
1955 last->depth = 0;
1956 }
912c13d5 1957 stream_blob(len, oidout, mark);
5eef828b
SP
1958 skip_optional_lf();
1959 }
1960}
1961
1962static void parse_new_blob(void)
1963{
c44cdc7e 1964 read_next_command();
b3031781 1965 parse_mark();
a965bb31 1966 parse_original_identifier();
5eef828b 1967 parse_and_store_blob(&last_blob, NULL, next_mark);
6143f064
SP
1968}
1969
fd99224e 1970static void unload_one_branch(void)
6bb5b329 1971{
41e5257f
SP
1972 while (cur_active_branches
1973 && cur_active_branches >= max_active_branches) {
6777a59f 1974 uintmax_t min_commit = ULONG_MAX;
463acbe1
SP
1975 struct branch *e, *l = NULL, *p = NULL;
1976
1977 for (e = active_branches; e; e = e->active_next_branch) {
1978 if (e->last_commit < min_commit) {
1979 p = l;
1980 min_commit = e->last_commit;
1981 }
1982 l = e;
1983 }
1984
1985 if (p) {
1986 e = p->active_next_branch;
1987 p->active_next_branch = e->active_next_branch;
1988 } else {
1989 e = active_branches;
1990 active_branches = e->active_next_branch;
1991 }
734c91f9 1992 e->active = 0;
463acbe1
SP
1993 e->active_next_branch = NULL;
1994 if (e->branch_tree.tree) {
afde8dd9 1995 release_tree_content_recursive(e->branch_tree.tree);
463acbe1
SP
1996 e->branch_tree.tree = NULL;
1997 }
1998 cur_active_branches--;
6bb5b329 1999 }
6bb5b329
SP
2000}
2001
463acbe1 2002static void load_branch(struct branch *b)
6bb5b329 2003{
463acbe1 2004 load_tree(&b->branch_tree);
734c91f9
SP
2005 if (!b->active) {
2006 b->active = 1;
2007 b->active_next_branch = active_branches;
2008 active_branches = b;
2009 cur_active_branches++;
2010 branch_load_count++;
2011 }
6bb5b329
SP
2012}
2013
2a113aee
JH
2014static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes)
2015{
2016 unsigned char fanout = 0;
2017 while ((num_notes >>= 8))
2018 fanout++;
2019 return fanout;
2020}
2021
2022static void construct_path_with_fanout(const char *hex_sha1,
2023 unsigned char fanout, char *path)
2024{
2025 unsigned int i = 0, j = 0;
7f89428d 2026 if (fanout >= the_hash_algo->rawsz)
2a113aee
JH
2027 die("Too large fanout (%u)", fanout);
2028 while (fanout) {
2029 path[i++] = hex_sha1[j++];
2030 path[i++] = hex_sha1[j++];
2031 path[i++] = '/';
2032 fanout--;
2033 }
7f89428d 2034 memcpy(path + i, hex_sha1 + j, the_hash_algo->hexsz - j);
2035 path[i + the_hash_algo->hexsz - j] = '\0';
2a113aee
JH
2036}
2037
2038static uintmax_t do_change_note_fanout(
2039 struct tree_entry *orig_root, struct tree_entry *root,
912c13d5 2040 char *hex_oid, unsigned int hex_oid_len,
2a113aee
JH
2041 char *fullpath, unsigned int fullpath_len,
2042 unsigned char fanout)
2043{
405d7f4a 2044 struct tree_content *t;
2a113aee 2045 struct tree_entry *e, leaf;
912c13d5 2046 unsigned int i, tmp_hex_oid_len, tmp_fullpath_len;
2a113aee 2047 uintmax_t num_notes = 0;
912c13d5 2048 struct object_id oid;
28d055bd 2049 /* hex oid + '/' between each pair of hex digits + NUL */
2050 char realpath[GIT_MAX_HEXSZ + ((GIT_MAX_HEXSZ / 2) - 1) + 1];
2051 const unsigned hexsz = the_hash_algo->hexsz;
2a113aee 2052
405d7f4a
MH
2053 if (!root->tree)
2054 load_tree(root);
2055 t = root->tree;
2056
2a113aee
JH
2057 for (i = 0; t && i < t->entry_count; i++) {
2058 e = t->entries[i];
912c13d5 2059 tmp_hex_oid_len = hex_oid_len + e->name->str_len;
2a113aee
JH
2060 tmp_fullpath_len = fullpath_len;
2061
2062 /*
2063 * We're interested in EITHER existing note entries (entries
2064 * with exactly 40 hex chars in path, not including directory
2065 * separators), OR directory entries that may contain note
2066 * entries (with < 40 hex chars in path).
2067 * Also, each path component in a note entry must be a multiple
2068 * of 2 chars.
2069 */
2070 if (!e->versions[1].mode ||
28d055bd 2071 tmp_hex_oid_len > hexsz ||
2a113aee
JH
2072 e->name->str_len % 2)
2073 continue;
2074
2075 /* This _may_ be a note entry, or a subdir containing notes */
912c13d5 2076 memcpy(hex_oid + hex_oid_len, e->name->str_dat,
2a113aee
JH
2077 e->name->str_len);
2078 if (tmp_fullpath_len)
2079 fullpath[tmp_fullpath_len++] = '/';
2080 memcpy(fullpath + tmp_fullpath_len, e->name->str_dat,
2081 e->name->str_len);
2082 tmp_fullpath_len += e->name->str_len;
2083 fullpath[tmp_fullpath_len] = '\0';
2084
28d055bd 2085 if (tmp_hex_oid_len == hexsz && !get_oid_hex(hex_oid, &oid)) {
2a113aee 2086 /* This is a note entry */
18386857
JH
2087 if (fanout == 0xff) {
2088 /* Counting mode, no rename */
2089 num_notes++;
2090 continue;
2091 }
912c13d5 2092 construct_path_with_fanout(hex_oid, fanout, realpath);
2a113aee
JH
2093 if (!strcmp(fullpath, realpath)) {
2094 /* Note entry is in correct location */
2095 num_notes++;
2096 continue;
2097 }
2098
2099 /* Rename fullpath to realpath */
62bfa11c 2100 if (!tree_content_remove(orig_root, fullpath, &leaf, 0))
2a113aee
JH
2101 die("Failed to remove path %s", fullpath);
2102 tree_content_set(orig_root, realpath,
912c13d5 2103 &leaf.versions[1].oid,
2a113aee
JH
2104 leaf.versions[1].mode,
2105 leaf.tree);
2106 } else if (S_ISDIR(e->versions[1].mode)) {
2107 /* This is a subdir that may contain note entries */
2a113aee 2108 num_notes += do_change_note_fanout(orig_root, e,
912c13d5 2109 hex_oid, tmp_hex_oid_len,
2a113aee
JH
2110 fullpath, tmp_fullpath_len, fanout);
2111 }
2112
2113 /* The above may have reallocated the current tree_content */
2114 t = root->tree;
2115 }
2116 return num_notes;
2117}
2118
2119static uintmax_t change_note_fanout(struct tree_entry *root,
2120 unsigned char fanout)
2121{
912c13d5 2122 /*
2123 * The size of path is due to one slash between every two hex digits,
2124 * plus the terminating NUL. Note that there is no slash at the end, so
2125 * the number of slashes is one less than half the number of hex
2126 * characters.
2127 */
2128 char hex_oid[GIT_MAX_HEXSZ], path[GIT_MAX_HEXSZ + (GIT_MAX_HEXSZ / 2) - 1 + 1];
2129 return do_change_note_fanout(root, root, hex_oid, 0, path, 0, fanout);
2a113aee
JH
2130}
2131
06454cb9
PW
2132/*
2133 * Given a pointer into a string, parse a mark reference:
2134 *
2135 * idnum ::= ':' bigint;
2136 *
2137 * Return the first character after the value in *endptr.
2138 *
2139 * Complain if the following character is not what is expected,
2140 * either a space or end of the string.
2141 */
2142static uintmax_t parse_mark_ref(const char *p, char **endptr)
2143{
2144 uintmax_t mark;
2145
2146 assert(*p == ':');
2147 p++;
2148 mark = strtoumax(p, endptr, 10);
2149 if (*endptr == p)
2150 die("No value after ':' in mark: %s", command_buf.buf);
2151 return mark;
2152}
2153
2154/*
2155 * Parse the mark reference, and complain if this is not the end of
2156 * the string.
2157 */
2158static uintmax_t parse_mark_ref_eol(const char *p)
2159{
2160 char *end;
2161 uintmax_t mark;
2162
2163 mark = parse_mark_ref(p, &end);
2164 if (*end != '\0')
2165 die("Garbage after mark: %s", command_buf.buf);
2166 return mark;
2167}
2168
2169/*
2170 * Parse the mark reference, demanding a trailing space. Return a
2171 * pointer to the space.
2172 */
2173static uintmax_t parse_mark_ref_space(const char **p)
2174{
2175 uintmax_t mark;
2176 char *end;
2177
2178 mark = parse_mark_ref(*p, &end);
e814c39c 2179 if (*end++ != ' ')
06454cb9
PW
2180 die("Missing space after mark: %s", command_buf.buf);
2181 *p = end;
2182 return mark;
2183}
2184
97313bef 2185static void file_change_m(const char *p, struct branch *b)
6bb5b329 2186{
7fb1011e 2187 static struct strbuf uq = STRBUF_INIT;
c44cdc7e 2188 const char *endp;
3aa99df8 2189 struct object_entry *oe;
912c13d5 2190 struct object_id oid;
10831c55 2191 uint16_t mode, inline_data = 0;
6bb5b329 2192
c44cdc7e
SP
2193 p = get_mode(p, &mode);
2194 if (!p)
2195 die("Corrupt mode: %s", command_buf.buf);
2196 switch (mode) {
3d1d81eb
FC
2197 case 0644:
2198 case 0755:
2199 mode |= S_IFREG;
c44cdc7e
SP
2200 case S_IFREG | 0644:
2201 case S_IFREG | 0755:
ace4a9d1 2202 case S_IFLNK:
334fba65 2203 case S_IFDIR:
03db4525 2204 case S_IFGITLINK:
c44cdc7e
SP
2205 /* ok */
2206 break;
2207 default:
2208 die("Corrupt mode: %s", command_buf.buf);
2209 }
2210
d8397168 2211 if (*p == ':') {
06454cb9 2212 oe = find_mark(parse_mark_ref_space(&p));
e6a492b7 2213 oidcpy(&oid, &oe->idx.oid);
e814c39c 2214 } else if (skip_prefix(p, "inline ", &p)) {
b715cfbb 2215 inline_data = 1;
3aa99df8 2216 oe = NULL; /* not used with inline_data, but makes gcc happy */
d8397168 2217 } else {
912c13d5 2218 if (parse_oid_hex(p, &oid, &p))
06454cb9 2219 die("Invalid dataref: %s", command_buf.buf);
912c13d5 2220 oe = find_object(&oid);
e814c39c 2221 if (*p++ != ' ')
06454cb9 2222 die("Missing space after SHA1: %s", command_buf.buf);
d8397168 2223 }
c44cdc7e 2224
7fb1011e
PH
2225 strbuf_reset(&uq);
2226 if (!unquote_c_style(&uq, p, &endp)) {
c44cdc7e
SP
2227 if (*endp)
2228 die("Garbage after path in: %s", command_buf.buf);
7fb1011e 2229 p = uq.buf;
c44cdc7e 2230 }
6bb5b329 2231
8fe533f6 2232 /* Git does not track empty, non-toplevel directories. */
912c13d5 2233 if (S_ISDIR(mode) && is_empty_tree_oid(&oid) && *p) {
62bfa11c 2234 tree_content_remove(&b->branch_tree, p, NULL, 0);
8fe533f6
JN
2235 return;
2236 }
2237
03db4525
AG
2238 if (S_ISGITLINK(mode)) {
2239 if (inline_data)
2240 die("Git links cannot be specified 'inline': %s",
2241 command_buf.buf);
2242 else if (oe) {
2243 if (oe->type != OBJ_COMMIT)
2244 die("Not a commit (actually a %s): %s",
debca9d2 2245 type_name(oe->type), command_buf.buf);
03db4525
AG
2246 }
2247 /*
2248 * Accept the sha1 without checking; it expected to be in
2249 * another repository.
2250 */
2251 } else if (inline_data) {
334fba65
JN
2252 if (S_ISDIR(mode))
2253 die("Directories cannot be specified 'inline': %s",
2254 command_buf.buf);
7fb1011e
PH
2255 if (p != uq.buf) {
2256 strbuf_addstr(&uq, p);
2257 p = uq.buf;
2258 }
7ffde293
EN
2259 while (read_next_command() != EOF) {
2260 const char *v;
2261 if (skip_prefix(command_buf.buf, "cat-blob ", &v))
2262 parse_cat_blob(v);
2263 else {
2264 parse_and_store_blob(&last_blob, &oid, 0);
2265 break;
2266 }
2267 }
7111feed 2268 } else {
334fba65
JN
2269 enum object_type expected = S_ISDIR(mode) ?
2270 OBJ_TREE: OBJ_BLOB;
2271 enum object_type type = oe ? oe->type :
0df8e965
SB
2272 oid_object_info(the_repository, &oid,
2273 NULL);
21666f1a 2274 if (type < 0)
334fba65
JN
2275 die("%s not found: %s",
2276 S_ISDIR(mode) ? "Tree" : "Blob",
2277 command_buf.buf);
2278 if (type != expected)
2279 die("Not a %s (actually a %s): %s",
debca9d2 2280 type_name(expected), type_name(type),
334fba65 2281 command_buf.buf);
7111feed 2282 }
6bb5b329 2283
34215783 2284 if (!*p) {
912c13d5 2285 tree_content_replace(&b->branch_tree, &oid, mode, NULL);
34215783
JN
2286 return;
2287 }
912c13d5 2288 tree_content_set(&b->branch_tree, p, &oid, mode, NULL);
463acbe1 2289}
6bb5b329 2290
97313bef 2291static void file_change_d(const char *p, struct branch *b)
463acbe1 2292{
7fb1011e 2293 static struct strbuf uq = STRBUF_INIT;
c44cdc7e
SP
2294 const char *endp;
2295
7fb1011e
PH
2296 strbuf_reset(&uq);
2297 if (!unquote_c_style(&uq, p, &endp)) {
c44cdc7e
SP
2298 if (*endp)
2299 die("Garbage after path in: %s", command_buf.buf);
7fb1011e 2300 p = uq.buf;
c44cdc7e 2301 }
62bfa11c 2302 tree_content_remove(&b->branch_tree, p, NULL, 1);
6bb5b329
SP
2303}
2304
97313bef 2305static void file_change_cr(const char *s, struct branch *b, int rename)
f39a946a 2306{
97313bef 2307 const char *d;
7fb1011e
PH
2308 static struct strbuf s_uq = STRBUF_INIT;
2309 static struct strbuf d_uq = STRBUF_INIT;
f39a946a
SP
2310 const char *endp;
2311 struct tree_entry leaf;
2312
7fb1011e
PH
2313 strbuf_reset(&s_uq);
2314 if (!unquote_c_style(&s_uq, s, &endp)) {
f39a946a
SP
2315 if (*endp != ' ')
2316 die("Missing space after source: %s", command_buf.buf);
7fb1011e 2317 } else {
f39a946a
SP
2318 endp = strchr(s, ' ');
2319 if (!endp)
2320 die("Missing space after source: %s", command_buf.buf);
7fb1011e 2321 strbuf_add(&s_uq, s, endp - s);
f39a946a 2322 }
7fb1011e 2323 s = s_uq.buf;
f39a946a
SP
2324
2325 endp++;
2326 if (!*endp)
2327 die("Missing dest: %s", command_buf.buf);
2328
2329 d = endp;
7fb1011e
PH
2330 strbuf_reset(&d_uq);
2331 if (!unquote_c_style(&d_uq, d, &endp)) {
f39a946a
SP
2332 if (*endp)
2333 die("Garbage after dest in: %s", command_buf.buf);
7fb1011e 2334 d = d_uq.buf;
f39a946a
SP
2335 }
2336
2337 memset(&leaf, 0, sizeof(leaf));
b6f3481b 2338 if (rename)
62bfa11c 2339 tree_content_remove(&b->branch_tree, s, &leaf, 1);
b6f3481b 2340 else
e0eb6b97 2341 tree_content_get(&b->branch_tree, s, &leaf, 1);
f39a946a
SP
2342 if (!leaf.versions[1].mode)
2343 die("Path %s not in branch", s);
34215783
JN
2344 if (!*d) { /* C "path/to/subdir" "" */
2345 tree_content_replace(&b->branch_tree,
912c13d5 2346 &leaf.versions[1].oid,
34215783
JN
2347 leaf.versions[1].mode,
2348 leaf.tree);
2349 return;
2350 }
f39a946a 2351 tree_content_set(&b->branch_tree, d,
912c13d5 2352 &leaf.versions[1].oid,
f39a946a
SP
2353 leaf.versions[1].mode,
2354 leaf.tree);
f39a946a
SP
2355}
2356
97313bef 2357static void note_change_n(const char *p, struct branch *b, unsigned char *old_fanout)
a8dd2e7d 2358{
a8dd2e7d 2359 static struct strbuf uq = STRBUF_INIT;
cbfd5e1c 2360 struct object_entry *oe;
a8dd2e7d 2361 struct branch *s;
912c13d5 2362 struct object_id oid, commit_oid;
28d055bd 2363 char path[GIT_MAX_RAWSZ * 3];
a8dd2e7d 2364 uint16_t inline_data = 0;
2a113aee 2365 unsigned char new_fanout;
a8dd2e7d 2366
18386857
JH
2367 /*
2368 * When loading a branch, we don't traverse its tree to count the real
2369 * number of notes (too expensive to do this for all non-note refs).
2370 * This means that recently loaded notes refs might incorrectly have
2371 * b->num_notes == 0, and consequently, old_fanout might be wrong.
2372 *
2373 * Fix this by traversing the tree and counting the number of notes
2374 * when b->num_notes == 0. If the notes tree is truly empty, the
2375 * calculation should not take long.
2376 */
2377 if (b->num_notes == 0 && *old_fanout == 0) {
2378 /* Invoke change_note_fanout() in "counting mode". */
2379 b->num_notes = change_note_fanout(&b->branch_tree, 0xff);
2380 *old_fanout = convert_num_notes_to_fanout(b->num_notes);
2381 }
2382
2383 /* Now parse the notemodify command. */
a8dd2e7d
JH
2384 /* <dataref> or 'inline' */
2385 if (*p == ':') {
06454cb9 2386 oe = find_mark(parse_mark_ref_space(&p));
e6a492b7 2387 oidcpy(&oid, &oe->idx.oid);
e814c39c 2388 } else if (skip_prefix(p, "inline ", &p)) {
a8dd2e7d 2389 inline_data = 1;
0a34594c 2390 oe = NULL; /* not used with inline_data, but makes gcc happy */
a8dd2e7d 2391 } else {
912c13d5 2392 if (parse_oid_hex(p, &oid, &p))
06454cb9 2393 die("Invalid dataref: %s", command_buf.buf);
912c13d5 2394 oe = find_object(&oid);
e814c39c 2395 if (*p++ != ' ')
06454cb9 2396 die("Missing space after SHA1: %s", command_buf.buf);
a8dd2e7d 2397 }
a8dd2e7d 2398
a8a5406a 2399 /* <commit-ish> */
a8dd2e7d
JH
2400 s = lookup_branch(p);
2401 if (s) {
d7e6b6a8 2402 if (is_null_oid(&s->oid))
0bc69881 2403 die("Can't add a note on empty branch.");
912c13d5 2404 oidcpy(&commit_oid, &s->oid);
a8dd2e7d 2405 } else if (*p == ':') {
06454cb9 2406 uintmax_t commit_mark = parse_mark_ref_eol(p);
a8dd2e7d
JH
2407 struct object_entry *commit_oe = find_mark(commit_mark);
2408 if (commit_oe->type != OBJ_COMMIT)
2409 die("Mark :%" PRIuMAX " not a commit", commit_mark);
e6a492b7 2410 oidcpy(&commit_oid, &commit_oe->idx.oid);
912c13d5 2411 } else if (!get_oid(p, &commit_oid)) {
a8dd2e7d 2412 unsigned long size;
d3b4705a
NTND
2413 char *buf = read_object_with_reference(the_repository,
2414 &commit_oid,
02f0547e 2415 commit_type, &size,
2416 &commit_oid);
28d055bd 2417 if (!buf || size < the_hash_algo->hexsz + 6)
a8dd2e7d
JH
2418 die("Not a valid commit: %s", p);
2419 free(buf);
2420 } else
2421 die("Invalid ref name or SHA1 expression: %s", p);
2422
2423 if (inline_data) {
a8dd2e7d
JH
2424 if (p != uq.buf) {
2425 strbuf_addstr(&uq, p);
2426 p = uq.buf;
2427 }
2428 read_next_command();
912c13d5 2429 parse_and_store_blob(&last_blob, &oid, 0);
a8dd2e7d
JH
2430 } else if (oe) {
2431 if (oe->type != OBJ_BLOB)
2432 die("Not a blob (actually a %s): %s",
debca9d2 2433 type_name(oe->type), command_buf.buf);
912c13d5 2434 } else if (!is_null_oid(&oid)) {
0df8e965
SB
2435 enum object_type type = oid_object_info(the_repository, &oid,
2436 NULL);
a8dd2e7d
JH
2437 if (type < 0)
2438 die("Blob not found: %s", command_buf.buf);
2439 if (type != OBJ_BLOB)
2440 die("Not a blob (actually a %s): %s",
debca9d2 2441 type_name(type), command_buf.buf);
a8dd2e7d
JH
2442 }
2443
912c13d5 2444 construct_path_with_fanout(oid_to_hex(&commit_oid), *old_fanout, path);
62bfa11c 2445 if (tree_content_remove(&b->branch_tree, path, NULL, 0))
2a113aee
JH
2446 b->num_notes--;
2447
912c13d5 2448 if (is_null_oid(&oid))
2a113aee
JH
2449 return; /* nothing to insert */
2450
2451 b->num_notes++;
2452 new_fanout = convert_num_notes_to_fanout(b->num_notes);
912c13d5 2453 construct_path_with_fanout(oid_to_hex(&commit_oid), new_fanout, path);
2454 tree_content_set(&b->branch_tree, path, &oid, S_IFREG | 0644, NULL);
a8dd2e7d
JH
2455}
2456
825769a8
SP
2457static void file_change_deleteall(struct branch *b)
2458{
2459 release_tree_content_recursive(b->branch_tree.tree);
d7e6b6a8 2460 oidclr(&b->branch_tree.versions[0].oid);
2461 oidclr(&b->branch_tree.versions[1].oid);
825769a8 2462 load_tree(&b->branch_tree);
2a113aee 2463 b->num_notes = 0;
825769a8
SP
2464}
2465
b3031781 2466static void parse_from_commit(struct branch *b, char *buf, unsigned long size)
654aaa37 2467{
28d055bd 2468 if (!buf || size < the_hash_algo->hexsz + 6)
d7e6b6a8 2469 die("Not a valid commit: %s", oid_to_hex(&b->oid));
654aaa37 2470 if (memcmp("tree ", buf, 5)
912c13d5 2471 || get_oid_hex(buf + 5, &b->branch_tree.versions[1].oid))
d7e6b6a8 2472 die("The commit %s is corrupt", oid_to_hex(&b->oid));
2473 oidcpy(&b->branch_tree.versions[0].oid,
2474 &b->branch_tree.versions[1].oid);
654aaa37
SP
2475}
2476
b3031781 2477static void parse_from_existing(struct branch *b)
654aaa37 2478{
d7e6b6a8 2479 if (is_null_oid(&b->oid)) {
2480 oidclr(&b->branch_tree.versions[0].oid);
2481 oidclr(&b->branch_tree.versions[1].oid);
654aaa37
SP
2482 } else {
2483 unsigned long size;
2484 char *buf;
2485
d3b4705a
NTND
2486 buf = read_object_with_reference(the_repository,
2487 &b->oid, commit_type, &size,
02f0547e 2488 &b->oid);
b3031781 2489 parse_from_commit(b, buf, size);
654aaa37
SP
2490 free(buf);
2491 }
2492}
2493
b3031781 2494static int parse_from(struct branch *b)
00e2b884 2495{
6c3aac1c 2496 const char *from;
00e2b884 2497 struct branch *s;
912c13d5 2498 struct object_id oid;
00e2b884 2499
97313bef 2500 if (!skip_prefix(command_buf.buf, "from ", &from))
1fdb649c 2501 return 0;
00e2b884 2502
912c13d5 2503 oidcpy(&oid, &b->branch_tree.versions[1].oid);
00e2b884 2504
00e2b884
SP
2505 s = lookup_branch(from);
2506 if (b == s)
2507 die("Can't create a branch from itself: %s", b->name);
2508 else if (s) {
912c13d5 2509 struct object_id *t = &s->branch_tree.versions[1].oid;
d7e6b6a8 2510 oidcpy(&b->oid, &s->oid);
912c13d5 2511 oidcpy(&b->branch_tree.versions[0].oid, t);
2512 oidcpy(&b->branch_tree.versions[1].oid, t);
00e2b884 2513 } else if (*from == ':') {
06454cb9 2514 uintmax_t idnum = parse_mark_ref_eol(from);
00e2b884 2515 struct object_entry *oe = find_mark(idnum);
00e2b884 2516 if (oe->type != OBJ_COMMIT)
3efb1f34 2517 die("Mark :%" PRIuMAX " not a commit", idnum);
9001dc2a 2518 if (!oideq(&b->oid, &oe->idx.oid)) {
e6a492b7 2519 oidcpy(&b->oid, &oe->idx.oid);
0df32457
MH
2520 if (oe->pack_id != MAX_PACK_ID) {
2521 unsigned long size;
2522 char *buf = gfi_unpack_entry(oe, &size);
2523 parse_from_commit(b, buf, size);
2524 free(buf);
2525 } else
2526 parse_from_existing(b);
2527 }
912c13d5 2528 } else if (!get_oid(from, &b->oid)) {
b3031781 2529 parse_from_existing(b);
d7e6b6a8 2530 if (is_null_oid(&b->oid))
4ee1b225
FC
2531 b->delete = 1;
2532 }
654aaa37 2533 else
00e2b884
SP
2534 die("Invalid ref name or SHA1 expression: %s", from);
2535
9001dc2a 2536 if (b->branch_tree.tree && !oideq(&oid, &b->branch_tree.versions[1].oid)) {
0df32457
MH
2537 release_tree_content_recursive(b->branch_tree.tree);
2538 b->branch_tree.tree = NULL;
2539 }
2540
00e2b884 2541 read_next_command();
1fdb649c 2542 return 1;
00e2b884
SP
2543}
2544
b3031781 2545static struct hash_list *parse_merge(unsigned int *count)
62b6f483 2546{
4db34cc1 2547 struct hash_list *list = NULL, **tail = &list, *n;
6c3aac1c 2548 const char *from;
62b6f483
SP
2549 struct branch *s;
2550
2551 *count = 0;
97313bef 2552 while (skip_prefix(command_buf.buf, "merge ", &from)) {
62b6f483
SP
2553 n = xmalloc(sizeof(*n));
2554 s = lookup_branch(from);
2555 if (s)
d7e6b6a8 2556 oidcpy(&n->oid, &s->oid);
62b6f483 2557 else if (*from == ':') {
06454cb9 2558 uintmax_t idnum = parse_mark_ref_eol(from);
62b6f483
SP
2559 struct object_entry *oe = find_mark(idnum);
2560 if (oe->type != OBJ_COMMIT)
3efb1f34 2561 die("Mark :%" PRIuMAX " not a commit", idnum);
e6a492b7 2562 oidcpy(&n->oid, &oe->idx.oid);
912c13d5 2563 } else if (!get_oid(from, &n->oid)) {
2f6dc35d 2564 unsigned long size;
d3b4705a
NTND
2565 char *buf = read_object_with_reference(the_repository,
2566 &n->oid,
02f0547e 2567 commit_type,
2568 &size, &n->oid);
28d055bd 2569 if (!buf || size < the_hash_algo->hexsz + 6)
2f6dc35d
SP
2570 die("Not a valid commit: %s", from);
2571 free(buf);
2572 } else
62b6f483
SP
2573 die("Invalid ref name or SHA1 expression: %s", from);
2574
2575 n->next = NULL;
4db34cc1
JK
2576 *tail = n;
2577 tail = &n->next;
2578
10e8d688 2579 (*count)++;
62b6f483
SP
2580 read_next_command();
2581 }
2582 return list;
2583}
2584
97313bef 2585static void parse_new_commit(const char *arg)
6bb5b329 2586{
eec813cf 2587 static struct strbuf msg = STRBUF_INIT;
c44cdc7e 2588 struct branch *b;
c44cdc7e
SP
2589 char *author = NULL;
2590 char *committer = NULL;
3edfcc65 2591 const char *encoding = NULL;
62b6f483
SP
2592 struct hash_list *merge_list = NULL;
2593 unsigned int merge_count;
2a113aee 2594 unsigned char prev_fanout, new_fanout;
ae021d87 2595 const char *v;
c44cdc7e 2596
97313bef 2597 b = lookup_branch(arg);
463acbe1 2598 if (!b)
97313bef 2599 b = new_branch(arg);
c44cdc7e
SP
2600
2601 read_next_command();
b3031781 2602 parse_mark();
a965bb31 2603 parse_original_identifier();
ae021d87
JK
2604 if (skip_prefix(command_buf.buf, "author ", &v)) {
2605 author = parse_ident(v);
c44cdc7e
SP
2606 read_next_command();
2607 }
ae021d87
JK
2608 if (skip_prefix(command_buf.buf, "committer ", &v)) {
2609 committer = parse_ident(v);
c44cdc7e
SP
2610 read_next_command();
2611 }
2612 if (!committer)
2613 die("Expected committer but didn't get one");
3edfcc65
EN
2614 if (skip_prefix(command_buf.buf, "encoding ", &encoding))
2615 read_next_command();
5eef828b 2616 parse_data(&msg, 0, NULL);
02f3389d 2617 read_next_command();
b3031781
MV
2618 parse_from(b);
2619 merge_list = parse_merge(&merge_count);
c44cdc7e
SP
2620
2621 /* ensure the branch is active/loaded */
41e5257f 2622 if (!b->branch_tree.tree || !max_active_branches) {
463acbe1
SP
2623 unload_one_branch();
2624 load_branch(b);
2625 }
6bb5b329 2626
2a113aee
JH
2627 prev_fanout = convert_num_notes_to_fanout(b->num_notes);
2628
463acbe1 2629 /* file_change* */
e6c019d0 2630 while (command_buf.len > 0) {
97313bef
JK
2631 if (skip_prefix(command_buf.buf, "M ", &v))
2632 file_change_m(v, b);
2633 else if (skip_prefix(command_buf.buf, "D ", &v))
2634 file_change_d(v, b);
2635 else if (skip_prefix(command_buf.buf, "R ", &v))
2636 file_change_cr(v, b, 1);
2637 else if (skip_prefix(command_buf.buf, "C ", &v))
2638 file_change_cr(v, b, 0);
2639 else if (skip_prefix(command_buf.buf, "N ", &v))
2640 note_change_n(v, b, &prev_fanout);
825769a8
SP
2641 else if (!strcmp("deleteall", command_buf.buf))
2642 file_change_deleteall(b);
97313bef
JK
2643 else if (skip_prefix(command_buf.buf, "ls ", &v))
2644 parse_ls(v, b);
7ffde293
EN
2645 else if (skip_prefix(command_buf.buf, "cat-blob ", &v))
2646 parse_cat_blob(v);
1fdb649c
SP
2647 else {
2648 unread_command_buf = 1;
2649 break;
2650 }
e6c019d0
PH
2651 if (read_next_command() == EOF)
2652 break;
6bb5b329 2653 }
6bb5b329 2654
2a113aee
JH
2655 new_fanout = convert_num_notes_to_fanout(b->num_notes);
2656 if (new_fanout != prev_fanout)
2657 b->num_notes = change_note_fanout(&b->branch_tree, new_fanout);
2658
c44cdc7e 2659 /* build the tree and the commit */
463acbe1 2660 store_tree(&b->branch_tree);
d7e6b6a8 2661 oidcpy(&b->branch_tree.versions[0].oid,
2662 &b->branch_tree.versions[1].oid);
eec813cf
PH
2663
2664 strbuf_reset(&new_data);
2665 strbuf_addf(&new_data, "tree %s\n",
d7e6b6a8 2666 oid_to_hex(&b->branch_tree.versions[1].oid));
2667 if (!is_null_oid(&b->oid))
2668 strbuf_addf(&new_data, "parent %s\n",
2669 oid_to_hex(&b->oid));
62b6f483
SP
2670 while (merge_list) {
2671 struct hash_list *next = merge_list->next;
d7e6b6a8 2672 strbuf_addf(&new_data, "parent %s\n",
2673 oid_to_hex(&merge_list->oid));
62b6f483
SP
2674 free(merge_list);
2675 merge_list = next;
2676 }
eec813cf
PH
2677 strbuf_addf(&new_data,
2678 "author %s\n"
3edfcc65 2679 "committer %s\n",
eec813cf 2680 author ? author : committer, committer);
3edfcc65
EN
2681 if (encoding)
2682 strbuf_addf(&new_data,
2683 "encoding %s\n",
2684 encoding);
2685 strbuf_addch(&new_data, '\n');
eec813cf 2686 strbuf_addbuf(&new_data, &msg);
e7d06a4b 2687 free(author);
c44cdc7e 2688 free(committer);
c44cdc7e 2689
912c13d5 2690 if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark))
69e74e74 2691 b->pack_id = pack_id;
463acbe1 2692 b->last_commit = object_count_by_type[OBJ_COMMIT];
6bb5b329
SP
2693}
2694
97313bef 2695static void parse_new_tag(const char *arg)
72303d44 2696{
eec813cf 2697 static struct strbuf msg = STRBUF_INIT;
72303d44
SP
2698 const char *from;
2699 char *tagger;
2700 struct branch *s;
72303d44 2701 struct tag *t;
0ea9f045 2702 uintmax_t from_mark = 0;
912c13d5 2703 struct object_id oid;
8db751a8 2704 enum object_type type;
ae021d87 2705 const char *v;
72303d44 2706
96c47d14 2707 t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag));
a8ea1b7a 2708 memset(t, 0, sizeof(struct tag));
97313bef 2709 t->name = pool_strdup(arg);
72303d44
SP
2710 if (last_tag)
2711 last_tag->next_tag = t;
2712 else
2713 first_tag = t;
2714 last_tag = t;
72303d44
SP
2715 read_next_command();
2716
2717 /* from ... */
97313bef 2718 if (!skip_prefix(command_buf.buf, "from ", &from))
72303d44 2719 die("Expected from command, got %s", command_buf.buf);
72303d44
SP
2720 s = lookup_branch(from);
2721 if (s) {
d7e6b6a8 2722 if (is_null_oid(&s->oid))
2c9c8ee2 2723 die("Can't tag an empty branch.");
912c13d5 2724 oidcpy(&oid, &s->oid);
8db751a8 2725 type = OBJ_COMMIT;
72303d44 2726 } else if (*from == ':') {
10e8d688 2727 struct object_entry *oe;
06454cb9 2728 from_mark = parse_mark_ref_eol(from);
10e8d688 2729 oe = find_mark(from_mark);
8db751a8 2730 type = oe->type;
e6a492b7 2731 oidcpy(&oid, &oe->idx.oid);
912c13d5 2732 } else if (!get_oid(from, &oid)) {
2733 struct object_entry *oe = find_object(&oid);
6c447f63 2734 if (!oe) {
0df8e965 2735 type = oid_object_info(the_repository, &oid, NULL);
6c447f63
DI
2736 if (type < 0)
2737 die("Not a valid object: %s", from);
2738 } else
2739 type = oe->type;
72303d44
SP
2740 } else
2741 die("Invalid ref name or SHA1 expression: %s", from);
72303d44
SP
2742 read_next_command();
2743
a965bb31
EN
2744 /* original-oid ... */
2745 parse_original_identifier();
2746
72303d44 2747 /* tagger ... */
ae021d87
JK
2748 if (skip_prefix(command_buf.buf, "tagger ", &v)) {
2749 tagger = parse_ident(v);
88fbf67b
JH
2750 read_next_command();
2751 } else
2752 tagger = NULL;
72303d44
SP
2753
2754 /* tag payload/message */
5eef828b 2755 parse_data(&msg, 0, NULL);
72303d44
SP
2756
2757 /* build the tag object */
eec813cf 2758 strbuf_reset(&new_data);
88fbf67b 2759
eec813cf 2760 strbuf_addf(&new_data,
88fbf67b
JH
2761 "object %s\n"
2762 "type %s\n"
2763 "tag %s\n",
debca9d2 2764 oid_to_hex(&oid), type_name(type), t->name);
88fbf67b
JH
2765 if (tagger)
2766 strbuf_addf(&new_data,
2767 "tagger %s\n", tagger);
2768 strbuf_addch(&new_data, '\n');
eec813cf 2769 strbuf_addbuf(&new_data, &msg);
72303d44 2770 free(tagger);
72303d44 2771
912c13d5 2772 if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, 0))
69e74e74
SP
2773 t->pack_id = MAX_PACK_ID;
2774 else
2775 t->pack_id = pack_id;
72303d44
SP
2776}
2777
97313bef 2778static void parse_reset_branch(const char *arg)
5fced8dc
SP
2779{
2780 struct branch *b;
5fced8dc 2781
97313bef 2782 b = lookup_branch(arg);
5fced8dc 2783 if (b) {
d7e6b6a8 2784 oidclr(&b->oid);
2785 oidclr(&b->branch_tree.versions[0].oid);
2786 oidclr(&b->branch_tree.versions[1].oid);
5fced8dc
SP
2787 if (b->branch_tree.tree) {
2788 release_tree_content_recursive(b->branch_tree.tree);
2789 b->branch_tree.tree = NULL;
2790 }
2791 }
9938ffc5 2792 else
97313bef 2793 b = new_branch(arg);
9938ffc5 2794 read_next_command();
b3031781 2795 parse_from(b);
655e8515 2796 if (command_buf.len > 0)
1fdb649c 2797 unread_command_buf = 1;
5fced8dc
SP
2798}
2799
85c62395
DB
2800static void cat_blob_write(const char *buf, unsigned long size)
2801{
06f46f23 2802 if (write_in_full(cat_blob_fd, buf, size) < 0)
85c62395
DB
2803 die_errno("Write to frontend failed");
2804}
2805
912c13d5 2806static void cat_blob(struct object_entry *oe, struct object_id *oid)
85c62395
DB
2807{
2808 struct strbuf line = STRBUF_INIT;
2809 unsigned long size;
2810 enum object_type type = 0;
2811 char *buf;
2812
2813 if (!oe || oe->pack_id == MAX_PACK_ID) {
b4f5aca4 2814 buf = read_object_file(oid, &type, &size);
85c62395
DB
2815 } else {
2816 type = oe->type;
2817 buf = gfi_unpack_entry(oe, &size);
2818 }
2819
2820 /*
2821 * Output based on batch_one_object() from cat-file.c.
2822 */
2823 if (type <= 0) {
2824 strbuf_reset(&line);
912c13d5 2825 strbuf_addf(&line, "%s missing\n", oid_to_hex(oid));
85c62395
DB
2826 cat_blob_write(line.buf, line.len);
2827 strbuf_release(&line);
2828 free(buf);
2829 return;
2830 }
2831 if (!buf)
912c13d5 2832 die("Can't read object %s", oid_to_hex(oid));
85c62395
DB
2833 if (type != OBJ_BLOB)
2834 die("Object %s is a %s but a blob was expected.",
debca9d2 2835 oid_to_hex(oid), type_name(type));
85c62395 2836 strbuf_reset(&line);
ca473cef
TB
2837 strbuf_addf(&line, "%s %s %"PRIuMAX"\n", oid_to_hex(oid),
2838 type_name(type), (uintmax_t)size);
85c62395
DB
2839 cat_blob_write(line.buf, line.len);
2840 strbuf_release(&line);
2841 cat_blob_write(buf, size);
2842 cat_blob_write("\n", 1);
a7e9c341
DI
2843 if (oe && oe->pack_id == pack_id) {
2844 last_blob.offset = oe->idx.offset;
2845 strbuf_attach(&last_blob.data, buf, size, size);
2846 last_blob.depth = oe->depth;
2847 } else
2848 free(buf);
85c62395
DB
2849}
2850
28c7b1f7
MH
2851static void parse_get_mark(const char *p)
2852{
156e1782 2853 struct object_entry *oe;
912c13d5 2854 char output[GIT_MAX_HEXSZ + 2];
28c7b1f7
MH
2855
2856 /* get-mark SP <object> LF */
2857 if (*p != ':')
2858 die("Not a mark: %s", p);
2859
2860 oe = find_mark(parse_mark_ref_eol(p));
2861 if (!oe)
2862 die("Unknown mark: %s", command_buf.buf);
2863
e6a492b7 2864 xsnprintf(output, sizeof(output), "%s\n", oid_to_hex(&oe->idx.oid));
28d055bd 2865 cat_blob_write(output, the_hash_algo->hexsz + 1);
28c7b1f7
MH
2866}
2867
97313bef 2868static void parse_cat_blob(const char *p)
85c62395 2869{
156e1782 2870 struct object_entry *oe;
912c13d5 2871 struct object_id oid;
85c62395
DB
2872
2873 /* cat-blob SP <object> LF */
85c62395 2874 if (*p == ':') {
06454cb9 2875 oe = find_mark(parse_mark_ref_eol(p));
85c62395
DB
2876 if (!oe)
2877 die("Unknown mark: %s", command_buf.buf);
e6a492b7 2878 oidcpy(&oid, &oe->idx.oid);
85c62395 2879 } else {
912c13d5 2880 if (parse_oid_hex(p, &oid, &p))
06454cb9 2881 die("Invalid dataref: %s", command_buf.buf);
912c13d5 2882 if (*p)
85c62395 2883 die("Garbage after SHA1: %s", command_buf.buf);
912c13d5 2884 oe = find_object(&oid);
85c62395
DB
2885 }
2886
912c13d5 2887 cat_blob(oe, &oid);
85c62395
DB
2888}
2889
8dc6a373 2890static struct object_entry *dereference(struct object_entry *oe,
912c13d5 2891 struct object_id *oid)
8dc6a373
DB
2892{
2893 unsigned long size;
6288e3e1 2894 char *buf = NULL;
28d055bd 2895 const unsigned hexsz = the_hash_algo->hexsz;
2896
8dc6a373 2897 if (!oe) {
0df8e965
SB
2898 enum object_type type = oid_object_info(the_repository, oid,
2899 NULL);
8dc6a373 2900 if (type < 0)
912c13d5 2901 die("object not found: %s", oid_to_hex(oid));
8dc6a373 2902 /* cache it! */
912c13d5 2903 oe = insert_object(oid);
8dc6a373
DB
2904 oe->type = type;
2905 oe->pack_id = MAX_PACK_ID;
2906 oe->idx.offset = 1;
2907 }
2908 switch (oe->type) {
2909 case OBJ_TREE: /* easy case. */
2910 return oe;
2911 case OBJ_COMMIT:
2912 case OBJ_TAG:
2913 break;
2914 default:
bb8040f9 2915 die("Not a tree-ish: %s", command_buf.buf);
8dc6a373
DB
2916 }
2917
2918 if (oe->pack_id != MAX_PACK_ID) { /* in a pack being written */
2919 buf = gfi_unpack_entry(oe, &size);
2920 } else {
2921 enum object_type unused;
b4f5aca4 2922 buf = read_object_file(oid, &unused, &size);
8dc6a373
DB
2923 }
2924 if (!buf)
912c13d5 2925 die("Can't load object %s", oid_to_hex(oid));
8dc6a373
DB
2926
2927 /* Peel one layer. */
2928 switch (oe->type) {
2929 case OBJ_TAG:
28d055bd 2930 if (size < hexsz + strlen("object ") ||
912c13d5 2931 get_oid_hex(buf + strlen("object "), oid))
8dc6a373
DB
2932 die("Invalid SHA1 in tag: %s", command_buf.buf);
2933 break;
2934 case OBJ_COMMIT:
28d055bd 2935 if (size < hexsz + strlen("tree ") ||
912c13d5 2936 get_oid_hex(buf + strlen("tree "), oid))
8dc6a373
DB
2937 die("Invalid SHA1 in commit: %s", command_buf.buf);
2938 }
2939
2940 free(buf);
912c13d5 2941 return find_object(oid);
8dc6a373
DB
2942}
2943
2944static struct object_entry *parse_treeish_dataref(const char **p)
2945{
912c13d5 2946 struct object_id oid;
8dc6a373
DB
2947 struct object_entry *e;
2948
2949 if (**p == ':') { /* <mark> */
06454cb9 2950 e = find_mark(parse_mark_ref_space(p));
8dc6a373
DB
2951 if (!e)
2952 die("Unknown mark: %s", command_buf.buf);
e6a492b7 2953 oidcpy(&oid, &e->idx.oid);
8dc6a373 2954 } else { /* <sha1> */
912c13d5 2955 if (parse_oid_hex(*p, &oid, p))
06454cb9 2956 die("Invalid dataref: %s", command_buf.buf);
912c13d5 2957 e = find_object(&oid);
e814c39c
JK
2958 if (*(*p)++ != ' ')
2959 die("Missing space after tree-ish: %s", command_buf.buf);
8dc6a373
DB
2960 }
2961
2962 while (!e || e->type != OBJ_TREE)
912c13d5 2963 e = dereference(e, &oid);
8dc6a373
DB
2964 return e;
2965}
2966
ef479a12 2967static void print_ls(int mode, const unsigned char *hash, const char *path)
8dc6a373
DB
2968{
2969 static struct strbuf line = STRBUF_INIT;
2970
2971 /* See show_tree(). */
2972 const char *type =
2973 S_ISGITLINK(mode) ? commit_type :
2974 S_ISDIR(mode) ? tree_type :
2975 blob_type;
2976
2977 if (!mode) {
2978 /* missing SP path LF */
2979 strbuf_reset(&line);
2980 strbuf_addstr(&line, "missing ");
2981 quote_c_style(path, &line, NULL, 0);
2982 strbuf_addch(&line, '\n');
2983 } else {
2984 /* mode SP type SP object_name TAB path LF */
2985 strbuf_reset(&line);
2986 strbuf_addf(&line, "%06o %s %s\t",
ef479a12 2987 mode & ~NO_DELTA, type, hash_to_hex(hash));
8dc6a373
DB
2988 quote_c_style(path, &line, NULL, 0);
2989 strbuf_addch(&line, '\n');
2990 }
2991 cat_blob_write(line.buf, line.len);
2992}
2993
97313bef 2994static void parse_ls(const char *p, struct branch *b)
8dc6a373 2995{
8dc6a373 2996 struct tree_entry *root = NULL;
c2e86add 2997 struct tree_entry leaf = {NULL};
8dc6a373 2998
bb8040f9 2999 /* ls SP (<tree-ish> SP)? <path> */
8dc6a373
DB
3000 if (*p == '"') {
3001 if (!b)
3002 die("Not in a commit: %s", command_buf.buf);
3003 root = &b->branch_tree;
3004 } else {
3005 struct object_entry *e = parse_treeish_dataref(&p);
3006 root = new_tree_entry();
e6a492b7 3007 oidcpy(&root->versions[1].oid, &e->idx.oid);
d7e6b6a8 3008 if (!is_null_oid(&root->versions[1].oid))
adefdba5 3009 root->versions[1].mode = S_IFDIR;
8dc6a373 3010 load_tree(root);
8dc6a373
DB
3011 }
3012 if (*p == '"') {
3013 static struct strbuf uq = STRBUF_INIT;
3014 const char *endp;
3015 strbuf_reset(&uq);
3016 if (unquote_c_style(&uq, p, &endp))
3017 die("Invalid path: %s", command_buf.buf);
3018 if (*endp)
3019 die("Garbage after path in: %s", command_buf.buf);
3020 p = uq.buf;
3021 }
e0eb6b97 3022 tree_content_get(root, p, &leaf, 1);
8dc6a373
DB
3023 /*
3024 * A directory in preparation would have a sha1 of zero
3025 * until it is saved. Save, for simplicity.
3026 */
3027 if (S_ISDIR(leaf.versions[1].mode))
3028 store_tree(&leaf);
3029
d7e6b6a8 3030 print_ls(leaf.versions[1].mode, leaf.versions[1].oid.hash, p);
c27e559d
JN
3031 if (leaf.tree)
3032 release_tree_content_recursive(leaf.tree);
8dc6a373
DB
3033 if (!b || root != &b->branch_tree)
3034 release_tree_entry(root);
3035}
3036
dc01f59d 3037static void checkpoint(void)
7bfe6e26 3038{
dc01f59d 3039 checkpoint_requested = 0;
820b9310
SP
3040 if (object_count) {
3041 cycle_packfile();
820b9310 3042 }
30e215a6
ER
3043 dump_branches();
3044 dump_tags();
3045 dump_marks();
dc01f59d
JN
3046}
3047
3048static void parse_checkpoint(void)
3049{
3050 checkpoint_requested = 1;
1fdb649c 3051 skip_optional_lf();
7bfe6e26
SP
3052}
3053
b3031781 3054static void parse_progress(void)
ac053c02 3055{
b449f4cf 3056 fwrite(command_buf.buf, 1, command_buf.len, stdout);
ac053c02
SP
3057 fputc('\n', stdout);
3058 fflush(stdout);
3059 skip_optional_lf();
3060}
3061
bc3c79ae 3062static char* make_fast_import_path(const char *path)
e8438420 3063{
bc3c79ae
SR
3064 if (!relative_marks_paths || is_absolute_path(path))
3065 return xstrdup(path);
d9c69644 3066 return git_pathdup("info/fast-import/%s", path);
bc3c79ae
SR
3067}
3068
dded4f12
RR
3069static void option_import_marks(const char *marks,
3070 int from_stream, int ignore_missing)
e8438420 3071{
081751c8
SR
3072 if (import_marks_file) {
3073 if (from_stream)
3074 die("Only one import-marks command allowed per stream");
3075
3076 /* read previous mark file */
3077 if(!import_marks_file_from_stream)
3078 read_marks();
e8438420 3079 }
081751c8 3080
bc3c79ae 3081 import_marks_file = make_fast_import_path(marks);
580d5f83 3082 safe_create_leading_directories_const(import_marks_file);
081751c8 3083 import_marks_file_from_stream = from_stream;
dded4f12 3084 import_marks_file_ignore_missing = ignore_missing;
e8438420
SP
3085}
3086
0f6927c2
SR
3087static void option_date_format(const char *fmt)
3088{
3089 if (!strcmp(fmt, "raw"))
3090 whenspec = WHENSPEC_RAW;
3091 else if (!strcmp(fmt, "rfc2822"))
3092 whenspec = WHENSPEC_RFC2822;
3093 else if (!strcmp(fmt, "now"))
3094 whenspec = WHENSPEC_NOW;
3095 else
3096 die("unknown --date-format argument %s", fmt);
3097}
3098
a9ff277e
JN
3099static unsigned long ulong_arg(const char *option, const char *arg)
3100{
3101 char *endptr;
3102 unsigned long rv = strtoul(arg, &endptr, 0);
3103 if (strchr(arg, '-') || endptr == arg || *endptr)
3104 die("%s: argument must be a non-negative integer", option);
3105 return rv;
3106}
3107
0f6927c2
SR
3108static void option_depth(const char *depth)
3109{
a9ff277e 3110 max_depth = ulong_arg("--depth", depth);
0f6927c2
SR
3111 if (max_depth > MAX_DEPTH)
3112 die("--depth cannot exceed %u", MAX_DEPTH);
3113}
3114
3115static void option_active_branches(const char *branches)
3116{
a9ff277e 3117 max_active_branches = ulong_arg("--active-branches", branches);
0f6927c2
SR
3118}
3119
3120static void option_export_marks(const char *marks)
3121{
bc3c79ae 3122 export_marks_file = make_fast_import_path(marks);
580d5f83 3123 safe_create_leading_directories_const(export_marks_file);
0f6927c2
SR
3124}
3125
85c62395
DB
3126static void option_cat_blob_fd(const char *fd)
3127{
3128 unsigned long n = ulong_arg("--cat-blob-fd", fd);
3129 if (n > (unsigned long) INT_MAX)
3130 die("--cat-blob-fd cannot exceed %d", INT_MAX);
3131 cat_blob_fd = (int) n;
3132}
3133
0f6927c2
SR
3134static void option_export_pack_edges(const char *edges)
3135{
3136 if (pack_edges)
3137 fclose(pack_edges);
23a9e071 3138 pack_edges = xfopen(edges, "a");
0f6927c2
SR
3139}
3140
9c8398f0 3141static int parse_one_option(const char *option)
0f6927c2 3142{
ae021d87 3143 if (skip_prefix(option, "max-pack-size=", &option)) {
4d0cc224 3144 unsigned long v;
ae021d87 3145 if (!git_parse_ulong(option, &v))
4d0cc224
JH
3146 return 0;
3147 if (v < 8192) {
3148 warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v);
3149 v *= 1024 * 1024;
3150 } else if (v < 1024 * 1024) {
3151 warning("minimum max-pack-size is 1 MiB");
3152 v = 1024 * 1024;
3153 }
3154 max_packsize = v;
ae021d87 3155 } else if (skip_prefix(option, "big-file-threshold=", &option)) {
76ea93cc 3156 unsigned long v;
ae021d87 3157 if (!git_parse_ulong(option, &v))
76ea93cc
JH
3158 return 0;
3159 big_file_threshold = v;
ae021d87
JK
3160 } else if (skip_prefix(option, "depth=", &option)) {
3161 option_depth(option);
3162 } else if (skip_prefix(option, "active-branches=", &option)) {
3163 option_active_branches(option);
3164 } else if (skip_prefix(option, "export-pack-edges=", &option)) {
3165 option_export_pack_edges(option);
59556548 3166 } else if (starts_with(option, "quiet")) {
0f6927c2 3167 show_stats = 0;
59556548 3168 } else if (starts_with(option, "stats")) {
0f6927c2
SR
3169 show_stats = 1;
3170 } else {
9c8398f0 3171 return 0;
0f6927c2 3172 }
9c8398f0
SR
3173
3174 return 1;
0f6927c2
SR
3175}
3176
081751c8 3177static int parse_one_feature(const char *feature, int from_stream)
f963bd5d 3178{
ae021d87
JK
3179 const char *arg;
3180
3181 if (skip_prefix(feature, "date-format=", &arg)) {
3182 option_date_format(arg);
3183 } else if (skip_prefix(feature, "import-marks=", &arg)) {
3184 option_import_marks(arg, from_stream, 0);
3185 } else if (skip_prefix(feature, "import-marks-if-exists=", &arg)) {
3186 option_import_marks(arg, from_stream, 1);
3187 } else if (skip_prefix(feature, "export-marks=", &arg)) {
3188 option_export_marks(arg);
28c7b1f7
MH
3189 } else if (!strcmp(feature, "get-mark")) {
3190 ; /* Don't die - this feature is supported */
85c62395
DB
3191 } else if (!strcmp(feature, "cat-blob")) {
3192 ; /* Don't die - this feature is supported */
4cce4ef2 3193 } else if (!strcmp(feature, "relative-marks")) {
bc3c79ae 3194 relative_marks_paths = 1;
4cce4ef2 3195 } else if (!strcmp(feature, "no-relative-marks")) {
bc3c79ae 3196 relative_marks_paths = 0;
be56862f
SR
3197 } else if (!strcmp(feature, "done")) {
3198 require_explicit_termination = 1;
4cce4ef2 3199 } else if (!strcmp(feature, "force")) {
f963bd5d 3200 force_update = 1;
8dc6a373 3201 } else if (!strcmp(feature, "notes") || !strcmp(feature, "ls")) {
547e8b92 3202 ; /* do nothing; we have the feature */
f963bd5d
SR
3203 } else {
3204 return 0;
3205 }
3206
3207 return 1;
3208}
3209
97313bef 3210static void parse_feature(const char *feature)
f963bd5d 3211{
f963bd5d
SR
3212 if (seen_data_command)
3213 die("Got feature command '%s' after data command", feature);
3214
081751c8 3215 if (parse_one_feature(feature, 1))
f963bd5d
SR
3216 return;
3217
3218 die("This version of fast-import does not support feature %s.", feature);
3219}
3220
97313bef 3221static void parse_option(const char *option)
9c8398f0 3222{
9c8398f0
SR
3223 if (seen_data_command)
3224 die("Got option command '%s' after data command", option);
3225
3226 if (parse_one_option(option))
3227 return;
3228
3229 die("This version of fast-import does not support option: %s", option);
e8438420
SP
3230}
3231
536900e5 3232static void git_pack_config(void)
bb23fdfa 3233{
536900e5 3234 int indexversion_value;
d9545c7f 3235 int limit;
536900e5
TA
3236 unsigned long packsizelimit_value;
3237
3238 if (!git_config_get_ulong("pack.depth", &max_depth)) {
bb23fdfa
SP
3239 if (max_depth > MAX_DEPTH)
3240 max_depth = MAX_DEPTH;
bb23fdfa 3241 }
536900e5
TA
3242 if (!git_config_get_int("pack.indexversion", &indexversion_value)) {
3243 pack_idx_opts.version = indexversion_value;
ebcfb379 3244 if (pack_idx_opts.version > 2)
536900e5
TA
3245 git_die_config("pack.indexversion",
3246 "bad pack.indexversion=%"PRIu32, pack_idx_opts.version);
8c2ca8dd 3247 }
536900e5
TA
3248 if (!git_config_get_ulong("pack.packsizelimit", &packsizelimit_value))
3249 max_packsize = packsizelimit_value;
3250
d9545c7f
EW
3251 if (!git_config_get_int("fastimport.unpacklimit", &limit))
3252 unpack_limit = limit;
3253 else if (!git_config_get_int("transfer.unpacklimit", &limit))
3254 unpack_limit = limit;
3255
536900e5 3256 git_config(git_default_config, NULL);
bb23fdfa
SP
3257}
3258
d5c57b28 3259static const char fast_import_usage[] =
62b4698e 3260"git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
d5c57b28 3261
9c8398f0
SR
3262static void parse_argv(void)
3263{
3264 unsigned int i;
3265
3266 for (i = 1; i < global_argc; i++) {
3267 const char *a = global_argv[i];
3268
3269 if (*a != '-' || !strcmp(a, "--"))
3270 break;
3271
ff45c0d4
JK
3272 if (!skip_prefix(a, "--", &a))
3273 die("unknown option %s", a);
3274
3275 if (parse_one_option(a))
9c8398f0
SR
3276 continue;
3277
ff45c0d4 3278 if (parse_one_feature(a, 0))
9c8398f0
SR
3279 continue;
3280
ff45c0d4
JK
3281 if (skip_prefix(a, "cat-blob-fd=", &a)) {
3282 option_cat_blob_fd(a);
85c62395
DB
3283 continue;
3284 }
3285
ff45c0d4 3286 die("unknown option --%s", a);
9c8398f0
SR
3287 }
3288 if (i != global_argc)
3289 usage(fast_import_usage);
3290
3291 seen_data_command = 1;
3292 if (import_marks_file)
3293 read_marks();
3294}
3295
3f2e2297 3296int cmd_main(int argc, const char **argv)
8bcce301 3297{
0f6927c2 3298 unsigned int i;
8bcce301 3299
71a04a8b
JN
3300 if (argc == 2 && !strcmp(argv[1], "-h"))
3301 usage(fast_import_usage);
3302
733ee2b7 3303 setup_git_directory();
ebcfb379 3304 reset_pack_idx_option(&pack_idx_opts);
536900e5 3305 git_pack_config();
bb23fdfa 3306
93e72d8d 3307 alloc_objects(object_entry_alloc);
f1696ee3 3308 strbuf_init(&command_buf, 0);
93e72d8d
SP
3309 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
3310 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
3311 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
96c47d14 3312 marks = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
463acbe1 3313
9c8398f0
SR
3314 global_argc = argc;
3315 global_argv = argv;
d5c57b28 3316
96c47d14 3317 rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free));
904b1941
SP
3318 for (i = 0; i < (cmd_save - 1); i++)
3319 rc_free[i].next = &rc_free[i + 1];
3320 rc_free[cmd_save - 1].next = NULL;
3321
f70b6534 3322 start_packfile();
8acb3297 3323 set_die_routine(die_nicely);
dc01f59d 3324 set_checkpoint_signal();
e6c019d0 3325 while (read_next_command() != EOF) {
97313bef 3326 const char *v;
e6c019d0 3327 if (!strcmp("blob", command_buf.buf))
b3031781 3328 parse_new_blob();
97313bef
JK
3329 else if (skip_prefix(command_buf.buf, "commit ", &v))
3330 parse_new_commit(v);
3331 else if (skip_prefix(command_buf.buf, "tag ", &v))
3332 parse_new_tag(v);
3333 else if (skip_prefix(command_buf.buf, "reset ", &v))
3334 parse_reset_branch(v);
5056bb76
EN
3335 else if (skip_prefix(command_buf.buf, "ls ", &v))
3336 parse_ls(v, NULL);
7ffde293
EN
3337 else if (skip_prefix(command_buf.buf, "cat-blob ", &v))
3338 parse_cat_blob(v);
cf7b857a
EN
3339 else if (skip_prefix(command_buf.buf, "get-mark ", &v))
3340 parse_get_mark(v);
7bfe6e26 3341 else if (!strcmp("checkpoint", command_buf.buf))
b3031781 3342 parse_checkpoint();
be56862f
SR
3343 else if (!strcmp("done", command_buf.buf))
3344 break;
59556548 3345 else if (starts_with(command_buf.buf, "progress "))
b3031781 3346 parse_progress();
97313bef
JK
3347 else if (skip_prefix(command_buf.buf, "feature ", &v))
3348 parse_feature(v);
3349 else if (skip_prefix(command_buf.buf, "option git ", &v))
3350 parse_option(v);
59556548 3351 else if (starts_with(command_buf.buf, "option "))
9c8398f0 3352 /* ignore non-git options*/;
c44cdc7e
SP
3353 else
3354 die("Unsupported command: %s", command_buf.buf);
dc01f59d
JN
3355
3356 if (checkpoint_requested)
3357 checkpoint();
db5e523f 3358 }
9c8398f0
SR
3359
3360 /* argv hasn't been parsed yet, do so */
3361 if (!seen_data_command)
3362 parse_argv();
3363
be56862f
SR
3364 if (require_explicit_termination && feof(stdin))
3365 die("stream ends early");
3366
f70b6534 3367 end_packfile();
c44cdc7e 3368
463acbe1 3369 dump_branches();
72303d44 3370 dump_tags();
8455e484 3371 unkeep_all_packs();
a6a1a831 3372 dump_marks();
8bcce301 3373
bdf1c06d
SP
3374 if (pack_edges)
3375 fclose(pack_edges);
3376
c499d768
SP
3377 if (show_stats) {
3378 uintmax_t total_count = 0, duplicate_count = 0;
3379 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
3380 total_count += object_count_by_type[i];
3381 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
3382 duplicate_count += duplicate_count_by_type[i];
3383
3384 fprintf(stderr, "%s statistics:\n", argv[0]);
3385 fprintf(stderr, "---------------------------------------------------------------------\n");
3efb1f34
JR
3386 fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
3387 fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
94c3b482
DI
3388 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]);
3389 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]);
3390 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]);
3391 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]);
c499d768 3392 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
3efb1f34 3393 fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
c499d768 3394 fprintf(stderr, " atoms: %10u\n", atom_cnt);
96c47d14
JM
3395 fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (tree_entry_allocd + fi_mem_pool.pool_alloc + alloc_count*sizeof(struct object_entry))/1024);
3396 fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd + fi_mem_pool.pool_alloc) /1024));
3efb1f34 3397 fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
c499d768
SP
3398 fprintf(stderr, "---------------------------------------------------------------------\n");
3399 pack_report();
3400 fprintf(stderr, "---------------------------------------------------------------------\n");
3401 fprintf(stderr, "\n");
3402 }
db5e523f 3403
7073e69e 3404 return failure ? 1 : 0;
db5e523f 3405}