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