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