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