]> git.ipfire.org Git - thirdparty/git.git/blame - fast-import.c
Include recent command history in fast-import crash reports
[thirdparty/git.git] / fast-import.c
CommitLineData
463acbe1
SP
1/*
2Format of STDIN stream:
3
4 stream ::= cmd*;
5
6 cmd ::= new_blob
c44cdc7e 7 | new_commit
463acbe1 8 | new_tag
5fced8dc 9 | reset_branch
8c1f22da 10 | checkpoint
ac053c02 11 | progress
463acbe1
SP
12 ;
13
c44cdc7e 14 new_blob ::= 'blob' lf
c905e090 15 mark?
c44cdc7e
SP
16 file_content;
17 file_content ::= data;
463acbe1 18
c44cdc7e 19 new_commit ::= 'commit' sp ref_str lf
00e2b884 20 mark?
63e0c8b3
SP
21 ('author' sp name '<' email '>' when lf)?
22 'committer' sp name '<' email '>' when lf
00e2b884 23 commit_msg
02f3389d 24 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
62b6f483 25 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
c44cdc7e 26 file_change*
1fdb649c 27 lf?;
c44cdc7e 28 commit_msg ::= data;
463acbe1 29
b6f3481b
SP
30 file_change ::= file_clr
31 | file_del
32 | file_rnm
33 | file_cpy
34 | file_obm
35 | file_inm;
825769a8 36 file_clr ::= 'deleteall' lf;
b715cfbb 37 file_del ::= 'D' sp path_str lf;
f39a946a 38 file_rnm ::= 'R' sp path_str sp path_str lf;
b6f3481b 39 file_cpy ::= 'C' sp path_str sp path_str lf;
b715cfbb
SP
40 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
41 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
42 data;
c44cdc7e
SP
43
44 new_tag ::= 'tag' sp tag_str lf
45 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
c905e090 46 'tagger' sp name '<' email '>' when lf
c44cdc7e
SP
47 tag_msg;
48 tag_msg ::= data;
49
9938ffc5
SP
50 reset_branch ::= 'reset' sp ref_str lf
51 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
1fdb649c 52 lf?;
5fced8dc 53
7bfe6e26 54 checkpoint ::= 'checkpoint' lf
1fdb649c 55 lf?;
7bfe6e26 56
ac053c02
SP
57 progress ::= 'progress' sp not_lf* lf
58 lf?;
59
c44cdc7e
SP
60 # note: the first idnum in a stream should be 1 and subsequent
61 # idnums should not have gaps between values as this will cause
62 # the stream parser to reserve space for the gapped values. An
c905e090 63 # idnum can be updated in the future to a new object by issuing
c44cdc7e 64 # a new mark directive with the old idnum.
c905e090 65 #
c44cdc7e 66 mark ::= 'mark' sp idnum lf;
3b4dce02 67 data ::= (delimited_data | exact_data)
2c570cde 68 lf?;
3b4dce02
SP
69
70 # note: delim may be any string but must not contain lf.
71 # data_line may contain any data but must not be exactly
72 # delim.
73 delimited_data ::= 'data' sp '<<' delim lf
74 (data_line lf)*
c905e090 75 delim lf;
c44cdc7e
SP
76
77 # note: declen indicates the length of binary_data in bytes.
3b4dce02 78 # declen does not include the lf preceeding the binary data.
c44cdc7e 79 #
3b4dce02
SP
80 exact_data ::= 'data' sp declen lf
81 binary_data;
c44cdc7e
SP
82
83 # note: quoted strings are C-style quoting supporting \c for
84 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
c905e090 85 # is the signed byte value in octal. Note that the only
c44cdc7e
SP
86 # characters which must actually be escaped to protect the
87 # stream formatting is: \, " and LF. Otherwise these values
c905e090 88 # are UTF8.
c44cdc7e 89 #
6c3aac1c
SP
90 ref_str ::= ref;
91 sha1exp_str ::= sha1exp;
92 tag_str ::= tag;
c44cdc7e 93 path_str ::= path | '"' quoted(path) '"' ;
b715cfbb
SP
94 mode ::= '100644' | '644'
95 | '100755' | '755'
9981b6d9 96 | '120000'
b715cfbb 97 ;
c44cdc7e
SP
98
99 declen ::= # unsigned 32 bit value, ascii base10 notation;
2104838b 100 bigint ::= # unsigned integer value, ascii base10 notation;
463acbe1 101 binary_data ::= # file content, not interpreted;
c44cdc7e 102
63e0c8b3
SP
103 when ::= raw_when | rfc2822_when;
104 raw_when ::= ts sp tz;
105 rfc2822_when ::= # Valid RFC 2822 date and time;
106
463acbe1
SP
107 sp ::= # ASCII space character;
108 lf ::= # ASCII newline (LF) character;
c44cdc7e
SP
109
110 # note: a colon (':') must precede the numerical value assigned to
c905e090 111 # an idnum. This is to distinguish it from a ref or tag name as
c44cdc7e 112 # GIT does not permit ':' in ref or tag strings.
c905e090 113 #
2104838b 114 idnum ::= ':' bigint;
c44cdc7e
SP
115 path ::= # GIT style file path, e.g. "a/b/c";
116 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
117 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
463acbe1
SP
118 sha1exp ::= # Any valid GIT SHA1 expression;
119 hexsha1 ::= # SHA1 in hexadecimal format;
c44cdc7e
SP
120
121 # note: name and email are UTF8 strings, however name must not
c905e090 122 # contain '<' or lf and email must not contain any of the
c44cdc7e 123 # following: '<', '>', lf.
c905e090 124 #
c44cdc7e 125 name ::= # valid GIT author/committer name;
463acbe1 126 email ::= # valid GIT author/committer email;
c44cdc7e
SP
127 ts ::= # time since the epoch in seconds, ascii base10 notation;
128 tz ::= # GIT style timezone;
401d53fa
SP
129
130 # note: comments may appear anywhere in the input, except
131 # within a data command. Any form of the data command
132 # always escapes the related input from comment processing.
133 #
134 # In case it is not clear, the '#' that starts the comment
135 # must be the first character on that the line (an lf have
136 # preceeded it).
137 #
138 comment ::= '#' not_lf* lf;
139 not_lf ::= # Any byte that is not ASCII newline (LF);
463acbe1
SP
140*/
141
db5e523f
SP
142#include "builtin.h"
143#include "cache.h"
144#include "object.h"
145#include "blob.h"
463acbe1 146#include "tree.h"
7073e69e 147#include "commit.h"
db5e523f
SP
148#include "delta.h"
149#include "pack.h"
463acbe1 150#include "refs.h"
db5e523f 151#include "csum-file.h"
c44cdc7e
SP
152#include "strbuf.h"
153#include "quote.h"
db5e523f 154
69e74e74
SP
155#define PACK_ID_BITS 16
156#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
157
27d6d290
SP
158struct object_entry
159{
160 struct object_entry *next;
10831c55 161 uint32_t offset;
03842d8e 162 unsigned type : TYPE_BITS;
69e74e74 163 unsigned pack_id : PACK_ID_BITS;
27d6d290
SP
164 unsigned char sha1[20];
165};
166
463acbe1 167struct object_entry_pool
27d6d290 168{
463acbe1 169 struct object_entry_pool *next_pool;
27d6d290
SP
170 struct object_entry *next_free;
171 struct object_entry *end;
ac47a738 172 struct object_entry entries[FLEX_ARRAY]; /* more */
27d6d290
SP
173};
174
d8397168
SP
175struct mark_set
176{
d8397168
SP
177 union {
178 struct object_entry *marked[1024];
179 struct mark_set *sets[1024];
180 } data;
6f64f6d9 181 unsigned int shift;
d8397168
SP
182};
183
ac47a738
SP
184struct last_object
185{
186 void *data;
4cabf858 187 unsigned long len;
10831c55 188 uint32_t offset;
6bb5b329 189 unsigned int depth;
d489bc14 190 unsigned no_free:1;
ac47a738
SP
191};
192
463acbe1
SP
193struct mem_pool
194{
195 struct mem_pool *next_pool;
196 char *next_free;
197 char *end;
198 char space[FLEX_ARRAY]; /* more */
199};
200
201struct atom_str
202{
203 struct atom_str *next_atom;
10831c55 204 unsigned short str_len;
463acbe1
SP
205 char str_dat[FLEX_ARRAY]; /* more */
206};
207
208struct tree_content;
6bb5b329
SP
209struct tree_entry
210{
463acbe1
SP
211 struct tree_content *tree;
212 struct atom_str* name;
4cabf858
SP
213 struct tree_entry_ms
214 {
10831c55 215 uint16_t mode;
4cabf858
SP
216 unsigned char sha1[20];
217 } versions[2];
6bb5b329
SP
218};
219
463acbe1 220struct tree_content
6bb5b329 221{
463acbe1
SP
222 unsigned int entry_capacity; /* must match avail_tree_content */
223 unsigned int entry_count;
4cabf858 224 unsigned int delta_depth;
463acbe1
SP
225 struct tree_entry *entries[FLEX_ARRAY]; /* more */
226};
227
228struct avail_tree_content
229{
230 unsigned int entry_capacity; /* must match tree_content */
231 struct avail_tree_content *next_avail;
6bb5b329
SP
232};
233
234struct branch
235{
463acbe1
SP
236 struct branch *table_next_branch;
237 struct branch *active_next_branch;
6bb5b329 238 const char *name;
463acbe1 239 struct tree_entry branch_tree;
69e74e74 240 uintmax_t last_commit;
734c91f9
SP
241 unsigned active : 1;
242 unsigned pack_id : PACK_ID_BITS;
463acbe1 243 unsigned char sha1[20];
6bb5b329
SP
244};
245
72303d44
SP
246struct tag
247{
248 struct tag *next_tag;
249 const char *name;
2369ed79 250 unsigned int pack_id;
72303d44
SP
251 unsigned char sha1[20];
252};
253
e2eb469d
SP
254struct dbuf
255{
256 void *buffer;
257 size_t capacity;
258};
259
62b6f483
SP
260struct hash_list
261{
262 struct hash_list *next;
263 unsigned char sha1[20];
264};
463acbe1 265
63e0c8b3
SP
266typedef enum {
267 WHENSPEC_RAW = 1,
268 WHENSPEC_RFC2822,
269 WHENSPEC_NOW,
270} whenspec_type;
271
904b1941
SP
272struct recent_command
273{
274 struct recent_command *prev;
275 struct recent_command *next;
276 char *buf;
277};
278
0ea9f045 279/* Configured limits on output */
d5c57b28 280static unsigned long max_depth = 10;
6777a59f 281static off_t max_packsize = (1LL << 32) - 1;
7073e69e 282static int force_update;
0ea9f045
SP
283
284/* Stats and misc. counters */
285static uintmax_t alloc_count;
0ea9f045
SP
286static uintmax_t marks_set_count;
287static uintmax_t object_count_by_type[1 << TYPE_BITS];
288static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
289static uintmax_t delta_count_by_type[1 << TYPE_BITS];
a7ddc487 290static unsigned long object_count;
6bb5b329 291static unsigned long branch_count;
d6c7eb2c 292static unsigned long branch_load_count;
7073e69e 293static int failure;
bdf1c06d 294static FILE *pack_edges;
ac47a738 295
463acbe1
SP
296/* Memory pools */
297static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
298static size_t total_allocd;
299static struct mem_pool *mem_pool;
300
c44cdc7e 301/* Atom management */
463acbe1
SP
302static unsigned int atom_table_sz = 4451;
303static unsigned int atom_cnt;
304static struct atom_str **atom_table;
305
306/* The .pack file being generated */
7bfe6e26 307static unsigned int pack_id;
d489bc14 308static struct packed_git *pack_data;
7bfe6e26 309static struct packed_git **all_packs;
41e5257f 310static unsigned long pack_size;
ac47a738
SP
311
312/* Table of objects we've written. */
4cabf858 313static unsigned int object_entry_alloc = 5000;
463acbe1
SP
314static struct object_entry_pool *blocks;
315static struct object_entry *object_table[1 << 16];
d8397168 316static struct mark_set *marks;
a6a1a831 317static const char* mark_file;
ac47a738
SP
318
319/* Our last blob */
463acbe1
SP
320static struct last_object last_blob;
321
322/* Tree management */
323static unsigned int tree_entry_alloc = 1000;
324static void *avail_tree_entry;
325static unsigned int avail_tree_table_sz = 100;
326static struct avail_tree_content **avail_tree_table;
e2eb469d
SP
327static struct dbuf old_tree;
328static struct dbuf new_tree;
8bcce301 329
6bb5b329 330/* Branch data */
d5c57b28
SP
331static unsigned long max_active_branches = 5;
332static unsigned long cur_active_branches;
333static unsigned long branch_table_sz = 1039;
463acbe1
SP
334static struct branch **branch_table;
335static struct branch *active_branches;
336
72303d44
SP
337/* Tag data */
338static struct tag *first_tag;
339static struct tag *last_tag;
340
c44cdc7e 341/* Input stream parsing */
63e0c8b3 342static whenspec_type whenspec = WHENSPEC_RAW;
c44cdc7e 343static struct strbuf command_buf;
1fdb649c 344static int unread_command_buf;
904b1941
SP
345static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
346static struct recent_command *cmd_tail = &cmd_hist;
347static struct recent_command *rc_free;
348static unsigned int cmd_save = 100;
0ea9f045 349static uintmax_t next_mark;
243f801d 350static struct dbuf new_data;
c44cdc7e 351
8acb3297
SP
352static void write_branch_report(FILE *rpt, struct branch *b)
353{
354 fprintf(rpt, "%s:\n", b->name);
355
356 fprintf(rpt, " status :");
357 if (b->active)
358 fputs(" active", rpt);
359 if (b->branch_tree.tree)
360 fputs(" loaded", rpt);
361 if (is_null_sha1(b->branch_tree.versions[1].sha1))
362 fputs(" dirty", rpt);
363 fputc('\n', rpt);
364
365 fprintf(rpt, " tip commit : %s\n", sha1_to_hex(b->sha1));
366 fprintf(rpt, " old tree : %s\n", sha1_to_hex(b->branch_tree.versions[0].sha1));
367 fprintf(rpt, " cur tree : %s\n", sha1_to_hex(b->branch_tree.versions[1].sha1));
368 fprintf(rpt, " commit clock: %" PRIuMAX "\n", b->last_commit);
369
370 fputs(" last pack : ", rpt);
371 if (b->pack_id < MAX_PACK_ID)
372 fprintf(rpt, "%u", b->pack_id);
373 fputc('\n', rpt);
374
375 fputc('\n', rpt);
376}
377
378static void write_crash_report(const char *err, va_list params)
379{
380 char *loc = git_path("fast_import_crash_%d", getpid());
381 FILE *rpt = fopen(loc, "w");
382 struct branch *b;
383 unsigned long lu;
904b1941 384 struct recent_command *rc;
8acb3297
SP
385
386 if (!rpt) {
387 error("can't write crash report %s: %s", loc, strerror(errno));
388 return;
389 }
390
391 fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
392
393 fprintf(rpt, "fast-import crash report:\n");
394 fprintf(rpt, " fast-import process: %d\n", getpid());
395 fprintf(rpt, " parent process : %d\n", getppid());
396 fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_LOCAL));
397 fputc('\n', rpt);
398
399 fputs("fatal: ", rpt);
400 vfprintf(rpt, err, params);
401 fputc('\n', rpt);
402
904b1941
SP
403 fputc('\n', rpt);
404 fputs("Most Recent Commands Before Crash\n", rpt);
405 fputs("---------------------------------\n", rpt);
406 for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
407 if (rc->next == &cmd_hist)
408 fputs("* ", rpt);
409 else
410 fputs(" ", rpt);
411 fputs(rc->buf, rpt);
412 fputc('\n', rpt);
413 }
414
8acb3297
SP
415 fputc('\n', rpt);
416 fputs("Active Branch LRU\n", rpt);
417 fputs("-----------------\n", rpt);
418 fprintf(rpt, " active_branches = %lu cur, %lu max\n",
419 cur_active_branches,
420 max_active_branches);
421 fputc('\n', rpt);
422 fputs(" pos clock name\n", rpt);
423 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt);
424 for (b = active_branches, lu = 0; b; b = b->active_next_branch)
425 fprintf(rpt, " %2lu) %6" PRIuMAX" %s\n",
426 ++lu, b->last_commit, b->name);
427
428 fputc('\n', rpt);
429 fputs("Inactive Branches\n", rpt);
430 fputs("-----------------\n", rpt);
431 for (lu = 0; lu < branch_table_sz; lu++) {
432 for (b = branch_table[lu]; b; b = b->table_next_branch)
433 write_branch_report(rpt, b);
434 }
435
436 fputc('\n', rpt);
437 fputs("-------------------\n", rpt);
438 fputs("END OF CRASH REPORT\n", rpt);
439 fclose(rpt);
440}
441
442static NORETURN void die_nicely(const char *err, va_list params)
443{
444 static int zombie;
445
446 fputs("fatal: ", stderr);
447 vfprintf(stderr, err, params);
448 fputc('\n', stderr);
449
450 if (!zombie) {
451 zombie = 1;
452 write_crash_report(err, params);
453 }
454
455 exit(128);
456}
6bb5b329 457
03842d8e 458static void alloc_objects(unsigned int cnt)
8bcce301 459{
463acbe1 460 struct object_entry_pool *b;
27d6d290 461
463acbe1 462 b = xmalloc(sizeof(struct object_entry_pool)
27d6d290 463 + cnt * sizeof(struct object_entry));
463acbe1 464 b->next_pool = blocks;
27d6d290
SP
465 b->next_free = b->entries;
466 b->end = b->entries + cnt;
467 blocks = b;
468 alloc_count += cnt;
469}
8bcce301 470
e5b1444b 471static struct object_entry *new_object(unsigned char *sha1)
8bcce301 472{
27d6d290 473 struct object_entry *e;
8bcce301 474
27d6d290 475 if (blocks->next_free == blocks->end)
463acbe1 476 alloc_objects(object_entry_alloc);
8bcce301 477
27d6d290 478 e = blocks->next_free++;
445b8599 479 hashcpy(e->sha1, sha1);
27d6d290 480 return e;
8bcce301
SP
481}
482
e5b1444b 483static struct object_entry *find_object(unsigned char *sha1)
463acbe1
SP
484{
485 unsigned int h = sha1[0] << 8 | sha1[1];
486 struct object_entry *e;
487 for (e = object_table[h]; e; e = e->next)
445b8599 488 if (!hashcmp(sha1, e->sha1))
463acbe1
SP
489 return e;
490 return NULL;
491}
492
e5b1444b 493static struct object_entry *insert_object(unsigned char *sha1)
8bcce301
SP
494{
495 unsigned int h = sha1[0] << 8 | sha1[1];
27d6d290 496 struct object_entry *e = object_table[h];
463acbe1 497 struct object_entry *p = NULL;
8bcce301
SP
498
499 while (e) {
445b8599 500 if (!hashcmp(sha1, e->sha1))
8bcce301
SP
501 return e;
502 p = e;
503 e = e->next;
504 }
505
506 e = new_object(sha1);
463acbe1 507 e->next = NULL;
8bcce301
SP
508 e->offset = 0;
509 if (p)
510 p->next = e;
511 else
27d6d290 512 object_table[h] = e;
8bcce301
SP
513 return e;
514}
db5e523f 515
463acbe1
SP
516static unsigned int hc_str(const char *s, size_t len)
517{
518 unsigned int r = 0;
519 while (len-- > 0)
520 r = r * 31 + *s++;
521 return r;
522}
523
e5b1444b 524static void *pool_alloc(size_t len)
463acbe1
SP
525{
526 struct mem_pool *p;
527 void *r;
528
529 for (p = mem_pool; p; p = p->next_pool)
530 if ((p->end - p->next_free >= len))
531 break;
532
533 if (!p) {
534 if (len >= (mem_pool_alloc/2)) {
535 total_allocd += len;
536 return xmalloc(len);
537 }
538 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
539 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
540 p->next_pool = mem_pool;
541 p->next_free = p->space;
542 p->end = p->next_free + mem_pool_alloc;
543 mem_pool = p;
544 }
545
546 r = p->next_free;
8d8928b0
SP
547 /* round out to a pointer alignment */
548 if (len & (sizeof(void*) - 1))
549 len += sizeof(void*) - (len & (sizeof(void*) - 1));
463acbe1
SP
550 p->next_free += len;
551 return r;
552}
553
e5b1444b 554static void *pool_calloc(size_t count, size_t size)
463acbe1
SP
555{
556 size_t len = count * size;
557 void *r = pool_alloc(len);
558 memset(r, 0, len);
559 return r;
560}
561
e5b1444b 562static char *pool_strdup(const char *s)
463acbe1
SP
563{
564 char *r = pool_alloc(strlen(s) + 1);
565 strcpy(r, s);
566 return r;
567}
568
243f801d
SP
569static void size_dbuf(struct dbuf *b, size_t maxlen)
570{
571 if (b->buffer) {
572 if (b->capacity >= maxlen)
573 return;
574 free(b->buffer);
575 }
576 b->capacity = ((maxlen / 1024) + 1) * 1024;
577 b->buffer = xmalloc(b->capacity);
578}
579
0ea9f045 580static void insert_mark(uintmax_t idnum, struct object_entry *oe)
d8397168
SP
581{
582 struct mark_set *s = marks;
583 while ((idnum >> s->shift) >= 1024) {
584 s = pool_calloc(1, sizeof(struct mark_set));
585 s->shift = marks->shift + 10;
586 s->data.sets[0] = marks;
587 marks = s;
588 }
589 while (s->shift) {
0ea9f045 590 uintmax_t i = idnum >> s->shift;
d8397168
SP
591 idnum -= i << s->shift;
592 if (!s->data.sets[i]) {
593 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
594 s->data.sets[i]->shift = s->shift - 10;
595 }
596 s = s->data.sets[i];
597 }
598 if (!s->data.marked[idnum])
599 marks_set_count++;
600 s->data.marked[idnum] = oe;
601}
602
e5b1444b 603static struct object_entry *find_mark(uintmax_t idnum)
d8397168 604{
0ea9f045 605 uintmax_t orig_idnum = idnum;
d8397168
SP
606 struct mark_set *s = marks;
607 struct object_entry *oe = NULL;
608 if ((idnum >> s->shift) < 1024) {
609 while (s && s->shift) {
0ea9f045 610 uintmax_t i = idnum >> s->shift;
d8397168
SP
611 idnum -= i << s->shift;
612 s = s->data.sets[i];
613 }
614 if (s)
615 oe = s->data.marked[idnum];
616 }
617 if (!oe)
3efb1f34 618 die("mark :%" PRIuMAX " not declared", orig_idnum);
d8397168
SP
619 return oe;
620}
621
e5b1444b 622static struct atom_str *to_atom(const char *s, unsigned short len)
463acbe1
SP
623{
624 unsigned int hc = hc_str(s, len) % atom_table_sz;
625 struct atom_str *c;
626
627 for (c = atom_table[hc]; c; c = c->next_atom)
628 if (c->str_len == len && !strncmp(s, c->str_dat, len))
629 return c;
630
631 c = pool_alloc(sizeof(struct atom_str) + len + 1);
632 c->str_len = len;
633 strncpy(c->str_dat, s, len);
634 c->str_dat[len] = 0;
635 c->next_atom = atom_table[hc];
636 atom_table[hc] = c;
637 atom_cnt++;
638 return c;
639}
640
e5b1444b 641static struct branch *lookup_branch(const char *name)
463acbe1
SP
642{
643 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
644 struct branch *b;
645
646 for (b = branch_table[hc]; b; b = b->table_next_branch)
647 if (!strcmp(name, b->name))
648 return b;
649 return NULL;
650}
651
e5b1444b 652static struct branch *new_branch(const char *name)
463acbe1
SP
653{
654 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
655 struct branch* b = lookup_branch(name);
656
657 if (b)
658 die("Invalid attempt to create duplicate branch: %s", name);
ea08a6fd
SP
659 switch (check_ref_format(name)) {
660 case 0: break; /* its valid */
661 case -2: break; /* valid, but too few '/', allow anyway */
662 default:
c44cdc7e 663 die("Branch name doesn't conform to GIT standards: %s", name);
ea08a6fd 664 }
463acbe1
SP
665
666 b = pool_calloc(1, sizeof(struct branch));
667 b->name = pool_strdup(name);
668 b->table_next_branch = branch_table[hc];
8a8c55ea
SP
669 b->branch_tree.versions[0].mode = S_IFDIR;
670 b->branch_tree.versions[1].mode = S_IFDIR;
734c91f9 671 b->active = 0;
69e74e74 672 b->pack_id = MAX_PACK_ID;
463acbe1
SP
673 branch_table[hc] = b;
674 branch_count++;
675 return b;
676}
677
678static unsigned int hc_entries(unsigned int cnt)
679{
680 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
681 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
682}
683
e5b1444b 684static struct tree_content *new_tree_content(unsigned int cnt)
463acbe1
SP
685{
686 struct avail_tree_content *f, *l = NULL;
687 struct tree_content *t;
688 unsigned int hc = hc_entries(cnt);
689
690 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
691 if (f->entry_capacity >= cnt)
692 break;
693
694 if (f) {
695 if (l)
696 l->next_avail = f->next_avail;
697 else
698 avail_tree_table[hc] = f->next_avail;
699 } else {
700 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
701 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
702 f->entry_capacity = cnt;
703 }
704
705 t = (struct tree_content*)f;
706 t->entry_count = 0;
4cabf858 707 t->delta_depth = 0;
463acbe1
SP
708 return t;
709}
710
711static void release_tree_entry(struct tree_entry *e);
712static void release_tree_content(struct tree_content *t)
713{
714 struct avail_tree_content *f = (struct avail_tree_content*)t;
715 unsigned int hc = hc_entries(f->entry_capacity);
afde8dd9
SP
716 f->next_avail = avail_tree_table[hc];
717 avail_tree_table[hc] = f;
718}
719
720static void release_tree_content_recursive(struct tree_content *t)
721{
463acbe1
SP
722 unsigned int i;
723 for (i = 0; i < t->entry_count; i++)
724 release_tree_entry(t->entries[i]);
afde8dd9 725 release_tree_content(t);
463acbe1
SP
726}
727
e5b1444b 728static struct tree_content *grow_tree_content(
463acbe1
SP
729 struct tree_content *t,
730 int amt)
731{
732 struct tree_content *r = new_tree_content(t->entry_count + amt);
733 r->entry_count = t->entry_count;
4cabf858 734 r->delta_depth = t->delta_depth;
463acbe1
SP
735 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
736 release_tree_content(t);
737 return r;
738}
739
e5b1444b 740static struct tree_entry *new_tree_entry(void)
463acbe1
SP
741{
742 struct tree_entry *e;
743
744 if (!avail_tree_entry) {
745 unsigned int n = tree_entry_alloc;
8435a9cb 746 total_allocd += n * sizeof(struct tree_entry);
463acbe1 747 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
2eb26d84 748 while (n-- > 1) {
463acbe1
SP
749 *((void**)e) = e + 1;
750 e++;
751 }
35ef237c 752 *((void**)e) = NULL;
463acbe1
SP
753 }
754
755 e = avail_tree_entry;
756 avail_tree_entry = *((void**)e);
757 return e;
758}
759
760static void release_tree_entry(struct tree_entry *e)
761{
762 if (e->tree)
afde8dd9 763 release_tree_content_recursive(e->tree);
463acbe1
SP
764 *((void**)e) = avail_tree_entry;
765 avail_tree_entry = e;
766}
767
b6f3481b
SP
768static struct tree_content *dup_tree_content(struct tree_content *s)
769{
770 struct tree_content *d;
771 struct tree_entry *a, *b;
772 unsigned int i;
773
774 if (!s)
775 return NULL;
776 d = new_tree_content(s->entry_count);
777 for (i = 0; i < s->entry_count; i++) {
778 a = s->entries[i];
779 b = new_tree_entry();
780 memcpy(b, a, sizeof(*a));
781 if (a->tree && is_null_sha1(b->versions[1].sha1))
782 b->tree = dup_tree_content(a->tree);
783 else
784 b->tree = NULL;
785 d->entries[i] = b;
786 }
787 d->entry_count = s->entry_count;
788 d->delta_depth = s->delta_depth;
789
790 return d;
791}
792
fd99224e 793static void start_packfile(void)
f70b6534 794{
8455e484 795 static char tmpfile[PATH_MAX];
7bfe6e26 796 struct packed_git *p;
f70b6534 797 struct pack_header hdr;
0fcbcae7 798 int pack_fd;
f70b6534 799
8455e484 800 snprintf(tmpfile, sizeof(tmpfile),
0e55181f 801 "%s/tmp_pack_XXXXXX", get_object_directory());
7647b17f 802 pack_fd = xmkstemp(tmpfile);
8455e484
SP
803 p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
804 strcpy(p->pack_name, tmpfile);
7bfe6e26 805 p->pack_fd = pack_fd;
f70b6534
SP
806
807 hdr.hdr_signature = htonl(PACK_SIGNATURE);
808 hdr.hdr_version = htonl(2);
809 hdr.hdr_entries = 0;
0fcbcae7 810 write_or_die(p->pack_fd, &hdr, sizeof(hdr));
7bfe6e26
SP
811
812 pack_data = p;
f70b6534
SP
813 pack_size = sizeof(hdr);
814 object_count = 0;
7bfe6e26
SP
815
816 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
817 all_packs[pack_id] = p;
f70b6534
SP
818}
819
f70b6534
SP
820static int oecmp (const void *a_, const void *b_)
821{
822 struct object_entry *a = *((struct object_entry**)a_);
823 struct object_entry *b = *((struct object_entry**)b_);
824 return hashcmp(a->sha1, b->sha1);
825}
826
e5b1444b 827static char *create_index(void)
f70b6534 828{
8455e484
SP
829 static char tmpfile[PATH_MAX];
830 SHA_CTX ctx;
f70b6534
SP
831 struct sha1file *f;
832 struct object_entry **idx, **c, **last, *e;
833 struct object_entry_pool *o;
ebea9dd4 834 uint32_t array[256];
8455e484 835 int i, idx_fd;
f70b6534
SP
836
837 /* Build the sorted table of object IDs. */
838 idx = xmalloc(object_count * sizeof(struct object_entry*));
839 c = idx;
840 for (o = blocks; o; o = o->next_pool)
d9ee53ce
SP
841 for (e = o->next_free; e-- != o->entries;)
842 if (pack_id == e->pack_id)
843 *c++ = e;
f70b6534 844 last = idx + object_count;
2fce1f3c
SP
845 if (c != last)
846 die("internal consistency error creating the index");
f70b6534
SP
847 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
848
849 /* Generate the fan-out array. */
850 c = idx;
851 for (i = 0; i < 256; i++) {
852 struct object_entry **next = c;;
853 while (next < last) {
854 if ((*next)->sha1[0] != i)
855 break;
856 next++;
857 }
858 array[i] = htonl(next - idx);
859 c = next;
860 }
861
8455e484 862 snprintf(tmpfile, sizeof(tmpfile),
0e55181f 863 "%s/tmp_idx_XXXXXX", get_object_directory());
7647b17f 864 idx_fd = xmkstemp(tmpfile);
8455e484 865 f = sha1fd(idx_fd, tmpfile);
f70b6534 866 sha1write(f, array, 256 * sizeof(int));
8455e484 867 SHA1_Init(&ctx);
f70b6534 868 for (c = idx; c != last; c++) {
ebea9dd4 869 uint32_t offset = htonl((*c)->offset);
f70b6534
SP
870 sha1write(f, &offset, 4);
871 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
8455e484 872 SHA1_Update(&ctx, (*c)->sha1, 20);
f70b6534 873 }
09543c96 874 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
8455e484 875 sha1close(f, NULL, 1);
f70b6534 876 free(idx);
8455e484
SP
877 SHA1_Final(pack_data->sha1, &ctx);
878 return tmpfile;
879}
880
e5b1444b 881static char *keep_pack(char *curr_index_name)
8455e484
SP
882{
883 static char name[PATH_MAX];
3a55602e 884 static const char *keep_msg = "fast-import";
8455e484
SP
885 int keep_fd;
886
887 chmod(pack_data->pack_name, 0444);
888 chmod(curr_index_name, 0444);
889
890 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
891 get_object_directory(), sha1_to_hex(pack_data->sha1));
892 keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
893 if (keep_fd < 0)
894 die("cannot create keep file");
895 write(keep_fd, keep_msg, strlen(keep_msg));
896 close(keep_fd);
897
898 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
899 get_object_directory(), sha1_to_hex(pack_data->sha1));
900 if (move_temp_to_file(pack_data->pack_name, name))
901 die("cannot store pack file");
8455e484
SP
902
903 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
904 get_object_directory(), sha1_to_hex(pack_data->sha1));
905 if (move_temp_to_file(curr_index_name, name))
906 die("cannot store index file");
907 return name;
908}
909
fd99224e 910static void unkeep_all_packs(void)
8455e484
SP
911{
912 static char name[PATH_MAX];
913 int k;
914
915 for (k = 0; k < pack_id; k++) {
916 struct packed_git *p = all_packs[k];
917 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
918 get_object_directory(), sha1_to_hex(p->sha1));
919 unlink(name);
920 }
f70b6534
SP
921}
922
fd99224e 923static void end_packfile(void)
f70b6534 924{
7bfe6e26
SP
925 struct packed_git *old_p = pack_data, *new_p;
926
3e005baf 927 if (object_count) {
8455e484 928 char *idx_name;
2369ed79
SP
929 int i;
930 struct branch *b;
931 struct tag *t;
8455e484 932
8b0eca7c
DH
933 fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
934 pack_data->pack_name, object_count);
935 close(pack_data->pack_fd);
8455e484 936 idx_name = keep_pack(create_index());
3e005baf
SP
937
938 /* Register the packfile with core git's machinary. */
939 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
940 if (!new_p)
941 die("core git rejected index %s", idx_name);
942 new_p->windows = old_p->windows;
2369ed79 943 all_packs[pack_id] = new_p;
3e005baf 944 install_packed_git(new_p);
2369ed79
SP
945
946 /* Print the boundary */
bdf1c06d
SP
947 if (pack_edges) {
948 fprintf(pack_edges, "%s:", new_p->pack_name);
949 for (i = 0; i < branch_table_sz; i++) {
950 for (b = branch_table[i]; b; b = b->table_next_branch) {
951 if (b->pack_id == pack_id)
952 fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
953 }
2369ed79 954 }
bdf1c06d
SP
955 for (t = first_tag; t; t = t->next_tag) {
956 if (t->pack_id == pack_id)
957 fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
958 }
959 fputc('\n', pack_edges);
960 fflush(pack_edges);
2369ed79 961 }
2369ed79
SP
962
963 pack_id++;
3e005baf 964 }
12801587 965 else
3e005baf 966 unlink(old_p->pack_name);
7bfe6e26 967 free(old_p);
7bfe6e26
SP
968
969 /* We can't carry a delta across packfiles. */
970 free(last_blob.data);
971 last_blob.data = NULL;
972 last_blob.len = 0;
973 last_blob.offset = 0;
974 last_blob.depth = 0;
f70b6534
SP
975}
976
820b9310 977static void cycle_packfile(void)
d9ee53ce
SP
978{
979 end_packfile();
980 start_packfile();
981}
982
c44cdc7e 983static size_t encode_header(
ac47a738 984 enum object_type type,
c44cdc7e 985 size_t size,
ac47a738 986 unsigned char *hdr)
db5e523f
SP
987{
988 int n = 1;
989 unsigned char c;
990
1fcdd62a 991 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
db5e523f
SP
992 die("bad type %d", type);
993
994 c = (type << 4) | (size & 15);
995 size >>= 4;
996 while (size) {
997 *hdr++ = c | 0x80;
998 c = size & 0x7f;
999 size >>= 7;
1000 n++;
1001 }
1002 *hdr = c;
1003 return n;
1004}
1005
ac47a738
SP
1006static int store_object(
1007 enum object_type type,
1008 void *dat,
c44cdc7e 1009 size_t datlen,
6bb5b329 1010 struct last_object *last,
d8397168 1011 unsigned char *sha1out,
0ea9f045 1012 uintmax_t mark)
db5e523f 1013{
db5e523f 1014 void *out, *delta;
ac47a738
SP
1015 struct object_entry *e;
1016 unsigned char hdr[96];
1017 unsigned char sha1[20];
db5e523f 1018 unsigned long hdrlen, deltalen;
ac47a738
SP
1019 SHA_CTX c;
1020 z_stream s;
1021
df843662 1022 hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
40db58b8 1023 (unsigned long)datlen) + 1;
ac47a738
SP
1024 SHA1_Init(&c);
1025 SHA1_Update(&c, hdr, hdrlen);
1026 SHA1_Update(&c, dat, datlen);
1027 SHA1_Final(sha1, &c);
6bb5b329 1028 if (sha1out)
445b8599 1029 hashcpy(sha1out, sha1);
ac47a738
SP
1030
1031 e = insert_object(sha1);
d8397168
SP
1032 if (mark)
1033 insert_mark(mark, e);
ac47a738 1034 if (e->offset) {
6143f064 1035 duplicate_count_by_type[type]++;
463acbe1 1036 return 1;
a5c1780a
SP
1037 } else if (find_sha1_pack(sha1, packed_git)) {
1038 e->type = type;
1039 e->pack_id = MAX_PACK_ID;
1040 e->offset = 1; /* just not zero! */
1041 duplicate_count_by_type[type]++;
1042 return 1;
ac47a738 1043 }
db5e523f 1044
d9ee53ce 1045 if (last && last->data && last->depth < max_depth) {
ac47a738 1046 delta = diff_delta(last->data, last->len,
db5e523f
SP
1047 dat, datlen,
1048 &deltalen, 0);
d9ee53ce
SP
1049 if (delta && deltalen >= datlen) {
1050 free(delta);
1051 delta = NULL;
1052 }
1053 } else
1054 delta = NULL;
db5e523f
SP
1055
1056 memset(&s, 0, sizeof(s));
1057 deflateInit(&s, zlib_compression_level);
d9ee53ce
SP
1058 if (delta) {
1059 s.next_in = delta;
1060 s.avail_in = deltalen;
1061 } else {
1062 s.next_in = dat;
1063 s.avail_in = datlen;
1064 }
1065 s.avail_out = deflateBound(&s, s.avail_in);
1066 s.next_out = out = xmalloc(s.avail_out);
1067 while (deflate(&s, Z_FINISH) == Z_OK)
1068 /* nothing */;
1069 deflateEnd(&s);
1070
1071 /* Determine if we should auto-checkpoint. */
e5808826 1072 if ((pack_size + 60 + s.total_out) > max_packsize
d9ee53ce
SP
1073 || (pack_size + 60 + s.total_out) < pack_size) {
1074
1075 /* This new object needs to *not* have the current pack_id. */
1076 e->pack_id = pack_id + 1;
820b9310 1077 cycle_packfile();
d9ee53ce
SP
1078
1079 /* We cannot carry a delta into the new pack. */
1080 if (delta) {
1081 free(delta);
1082 delta = NULL;
5d6f3ef6
SP
1083
1084 memset(&s, 0, sizeof(s));
1085 deflateInit(&s, zlib_compression_level);
1086 s.next_in = dat;
1087 s.avail_in = datlen;
1088 s.avail_out = deflateBound(&s, s.avail_in);
1089 s.next_out = out = xrealloc(out, s.avail_out);
1090 while (deflate(&s, Z_FINISH) == Z_OK)
1091 /* nothing */;
1092 deflateEnd(&s);
d9ee53ce 1093 }
d9ee53ce
SP
1094 }
1095
1096 e->type = type;
1097 e->pack_id = pack_id;
1098 e->offset = pack_size;
1099 object_count++;
1100 object_count_by_type[type]++;
db5e523f
SP
1101
1102 if (delta) {
d489bc14
SP
1103 unsigned long ofs = e->offset - last->offset;
1104 unsigned pos = sizeof(hdr) - 1;
1105
4cabf858 1106 delta_count_by_type[type]++;
ac47a738 1107 last->depth++;
d489bc14
SP
1108
1109 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
0fcbcae7 1110 write_or_die(pack_data->pack_fd, hdr, hdrlen);
d489bc14
SP
1111 pack_size += hdrlen;
1112
1113 hdr[pos] = ofs & 127;
1114 while (ofs >>= 7)
1115 hdr[--pos] = 128 | (--ofs & 127);
0fcbcae7 1116 write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
d489bc14 1117 pack_size += sizeof(hdr) - pos;
db5e523f 1118 } else {
463acbe1
SP
1119 if (last)
1120 last->depth = 0;
ac47a738 1121 hdrlen = encode_header(type, datlen, hdr);
0fcbcae7 1122 write_or_die(pack_data->pack_fd, hdr, hdrlen);
41e5257f 1123 pack_size += hdrlen;
db5e523f
SP
1124 }
1125
0fcbcae7 1126 write_or_die(pack_data->pack_fd, out, s.total_out);
41e5257f 1127 pack_size += s.total_out;
db5e523f
SP
1128
1129 free(out);
e7d06a4b 1130 free(delta);
463acbe1 1131 if (last) {
e7d06a4b 1132 if (!last->no_free)
463acbe1
SP
1133 free(last->data);
1134 last->data = dat;
d489bc14 1135 last->offset = e->offset;
463acbe1 1136 last->len = datlen;
463acbe1
SP
1137 }
1138 return 0;
1139}
1140
7bfe6e26
SP
1141static void *gfi_unpack_entry(
1142 struct object_entry *oe,
1143 unsigned long *sizep)
41e5257f 1144{
21666f1a 1145 enum object_type type;
7bfe6e26
SP
1146 struct packed_git *p = all_packs[oe->pack_id];
1147 if (p == pack_data)
1148 p->pack_size = pack_size + 20;
21666f1a 1149 return unpack_entry(p, oe->offset, &type, sizep);
41e5257f
SP
1150}
1151
10831c55 1152static const char *get_mode(const char *str, uint16_t *modep)
463acbe1
SP
1153{
1154 unsigned char c;
10831c55 1155 uint16_t mode = 0;
463acbe1
SP
1156
1157 while ((c = *str++) != ' ') {
1158 if (c < '0' || c > '7')
1159 return NULL;
1160 mode = (mode << 3) + (c - '0');
1161 }
1162 *modep = mode;
1163 return str;
1164}
1165
1166static void load_tree(struct tree_entry *root)
1167{
4cabf858 1168 unsigned char* sha1 = root->versions[1].sha1;
463acbe1
SP
1169 struct object_entry *myoe;
1170 struct tree_content *t;
1171 unsigned long size;
1172 char *buf;
1173 const char *c;
463acbe1
SP
1174
1175 root->tree = t = new_tree_content(8);
4cabf858 1176 if (is_null_sha1(sha1))
463acbe1
SP
1177 return;
1178
4cabf858 1179 myoe = find_object(sha1);
20f546a8 1180 if (myoe && myoe->pack_id != MAX_PACK_ID) {
41e5257f 1181 if (myoe->type != OBJ_TREE)
4cabf858 1182 die("Not a tree: %s", sha1_to_hex(sha1));
d489bc14 1183 t->delta_depth = 0;
7bfe6e26 1184 buf = gfi_unpack_entry(myoe, &size);
463acbe1 1185 } else {
21666f1a
NP
1186 enum object_type type;
1187 buf = read_sha1_file(sha1, &type, &size);
1188 if (!buf || type != OBJ_TREE)
4cabf858 1189 die("Can't load tree %s", sha1_to_hex(sha1));
463acbe1
SP
1190 }
1191
1192 c = buf;
1193 while (c != (buf + size)) {
1194 struct tree_entry *e = new_tree_entry();
1195
1196 if (t->entry_count == t->entry_capacity)
f022f85f 1197 root->tree = t = grow_tree_content(t, t->entry_count);
463acbe1
SP
1198 t->entries[t->entry_count++] = e;
1199
1200 e->tree = NULL;
4cabf858 1201 c = get_mode(c, &e->versions[1].mode);
463acbe1 1202 if (!c)
4cabf858
SP
1203 die("Corrupt mode in %s", sha1_to_hex(sha1));
1204 e->versions[0].mode = e->versions[1].mode;
061e35c5 1205 e->name = to_atom(c, strlen(c));
463acbe1 1206 c += e->name->str_len + 1;
4cabf858
SP
1207 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1208 hashcpy(e->versions[1].sha1, (unsigned char*)c);
463acbe1
SP
1209 c += 20;
1210 }
1211 free(buf);
1212}
1213
4cabf858 1214static int tecmp0 (const void *_a, const void *_b)
463acbe1
SP
1215{
1216 struct tree_entry *a = *((struct tree_entry**)_a);
1217 struct tree_entry *b = *((struct tree_entry**)_b);
1218 return base_name_compare(
4cabf858
SP
1219 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1220 b->name->str_dat, b->name->str_len, b->versions[0].mode);
463acbe1
SP
1221}
1222
4cabf858 1223static int tecmp1 (const void *_a, const void *_b)
463acbe1 1224{
4cabf858
SP
1225 struct tree_entry *a = *((struct tree_entry**)_a);
1226 struct tree_entry *b = *((struct tree_entry**)_b);
1227 return base_name_compare(
1228 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1229 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1230}
1231
e2eb469d
SP
1232static void mktree(struct tree_content *t,
1233 int v,
1234 unsigned long *szp,
1235 struct dbuf *b)
4cabf858
SP
1236{
1237 size_t maxlen = 0;
463acbe1 1238 unsigned int i;
e2eb469d 1239 char *c;
463acbe1 1240
4cabf858
SP
1241 if (!v)
1242 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1243 else
1244 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
463acbe1 1245
463acbe1 1246 for (i = 0; i < t->entry_count; i++) {
4cabf858
SP
1247 if (t->entries[i]->versions[v].mode)
1248 maxlen += t->entries[i]->name->str_len + 34;
463acbe1
SP
1249 }
1250
243f801d 1251 size_dbuf(b, maxlen);
e2eb469d 1252 c = b->buffer;
463acbe1
SP
1253 for (i = 0; i < t->entry_count; i++) {
1254 struct tree_entry *e = t->entries[i];
4cabf858
SP
1255 if (!e->versions[v].mode)
1256 continue;
10831c55 1257 c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
463acbe1
SP
1258 *c++ = ' ';
1259 strcpy(c, e->name->str_dat);
1260 c += e->name->str_len + 1;
4cabf858 1261 hashcpy((unsigned char*)c, e->versions[v].sha1);
463acbe1
SP
1262 c += 20;
1263 }
e2eb469d 1264 *szp = c - (char*)b->buffer;
4cabf858
SP
1265}
1266
1267static void store_tree(struct tree_entry *root)
1268{
1269 struct tree_content *t = root->tree;
1270 unsigned int i, j, del;
e2eb469d 1271 unsigned long new_len;
4cabf858 1272 struct last_object lo;
d489bc14 1273 struct object_entry *le;
4cabf858
SP
1274
1275 if (!is_null_sha1(root->versions[1].sha1))
1276 return;
1277
1278 for (i = 0; i < t->entry_count; i++) {
1279 if (t->entries[i]->tree)
1280 store_tree(t->entries[i]);
1281 }
1282
d489bc14 1283 le = find_object(root->versions[0].sha1);
7bfe6e26
SP
1284 if (!S_ISDIR(root->versions[0].mode)
1285 || !le
1286 || le->pack_id != pack_id) {
4cabf858
SP
1287 lo.data = NULL;
1288 lo.depth = 0;
b259157f 1289 lo.no_free = 0;
4cabf858 1290 } else {
e2eb469d
SP
1291 mktree(t, 0, &lo.len, &old_tree);
1292 lo.data = old_tree.buffer;
d489bc14 1293 lo.offset = le->offset;
4cabf858 1294 lo.depth = t->delta_depth;
e2eb469d 1295 lo.no_free = 1;
4cabf858 1296 }
4cabf858 1297
8a8c55ea 1298 mktree(t, 1, &new_len, &new_tree);
e2eb469d 1299 store_object(OBJ_TREE, new_tree.buffer, new_len,
4cabf858 1300 &lo, root->versions[1].sha1, 0);
4cabf858
SP
1301
1302 t->delta_depth = lo.depth;
4cabf858
SP
1303 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1304 struct tree_entry *e = t->entries[i];
1305 if (e->versions[1].mode) {
1306 e->versions[0].mode = e->versions[1].mode;
1307 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1308 t->entries[j++] = e;
1309 } else {
1310 release_tree_entry(e);
1311 del++;
1312 }
1313 }
1314 t->entry_count -= del;
463acbe1
SP
1315}
1316
1317static int tree_content_set(
1318 struct tree_entry *root,
1319 const char *p,
1320 const unsigned char *sha1,
f39a946a
SP
1321 const uint16_t mode,
1322 struct tree_content *subtree)
463acbe1
SP
1323{
1324 struct tree_content *t = root->tree;
1325 const char *slash1;
1326 unsigned int i, n;
1327 struct tree_entry *e;
1328
1329 slash1 = strchr(p, '/');
1330 if (slash1)
1331 n = slash1 - p;
1332 else
1333 n = strlen(p);
475d1b33
SP
1334 if (!n)
1335 die("Empty path component found in input");
f39a946a
SP
1336 if (!slash1 && !S_ISDIR(mode) && subtree)
1337 die("Non-directories cannot have subtrees");
463acbe1
SP
1338
1339 for (i = 0; i < t->entry_count; i++) {
1340 e = t->entries[i];
1341 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1342 if (!slash1) {
f39a946a
SP
1343 if (!S_ISDIR(mode)
1344 && e->versions[1].mode == mode
4cabf858 1345 && !hashcmp(e->versions[1].sha1, sha1))
463acbe1 1346 return 0;
4cabf858
SP
1347 e->versions[1].mode = mode;
1348 hashcpy(e->versions[1].sha1, sha1);
f39a946a 1349 if (e->tree)
afde8dd9 1350 release_tree_content_recursive(e->tree);
f39a946a 1351 e->tree = subtree;
4cabf858 1352 hashclr(root->versions[1].sha1);
463acbe1
SP
1353 return 1;
1354 }
4cabf858 1355 if (!S_ISDIR(e->versions[1].mode)) {
463acbe1 1356 e->tree = new_tree_content(8);
4cabf858 1357 e->versions[1].mode = S_IFDIR;
463acbe1
SP
1358 }
1359 if (!e->tree)
1360 load_tree(e);
f39a946a 1361 if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
4cabf858 1362 hashclr(root->versions[1].sha1);
463acbe1
SP
1363 return 1;
1364 }
1365 return 0;
1366 }
1367 }
1368
1369 if (t->entry_count == t->entry_capacity)
f022f85f 1370 root->tree = t = grow_tree_content(t, t->entry_count);
463acbe1 1371 e = new_tree_entry();
061e35c5 1372 e->name = to_atom(p, n);
4cabf858
SP
1373 e->versions[0].mode = 0;
1374 hashclr(e->versions[0].sha1);
463acbe1
SP
1375 t->entries[t->entry_count++] = e;
1376 if (slash1) {
1377 e->tree = new_tree_content(8);
4cabf858 1378 e->versions[1].mode = S_IFDIR;
f39a946a 1379 tree_content_set(e, slash1 + 1, sha1, mode, subtree);
463acbe1 1380 } else {
f39a946a 1381 e->tree = subtree;
4cabf858
SP
1382 e->versions[1].mode = mode;
1383 hashcpy(e->versions[1].sha1, sha1);
463acbe1 1384 }
4cabf858 1385 hashclr(root->versions[1].sha1);
463acbe1
SP
1386 return 1;
1387}
1388
f39a946a
SP
1389static int tree_content_remove(
1390 struct tree_entry *root,
1391 const char *p,
1392 struct tree_entry *backup_leaf)
463acbe1
SP
1393{
1394 struct tree_content *t = root->tree;
1395 const char *slash1;
1396 unsigned int i, n;
1397 struct tree_entry *e;
1398
1399 slash1 = strchr(p, '/');
1400 if (slash1)
1401 n = slash1 - p;
1402 else
1403 n = strlen(p);
1404
1405 for (i = 0; i < t->entry_count; i++) {
1406 e = t->entries[i];
1407 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
4cabf858 1408 if (!slash1 || !S_ISDIR(e->versions[1].mode))
463acbe1
SP
1409 goto del_entry;
1410 if (!e->tree)
1411 load_tree(e);
f39a946a 1412 if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
b54d6422
SP
1413 for (n = 0; n < e->tree->entry_count; n++) {
1414 if (e->tree->entries[n]->versions[1].mode) {
1415 hashclr(root->versions[1].sha1);
1416 return 1;
1417 }
1418 }
f39a946a 1419 backup_leaf = NULL;
b54d6422 1420 goto del_entry;
463acbe1
SP
1421 }
1422 return 0;
1423 }
1424 }
1425 return 0;
1426
1427del_entry:
f39a946a
SP
1428 if (backup_leaf)
1429 memcpy(backup_leaf, e, sizeof(*backup_leaf));
1430 else if (e->tree)
4cabf858 1431 release_tree_content_recursive(e->tree);
f39a946a 1432 e->tree = NULL;
4cabf858
SP
1433 e->versions[1].mode = 0;
1434 hashclr(e->versions[1].sha1);
1435 hashclr(root->versions[1].sha1);
ac47a738 1436 return 1;
db5e523f
SP
1437}
1438
b6f3481b
SP
1439static int tree_content_get(
1440 struct tree_entry *root,
1441 const char *p,
1442 struct tree_entry *leaf)
1443{
1444 struct tree_content *t = root->tree;
1445 const char *slash1;
1446 unsigned int i, n;
1447 struct tree_entry *e;
1448
1449 slash1 = strchr(p, '/');
1450 if (slash1)
1451 n = slash1 - p;
1452 else
1453 n = strlen(p);
1454
1455 for (i = 0; i < t->entry_count; i++) {
1456 e = t->entries[i];
1457 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1458 if (!slash1) {
1459 memcpy(leaf, e, sizeof(*leaf));
1460 if (e->tree && is_null_sha1(e->versions[1].sha1))
1461 leaf->tree = dup_tree_content(e->tree);
1462 else
1463 leaf->tree = NULL;
1464 return 1;
1465 }
1466 if (!S_ISDIR(e->versions[1].mode))
1467 return 0;
1468 if (!e->tree)
1469 load_tree(e);
1470 return tree_content_get(e, slash1 + 1, leaf);
1471 }
1472 }
1473 return 0;
1474}
1475
7073e69e 1476static int update_branch(struct branch *b)
463acbe1
SP
1477{
1478 static const char *msg = "fast-import";
7073e69e
SP
1479 struct ref_lock *lock;
1480 unsigned char old_sha1[20];
1481
1482 if (read_ref(b->name, old_sha1))
1483 hashclr(old_sha1);
68db31cc 1484 lock = lock_any_ref_for_update(b->name, old_sha1, 0);
7073e69e
SP
1485 if (!lock)
1486 return error("Unable to lock %s", b->name);
1487 if (!force_update && !is_null_sha1(old_sha1)) {
1488 struct commit *old_cmit, *new_cmit;
1489
1490 old_cmit = lookup_commit_reference_gently(old_sha1, 0);
1491 new_cmit = lookup_commit_reference_gently(b->sha1, 0);
1492 if (!old_cmit || !new_cmit) {
1493 unlock_ref(lock);
1494 return error("Branch %s is missing commits.", b->name);
1495 }
1496
4a164d48 1497 if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
7073e69e 1498 unlock_ref(lock);
46efd2d9 1499 warning("Not updating %s"
7073e69e
SP
1500 " (new tip %s does not contain %s)",
1501 b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
1502 return -1;
1503 }
1504 }
1505 if (write_ref_sha1(lock, b->sha1, msg) < 0)
1506 return error("Unable to update %s", b->name);
1507 return 0;
1508}
1509
1510static void dump_branches(void)
1511{
463acbe1
SP
1512 unsigned int i;
1513 struct branch *b;
463acbe1
SP
1514
1515 for (i = 0; i < branch_table_sz; i++) {
7073e69e
SP
1516 for (b = branch_table[i]; b; b = b->table_next_branch)
1517 failure |= update_branch(b);
463acbe1
SP
1518 }
1519}
1520
fd99224e 1521static void dump_tags(void)
72303d44
SP
1522{
1523 static const char *msg = "fast-import";
1524 struct tag *t;
1525 struct ref_lock *lock;
7073e69e 1526 char ref_name[PATH_MAX];
72303d44
SP
1527
1528 for (t = first_tag; t; t = t->next_tag) {
7073e69e
SP
1529 sprintf(ref_name, "tags/%s", t->name);
1530 lock = lock_ref_sha1(ref_name, NULL);
72303d44 1531 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
7073e69e 1532 failure |= error("Unable to update %s", ref_name);
72303d44
SP
1533 }
1534}
1535
a6a1a831 1536static void dump_marks_helper(FILE *f,
0ea9f045 1537 uintmax_t base,
a6a1a831
SP
1538 struct mark_set *m)
1539{
0ea9f045 1540 uintmax_t k;
a6a1a831
SP
1541 if (m->shift) {
1542 for (k = 0; k < 1024; k++) {
1543 if (m->data.sets[k])
1544 dump_marks_helper(f, (base + k) << m->shift,
1545 m->data.sets[k]);
1546 }
1547 } else {
1548 for (k = 0; k < 1024; k++) {
1549 if (m->data.marked[k])
3efb1f34 1550 fprintf(f, ":%" PRIuMAX " %s\n", base + k,
a6a1a831
SP
1551 sha1_to_hex(m->data.marked[k]->sha1));
1552 }
1553 }
1554}
1555
fd99224e 1556static void dump_marks(void)
a6a1a831 1557{
60b9004c
SP
1558 static struct lock_file mark_lock;
1559 int mark_fd;
1560 FILE *f;
1561
1562 if (!mark_file)
1563 return;
1564
1565 mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0);
1566 if (mark_fd < 0) {
1567 failure |= error("Unable to write marks file %s: %s",
1568 mark_file, strerror(errno));
1569 return;
a6a1a831 1570 }
60b9004c
SP
1571
1572 f = fdopen(mark_fd, "w");
1573 if (!f) {
1574 rollback_lock_file(&mark_lock);
1575 failure |= error("Unable to write marks file %s: %s",
1576 mark_file, strerror(errno));
1577 return;
a6a1a831 1578 }
60b9004c
SP
1579
1580 dump_marks_helper(f, 0, marks);
1581 fclose(f);
1582 if (commit_lock_file(&mark_lock))
1583 failure |= error("Unable to write marks file %s: %s",
1584 mark_file, strerror(errno));
a6a1a831
SP
1585}
1586
fd99224e 1587static void read_next_command(void)
c44cdc7e 1588{
401d53fa 1589 do {
904b1941 1590 if (unread_command_buf) {
1fdb649c 1591 unread_command_buf = 0;
904b1941
SP
1592 if (command_buf.eof)
1593 return;
1594 } else {
1595 struct recent_command *rc;
1596
1597 command_buf.buf = NULL;
1fdb649c 1598 read_line(&command_buf, stdin, '\n');
904b1941
SP
1599 if (command_buf.eof)
1600 return;
1601
1602 rc = rc_free;
1603 if (rc)
1604 rc_free = rc->next;
1605 else {
1606 rc = cmd_hist.next;
1607 cmd_hist.next = rc->next;
1608 cmd_hist.next->prev = &cmd_hist;
1609 free(rc->buf);
1610 }
1611
1612 rc->buf = command_buf.buf;
1613 rc->prev = cmd_tail;
1614 rc->next = cmd_hist.prev;
1615 rc->prev->next = rc;
1616 cmd_tail = rc;
1617 }
1618 } while (command_buf.buf[0] == '#');
c44cdc7e
SP
1619}
1620
2c570cde
SP
1621static void skip_optional_lf()
1622{
1623 int term_char = fgetc(stdin);
1624 if (term_char != '\n' && term_char != EOF)
1625 ungetc(term_char, stdin);
1626}
1627
fd99224e 1628static void cmd_mark(void)
c44cdc7e 1629{
599065a3 1630 if (!prefixcmp(command_buf.buf, "mark :")) {
0ea9f045 1631 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
c44cdc7e
SP
1632 read_next_command();
1633 }
1634 else
d8397168 1635 next_mark = 0;
c44cdc7e
SP
1636}
1637
e5b1444b 1638static void *cmd_data (size_t *size)
c44cdc7e 1639{
c44cdc7e 1640 size_t length;
3b4dce02 1641 char *buffer;
c44cdc7e 1642
599065a3 1643 if (prefixcmp(command_buf.buf, "data "))
c44cdc7e
SP
1644 die("Expected 'data n' command, found: %s", command_buf.buf);
1645
599065a3 1646 if (!prefixcmp(command_buf.buf + 5, "<<")) {
3b4dce02
SP
1647 char *term = xstrdup(command_buf.buf + 5 + 2);
1648 size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1649 length = 0;
1650 buffer = xmalloc(sz);
904b1941 1651 command_buf.buf = NULL;
3b4dce02 1652 for (;;) {
401d53fa 1653 read_line(&command_buf, stdin, '\n');
3b4dce02
SP
1654 if (command_buf.eof)
1655 die("EOF in data (terminator '%s' not found)", term);
1656 if (term_len == command_buf.len
1657 && !strcmp(term, command_buf.buf))
1658 break;
31490074 1659 ALLOC_GROW(buffer, length + command_buf.len, sz);
3b4dce02
SP
1660 memcpy(buffer + length,
1661 command_buf.buf,
1662 command_buf.len - 1);
1663 length += command_buf.len - 1;
1664 buffer[length++] = '\n';
1665 }
1666 free(term);
1667 }
1668 else {
1669 size_t n = 0;
1670 length = strtoul(command_buf.buf + 5, NULL, 10);
1671 buffer = xmalloc(length);
1672 while (n < length) {
1673 size_t s = fread(buffer + n, 1, length - n, stdin);
1674 if (!s && feof(stdin))
40db58b8
JS
1675 die("EOF in data (%lu bytes remaining)",
1676 (unsigned long)(length - n));
3b4dce02
SP
1677 n += s;
1678 }
c44cdc7e
SP
1679 }
1680
2c570cde 1681 skip_optional_lf();
c44cdc7e
SP
1682 *size = length;
1683 return buffer;
1684}
1685
63e0c8b3
SP
1686static int validate_raw_date(const char *src, char *result, int maxlen)
1687{
1688 const char *orig_src = src;
1689 char *endp, sign;
1690
1691 strtoul(src, &endp, 10);
1692 if (endp == src || *endp != ' ')
1693 return -1;
1694
1695 src = endp + 1;
1696 if (*src != '-' && *src != '+')
1697 return -1;
1698 sign = *src;
1699
1700 strtoul(src + 1, &endp, 10);
1701 if (endp == src || *endp || (endp - orig_src) >= maxlen)
1702 return -1;
1703
1704 strcpy(result, orig_src);
1705 return 0;
1706}
1707
1708static char *parse_ident(const char *buf)
1709{
1710 const char *gt;
1711 size_t name_len;
1712 char *ident;
1713
1714 gt = strrchr(buf, '>');
1715 if (!gt)
1716 die("Missing > in ident string: %s", buf);
1717 gt++;
1718 if (*gt != ' ')
1719 die("Missing space after > in ident string: %s", buf);
1720 gt++;
1721 name_len = gt - buf;
1722 ident = xmalloc(name_len + 24);
1723 strncpy(ident, buf, name_len);
1724
1725 switch (whenspec) {
1726 case WHENSPEC_RAW:
1727 if (validate_raw_date(gt, ident + name_len, 24) < 0)
1728 die("Invalid raw date \"%s\" in ident: %s", gt, buf);
1729 break;
1730 case WHENSPEC_RFC2822:
1731 if (parse_date(gt, ident + name_len, 24) < 0)
1732 die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
1733 break;
1734 case WHENSPEC_NOW:
1735 if (strcmp("now", gt))
1736 die("Date in ident must be 'now': %s", buf);
1737 datestamp(ident + name_len, 24);
1738 break;
1739 }
1740
1741 return ident;
1742}
1743
fd99224e 1744static void cmd_new_blob(void)
6143f064 1745{
d8397168
SP
1746 size_t l;
1747 void *d;
c44cdc7e
SP
1748
1749 read_next_command();
1750 cmd_mark();
d8397168 1751 d = cmd_data(&l);
6143f064 1752
d8397168
SP
1753 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1754 free(d);
6143f064
SP
1755}
1756
fd99224e 1757static void unload_one_branch(void)
6bb5b329 1758{
41e5257f
SP
1759 while (cur_active_branches
1760 && cur_active_branches >= max_active_branches) {
6777a59f 1761 uintmax_t min_commit = ULONG_MAX;
463acbe1
SP
1762 struct branch *e, *l = NULL, *p = NULL;
1763
1764 for (e = active_branches; e; e = e->active_next_branch) {
1765 if (e->last_commit < min_commit) {
1766 p = l;
1767 min_commit = e->last_commit;
1768 }
1769 l = e;
1770 }
1771
1772 if (p) {
1773 e = p->active_next_branch;
1774 p->active_next_branch = e->active_next_branch;
1775 } else {
1776 e = active_branches;
1777 active_branches = e->active_next_branch;
1778 }
734c91f9 1779 e->active = 0;
463acbe1
SP
1780 e->active_next_branch = NULL;
1781 if (e->branch_tree.tree) {
afde8dd9 1782 release_tree_content_recursive(e->branch_tree.tree);
463acbe1
SP
1783 e->branch_tree.tree = NULL;
1784 }
1785 cur_active_branches--;
6bb5b329 1786 }
6bb5b329
SP
1787}
1788
463acbe1 1789static void load_branch(struct branch *b)
6bb5b329 1790{
463acbe1 1791 load_tree(&b->branch_tree);
734c91f9
SP
1792 if (!b->active) {
1793 b->active = 1;
1794 b->active_next_branch = active_branches;
1795 active_branches = b;
1796 cur_active_branches++;
1797 branch_load_count++;
1798 }
6bb5b329
SP
1799}
1800
463acbe1 1801static void file_change_m(struct branch *b)
6bb5b329 1802{
c44cdc7e
SP
1803 const char *p = command_buf.buf + 2;
1804 char *p_uq;
1805 const char *endp;
10e8d688 1806 struct object_entry *oe = oe;
463acbe1 1807 unsigned char sha1[20];
10831c55 1808 uint16_t mode, inline_data = 0;
6bb5b329 1809
c44cdc7e
SP
1810 p = get_mode(p, &mode);
1811 if (!p)
1812 die("Corrupt mode: %s", command_buf.buf);
1813 switch (mode) {
1814 case S_IFREG | 0644:
1815 case S_IFREG | 0755:
ace4a9d1 1816 case S_IFLNK:
c44cdc7e
SP
1817 case 0644:
1818 case 0755:
1819 /* ok */
1820 break;
1821 default:
1822 die("Corrupt mode: %s", command_buf.buf);
1823 }
1824
d8397168
SP
1825 if (*p == ':') {
1826 char *x;
0ea9f045 1827 oe = find_mark(strtoumax(p + 1, &x, 10));
cacbdd0a 1828 hashcpy(sha1, oe->sha1);
d8397168 1829 p = x;
599065a3 1830 } else if (!prefixcmp(p, "inline")) {
b715cfbb
SP
1831 inline_data = 1;
1832 p += 6;
d8397168
SP
1833 } else {
1834 if (get_sha1_hex(p, sha1))
1835 die("Invalid SHA1: %s", command_buf.buf);
1836 oe = find_object(sha1);
1837 p += 40;
1838 }
c44cdc7e
SP
1839 if (*p++ != ' ')
1840 die("Missing space after SHA1: %s", command_buf.buf);
1841
1842 p_uq = unquote_c_style(p, &endp);
1843 if (p_uq) {
1844 if (*endp)
1845 die("Garbage after path in: %s", command_buf.buf);
1846 p = p_uq;
1847 }
6bb5b329 1848
b715cfbb
SP
1849 if (inline_data) {
1850 size_t l;
1851 void *d;
1852 if (!p_uq)
1853 p = p_uq = xstrdup(p);
1854 read_next_command();
1855 d = cmd_data(&l);
1856 if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
1857 free(d);
1858 } else if (oe) {
7111feed 1859 if (oe->type != OBJ_BLOB)
c44cdc7e 1860 die("Not a blob (actually a %s): %s",
df843662 1861 command_buf.buf, typename(oe->type));
7111feed 1862 } else {
21666f1a
NP
1863 enum object_type type = sha1_object_info(sha1, NULL);
1864 if (type < 0)
c44cdc7e 1865 die("Blob not found: %s", command_buf.buf);
21666f1a 1866 if (type != OBJ_BLOB)
c44cdc7e 1867 die("Not a blob (actually a %s): %s",
21666f1a 1868 typename(type), command_buf.buf);
7111feed 1869 }
6bb5b329 1870
f39a946a 1871 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
e7d06a4b 1872 free(p_uq);
463acbe1 1873}
6bb5b329 1874
463acbe1
SP
1875static void file_change_d(struct branch *b)
1876{
c44cdc7e
SP
1877 const char *p = command_buf.buf + 2;
1878 char *p_uq;
1879 const char *endp;
1880
1881 p_uq = unquote_c_style(p, &endp);
1882 if (p_uq) {
1883 if (*endp)
1884 die("Garbage after path in: %s", command_buf.buf);
1885 p = p_uq;
1886 }
f39a946a 1887 tree_content_remove(&b->branch_tree, p, NULL);
e7d06a4b 1888 free(p_uq);
6bb5b329
SP
1889}
1890
b6f3481b 1891static void file_change_cr(struct branch *b, int rename)
f39a946a
SP
1892{
1893 const char *s, *d;
1894 char *s_uq, *d_uq;
1895 const char *endp;
1896 struct tree_entry leaf;
1897
1898 s = command_buf.buf + 2;
1899 s_uq = unquote_c_style(s, &endp);
1900 if (s_uq) {
1901 if (*endp != ' ')
1902 die("Missing space after source: %s", command_buf.buf);
1903 }
1904 else {
1905 endp = strchr(s, ' ');
1906 if (!endp)
1907 die("Missing space after source: %s", command_buf.buf);
1908 s_uq = xmalloc(endp - s + 1);
1909 memcpy(s_uq, s, endp - s);
1910 s_uq[endp - s] = 0;
1911 }
1912 s = s_uq;
1913
1914 endp++;
1915 if (!*endp)
1916 die("Missing dest: %s", command_buf.buf);
1917
1918 d = endp;
1919 d_uq = unquote_c_style(d, &endp);
1920 if (d_uq) {
1921 if (*endp)
1922 die("Garbage after dest in: %s", command_buf.buf);
1923 d = d_uq;
1924 }
1925
1926 memset(&leaf, 0, sizeof(leaf));
b6f3481b
SP
1927 if (rename)
1928 tree_content_remove(&b->branch_tree, s, &leaf);
1929 else
1930 tree_content_get(&b->branch_tree, s, &leaf);
f39a946a
SP
1931 if (!leaf.versions[1].mode)
1932 die("Path %s not in branch", s);
1933 tree_content_set(&b->branch_tree, d,
1934 leaf.versions[1].sha1,
1935 leaf.versions[1].mode,
1936 leaf.tree);
1937
1938 free(s_uq);
1939 free(d_uq);
1940}
1941
825769a8
SP
1942static void file_change_deleteall(struct branch *b)
1943{
1944 release_tree_content_recursive(b->branch_tree.tree);
1945 hashclr(b->branch_tree.versions[0].sha1);
1946 hashclr(b->branch_tree.versions[1].sha1);
1947 load_tree(&b->branch_tree);
1948}
1949
654aaa37
SP
1950static void cmd_from_commit(struct branch *b, char *buf, unsigned long size)
1951{
1952 if (!buf || size < 46)
1953 die("Not a valid commit: %s", sha1_to_hex(b->sha1));
1954 if (memcmp("tree ", buf, 5)
1955 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1956 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1957 hashcpy(b->branch_tree.versions[0].sha1,
1958 b->branch_tree.versions[1].sha1);
1959}
1960
1961static void cmd_from_existing(struct branch *b)
1962{
1963 if (is_null_sha1(b->sha1)) {
1964 hashclr(b->branch_tree.versions[0].sha1);
1965 hashclr(b->branch_tree.versions[1].sha1);
1966 } else {
1967 unsigned long size;
1968 char *buf;
1969
1970 buf = read_object_with_reference(b->sha1,
1971 commit_type, &size, b->sha1);
1972 cmd_from_commit(b, buf, size);
1973 free(buf);
1974 }
1975}
1976
1fdb649c 1977static int cmd_from(struct branch *b)
00e2b884 1978{
6c3aac1c 1979 const char *from;
00e2b884
SP
1980 struct branch *s;
1981
599065a3 1982 if (prefixcmp(command_buf.buf, "from "))
1fdb649c 1983 return 0;
00e2b884 1984
ea5e370a
SP
1985 if (b->branch_tree.tree) {
1986 release_tree_content_recursive(b->branch_tree.tree);
1987 b->branch_tree.tree = NULL;
1988 }
00e2b884
SP
1989
1990 from = strchr(command_buf.buf, ' ') + 1;
00e2b884
SP
1991 s = lookup_branch(from);
1992 if (b == s)
1993 die("Can't create a branch from itself: %s", b->name);
1994 else if (s) {
4cabf858 1995 unsigned char *t = s->branch_tree.versions[1].sha1;
445b8599 1996 hashcpy(b->sha1, s->sha1);
4cabf858
SP
1997 hashcpy(b->branch_tree.versions[0].sha1, t);
1998 hashcpy(b->branch_tree.versions[1].sha1, t);
00e2b884 1999 } else if (*from == ':') {
0ea9f045 2000 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
00e2b884 2001 struct object_entry *oe = find_mark(idnum);
00e2b884 2002 if (oe->type != OBJ_COMMIT)
3efb1f34 2003 die("Mark :%" PRIuMAX " not a commit", idnum);
445b8599 2004 hashcpy(b->sha1, oe->sha1);
aac65ed1 2005 if (oe->pack_id != MAX_PACK_ID) {
00e2b884 2006 unsigned long size;
aac65ed1
SP
2007 char *buf = gfi_unpack_entry(oe, &size);
2008 cmd_from_commit(b, buf, size);
00e2b884 2009 free(buf);
aac65ed1
SP
2010 } else
2011 cmd_from_existing(b);
654aaa37
SP
2012 } else if (!get_sha1(from, b->sha1))
2013 cmd_from_existing(b);
2014 else
00e2b884
SP
2015 die("Invalid ref name or SHA1 expression: %s", from);
2016
2017 read_next_command();
1fdb649c 2018 return 1;
00e2b884
SP
2019}
2020
e5b1444b 2021static struct hash_list *cmd_merge(unsigned int *count)
62b6f483 2022{
10e8d688 2023 struct hash_list *list = NULL, *n, *e = e;
6c3aac1c 2024 const char *from;
62b6f483
SP
2025 struct branch *s;
2026
2027 *count = 0;
599065a3 2028 while (!prefixcmp(command_buf.buf, "merge ")) {
62b6f483 2029 from = strchr(command_buf.buf, ' ') + 1;
62b6f483
SP
2030 n = xmalloc(sizeof(*n));
2031 s = lookup_branch(from);
2032 if (s)
2033 hashcpy(n->sha1, s->sha1);
2034 else if (*from == ':') {
0ea9f045 2035 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
62b6f483
SP
2036 struct object_entry *oe = find_mark(idnum);
2037 if (oe->type != OBJ_COMMIT)
3efb1f34 2038 die("Mark :%" PRIuMAX " not a commit", idnum);
62b6f483 2039 hashcpy(n->sha1, oe->sha1);
2f6dc35d
SP
2040 } else if (!get_sha1(from, n->sha1)) {
2041 unsigned long size;
2042 char *buf = read_object_with_reference(n->sha1,
6b4318e6 2043 commit_type, &size, n->sha1);
2f6dc35d
SP
2044 if (!buf || size < 46)
2045 die("Not a valid commit: %s", from);
2046 free(buf);
2047 } else
62b6f483
SP
2048 die("Invalid ref name or SHA1 expression: %s", from);
2049
2050 n->next = NULL;
2051 if (list)
2052 e->next = n;
2053 else
2054 list = n;
2055 e = n;
10e8d688 2056 (*count)++;
62b6f483
SP
2057 read_next_command();
2058 }
2059 return list;
2060}
2061
fd99224e 2062static void cmd_new_commit(void)
6bb5b329 2063{
c44cdc7e
SP
2064 struct branch *b;
2065 void *msg;
2066 size_t msglen;
c44cdc7e
SP
2067 char *sp;
2068 char *author = NULL;
2069 char *committer = NULL;
62b6f483
SP
2070 struct hash_list *merge_list = NULL;
2071 unsigned int merge_count;
c44cdc7e
SP
2072
2073 /* Obtain the branch name from the rest of our command */
2074 sp = strchr(command_buf.buf, ' ') + 1;
c44cdc7e 2075 b = lookup_branch(sp);
463acbe1 2076 if (!b)
00e2b884 2077 b = new_branch(sp);
c44cdc7e
SP
2078
2079 read_next_command();
2080 cmd_mark();
599065a3 2081 if (!prefixcmp(command_buf.buf, "author ")) {
63e0c8b3 2082 author = parse_ident(command_buf.buf + 7);
c44cdc7e
SP
2083 read_next_command();
2084 }
599065a3 2085 if (!prefixcmp(command_buf.buf, "committer ")) {
63e0c8b3 2086 committer = parse_ident(command_buf.buf + 10);
c44cdc7e
SP
2087 read_next_command();
2088 }
2089 if (!committer)
2090 die("Expected committer but didn't get one");
2091 msg = cmd_data(&msglen);
02f3389d
SP
2092 read_next_command();
2093 cmd_from(b);
62b6f483 2094 merge_list = cmd_merge(&merge_count);
c44cdc7e
SP
2095
2096 /* ensure the branch is active/loaded */
41e5257f 2097 if (!b->branch_tree.tree || !max_active_branches) {
463acbe1
SP
2098 unload_one_branch();
2099 load_branch(b);
2100 }
6bb5b329 2101
463acbe1 2102 /* file_change* */
1fdb649c
SP
2103 while (!command_buf.eof && command_buf.len > 1) {
2104 if (!prefixcmp(command_buf.buf, "M "))
463acbe1 2105 file_change_m(b);
599065a3 2106 else if (!prefixcmp(command_buf.buf, "D "))
463acbe1 2107 file_change_d(b);
f39a946a 2108 else if (!prefixcmp(command_buf.buf, "R "))
b6f3481b
SP
2109 file_change_cr(b, 1);
2110 else if (!prefixcmp(command_buf.buf, "C "))
2111 file_change_cr(b, 0);
825769a8
SP
2112 else if (!strcmp("deleteall", command_buf.buf))
2113 file_change_deleteall(b);
1fdb649c
SP
2114 else {
2115 unread_command_buf = 1;
2116 break;
2117 }
02f3389d 2118 read_next_command();
6bb5b329 2119 }
6bb5b329 2120
c44cdc7e 2121 /* build the tree and the commit */
463acbe1 2122 store_tree(&b->branch_tree);
8a8c55ea
SP
2123 hashcpy(b->branch_tree.versions[0].sha1,
2124 b->branch_tree.versions[1].sha1);
63e0c8b3 2125 size_dbuf(&new_data, 114 + msglen
62b6f483 2126 + merge_count * 49
c44cdc7e
SP
2127 + (author
2128 ? strlen(author) + strlen(committer)
2129 : 2 * strlen(committer)));
243f801d 2130 sp = new_data.buffer;
4cabf858
SP
2131 sp += sprintf(sp, "tree %s\n",
2132 sha1_to_hex(b->branch_tree.versions[1].sha1));
445b8599 2133 if (!is_null_sha1(b->sha1))
c44cdc7e 2134 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
62b6f483
SP
2135 while (merge_list) {
2136 struct hash_list *next = merge_list->next;
2137 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
2138 free(merge_list);
2139 merge_list = next;
2140 }
63e0c8b3
SP
2141 sp += sprintf(sp, "author %s\n", author ? author : committer);
2142 sp += sprintf(sp, "committer %s\n", committer);
2143 *sp++ = '\n';
c44cdc7e
SP
2144 memcpy(sp, msg, msglen);
2145 sp += msglen;
e7d06a4b 2146 free(author);
c44cdc7e
SP
2147 free(committer);
2148 free(msg);
2149
69e74e74 2150 if (!store_object(OBJ_COMMIT,
243f801d 2151 new_data.buffer, sp - (char*)new_data.buffer,
69e74e74
SP
2152 NULL, b->sha1, next_mark))
2153 b->pack_id = pack_id;
463acbe1 2154 b->last_commit = object_count_by_type[OBJ_COMMIT];
6bb5b329
SP
2155}
2156
fd99224e 2157static void cmd_new_tag(void)
72303d44 2158{
72303d44
SP
2159 char *sp;
2160 const char *from;
2161 char *tagger;
2162 struct branch *s;
2163 void *msg;
2164 size_t msglen;
72303d44 2165 struct tag *t;
0ea9f045 2166 uintmax_t from_mark = 0;
72303d44
SP
2167 unsigned char sha1[20];
2168
2169 /* Obtain the new tag name from the rest of our command */
2170 sp = strchr(command_buf.buf, ' ') + 1;
72303d44
SP
2171 t = pool_alloc(sizeof(struct tag));
2172 t->next_tag = NULL;
2173 t->name = pool_strdup(sp);
2174 if (last_tag)
2175 last_tag->next_tag = t;
2176 else
2177 first_tag = t;
2178 last_tag = t;
72303d44
SP
2179 read_next_command();
2180
2181 /* from ... */
599065a3 2182 if (prefixcmp(command_buf.buf, "from "))
72303d44 2183 die("Expected from command, got %s", command_buf.buf);
72303d44 2184 from = strchr(command_buf.buf, ' ') + 1;
72303d44
SP
2185 s = lookup_branch(from);
2186 if (s) {
445b8599 2187 hashcpy(sha1, s->sha1);
72303d44 2188 } else if (*from == ':') {
10e8d688 2189 struct object_entry *oe;
0ea9f045 2190 from_mark = strtoumax(from + 1, NULL, 10);
10e8d688 2191 oe = find_mark(from_mark);
72303d44 2192 if (oe->type != OBJ_COMMIT)
3efb1f34 2193 die("Mark :%" PRIuMAX " not a commit", from_mark);
445b8599 2194 hashcpy(sha1, oe->sha1);
72303d44
SP
2195 } else if (!get_sha1(from, sha1)) {
2196 unsigned long size;
2197 char *buf;
2198
2199 buf = read_object_with_reference(sha1,
df843662 2200 commit_type, &size, sha1);
72303d44
SP
2201 if (!buf || size < 46)
2202 die("Not a valid commit: %s", from);
2203 free(buf);
2204 } else
2205 die("Invalid ref name or SHA1 expression: %s", from);
72303d44
SP
2206 read_next_command();
2207
2208 /* tagger ... */
599065a3 2209 if (prefixcmp(command_buf.buf, "tagger "))
72303d44 2210 die("Expected tagger command, got %s", command_buf.buf);
63e0c8b3 2211 tagger = parse_ident(command_buf.buf + 7);
72303d44
SP
2212
2213 /* tag payload/message */
2214 read_next_command();
2215 msg = cmd_data(&msglen);
2216
2217 /* build the tag object */
243f801d
SP
2218 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
2219 sp = new_data.buffer;
72303d44 2220 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
df843662 2221 sp += sprintf(sp, "type %s\n", commit_type);
72303d44 2222 sp += sprintf(sp, "tag %s\n", t->name);
63e0c8b3
SP
2223 sp += sprintf(sp, "tagger %s\n", tagger);
2224 *sp++ = '\n';
72303d44
SP
2225 memcpy(sp, msg, msglen);
2226 sp += msglen;
2227 free(tagger);
2228 free(msg);
2229
69e74e74
SP
2230 if (store_object(OBJ_TAG, new_data.buffer,
2231 sp - (char*)new_data.buffer,
2232 NULL, t->sha1, 0))
2233 t->pack_id = MAX_PACK_ID;
2234 else
2235 t->pack_id = pack_id;
72303d44
SP
2236}
2237
fd99224e 2238static void cmd_reset_branch(void)
5fced8dc
SP
2239{
2240 struct branch *b;
5fced8dc
SP
2241 char *sp;
2242
2243 /* Obtain the branch name from the rest of our command */
2244 sp = strchr(command_buf.buf, ' ') + 1;
5fced8dc
SP
2245 b = lookup_branch(sp);
2246 if (b) {
ea5e370a
SP
2247 hashclr(b->sha1);
2248 hashclr(b->branch_tree.versions[0].sha1);
2249 hashclr(b->branch_tree.versions[1].sha1);
5fced8dc
SP
2250 if (b->branch_tree.tree) {
2251 release_tree_content_recursive(b->branch_tree.tree);
2252 b->branch_tree.tree = NULL;
2253 }
2254 }
9938ffc5
SP
2255 else
2256 b = new_branch(sp);
9938ffc5 2257 read_next_command();
1fdb649c
SP
2258 if (!cmd_from(b) && command_buf.len > 1)
2259 unread_command_buf = 1;
5fced8dc
SP
2260}
2261
fd99224e 2262static void cmd_checkpoint(void)
7bfe6e26 2263{
820b9310
SP
2264 if (object_count) {
2265 cycle_packfile();
2266 dump_branches();
2267 dump_tags();
2268 dump_marks();
2269 }
1fdb649c 2270 skip_optional_lf();
7bfe6e26
SP
2271}
2272
ac053c02
SP
2273static void cmd_progress(void)
2274{
2275 fwrite(command_buf.buf, 1, command_buf.len - 1, stdout);
2276 fputc('\n', stdout);
2277 fflush(stdout);
2278 skip_optional_lf();
2279}
2280
e8438420
SP
2281static void import_marks(const char *input_file)
2282{
2283 char line[512];
2284 FILE *f = fopen(input_file, "r");
2285 if (!f)
2286 die("cannot read %s: %s", input_file, strerror(errno));
2287 while (fgets(line, sizeof(line), f)) {
2288 uintmax_t mark;
2289 char *end;
2290 unsigned char sha1[20];
2291 struct object_entry *e;
2292
2293 end = strchr(line, '\n');
2294 if (line[0] != ':' || !end)
2295 die("corrupt mark line: %s", line);
2296 *end = 0;
2297 mark = strtoumax(line + 1, &end, 10);
2298 if (!mark || end == line + 1
2299 || *end != ' ' || get_sha1(end + 1, sha1))
2300 die("corrupt mark line: %s", line);
2301 e = find_object(sha1);
2302 if (!e) {
2303 enum object_type type = sha1_object_info(sha1, NULL);
2304 if (type < 0)
2305 die("object not found: %s", sha1_to_hex(sha1));
2306 e = insert_object(sha1);
2307 e->type = type;
2308 e->pack_id = MAX_PACK_ID;
a5c1780a 2309 e->offset = 1; /* just not zero! */
e8438420
SP
2310 }
2311 insert_mark(mark, e);
2312 }
2313 fclose(f);
2314}
2315
d5c57b28 2316static const char fast_import_usage[] =
63e0c8b3 2317"git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
d5c57b28 2318
8bcce301
SP
2319int main(int argc, const char **argv)
2320{
904b1941 2321 unsigned int i, show_stats = 1;
8bcce301 2322
463acbe1 2323 git_config(git_default_config);
93e72d8d
SP
2324 alloc_objects(object_entry_alloc);
2325 strbuf_init(&command_buf);
2326 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
2327 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
2328 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
2329 marks = pool_calloc(1, sizeof(struct mark_set));
463acbe1 2330
d5c57b28
SP
2331 for (i = 1; i < argc; i++) {
2332 const char *a = argv[i];
2333
2334 if (*a != '-' || !strcmp(a, "--"))
2335 break;
cc44c765 2336 else if (!prefixcmp(a, "--date-format=")) {
63e0c8b3
SP
2337 const char *fmt = a + 14;
2338 if (!strcmp(fmt, "raw"))
2339 whenspec = WHENSPEC_RAW;
2340 else if (!strcmp(fmt, "rfc2822"))
2341 whenspec = WHENSPEC_RFC2822;
2342 else if (!strcmp(fmt, "now"))
2343 whenspec = WHENSPEC_NOW;
2344 else
2345 die("unknown --date-format argument %s", fmt);
2346 }
cc44c765 2347 else if (!prefixcmp(a, "--max-pack-size="))
0ea9f045 2348 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
cc44c765 2349 else if (!prefixcmp(a, "--depth="))
d5c57b28 2350 max_depth = strtoul(a + 8, NULL, 0);
cc44c765 2351 else if (!prefixcmp(a, "--active-branches="))
d5c57b28 2352 max_active_branches = strtoul(a + 18, NULL, 0);
e8438420
SP
2353 else if (!prefixcmp(a, "--import-marks="))
2354 import_marks(a + 15);
cc44c765 2355 else if (!prefixcmp(a, "--export-marks="))
a6a1a831 2356 mark_file = a + 15;
cc44c765 2357 else if (!prefixcmp(a, "--export-pack-edges=")) {
bdf1c06d
SP
2358 if (pack_edges)
2359 fclose(pack_edges);
2360 pack_edges = fopen(a + 20, "a");
2361 if (!pack_edges)
2362 die("Cannot open %s: %s", a + 20, strerror(errno));
2363 } else if (!strcmp(a, "--force"))
7073e69e 2364 force_update = 1;
c499d768
SP
2365 else if (!strcmp(a, "--quiet"))
2366 show_stats = 0;
2367 else if (!strcmp(a, "--stats"))
2368 show_stats = 1;
d5c57b28
SP
2369 else
2370 die("unknown option %s", a);
2371 }
8455e484 2372 if (i != argc)
d5c57b28 2373 usage(fast_import_usage);
d5c57b28 2374
904b1941
SP
2375 rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
2376 for (i = 0; i < (cmd_save - 1); i++)
2377 rc_free[i].next = &rc_free[i + 1];
2378 rc_free[cmd_save - 1].next = NULL;
2379
a5c1780a 2380 prepare_packed_git();
f70b6534 2381 start_packfile();
8acb3297 2382 set_die_routine(die_nicely);
db5e523f 2383 for (;;) {
c44cdc7e
SP
2384 read_next_command();
2385 if (command_buf.eof)
db5e523f 2386 break;
c44cdc7e
SP
2387 else if (!strcmp("blob", command_buf.buf))
2388 cmd_new_blob();
599065a3 2389 else if (!prefixcmp(command_buf.buf, "commit "))
c44cdc7e 2390 cmd_new_commit();
599065a3 2391 else if (!prefixcmp(command_buf.buf, "tag "))
72303d44 2392 cmd_new_tag();
599065a3 2393 else if (!prefixcmp(command_buf.buf, "reset "))
5fced8dc 2394 cmd_reset_branch();
7bfe6e26
SP
2395 else if (!strcmp("checkpoint", command_buf.buf))
2396 cmd_checkpoint();
ac053c02
SP
2397 else if (!prefixcmp(command_buf.buf, "progress "))
2398 cmd_progress();
c44cdc7e
SP
2399 else
2400 die("Unsupported command: %s", command_buf.buf);
db5e523f 2401 }
f70b6534 2402 end_packfile();
c44cdc7e 2403
463acbe1 2404 dump_branches();
72303d44 2405 dump_tags();
8455e484 2406 unkeep_all_packs();
a6a1a831 2407 dump_marks();
8bcce301 2408
bdf1c06d
SP
2409 if (pack_edges)
2410 fclose(pack_edges);
2411
c499d768
SP
2412 if (show_stats) {
2413 uintmax_t total_count = 0, duplicate_count = 0;
2414 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
2415 total_count += object_count_by_type[i];
2416 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
2417 duplicate_count += duplicate_count_by_type[i];
2418
2419 fprintf(stderr, "%s statistics:\n", argv[0]);
2420 fprintf(stderr, "---------------------------------------------------------------------\n");
3efb1f34
JR
2421 fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
2422 fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
2423 fprintf(stderr, " blobs : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
2424 fprintf(stderr, " trees : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
2425 fprintf(stderr, " commits: %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
2426 fprintf(stderr, " tags : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
c499d768 2427 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
3efb1f34 2428 fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
c499d768 2429 fprintf(stderr, " atoms: %10u\n", atom_cnt);
3efb1f34 2430 fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
40db58b8 2431 fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024));
3efb1f34 2432 fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
c499d768
SP
2433 fprintf(stderr, "---------------------------------------------------------------------\n");
2434 pack_report();
2435 fprintf(stderr, "---------------------------------------------------------------------\n");
2436 fprintf(stderr, "\n");
2437 }
db5e523f 2438
7073e69e 2439 return failure ? 1 : 0;
db5e523f 2440}