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