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