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