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