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