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