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