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