]>
Commit | Line | Data |
---|---|---|
463acbe1 SP |
1 | /* |
2 | Format of STDIN stream: | |
3 | ||
4 | stream ::= cmd*; | |
5 | ||
6 | cmd ::= new_blob | |
c44cdc7e | 7 | | new_commit |
463acbe1 SP |
8 | | new_tag |
9 | ; | |
10 | ||
c44cdc7e SP |
11 | new_blob ::= 'blob' lf |
12 | mark? | |
13 | file_content; | |
14 | file_content ::= data; | |
463acbe1 | 15 | |
c44cdc7e | 16 | new_commit ::= 'commit' sp ref_str lf |
00e2b884 SP |
17 | mark? |
18 | ('author' sp name '<' email '>' ts tz lf)? | |
19 | 'committer' sp name '<' email '>' ts tz lf | |
20 | commit_msg | |
02f3389d | 21 | ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)? |
c44cdc7e SP |
22 | file_change* |
23 | lf; | |
24 | commit_msg ::= data; | |
463acbe1 | 25 | |
c44cdc7e SP |
26 | file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf |
27 | | 'D' sp path_str lf | |
463acbe1 | 28 | ; |
c44cdc7e SP |
29 | mode ::= '644' | '755'; |
30 | ||
31 | new_tag ::= 'tag' sp tag_str lf | |
32 | 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf | |
33 | 'tagger' sp name '<' email '>' ts tz lf | |
34 | tag_msg; | |
35 | tag_msg ::= data; | |
36 | ||
37 | # note: the first idnum in a stream should be 1 and subsequent | |
38 | # idnums should not have gaps between values as this will cause | |
39 | # the stream parser to reserve space for the gapped values. An | |
40 | # idnum can be updated in the future to a new object by issuing | |
41 | # a new mark directive with the old idnum. | |
42 | # | |
43 | mark ::= 'mark' sp idnum lf; | |
44 | ||
45 | # note: declen indicates the length of binary_data in bytes. | |
46 | # declen does not include the lf preceeding or trailing the | |
47 | # binary data. | |
48 | # | |
49 | data ::= 'data' sp declen lf | |
50 | binary_data | |
51 | lf; | |
52 | ||
53 | # note: quoted strings are C-style quoting supporting \c for | |
54 | # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn | |
55 | # is the signed byte value in octal. Note that the only | |
56 | # characters which must actually be escaped to protect the | |
57 | # stream formatting is: \, " and LF. Otherwise these values | |
58 | # are UTF8. | |
59 | # | |
60 | ref_str ::= ref | '"' quoted(ref) '"' ; | |
61 | sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ; | |
62 | tag_str ::= tag | '"' quoted(tag) '"' ; | |
63 | path_str ::= path | '"' quoted(path) '"' ; | |
64 | ||
65 | declen ::= # unsigned 32 bit value, ascii base10 notation; | |
463acbe1 | 66 | binary_data ::= # file content, not interpreted; |
c44cdc7e | 67 | |
463acbe1 SP |
68 | sp ::= # ASCII space character; |
69 | lf ::= # ASCII newline (LF) character; | |
c44cdc7e SP |
70 | |
71 | # note: a colon (':') must precede the numerical value assigned to | |
72 | # an idnum. This is to distinguish it from a ref or tag name as | |
73 | # GIT does not permit ':' in ref or tag strings. | |
74 | # | |
75 | idnum ::= ':' declen; | |
76 | path ::= # GIT style file path, e.g. "a/b/c"; | |
77 | ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT"; | |
78 | tag ::= # GIT tag name, e.g. "FIREFOX_1_5"; | |
463acbe1 SP |
79 | sha1exp ::= # Any valid GIT SHA1 expression; |
80 | hexsha1 ::= # SHA1 in hexadecimal format; | |
c44cdc7e SP |
81 | |
82 | # note: name and email are UTF8 strings, however name must not | |
83 | # contain '<' or lf and email must not contain any of the | |
84 | # following: '<', '>', lf. | |
85 | # | |
86 | name ::= # valid GIT author/committer name; | |
463acbe1 | 87 | email ::= # valid GIT author/committer email; |
c44cdc7e SP |
88 | ts ::= # time since the epoch in seconds, ascii base10 notation; |
89 | tz ::= # GIT style timezone; | |
463acbe1 SP |
90 | */ |
91 | ||
db5e523f SP |
92 | #include "builtin.h" |
93 | #include "cache.h" | |
94 | #include "object.h" | |
95 | #include "blob.h" | |
463acbe1 | 96 | #include "tree.h" |
db5e523f SP |
97 | #include "delta.h" |
98 | #include "pack.h" | |
463acbe1 | 99 | #include "refs.h" |
db5e523f | 100 | #include "csum-file.h" |
c44cdc7e SP |
101 | #include "strbuf.h" |
102 | #include "quote.h" | |
db5e523f | 103 | |
27d6d290 SP |
104 | struct object_entry |
105 | { | |
106 | struct object_entry *next; | |
7111feed | 107 | enum object_type type; |
27d6d290 SP |
108 | unsigned long offset; |
109 | unsigned char sha1[20]; | |
110 | }; | |
111 | ||
463acbe1 | 112 | struct object_entry_pool |
27d6d290 | 113 | { |
463acbe1 | 114 | struct object_entry_pool *next_pool; |
27d6d290 SP |
115 | struct object_entry *next_free; |
116 | struct object_entry *end; | |
ac47a738 | 117 | struct object_entry entries[FLEX_ARRAY]; /* more */ |
27d6d290 SP |
118 | }; |
119 | ||
d8397168 SP |
120 | struct mark_set |
121 | { | |
122 | int shift; | |
123 | union { | |
124 | struct object_entry *marked[1024]; | |
125 | struct mark_set *sets[1024]; | |
126 | } data; | |
127 | }; | |
128 | ||
ac47a738 SP |
129 | struct last_object |
130 | { | |
131 | void *data; | |
6bb5b329 SP |
132 | unsigned int len; |
133 | unsigned int depth; | |
ac47a738 SP |
134 | unsigned char sha1[20]; |
135 | }; | |
136 | ||
463acbe1 SP |
137 | struct mem_pool |
138 | { | |
139 | struct mem_pool *next_pool; | |
140 | char *next_free; | |
141 | char *end; | |
142 | char space[FLEX_ARRAY]; /* more */ | |
143 | }; | |
144 | ||
145 | struct atom_str | |
146 | { | |
147 | struct atom_str *next_atom; | |
148 | int str_len; | |
149 | char str_dat[FLEX_ARRAY]; /* more */ | |
150 | }; | |
151 | ||
152 | struct tree_content; | |
6bb5b329 SP |
153 | struct tree_entry |
154 | { | |
463acbe1 SP |
155 | struct tree_content *tree; |
156 | struct atom_str* name; | |
157 | unsigned int mode; | |
6bb5b329 | 158 | unsigned char sha1[20]; |
6bb5b329 SP |
159 | }; |
160 | ||
463acbe1 | 161 | struct tree_content |
6bb5b329 | 162 | { |
463acbe1 SP |
163 | unsigned int entry_capacity; /* must match avail_tree_content */ |
164 | unsigned int entry_count; | |
165 | struct tree_entry *entries[FLEX_ARRAY]; /* more */ | |
166 | }; | |
167 | ||
168 | struct avail_tree_content | |
169 | { | |
170 | unsigned int entry_capacity; /* must match tree_content */ | |
171 | struct avail_tree_content *next_avail; | |
6bb5b329 SP |
172 | }; |
173 | ||
174 | struct branch | |
175 | { | |
463acbe1 SP |
176 | struct branch *table_next_branch; |
177 | struct branch *active_next_branch; | |
6bb5b329 | 178 | const char *name; |
463acbe1 SP |
179 | unsigned long last_commit; |
180 | struct tree_entry branch_tree; | |
181 | unsigned char sha1[20]; | |
6bb5b329 SP |
182 | }; |
183 | ||
72303d44 SP |
184 | struct tag |
185 | { | |
186 | struct tag *next_tag; | |
187 | const char *name; | |
188 | unsigned char sha1[20]; | |
189 | }; | |
190 | ||
463acbe1 SP |
191 | |
192 | /* Stats and misc. counters */ | |
d5c57b28 | 193 | static unsigned long max_depth = 10; |
27d6d290 | 194 | static unsigned long alloc_count; |
6bb5b329 | 195 | static unsigned long branch_count; |
d6c7eb2c | 196 | static unsigned long branch_load_count; |
41e5257f | 197 | static unsigned long remap_count; |
db5e523f | 198 | static unsigned long object_count; |
8bcce301 | 199 | static unsigned long duplicate_count; |
d8397168 | 200 | static unsigned long marks_set_count; |
6143f064 SP |
201 | static unsigned long object_count_by_type[9]; |
202 | static unsigned long duplicate_count_by_type[9]; | |
ac47a738 | 203 | |
463acbe1 SP |
204 | /* Memory pools */ |
205 | static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool); | |
206 | static size_t total_allocd; | |
207 | static struct mem_pool *mem_pool; | |
208 | ||
c44cdc7e | 209 | /* Atom management */ |
463acbe1 SP |
210 | static unsigned int atom_table_sz = 4451; |
211 | static unsigned int atom_cnt; | |
212 | static struct atom_str **atom_table; | |
213 | ||
214 | /* The .pack file being generated */ | |
ac47a738 | 215 | static int pack_fd; |
41e5257f | 216 | static unsigned long pack_size; |
ac47a738 | 217 | static unsigned char pack_sha1[20]; |
53dbce78 SP |
218 | static unsigned char* pack_base; |
219 | static unsigned long pack_moff; | |
220 | static unsigned long pack_mlen = 128*1024*1024; | |
221 | static unsigned long page_size; | |
ac47a738 SP |
222 | |
223 | /* Table of objects we've written. */ | |
463acbe1 SP |
224 | static unsigned int object_entry_alloc = 1000; |
225 | static struct object_entry_pool *blocks; | |
226 | static struct object_entry *object_table[1 << 16]; | |
d8397168 | 227 | static struct mark_set *marks; |
a6a1a831 | 228 | static const char* mark_file; |
ac47a738 SP |
229 | |
230 | /* Our last blob */ | |
463acbe1 SP |
231 | static struct last_object last_blob; |
232 | ||
233 | /* Tree management */ | |
234 | static unsigned int tree_entry_alloc = 1000; | |
235 | static void *avail_tree_entry; | |
236 | static unsigned int avail_tree_table_sz = 100; | |
237 | static struct avail_tree_content **avail_tree_table; | |
8bcce301 | 238 | |
6bb5b329 | 239 | /* Branch data */ |
d5c57b28 SP |
240 | static unsigned long max_active_branches = 5; |
241 | static unsigned long cur_active_branches; | |
242 | static unsigned long branch_table_sz = 1039; | |
463acbe1 SP |
243 | static struct branch **branch_table; |
244 | static struct branch *active_branches; | |
245 | ||
72303d44 SP |
246 | /* Tag data */ |
247 | static struct tag *first_tag; | |
248 | static struct tag *last_tag; | |
249 | ||
c44cdc7e SP |
250 | /* Input stream parsing */ |
251 | static struct strbuf command_buf; | |
d8397168 | 252 | static unsigned long next_mark; |
264244a0 | 253 | static FILE* branch_log; |
c44cdc7e | 254 | |
6bb5b329 | 255 | |
27d6d290 | 256 | static void alloc_objects(int cnt) |
8bcce301 | 257 | { |
463acbe1 | 258 | struct object_entry_pool *b; |
27d6d290 | 259 | |
463acbe1 | 260 | b = xmalloc(sizeof(struct object_entry_pool) |
27d6d290 | 261 | + cnt * sizeof(struct object_entry)); |
463acbe1 | 262 | b->next_pool = blocks; |
27d6d290 SP |
263 | b->next_free = b->entries; |
264 | b->end = b->entries + cnt; | |
265 | blocks = b; | |
266 | alloc_count += cnt; | |
267 | } | |
8bcce301 | 268 | |
27d6d290 | 269 | static struct object_entry* new_object(unsigned char *sha1) |
8bcce301 | 270 | { |
27d6d290 | 271 | struct object_entry *e; |
8bcce301 | 272 | |
27d6d290 | 273 | if (blocks->next_free == blocks->end) |
463acbe1 | 274 | alloc_objects(object_entry_alloc); |
8bcce301 | 275 | |
27d6d290 SP |
276 | e = blocks->next_free++; |
277 | memcpy(e->sha1, sha1, sizeof(e->sha1)); | |
278 | return e; | |
8bcce301 SP |
279 | } |
280 | ||
463acbe1 SP |
281 | static struct object_entry* find_object(unsigned char *sha1) |
282 | { | |
283 | unsigned int h = sha1[0] << 8 | sha1[1]; | |
284 | struct object_entry *e; | |
285 | for (e = object_table[h]; e; e = e->next) | |
286 | if (!memcmp(sha1, e->sha1, sizeof(e->sha1))) | |
287 | return e; | |
288 | return NULL; | |
289 | } | |
290 | ||
8bcce301 SP |
291 | static struct object_entry* insert_object(unsigned char *sha1) |
292 | { | |
293 | unsigned int h = sha1[0] << 8 | sha1[1]; | |
27d6d290 | 294 | struct object_entry *e = object_table[h]; |
463acbe1 | 295 | struct object_entry *p = NULL; |
8bcce301 SP |
296 | |
297 | while (e) { | |
298 | if (!memcmp(sha1, e->sha1, sizeof(e->sha1))) | |
299 | return e; | |
300 | p = e; | |
301 | e = e->next; | |
302 | } | |
303 | ||
304 | e = new_object(sha1); | |
463acbe1 | 305 | e->next = NULL; |
8bcce301 SP |
306 | e->offset = 0; |
307 | if (p) | |
308 | p->next = e; | |
309 | else | |
27d6d290 | 310 | object_table[h] = e; |
8bcce301 SP |
311 | return e; |
312 | } | |
db5e523f | 313 | |
463acbe1 SP |
314 | static unsigned int hc_str(const char *s, size_t len) |
315 | { | |
316 | unsigned int r = 0; | |
317 | while (len-- > 0) | |
318 | r = r * 31 + *s++; | |
319 | return r; | |
320 | } | |
321 | ||
322 | static void* pool_alloc(size_t len) | |
323 | { | |
324 | struct mem_pool *p; | |
325 | void *r; | |
326 | ||
327 | for (p = mem_pool; p; p = p->next_pool) | |
328 | if ((p->end - p->next_free >= len)) | |
329 | break; | |
330 | ||
331 | if (!p) { | |
332 | if (len >= (mem_pool_alloc/2)) { | |
333 | total_allocd += len; | |
334 | return xmalloc(len); | |
335 | } | |
336 | total_allocd += sizeof(struct mem_pool) + mem_pool_alloc; | |
337 | p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc); | |
338 | p->next_pool = mem_pool; | |
339 | p->next_free = p->space; | |
340 | p->end = p->next_free + mem_pool_alloc; | |
341 | mem_pool = p; | |
342 | } | |
343 | ||
344 | r = p->next_free; | |
8d8928b0 SP |
345 | /* round out to a pointer alignment */ |
346 | if (len & (sizeof(void*) - 1)) | |
347 | len += sizeof(void*) - (len & (sizeof(void*) - 1)); | |
463acbe1 SP |
348 | p->next_free += len; |
349 | return r; | |
350 | } | |
351 | ||
352 | static void* pool_calloc(size_t count, size_t size) | |
353 | { | |
354 | size_t len = count * size; | |
355 | void *r = pool_alloc(len); | |
356 | memset(r, 0, len); | |
357 | return r; | |
358 | } | |
359 | ||
360 | static char* pool_strdup(const char *s) | |
361 | { | |
362 | char *r = pool_alloc(strlen(s) + 1); | |
363 | strcpy(r, s); | |
364 | return r; | |
365 | } | |
366 | ||
d8397168 SP |
367 | static void insert_mark(unsigned long idnum, struct object_entry *oe) |
368 | { | |
369 | struct mark_set *s = marks; | |
370 | while ((idnum >> s->shift) >= 1024) { | |
371 | s = pool_calloc(1, sizeof(struct mark_set)); | |
372 | s->shift = marks->shift + 10; | |
373 | s->data.sets[0] = marks; | |
374 | marks = s; | |
375 | } | |
376 | while (s->shift) { | |
377 | unsigned long i = idnum >> s->shift; | |
378 | idnum -= i << s->shift; | |
379 | if (!s->data.sets[i]) { | |
380 | s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set)); | |
381 | s->data.sets[i]->shift = s->shift - 10; | |
382 | } | |
383 | s = s->data.sets[i]; | |
384 | } | |
385 | if (!s->data.marked[idnum]) | |
386 | marks_set_count++; | |
387 | s->data.marked[idnum] = oe; | |
388 | } | |
389 | ||
390 | static struct object_entry* find_mark(unsigned long idnum) | |
391 | { | |
392 | unsigned long orig_idnum = idnum; | |
393 | struct mark_set *s = marks; | |
394 | struct object_entry *oe = NULL; | |
395 | if ((idnum >> s->shift) < 1024) { | |
396 | while (s && s->shift) { | |
397 | unsigned long i = idnum >> s->shift; | |
398 | idnum -= i << s->shift; | |
399 | s = s->data.sets[i]; | |
400 | } | |
401 | if (s) | |
402 | oe = s->data.marked[idnum]; | |
403 | } | |
404 | if (!oe) | |
405 | die("mark :%lu not declared", orig_idnum); | |
406 | return oe; | |
407 | } | |
408 | ||
463acbe1 SP |
409 | static struct atom_str* to_atom(const char *s, size_t len) |
410 | { | |
411 | unsigned int hc = hc_str(s, len) % atom_table_sz; | |
412 | struct atom_str *c; | |
413 | ||
414 | for (c = atom_table[hc]; c; c = c->next_atom) | |
415 | if (c->str_len == len && !strncmp(s, c->str_dat, len)) | |
416 | return c; | |
417 | ||
418 | c = pool_alloc(sizeof(struct atom_str) + len + 1); | |
419 | c->str_len = len; | |
420 | strncpy(c->str_dat, s, len); | |
421 | c->str_dat[len] = 0; | |
422 | c->next_atom = atom_table[hc]; | |
423 | atom_table[hc] = c; | |
424 | atom_cnt++; | |
425 | return c; | |
426 | } | |
427 | ||
428 | static struct branch* lookup_branch(const char *name) | |
429 | { | |
430 | unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; | |
431 | struct branch *b; | |
432 | ||
433 | for (b = branch_table[hc]; b; b = b->table_next_branch) | |
434 | if (!strcmp(name, b->name)) | |
435 | return b; | |
436 | return NULL; | |
437 | } | |
438 | ||
439 | static struct branch* new_branch(const char *name) | |
440 | { | |
441 | unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; | |
442 | struct branch* b = lookup_branch(name); | |
443 | ||
444 | if (b) | |
445 | die("Invalid attempt to create duplicate branch: %s", name); | |
c44cdc7e SP |
446 | if (check_ref_format(name)) |
447 | die("Branch name doesn't conform to GIT standards: %s", name); | |
463acbe1 SP |
448 | |
449 | b = pool_calloc(1, sizeof(struct branch)); | |
450 | b->name = pool_strdup(name); | |
451 | b->table_next_branch = branch_table[hc]; | |
452 | branch_table[hc] = b; | |
453 | branch_count++; | |
454 | return b; | |
455 | } | |
456 | ||
457 | static unsigned int hc_entries(unsigned int cnt) | |
458 | { | |
459 | cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8; | |
460 | return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1; | |
461 | } | |
462 | ||
463 | static struct tree_content* new_tree_content(unsigned int cnt) | |
464 | { | |
465 | struct avail_tree_content *f, *l = NULL; | |
466 | struct tree_content *t; | |
467 | unsigned int hc = hc_entries(cnt); | |
468 | ||
469 | for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail) | |
470 | if (f->entry_capacity >= cnt) | |
471 | break; | |
472 | ||
473 | if (f) { | |
474 | if (l) | |
475 | l->next_avail = f->next_avail; | |
476 | else | |
477 | avail_tree_table[hc] = f->next_avail; | |
478 | } else { | |
479 | cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt; | |
480 | f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt); | |
481 | f->entry_capacity = cnt; | |
482 | } | |
483 | ||
484 | t = (struct tree_content*)f; | |
485 | t->entry_count = 0; | |
486 | return t; | |
487 | } | |
488 | ||
489 | static void release_tree_entry(struct tree_entry *e); | |
490 | static void release_tree_content(struct tree_content *t) | |
491 | { | |
492 | struct avail_tree_content *f = (struct avail_tree_content*)t; | |
493 | unsigned int hc = hc_entries(f->entry_capacity); | |
afde8dd9 SP |
494 | f->next_avail = avail_tree_table[hc]; |
495 | avail_tree_table[hc] = f; | |
496 | } | |
497 | ||
498 | static void release_tree_content_recursive(struct tree_content *t) | |
499 | { | |
463acbe1 SP |
500 | unsigned int i; |
501 | for (i = 0; i < t->entry_count; i++) | |
502 | release_tree_entry(t->entries[i]); | |
afde8dd9 | 503 | release_tree_content(t); |
463acbe1 SP |
504 | } |
505 | ||
506 | static struct tree_content* grow_tree_content( | |
507 | struct tree_content *t, | |
508 | int amt) | |
509 | { | |
510 | struct tree_content *r = new_tree_content(t->entry_count + amt); | |
511 | r->entry_count = t->entry_count; | |
512 | memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0])); | |
513 | release_tree_content(t); | |
514 | return r; | |
515 | } | |
516 | ||
517 | static struct tree_entry* new_tree_entry() | |
518 | { | |
519 | struct tree_entry *e; | |
520 | ||
521 | if (!avail_tree_entry) { | |
522 | unsigned int n = tree_entry_alloc; | |
8435a9cb | 523 | total_allocd += n * sizeof(struct tree_entry); |
463acbe1 | 524 | avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry)); |
2eb26d84 | 525 | while (n-- > 1) { |
463acbe1 SP |
526 | *((void**)e) = e + 1; |
527 | e++; | |
528 | } | |
35ef237c | 529 | *((void**)e) = NULL; |
463acbe1 SP |
530 | } |
531 | ||
532 | e = avail_tree_entry; | |
533 | avail_tree_entry = *((void**)e); | |
534 | return e; | |
535 | } | |
536 | ||
537 | static void release_tree_entry(struct tree_entry *e) | |
538 | { | |
539 | if (e->tree) | |
afde8dd9 | 540 | release_tree_content_recursive(e->tree); |
463acbe1 SP |
541 | *((void**)e) = avail_tree_entry; |
542 | avail_tree_entry = e; | |
543 | } | |
544 | ||
545 | static void yread(int fd, void *buffer, size_t length) | |
db5e523f SP |
546 | { |
547 | ssize_t ret = 0; | |
548 | while (ret < length) { | |
549 | ssize_t size = xread(fd, (char *) buffer + ret, length - ret); | |
463acbe1 SP |
550 | if (!size) |
551 | die("Read from descriptor %i: end of stream", fd); | |
552 | if (size < 0) | |
553 | die("Read from descriptor %i: %s", fd, strerror(errno)); | |
554 | ret += size; | |
555 | } | |
556 | } | |
557 | ||
463acbe1 | 558 | static void ywrite(int fd, void *buffer, size_t length) |
db5e523f SP |
559 | { |
560 | ssize_t ret = 0; | |
561 | while (ret < length) { | |
562 | ssize_t size = xwrite(fd, (char *) buffer + ret, length - ret); | |
463acbe1 SP |
563 | if (!size) |
564 | die("Write to descriptor %i: end of file", fd); | |
565 | if (size < 0) | |
566 | die("Write to descriptor %i: %s", fd, strerror(errno)); | |
db5e523f SP |
567 | ret += size; |
568 | } | |
db5e523f SP |
569 | } |
570 | ||
c44cdc7e | 571 | static size_t encode_header( |
ac47a738 | 572 | enum object_type type, |
c44cdc7e | 573 | size_t size, |
ac47a738 | 574 | unsigned char *hdr) |
db5e523f SP |
575 | { |
576 | int n = 1; | |
577 | unsigned char c; | |
578 | ||
579 | if (type < OBJ_COMMIT || type > OBJ_DELTA) | |
580 | die("bad type %d", type); | |
581 | ||
582 | c = (type << 4) | (size & 15); | |
583 | size >>= 4; | |
584 | while (size) { | |
585 | *hdr++ = c | 0x80; | |
586 | c = size & 0x7f; | |
587 | size >>= 7; | |
588 | n++; | |
589 | } | |
590 | *hdr = c; | |
591 | return n; | |
592 | } | |
593 | ||
ac47a738 SP |
594 | static int store_object( |
595 | enum object_type type, | |
596 | void *dat, | |
c44cdc7e | 597 | size_t datlen, |
6bb5b329 | 598 | struct last_object *last, |
d8397168 SP |
599 | unsigned char *sha1out, |
600 | unsigned long mark) | |
db5e523f | 601 | { |
db5e523f | 602 | void *out, *delta; |
ac47a738 SP |
603 | struct object_entry *e; |
604 | unsigned char hdr[96]; | |
605 | unsigned char sha1[20]; | |
db5e523f | 606 | unsigned long hdrlen, deltalen; |
ac47a738 SP |
607 | SHA_CTX c; |
608 | z_stream s; | |
609 | ||
610 | hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1; | |
611 | SHA1_Init(&c); | |
612 | SHA1_Update(&c, hdr, hdrlen); | |
613 | SHA1_Update(&c, dat, datlen); | |
614 | SHA1_Final(sha1, &c); | |
6bb5b329 SP |
615 | if (sha1out) |
616 | memcpy(sha1out, sha1, sizeof(sha1)); | |
ac47a738 SP |
617 | |
618 | e = insert_object(sha1); | |
d8397168 SP |
619 | if (mark) |
620 | insert_mark(mark, e); | |
ac47a738 SP |
621 | if (e->offset) { |
622 | duplicate_count++; | |
6143f064 | 623 | duplicate_count_by_type[type]++; |
463acbe1 | 624 | return 1; |
ac47a738 | 625 | } |
7111feed | 626 | e->type = type; |
41e5257f | 627 | e->offset = pack_size; |
ac47a738 | 628 | object_count++; |
6143f064 | 629 | object_count_by_type[type]++; |
db5e523f | 630 | |
463acbe1 | 631 | if (last && last->data && last->depth < max_depth) |
ac47a738 | 632 | delta = diff_delta(last->data, last->len, |
db5e523f SP |
633 | dat, datlen, |
634 | &deltalen, 0); | |
ac47a738 | 635 | else |
db5e523f SP |
636 | delta = 0; |
637 | ||
638 | memset(&s, 0, sizeof(s)); | |
639 | deflateInit(&s, zlib_compression_level); | |
640 | ||
641 | if (delta) { | |
ac47a738 | 642 | last->depth++; |
db5e523f SP |
643 | s.next_in = delta; |
644 | s.avail_in = deltalen; | |
645 | hdrlen = encode_header(OBJ_DELTA, deltalen, hdr); | |
463acbe1 SP |
646 | ywrite(pack_fd, hdr, hdrlen); |
647 | ywrite(pack_fd, last->sha1, sizeof(sha1)); | |
41e5257f | 648 | pack_size += hdrlen + sizeof(sha1); |
db5e523f | 649 | } else { |
463acbe1 SP |
650 | if (last) |
651 | last->depth = 0; | |
db5e523f SP |
652 | s.next_in = dat; |
653 | s.avail_in = datlen; | |
ac47a738 | 654 | hdrlen = encode_header(type, datlen, hdr); |
463acbe1 | 655 | ywrite(pack_fd, hdr, hdrlen); |
41e5257f | 656 | pack_size += hdrlen; |
db5e523f SP |
657 | } |
658 | ||
659 | s.avail_out = deflateBound(&s, s.avail_in); | |
660 | s.next_out = out = xmalloc(s.avail_out); | |
661 | while (deflate(&s, Z_FINISH) == Z_OK) | |
662 | /* nothing */; | |
663 | deflateEnd(&s); | |
664 | ||
463acbe1 | 665 | ywrite(pack_fd, out, s.total_out); |
41e5257f | 666 | pack_size += s.total_out; |
db5e523f SP |
667 | |
668 | free(out); | |
669 | if (delta) | |
670 | free(delta); | |
463acbe1 SP |
671 | if (last) { |
672 | if (last->data) | |
673 | free(last->data); | |
674 | last->data = dat; | |
675 | last->len = datlen; | |
676 | memcpy(last->sha1, sha1, sizeof(sha1)); | |
677 | } | |
678 | return 0; | |
679 | } | |
680 | ||
53dbce78 | 681 | static unsigned char* map_pack(unsigned long offset, unsigned int *left) |
41e5257f SP |
682 | { |
683 | if (offset >= pack_size) | |
684 | die("object offset outside of pack file"); | |
53dbce78 SP |
685 | if (!pack_base |
686 | || offset < pack_moff | |
687 | || (offset + 20) >= (pack_moff + pack_mlen)) { | |
41e5257f SP |
688 | if (pack_base) |
689 | munmap(pack_base, pack_mlen); | |
53dbce78 SP |
690 | pack_moff = (offset / page_size) * page_size; |
691 | pack_base = mmap(NULL,pack_mlen,PROT_READ,MAP_SHARED, | |
692 | pack_fd,pack_moff); | |
41e5257f SP |
693 | if (pack_base == MAP_FAILED) |
694 | die("Failed to map generated pack: %s", strerror(errno)); | |
695 | remap_count++; | |
696 | } | |
53dbce78 SP |
697 | offset -= pack_moff; |
698 | if (left) | |
699 | *left = pack_mlen - offset; | |
700 | return pack_base + offset; | |
41e5257f SP |
701 | } |
702 | ||
703 | static unsigned long unpack_object_header(unsigned long offset, | |
704 | enum object_type *type, | |
705 | unsigned long *sizep) | |
706 | { | |
707 | unsigned shift; | |
708 | unsigned char c; | |
709 | unsigned long size; | |
710 | ||
53dbce78 | 711 | c = *map_pack(offset++, NULL); |
41e5257f SP |
712 | *type = (c >> 4) & 7; |
713 | size = c & 15; | |
714 | shift = 4; | |
715 | while (c & 0x80) { | |
53dbce78 | 716 | c = *map_pack(offset++, NULL); |
41e5257f SP |
717 | size += (c & 0x7f) << shift; |
718 | shift += 7; | |
719 | } | |
720 | *sizep = size; | |
721 | return offset; | |
722 | } | |
723 | ||
724 | static void *unpack_non_delta_entry(unsigned long o, unsigned long sz) | |
725 | { | |
726 | z_stream stream; | |
727 | unsigned char *result; | |
728 | ||
729 | result = xmalloc(sz + 1); | |
730 | result[sz] = 0; | |
731 | ||
732 | memset(&stream, 0, sizeof(stream)); | |
53dbce78 | 733 | stream.next_in = map_pack(o, &stream.avail_in); |
41e5257f SP |
734 | stream.next_out = result; |
735 | stream.avail_out = sz; | |
736 | ||
737 | inflateInit(&stream); | |
738 | for (;;) { | |
739 | int st = inflate(&stream, Z_FINISH); | |
740 | if (st == Z_STREAM_END) | |
741 | break; | |
53dbce78 SP |
742 | if (st == Z_OK || st == Z_BUF_ERROR) { |
743 | o = stream.next_in - pack_base + pack_moff; | |
744 | stream.next_in = map_pack(o, &stream.avail_in); | |
41e5257f SP |
745 | continue; |
746 | } | |
53dbce78 | 747 | die("Error %i from zlib during inflate.", st); |
41e5257f SP |
748 | } |
749 | inflateEnd(&stream); | |
750 | if (stream.total_out != sz) | |
751 | die("Error after inflate: sizes mismatch"); | |
752 | return result; | |
753 | } | |
754 | ||
755 | static void *unpack_entry(unsigned long offset, unsigned long *sizep); | |
756 | ||
757 | static void *unpack_delta_entry(unsigned long offset, | |
758 | unsigned long delta_size, | |
759 | unsigned long *sizep) | |
760 | { | |
761 | struct object_entry *base_oe; | |
762 | unsigned char *base_sha1; | |
763 | void *delta_data, *base, *result; | |
764 | unsigned long base_size, result_size; | |
765 | ||
53dbce78 | 766 | base_sha1 = map_pack(offset, NULL); |
41e5257f SP |
767 | base_oe = find_object(base_sha1); |
768 | if (!base_oe) | |
769 | die("I'm broken; I can't find a base I know must be here."); | |
770 | base = unpack_entry(base_oe->offset, &base_size); | |
771 | delta_data = unpack_non_delta_entry(offset + 20, delta_size); | |
772 | result = patch_delta(base, base_size, | |
773 | delta_data, delta_size, | |
774 | &result_size); | |
775 | if (!result) | |
776 | die("failed to apply delta"); | |
777 | free(delta_data); | |
778 | free(base); | |
779 | *sizep = result_size; | |
780 | return result; | |
781 | } | |
782 | ||
783 | static void *unpack_entry(unsigned long offset, unsigned long *sizep) | |
784 | { | |
785 | unsigned long size; | |
786 | enum object_type kind; | |
787 | ||
788 | offset = unpack_object_header(offset, &kind, &size); | |
789 | switch (kind) { | |
790 | case OBJ_DELTA: | |
791 | return unpack_delta_entry(offset, size, sizep); | |
792 | case OBJ_COMMIT: | |
793 | case OBJ_TREE: | |
794 | case OBJ_BLOB: | |
795 | case OBJ_TAG: | |
796 | *sizep = size; | |
797 | return unpack_non_delta_entry(offset, size); | |
798 | default: | |
799 | die("I created an object I can't read!"); | |
800 | } | |
801 | } | |
802 | ||
463acbe1 SP |
803 | static const char *get_mode(const char *str, unsigned int *modep) |
804 | { | |
805 | unsigned char c; | |
806 | unsigned int mode = 0; | |
807 | ||
808 | while ((c = *str++) != ' ') { | |
809 | if (c < '0' || c > '7') | |
810 | return NULL; | |
811 | mode = (mode << 3) + (c - '0'); | |
812 | } | |
813 | *modep = mode; | |
814 | return str; | |
815 | } | |
816 | ||
817 | static void load_tree(struct tree_entry *root) | |
818 | { | |
819 | struct object_entry *myoe; | |
820 | struct tree_content *t; | |
821 | unsigned long size; | |
822 | char *buf; | |
823 | const char *c; | |
463acbe1 SP |
824 | |
825 | root->tree = t = new_tree_content(8); | |
826 | if (!memcmp(root->sha1, null_sha1, 20)) | |
827 | return; | |
828 | ||
829 | myoe = find_object(root->sha1); | |
830 | if (myoe) { | |
41e5257f SP |
831 | if (myoe->type != OBJ_TREE) |
832 | die("Not a tree: %s", sha1_to_hex(root->sha1)); | |
833 | buf = unpack_entry(myoe->offset, &size); | |
463acbe1 | 834 | } else { |
41e5257f | 835 | char type[20]; |
463acbe1 | 836 | buf = read_sha1_file(root->sha1, type, &size); |
00e2b884 | 837 | if (!buf || strcmp(type, tree_type)) |
41e5257f | 838 | die("Can't load tree %s", sha1_to_hex(root->sha1)); |
463acbe1 SP |
839 | } |
840 | ||
841 | c = buf; | |
842 | while (c != (buf + size)) { | |
843 | struct tree_entry *e = new_tree_entry(); | |
844 | ||
845 | if (t->entry_count == t->entry_capacity) | |
846 | root->tree = t = grow_tree_content(t, 8); | |
847 | t->entries[t->entry_count++] = e; | |
848 | ||
849 | e->tree = NULL; | |
850 | c = get_mode(c, &e->mode); | |
851 | if (!c) | |
852 | die("Corrupt mode in %s", sha1_to_hex(root->sha1)); | |
853 | e->name = to_atom(c, strlen(c)); | |
854 | c += e->name->str_len + 1; | |
855 | memcpy(e->sha1, c, sizeof(e->sha1)); | |
856 | c += 20; | |
857 | } | |
858 | free(buf); | |
859 | } | |
860 | ||
861 | static int tecmp (const void *_a, const void *_b) | |
862 | { | |
863 | struct tree_entry *a = *((struct tree_entry**)_a); | |
864 | struct tree_entry *b = *((struct tree_entry**)_b); | |
865 | return base_name_compare( | |
866 | a->name->str_dat, a->name->str_len, a->mode, | |
867 | b->name->str_dat, b->name->str_len, b->mode); | |
868 | } | |
869 | ||
870 | static void store_tree(struct tree_entry *root) | |
871 | { | |
872 | struct tree_content *t = root->tree; | |
873 | unsigned int i; | |
874 | size_t maxlen; | |
875 | char *buf, *c; | |
876 | ||
877 | if (memcmp(root->sha1, null_sha1, 20)) | |
878 | return; | |
879 | ||
880 | maxlen = 0; | |
881 | for (i = 0; i < t->entry_count; i++) { | |
882 | maxlen += t->entries[i]->name->str_len + 34; | |
883 | if (t->entries[i]->tree) | |
884 | store_tree(t->entries[i]); | |
885 | } | |
886 | ||
887 | qsort(t->entries, t->entry_count, sizeof(t->entries[0]), tecmp); | |
888 | buf = c = xmalloc(maxlen); | |
889 | for (i = 0; i < t->entry_count; i++) { | |
890 | struct tree_entry *e = t->entries[i]; | |
891 | c += sprintf(c, "%o", e->mode); | |
892 | *c++ = ' '; | |
893 | strcpy(c, e->name->str_dat); | |
894 | c += e->name->str_len + 1; | |
895 | memcpy(c, e->sha1, 20); | |
896 | c += 20; | |
897 | } | |
d8397168 | 898 | store_object(OBJ_TREE, buf, c - buf, NULL, root->sha1, 0); |
463acbe1 SP |
899 | free(buf); |
900 | } | |
901 | ||
902 | static int tree_content_set( | |
903 | struct tree_entry *root, | |
904 | const char *p, | |
905 | const unsigned char *sha1, | |
906 | const unsigned int mode) | |
907 | { | |
908 | struct tree_content *t = root->tree; | |
909 | const char *slash1; | |
910 | unsigned int i, n; | |
911 | struct tree_entry *e; | |
912 | ||
913 | slash1 = strchr(p, '/'); | |
914 | if (slash1) | |
915 | n = slash1 - p; | |
916 | else | |
917 | n = strlen(p); | |
918 | ||
919 | for (i = 0; i < t->entry_count; i++) { | |
920 | e = t->entries[i]; | |
921 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
922 | if (!slash1) { | |
923 | if (e->mode == mode && !memcmp(e->sha1, sha1, 20)) | |
924 | return 0; | |
925 | e->mode = mode; | |
926 | memcpy(e->sha1, sha1, 20); | |
927 | if (e->tree) { | |
afde8dd9 | 928 | release_tree_content_recursive(e->tree); |
463acbe1 SP |
929 | e->tree = NULL; |
930 | } | |
931 | memcpy(root->sha1, null_sha1, 20); | |
932 | return 1; | |
933 | } | |
934 | if (!S_ISDIR(e->mode)) { | |
935 | e->tree = new_tree_content(8); | |
7111feed | 936 | e->mode = S_IFDIR; |
463acbe1 SP |
937 | } |
938 | if (!e->tree) | |
939 | load_tree(e); | |
940 | if (tree_content_set(e, slash1 + 1, sha1, mode)) { | |
941 | memcpy(root->sha1, null_sha1, 20); | |
942 | return 1; | |
943 | } | |
944 | return 0; | |
945 | } | |
946 | } | |
947 | ||
948 | if (t->entry_count == t->entry_capacity) | |
949 | root->tree = t = grow_tree_content(t, 8); | |
950 | e = new_tree_entry(); | |
951 | e->name = to_atom(p, n); | |
952 | t->entries[t->entry_count++] = e; | |
953 | if (slash1) { | |
954 | e->tree = new_tree_content(8); | |
7111feed | 955 | e->mode = S_IFDIR; |
463acbe1 SP |
956 | tree_content_set(e, slash1 + 1, sha1, mode); |
957 | } else { | |
958 | e->tree = NULL; | |
959 | e->mode = mode; | |
960 | memcpy(e->sha1, sha1, 20); | |
961 | } | |
962 | memcpy(root->sha1, null_sha1, 20); | |
963 | return 1; | |
964 | } | |
965 | ||
966 | static int tree_content_remove(struct tree_entry *root, const char *p) | |
967 | { | |
968 | struct tree_content *t = root->tree; | |
969 | const char *slash1; | |
970 | unsigned int i, n; | |
971 | struct tree_entry *e; | |
972 | ||
973 | slash1 = strchr(p, '/'); | |
974 | if (slash1) | |
975 | n = slash1 - p; | |
976 | else | |
977 | n = strlen(p); | |
978 | ||
979 | for (i = 0; i < t->entry_count; i++) { | |
980 | e = t->entries[i]; | |
981 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
982 | if (!slash1 || !S_ISDIR(e->mode)) | |
983 | goto del_entry; | |
984 | if (!e->tree) | |
985 | load_tree(e); | |
986 | if (tree_content_remove(e, slash1 + 1)) { | |
987 | if (!e->tree->entry_count) | |
988 | goto del_entry; | |
989 | memcpy(root->sha1, null_sha1, 20); | |
990 | return 1; | |
991 | } | |
992 | return 0; | |
993 | } | |
994 | } | |
995 | return 0; | |
996 | ||
997 | del_entry: | |
998 | for (i++; i < t->entry_count; i++) | |
999 | t->entries[i-1] = t->entries[i]; | |
1000 | t->entry_count--; | |
1001 | release_tree_entry(e); | |
1002 | memcpy(root->sha1, null_sha1, 20); | |
ac47a738 | 1003 | return 1; |
db5e523f SP |
1004 | } |
1005 | ||
8bcce301 | 1006 | static void init_pack_header() |
db5e523f | 1007 | { |
c90be46a SP |
1008 | struct pack_header hdr; |
1009 | ||
1010 | hdr.hdr_signature = htonl(PACK_SIGNATURE); | |
1011 | hdr.hdr_version = htonl(2); | |
1012 | hdr.hdr_entries = 0; | |
1013 | ||
1014 | ywrite(pack_fd, &hdr, sizeof(hdr)); | |
41e5257f | 1015 | pack_size = sizeof(hdr); |
db5e523f SP |
1016 | } |
1017 | ||
8bcce301 | 1018 | static void fixup_header_footer() |
db5e523f SP |
1019 | { |
1020 | SHA_CTX c; | |
1021 | char hdr[8]; | |
db5e523f SP |
1022 | unsigned long cnt; |
1023 | char *buf; | |
1024 | size_t n; | |
1025 | ||
ac47a738 | 1026 | if (lseek(pack_fd, 0, SEEK_SET) != 0) |
db5e523f SP |
1027 | die("Failed seeking to start: %s", strerror(errno)); |
1028 | ||
1029 | SHA1_Init(&c); | |
463acbe1 | 1030 | yread(pack_fd, hdr, 8); |
db5e523f SP |
1031 | SHA1_Update(&c, hdr, 8); |
1032 | ||
db5e523f SP |
1033 | cnt = htonl(object_count); |
1034 | SHA1_Update(&c, &cnt, 4); | |
463acbe1 | 1035 | ywrite(pack_fd, &cnt, 4); |
db5e523f SP |
1036 | |
1037 | buf = xmalloc(128 * 1024); | |
1038 | for (;;) { | |
ac47a738 | 1039 | n = xread(pack_fd, buf, 128 * 1024); |
db5e523f SP |
1040 | if (n <= 0) |
1041 | break; | |
1042 | SHA1_Update(&c, buf, n); | |
1043 | } | |
1044 | free(buf); | |
1045 | ||
ac47a738 | 1046 | SHA1_Final(pack_sha1, &c); |
463acbe1 | 1047 | ywrite(pack_fd, pack_sha1, sizeof(pack_sha1)); |
db5e523f SP |
1048 | } |
1049 | ||
8bcce301 | 1050 | static int oecmp (const void *_a, const void *_b) |
db5e523f | 1051 | { |
8bcce301 SP |
1052 | struct object_entry *a = *((struct object_entry**)_a); |
1053 | struct object_entry *b = *((struct object_entry**)_b); | |
1054 | return memcmp(a->sha1, b->sha1, sizeof(a->sha1)); | |
1055 | } | |
1056 | ||
1057 | static void write_index(const char *idx_name) | |
1058 | { | |
1059 | struct sha1file *f; | |
1060 | struct object_entry **idx, **c, **last; | |
1061 | struct object_entry *e; | |
463acbe1 | 1062 | struct object_entry_pool *o; |
8bcce301 SP |
1063 | unsigned int array[256]; |
1064 | int i; | |
1065 | ||
1066 | /* Build the sorted table of object IDs. */ | |
1067 | idx = xmalloc(object_count * sizeof(struct object_entry*)); | |
1068 | c = idx; | |
463acbe1 | 1069 | for (o = blocks; o; o = o->next_pool) |
27d6d290 SP |
1070 | for (e = o->entries; e != o->next_free; e++) |
1071 | *c++ = e; | |
8bcce301 SP |
1072 | last = idx + object_count; |
1073 | qsort(idx, object_count, sizeof(struct object_entry*), oecmp); | |
1074 | ||
1075 | /* Generate the fan-out array. */ | |
1076 | c = idx; | |
1077 | for (i = 0; i < 256; i++) { | |
1078 | struct object_entry **next = c;; | |
1079 | while (next < last) { | |
1080 | if ((*next)->sha1[0] != i) | |
1081 | break; | |
1082 | next++; | |
1083 | } | |
1084 | array[i] = htonl(next - idx); | |
1085 | c = next; | |
1086 | } | |
1087 | ||
1088 | f = sha1create("%s", idx_name); | |
1089 | sha1write(f, array, 256 * sizeof(int)); | |
1090 | for (c = idx; c != last; c++) { | |
1091 | unsigned int offset = htonl((*c)->offset); | |
1092 | sha1write(f, &offset, 4); | |
1093 | sha1write(f, (*c)->sha1, sizeof((*c)->sha1)); | |
1094 | } | |
ac47a738 | 1095 | sha1write(f, pack_sha1, sizeof(pack_sha1)); |
8bcce301 SP |
1096 | sha1close(f, NULL, 1); |
1097 | free(idx); | |
1098 | } | |
1099 | ||
463acbe1 SP |
1100 | static void dump_branches() |
1101 | { | |
1102 | static const char *msg = "fast-import"; | |
1103 | unsigned int i; | |
1104 | struct branch *b; | |
1105 | struct ref_lock *lock; | |
1106 | ||
1107 | for (i = 0; i < branch_table_sz; i++) { | |
1108 | for (b = branch_table[i]; b; b = b->table_next_branch) { | |
1109 | lock = lock_any_ref_for_update(b->name, NULL, 0); | |
1110 | if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0) | |
1111 | die("Can't write %s", b->name); | |
1112 | } | |
1113 | } | |
1114 | } | |
1115 | ||
72303d44 SP |
1116 | static void dump_tags() |
1117 | { | |
1118 | static const char *msg = "fast-import"; | |
1119 | struct tag *t; | |
1120 | struct ref_lock *lock; | |
1121 | char path[PATH_MAX]; | |
1122 | ||
1123 | for (t = first_tag; t; t = t->next_tag) { | |
1124 | sprintf(path, "refs/tags/%s", t->name); | |
1125 | lock = lock_any_ref_for_update(path, NULL, 0); | |
1126 | if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0) | |
1127 | die("Can't write %s", path); | |
1128 | } | |
1129 | } | |
1130 | ||
a6a1a831 SP |
1131 | static void dump_marks_helper(FILE *f, |
1132 | unsigned long base, | |
1133 | struct mark_set *m) | |
1134 | { | |
1135 | int k; | |
1136 | if (m->shift) { | |
1137 | for (k = 0; k < 1024; k++) { | |
1138 | if (m->data.sets[k]) | |
1139 | dump_marks_helper(f, (base + k) << m->shift, | |
1140 | m->data.sets[k]); | |
1141 | } | |
1142 | } else { | |
1143 | for (k = 0; k < 1024; k++) { | |
1144 | if (m->data.marked[k]) | |
264244a0 | 1145 | fprintf(f, ":%lu %s\n", base + k, |
a6a1a831 SP |
1146 | sha1_to_hex(m->data.marked[k]->sha1)); |
1147 | } | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | static void dump_marks() | |
1152 | { | |
1153 | if (mark_file) | |
1154 | { | |
1155 | FILE *f = fopen(mark_file, "w"); | |
1156 | dump_marks_helper(f, 0, marks); | |
1157 | fclose(f); | |
1158 | } | |
1159 | } | |
1160 | ||
c44cdc7e SP |
1161 | static void read_next_command() |
1162 | { | |
1163 | read_line(&command_buf, stdin, '\n'); | |
1164 | } | |
1165 | ||
1166 | static void cmd_mark() | |
1167 | { | |
1168 | if (!strncmp("mark :", command_buf.buf, 6)) { | |
d8397168 | 1169 | next_mark = strtoul(command_buf.buf + 6, NULL, 10); |
c44cdc7e SP |
1170 | read_next_command(); |
1171 | } | |
1172 | else | |
d8397168 | 1173 | next_mark = 0; |
c44cdc7e SP |
1174 | } |
1175 | ||
1176 | static void* cmd_data (size_t *size) | |
1177 | { | |
1178 | size_t n = 0; | |
1179 | void *buffer; | |
1180 | size_t length; | |
1181 | ||
1182 | if (strncmp("data ", command_buf.buf, 5)) | |
1183 | die("Expected 'data n' command, found: %s", command_buf.buf); | |
1184 | ||
1185 | length = strtoul(command_buf.buf + 5, NULL, 10); | |
1186 | buffer = xmalloc(length); | |
1187 | ||
1188 | while (n < length) { | |
1189 | size_t s = fread((char*)buffer + n, 1, length - n, stdin); | |
1190 | if (!s && feof(stdin)) | |
1191 | die("EOF in data (%lu bytes remaining)", length - n); | |
1192 | n += s; | |
1193 | } | |
1194 | ||
1195 | if (fgetc(stdin) != '\n') | |
1196 | die("An lf did not trail the binary data as expected."); | |
1197 | ||
1198 | *size = length; | |
1199 | return buffer; | |
1200 | } | |
1201 | ||
463acbe1 | 1202 | static void cmd_new_blob() |
6143f064 | 1203 | { |
d8397168 SP |
1204 | size_t l; |
1205 | void *d; | |
c44cdc7e SP |
1206 | |
1207 | read_next_command(); | |
1208 | cmd_mark(); | |
d8397168 | 1209 | d = cmd_data(&l); |
6143f064 | 1210 | |
d8397168 SP |
1211 | if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark)) |
1212 | free(d); | |
6143f064 SP |
1213 | } |
1214 | ||
463acbe1 | 1215 | static void unload_one_branch() |
6bb5b329 | 1216 | { |
41e5257f SP |
1217 | while (cur_active_branches |
1218 | && cur_active_branches >= max_active_branches) { | |
463acbe1 SP |
1219 | unsigned long min_commit = ULONG_MAX; |
1220 | struct branch *e, *l = NULL, *p = NULL; | |
1221 | ||
1222 | for (e = active_branches; e; e = e->active_next_branch) { | |
1223 | if (e->last_commit < min_commit) { | |
1224 | p = l; | |
1225 | min_commit = e->last_commit; | |
1226 | } | |
1227 | l = e; | |
1228 | } | |
1229 | ||
1230 | if (p) { | |
1231 | e = p->active_next_branch; | |
1232 | p->active_next_branch = e->active_next_branch; | |
1233 | } else { | |
1234 | e = active_branches; | |
1235 | active_branches = e->active_next_branch; | |
1236 | } | |
1237 | e->active_next_branch = NULL; | |
1238 | if (e->branch_tree.tree) { | |
afde8dd9 | 1239 | release_tree_content_recursive(e->branch_tree.tree); |
463acbe1 SP |
1240 | e->branch_tree.tree = NULL; |
1241 | } | |
1242 | cur_active_branches--; | |
6bb5b329 | 1243 | } |
6bb5b329 SP |
1244 | } |
1245 | ||
463acbe1 | 1246 | static void load_branch(struct branch *b) |
6bb5b329 | 1247 | { |
463acbe1 SP |
1248 | load_tree(&b->branch_tree); |
1249 | b->active_next_branch = active_branches; | |
1250 | active_branches = b; | |
1251 | cur_active_branches++; | |
d6c7eb2c | 1252 | branch_load_count++; |
6bb5b329 SP |
1253 | } |
1254 | ||
463acbe1 | 1255 | static void file_change_m(struct branch *b) |
6bb5b329 | 1256 | { |
c44cdc7e SP |
1257 | const char *p = command_buf.buf + 2; |
1258 | char *p_uq; | |
1259 | const char *endp; | |
7111feed | 1260 | struct object_entry *oe; |
463acbe1 | 1261 | unsigned char sha1[20]; |
c44cdc7e | 1262 | unsigned int mode; |
7111feed | 1263 | char type[20]; |
6bb5b329 | 1264 | |
c44cdc7e SP |
1265 | p = get_mode(p, &mode); |
1266 | if (!p) | |
1267 | die("Corrupt mode: %s", command_buf.buf); | |
1268 | switch (mode) { | |
1269 | case S_IFREG | 0644: | |
1270 | case S_IFREG | 0755: | |
ace4a9d1 | 1271 | case S_IFLNK: |
c44cdc7e SP |
1272 | case 0644: |
1273 | case 0755: | |
1274 | /* ok */ | |
1275 | break; | |
1276 | default: | |
1277 | die("Corrupt mode: %s", command_buf.buf); | |
1278 | } | |
1279 | ||
d8397168 SP |
1280 | if (*p == ':') { |
1281 | char *x; | |
1282 | oe = find_mark(strtoul(p + 1, &x, 10)); | |
1283 | p = x; | |
1284 | } else { | |
1285 | if (get_sha1_hex(p, sha1)) | |
1286 | die("Invalid SHA1: %s", command_buf.buf); | |
1287 | oe = find_object(sha1); | |
1288 | p += 40; | |
1289 | } | |
c44cdc7e SP |
1290 | if (*p++ != ' ') |
1291 | die("Missing space after SHA1: %s", command_buf.buf); | |
1292 | ||
1293 | p_uq = unquote_c_style(p, &endp); | |
1294 | if (p_uq) { | |
1295 | if (*endp) | |
1296 | die("Garbage after path in: %s", command_buf.buf); | |
1297 | p = p_uq; | |
1298 | } | |
6bb5b329 | 1299 | |
7111feed SP |
1300 | if (oe) { |
1301 | if (oe->type != OBJ_BLOB) | |
c44cdc7e SP |
1302 | die("Not a blob (actually a %s): %s", |
1303 | command_buf.buf, type_names[oe->type]); | |
7111feed SP |
1304 | } else { |
1305 | if (sha1_object_info(sha1, type, NULL)) | |
c44cdc7e | 1306 | die("Blob not found: %s", command_buf.buf); |
7111feed | 1307 | if (strcmp(blob_type, type)) |
c44cdc7e SP |
1308 | die("Not a blob (actually a %s): %s", |
1309 | command_buf.buf, type); | |
7111feed | 1310 | } |
6bb5b329 | 1311 | |
c44cdc7e SP |
1312 | tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode); |
1313 | ||
1314 | if (p_uq) | |
1315 | free(p_uq); | |
463acbe1 | 1316 | } |
6bb5b329 | 1317 | |
463acbe1 SP |
1318 | static void file_change_d(struct branch *b) |
1319 | { | |
c44cdc7e SP |
1320 | const char *p = command_buf.buf + 2; |
1321 | char *p_uq; | |
1322 | const char *endp; | |
1323 | ||
1324 | p_uq = unquote_c_style(p, &endp); | |
1325 | if (p_uq) { | |
1326 | if (*endp) | |
1327 | die("Garbage after path in: %s", command_buf.buf); | |
1328 | p = p_uq; | |
1329 | } | |
1330 | tree_content_remove(&b->branch_tree, p); | |
1331 | if (p_uq) | |
1332 | free(p_uq); | |
6bb5b329 SP |
1333 | } |
1334 | ||
00e2b884 SP |
1335 | static void cmd_from(struct branch *b) |
1336 | { | |
1337 | const char *from, *endp; | |
1338 | char *str_uq; | |
1339 | struct branch *s; | |
1340 | ||
1341 | if (strncmp("from ", command_buf.buf, 5)) | |
1342 | return; | |
1343 | ||
1344 | if (b->last_commit) | |
1345 | die("Can't reinitailize branch %s", b->name); | |
1346 | ||
1347 | from = strchr(command_buf.buf, ' ') + 1; | |
1348 | str_uq = unquote_c_style(from, &endp); | |
1349 | if (str_uq) { | |
1350 | if (*endp) | |
1351 | die("Garbage after string in: %s", command_buf.buf); | |
1352 | from = str_uq; | |
1353 | } | |
1354 | ||
1355 | s = lookup_branch(from); | |
1356 | if (b == s) | |
1357 | die("Can't create a branch from itself: %s", b->name); | |
1358 | else if (s) { | |
1359 | memcpy(b->sha1, s->sha1, 20); | |
1360 | memcpy(b->branch_tree.sha1, s->branch_tree.sha1, 20); | |
1361 | } else if (*from == ':') { | |
1362 | unsigned long idnum = strtoul(from + 1, NULL, 10); | |
1363 | struct object_entry *oe = find_mark(idnum); | |
1364 | unsigned long size; | |
1365 | char *buf; | |
1366 | if (oe->type != OBJ_COMMIT) | |
1367 | die("Mark :%lu not a commit", idnum); | |
1368 | memcpy(b->sha1, oe->sha1, 20); | |
1369 | buf = unpack_entry(oe->offset, &size); | |
1370 | if (!buf || size < 46) | |
1371 | die("Not a valid commit: %s", from); | |
1372 | if (memcmp("tree ", buf, 5) | |
1373 | || get_sha1_hex(buf + 5, b->branch_tree.sha1)) | |
1374 | die("The commit %s is corrupt", sha1_to_hex(b->sha1)); | |
1375 | free(buf); | |
1376 | } else if (!get_sha1(from, b->sha1)) { | |
1377 | if (!memcmp(b->sha1, null_sha1, 20)) | |
1378 | memcpy(b->branch_tree.sha1, null_sha1, 20); | |
1379 | else { | |
1380 | unsigned long size; | |
1381 | char *buf; | |
1382 | ||
1383 | buf = read_object_with_reference(b->sha1, | |
1384 | type_names[OBJ_COMMIT], &size, b->sha1); | |
1385 | if (!buf || size < 46) | |
1386 | die("Not a valid commit: %s", from); | |
1387 | if (memcmp("tree ", buf, 5) | |
1388 | || get_sha1_hex(buf + 5, b->branch_tree.sha1)) | |
1389 | die("The commit %s is corrupt", sha1_to_hex(b->sha1)); | |
1390 | free(buf); | |
1391 | } | |
1392 | } else | |
1393 | die("Invalid ref name or SHA1 expression: %s", from); | |
1394 | ||
1395 | read_next_command(); | |
1396 | } | |
1397 | ||
463acbe1 | 1398 | static void cmd_new_commit() |
6bb5b329 | 1399 | { |
c44cdc7e SP |
1400 | struct branch *b; |
1401 | void *msg; | |
1402 | size_t msglen; | |
1403 | char *str_uq; | |
1404 | const char *endp; | |
1405 | char *sp; | |
1406 | char *author = NULL; | |
1407 | char *committer = NULL; | |
1408 | char *body; | |
1409 | ||
1410 | /* Obtain the branch name from the rest of our command */ | |
1411 | sp = strchr(command_buf.buf, ' ') + 1; | |
1412 | str_uq = unquote_c_style(sp, &endp); | |
1413 | if (str_uq) { | |
1414 | if (*endp) | |
1415 | die("Garbage after ref in: %s", command_buf.buf); | |
1416 | sp = str_uq; | |
1417 | } | |
1418 | b = lookup_branch(sp); | |
463acbe1 | 1419 | if (!b) |
00e2b884 | 1420 | b = new_branch(sp); |
c44cdc7e SP |
1421 | if (str_uq) |
1422 | free(str_uq); | |
1423 | ||
1424 | read_next_command(); | |
1425 | cmd_mark(); | |
1426 | if (!strncmp("author ", command_buf.buf, 7)) { | |
1427 | author = strdup(command_buf.buf); | |
1428 | read_next_command(); | |
1429 | } | |
1430 | if (!strncmp("committer ", command_buf.buf, 10)) { | |
1431 | committer = strdup(command_buf.buf); | |
1432 | read_next_command(); | |
1433 | } | |
1434 | if (!committer) | |
1435 | die("Expected committer but didn't get one"); | |
1436 | msg = cmd_data(&msglen); | |
02f3389d SP |
1437 | read_next_command(); |
1438 | cmd_from(b); | |
c44cdc7e SP |
1439 | |
1440 | /* ensure the branch is active/loaded */ | |
41e5257f | 1441 | if (!b->branch_tree.tree || !max_active_branches) { |
463acbe1 SP |
1442 | unload_one_branch(); |
1443 | load_branch(b); | |
1444 | } | |
6bb5b329 | 1445 | |
463acbe1 SP |
1446 | /* file_change* */ |
1447 | for (;;) { | |
c44cdc7e | 1448 | if (1 == command_buf.len) |
463acbe1 | 1449 | break; |
c44cdc7e | 1450 | else if (!strncmp("M ", command_buf.buf, 2)) |
463acbe1 | 1451 | file_change_m(b); |
c44cdc7e | 1452 | else if (!strncmp("D ", command_buf.buf, 2)) |
463acbe1 SP |
1453 | file_change_d(b); |
1454 | else | |
c44cdc7e | 1455 | die("Unsupported file_change: %s", command_buf.buf); |
02f3389d | 1456 | read_next_command(); |
6bb5b329 | 1457 | } |
6bb5b329 | 1458 | |
c44cdc7e | 1459 | /* build the tree and the commit */ |
463acbe1 | 1460 | store_tree(&b->branch_tree); |
c44cdc7e SP |
1461 | body = xmalloc(97 + msglen |
1462 | + (author | |
1463 | ? strlen(author) + strlen(committer) | |
1464 | : 2 * strlen(committer))); | |
1465 | sp = body; | |
1466 | sp += sprintf(sp, "tree %s\n", sha1_to_hex(b->branch_tree.sha1)); | |
1467 | if (memcmp(b->sha1, null_sha1, 20)) | |
1468 | sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1)); | |
1469 | if (author) | |
1470 | sp += sprintf(sp, "%s\n", author); | |
1471 | else | |
1472 | sp += sprintf(sp, "author %s\n", committer + 10); | |
1473 | sp += sprintf(sp, "%s\n\n", committer); | |
1474 | memcpy(sp, msg, msglen); | |
1475 | sp += msglen; | |
1476 | if (author) | |
1477 | free(author); | |
1478 | free(committer); | |
1479 | free(msg); | |
1480 | ||
d8397168 | 1481 | store_object(OBJ_COMMIT, body, sp - body, NULL, b->sha1, next_mark); |
463acbe1 SP |
1482 | free(body); |
1483 | b->last_commit = object_count_by_type[OBJ_COMMIT]; | |
264244a0 SP |
1484 | |
1485 | if (branch_log) { | |
1486 | int need_dq = quote_c_style(b->name, NULL, NULL, 0); | |
1487 | fprintf(branch_log, "commit "); | |
1488 | if (need_dq) { | |
1489 | fputc('"', branch_log); | |
1490 | quote_c_style(b->name, NULL, branch_log, 0); | |
1491 | fputc('"', branch_log); | |
1492 | } else | |
1493 | fprintf(branch_log, "%s", b->name); | |
1494 | fprintf(branch_log," :%lu %s\n",next_mark,sha1_to_hex(b->sha1)); | |
1495 | } | |
6bb5b329 SP |
1496 | } |
1497 | ||
72303d44 SP |
1498 | static void cmd_new_tag() |
1499 | { | |
1500 | char *str_uq; | |
1501 | const char *endp; | |
1502 | char *sp; | |
1503 | const char *from; | |
1504 | char *tagger; | |
1505 | struct branch *s; | |
1506 | void *msg; | |
1507 | size_t msglen; | |
1508 | char *body; | |
1509 | struct tag *t; | |
264244a0 | 1510 | unsigned long from_mark = 0; |
72303d44 SP |
1511 | unsigned char sha1[20]; |
1512 | ||
1513 | /* Obtain the new tag name from the rest of our command */ | |
1514 | sp = strchr(command_buf.buf, ' ') + 1; | |
1515 | str_uq = unquote_c_style(sp, &endp); | |
1516 | if (str_uq) { | |
1517 | if (*endp) | |
1518 | die("Garbage after tag name in: %s", command_buf.buf); | |
1519 | sp = str_uq; | |
1520 | } | |
1521 | t = pool_alloc(sizeof(struct tag)); | |
1522 | t->next_tag = NULL; | |
1523 | t->name = pool_strdup(sp); | |
1524 | if (last_tag) | |
1525 | last_tag->next_tag = t; | |
1526 | else | |
1527 | first_tag = t; | |
1528 | last_tag = t; | |
1529 | if (str_uq) | |
1530 | free(str_uq); | |
1531 | read_next_command(); | |
1532 | ||
1533 | /* from ... */ | |
1534 | if (strncmp("from ", command_buf.buf, 5)) | |
1535 | die("Expected from command, got %s", command_buf.buf); | |
1536 | ||
1537 | from = strchr(command_buf.buf, ' ') + 1; | |
1538 | str_uq = unquote_c_style(from, &endp); | |
1539 | if (str_uq) { | |
1540 | if (*endp) | |
1541 | die("Garbage after string in: %s", command_buf.buf); | |
1542 | from = str_uq; | |
1543 | } | |
1544 | ||
1545 | s = lookup_branch(from); | |
1546 | if (s) { | |
1547 | memcpy(sha1, s->sha1, 20); | |
1548 | } else if (*from == ':') { | |
264244a0 SP |
1549 | from_mark = strtoul(from + 1, NULL, 10); |
1550 | struct object_entry *oe = find_mark(from_mark); | |
72303d44 | 1551 | if (oe->type != OBJ_COMMIT) |
264244a0 | 1552 | die("Mark :%lu not a commit", from_mark); |
72303d44 SP |
1553 | memcpy(sha1, oe->sha1, 20); |
1554 | } else if (!get_sha1(from, sha1)) { | |
1555 | unsigned long size; | |
1556 | char *buf; | |
1557 | ||
1558 | buf = read_object_with_reference(sha1, | |
1559 | type_names[OBJ_COMMIT], &size, sha1); | |
1560 | if (!buf || size < 46) | |
1561 | die("Not a valid commit: %s", from); | |
1562 | free(buf); | |
1563 | } else | |
1564 | die("Invalid ref name or SHA1 expression: %s", from); | |
1565 | ||
1566 | if (str_uq) | |
1567 | free(str_uq); | |
1568 | read_next_command(); | |
1569 | ||
1570 | /* tagger ... */ | |
1571 | if (strncmp("tagger ", command_buf.buf, 7)) | |
1572 | die("Expected tagger command, got %s", command_buf.buf); | |
1573 | tagger = strdup(command_buf.buf); | |
1574 | ||
1575 | /* tag payload/message */ | |
1576 | read_next_command(); | |
1577 | msg = cmd_data(&msglen); | |
1578 | ||
1579 | /* build the tag object */ | |
1580 | body = xmalloc(67 + strlen(t->name) + strlen(tagger) + msglen); | |
1581 | sp = body; | |
1582 | sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1)); | |
1583 | sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]); | |
1584 | sp += sprintf(sp, "tag %s\n", t->name); | |
1585 | sp += sprintf(sp, "%s\n\n", tagger); | |
1586 | memcpy(sp, msg, msglen); | |
1587 | sp += msglen; | |
1588 | free(tagger); | |
1589 | free(msg); | |
1590 | ||
1591 | store_object(OBJ_TAG, body, sp - body, NULL, t->sha1, 0); | |
1592 | free(body); | |
264244a0 SP |
1593 | |
1594 | if (branch_log) { | |
1595 | int need_dq = quote_c_style(t->name, NULL, NULL, 0); | |
1596 | fprintf(branch_log, "tag "); | |
1597 | if (need_dq) { | |
1598 | fputc('"', branch_log); | |
1599 | quote_c_style(t->name, NULL, branch_log, 0); | |
1600 | fputc('"', branch_log); | |
1601 | } else | |
1602 | fprintf(branch_log, "%s", t->name); | |
1603 | fprintf(branch_log," :%lu %s\n",from_mark,sha1_to_hex(t->sha1)); | |
1604 | } | |
72303d44 SP |
1605 | } |
1606 | ||
d5c57b28 | 1607 | static const char fast_import_usage[] = |
264244a0 | 1608 | "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack"; |
d5c57b28 | 1609 | |
8bcce301 SP |
1610 | int main(int argc, const char **argv) |
1611 | { | |
d5c57b28 SP |
1612 | const char *base_name; |
1613 | int i; | |
1614 | unsigned long est_obj_cnt = 1000; | |
8bcce301 SP |
1615 | char *pack_name; |
1616 | char *idx_name; | |
6143f064 | 1617 | struct stat sb; |
8bcce301 | 1618 | |
463acbe1 SP |
1619 | setup_ident(); |
1620 | git_config(git_default_config); | |
53dbce78 | 1621 | page_size = getpagesize(); |
463acbe1 | 1622 | |
d5c57b28 SP |
1623 | for (i = 1; i < argc; i++) { |
1624 | const char *a = argv[i]; | |
1625 | ||
1626 | if (*a != '-' || !strcmp(a, "--")) | |
1627 | break; | |
1628 | else if (!strncmp(a, "--objects=", 10)) | |
1629 | est_obj_cnt = strtoul(a + 10, NULL, 0); | |
1630 | else if (!strncmp(a, "--depth=", 8)) | |
1631 | max_depth = strtoul(a + 8, NULL, 0); | |
1632 | else if (!strncmp(a, "--active-branches=", 18)) | |
1633 | max_active_branches = strtoul(a + 18, NULL, 0); | |
a6a1a831 SP |
1634 | else if (!strncmp(a, "--export-marks=", 15)) |
1635 | mark_file = a + 15; | |
264244a0 SP |
1636 | else if (!strncmp(a, "--branch-log=", 13)) { |
1637 | branch_log = fopen(a + 13, "w"); | |
1638 | if (!branch_log) | |
1639 | die("Can't create %s: %s", a + 13, strerror(errno)); | |
1640 | } | |
d5c57b28 SP |
1641 | else |
1642 | die("unknown option %s", a); | |
1643 | } | |
1644 | if ((i+1) != argc) | |
1645 | usage(fast_import_usage); | |
1646 | base_name = argv[i]; | |
1647 | ||
8bcce301 SP |
1648 | pack_name = xmalloc(strlen(base_name) + 6); |
1649 | sprintf(pack_name, "%s.pack", base_name); | |
1650 | idx_name = xmalloc(strlen(base_name) + 5); | |
1651 | sprintf(idx_name, "%s.idx", base_name); | |
1652 | ||
ac47a738 SP |
1653 | pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666); |
1654 | if (pack_fd < 0) | |
6143f064 | 1655 | die("Can't create %s: %s", pack_name, strerror(errno)); |
8bcce301 | 1656 | |
c44cdc7e | 1657 | init_pack_header(); |
27d6d290 | 1658 | alloc_objects(est_obj_cnt); |
c44cdc7e | 1659 | strbuf_init(&command_buf); |
463acbe1 SP |
1660 | |
1661 | atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*)); | |
1662 | branch_table = xcalloc(branch_table_sz, sizeof(struct branch*)); | |
1663 | avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*)); | |
d8397168 | 1664 | marks = pool_calloc(1, sizeof(struct mark_set)); |
463acbe1 | 1665 | |
db5e523f | 1666 | for (;;) { |
c44cdc7e SP |
1667 | read_next_command(); |
1668 | if (command_buf.eof) | |
db5e523f | 1669 | break; |
c44cdc7e SP |
1670 | else if (!strcmp("blob", command_buf.buf)) |
1671 | cmd_new_blob(); | |
c44cdc7e SP |
1672 | else if (!strncmp("commit ", command_buf.buf, 7)) |
1673 | cmd_new_commit(); | |
72303d44 SP |
1674 | else if (!strncmp("tag ", command_buf.buf, 4)) |
1675 | cmd_new_tag(); | |
c44cdc7e SP |
1676 | else |
1677 | die("Unsupported command: %s", command_buf.buf); | |
db5e523f | 1678 | } |
c44cdc7e | 1679 | |
db5e523f | 1680 | fixup_header_footer(); |
ac47a738 | 1681 | close(pack_fd); |
8bcce301 | 1682 | write_index(idx_name); |
463acbe1 | 1683 | dump_branches(); |
72303d44 | 1684 | dump_tags(); |
a6a1a831 | 1685 | dump_marks(); |
264244a0 | 1686 | fclose(branch_log); |
8bcce301 | 1687 | |
6143f064 SP |
1688 | fprintf(stderr, "%s statistics:\n", argv[0]); |
1689 | fprintf(stderr, "---------------------------------------------------\n"); | |
1690 | fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt); | |
1691 | fprintf(stderr, "Total objects: %10lu (%10lu duplicates)\n", object_count, duplicate_count); | |
1692 | fprintf(stderr, " blobs : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB]); | |
1693 | fprintf(stderr, " trees : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE]); | |
1694 | fprintf(stderr, " commits: %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT]); | |
1695 | fprintf(stderr, " tags : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG]); | |
d6c7eb2c | 1696 | fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count); |
d8397168 | 1697 | fprintf(stderr, " marks: %10u (%10lu unique )\n", (1 << marks->shift) * 1024, marks_set_count); |
d6c7eb2c | 1698 | fprintf(stderr, " atoms: %10u\n", atom_cnt); |
7111feed SP |
1699 | fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024); |
1700 | fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024); | |
1701 | fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024); | |
41e5257f | 1702 | fprintf(stderr, "Pack remaps: %10lu\n", remap_count); |
6143f064 SP |
1703 | fprintf(stderr, "---------------------------------------------------\n"); |
1704 | ||
1705 | stat(pack_name, &sb); | |
1706 | fprintf(stderr, "Pack size: %10lu KiB\n", (unsigned long)(sb.st_size/1024)); | |
1707 | stat(idx_name, &sb); | |
1708 | fprintf(stderr, "Index size: %10lu KiB\n", (unsigned long)(sb.st_size/1024)); | |
1709 | ||
1710 | fprintf(stderr, "\n"); | |
db5e523f SP |
1711 | |
1712 | return 0; | |
1713 | } |