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