]>
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) | |
b54d6422 SP |
959 | || !find_object(root->versions[0].sha1) |
960 | || !S_ISDIR(root->versions[0].mode)) { | |
4cabf858 SP |
961 | lo.data = NULL; |
962 | lo.depth = 0; | |
963 | } else { | |
e2eb469d SP |
964 | mktree(t, 0, &lo.len, &old_tree); |
965 | lo.data = old_tree.buffer; | |
4cabf858 | 966 | lo.depth = t->delta_depth; |
e2eb469d | 967 | lo.no_free = 1; |
4cabf858 SP |
968 | hashcpy(lo.sha1, root->versions[0].sha1); |
969 | } | |
e2eb469d | 970 | mktree(t, 1, &new_len, &new_tree); |
4cabf858 | 971 | |
e2eb469d | 972 | store_object(OBJ_TREE, new_tree.buffer, new_len, |
4cabf858 | 973 | &lo, root->versions[1].sha1, 0); |
4cabf858 SP |
974 | |
975 | t->delta_depth = lo.depth; | |
976 | hashcpy(root->versions[0].sha1, root->versions[1].sha1); | |
977 | for (i = 0, j = 0, del = 0; i < t->entry_count; i++) { | |
978 | struct tree_entry *e = t->entries[i]; | |
979 | if (e->versions[1].mode) { | |
980 | e->versions[0].mode = e->versions[1].mode; | |
981 | hashcpy(e->versions[0].sha1, e->versions[1].sha1); | |
982 | t->entries[j++] = e; | |
983 | } else { | |
984 | release_tree_entry(e); | |
985 | del++; | |
986 | } | |
987 | } | |
988 | t->entry_count -= del; | |
463acbe1 SP |
989 | } |
990 | ||
991 | static int tree_content_set( | |
992 | struct tree_entry *root, | |
993 | const char *p, | |
994 | const unsigned char *sha1, | |
995 | const unsigned int mode) | |
996 | { | |
997 | struct tree_content *t = root->tree; | |
998 | const char *slash1; | |
999 | unsigned int i, n; | |
1000 | struct tree_entry *e; | |
1001 | ||
1002 | slash1 = strchr(p, '/'); | |
1003 | if (slash1) | |
1004 | n = slash1 - p; | |
1005 | else | |
1006 | n = strlen(p); | |
1007 | ||
1008 | for (i = 0; i < t->entry_count; i++) { | |
1009 | e = t->entries[i]; | |
1010 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
1011 | if (!slash1) { | |
4cabf858 SP |
1012 | if (e->versions[1].mode == mode |
1013 | && !hashcmp(e->versions[1].sha1, sha1)) | |
463acbe1 | 1014 | return 0; |
4cabf858 SP |
1015 | e->versions[1].mode = mode; |
1016 | hashcpy(e->versions[1].sha1, sha1); | |
463acbe1 | 1017 | if (e->tree) { |
afde8dd9 | 1018 | release_tree_content_recursive(e->tree); |
463acbe1 SP |
1019 | e->tree = NULL; |
1020 | } | |
4cabf858 | 1021 | hashclr(root->versions[1].sha1); |
463acbe1 SP |
1022 | return 1; |
1023 | } | |
4cabf858 | 1024 | if (!S_ISDIR(e->versions[1].mode)) { |
463acbe1 | 1025 | e->tree = new_tree_content(8); |
4cabf858 | 1026 | e->versions[1].mode = S_IFDIR; |
b54d6422 | 1027 | hashclr(e->versions[1].sha1); |
463acbe1 SP |
1028 | } |
1029 | if (!e->tree) | |
1030 | load_tree(e); | |
1031 | if (tree_content_set(e, slash1 + 1, sha1, mode)) { | |
4cabf858 | 1032 | hashclr(root->versions[1].sha1); |
463acbe1 SP |
1033 | return 1; |
1034 | } | |
1035 | return 0; | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | if (t->entry_count == t->entry_capacity) | |
1040 | root->tree = t = grow_tree_content(t, 8); | |
1041 | e = new_tree_entry(); | |
1042 | e->name = to_atom(p, n); | |
4cabf858 SP |
1043 | e->versions[0].mode = 0; |
1044 | hashclr(e->versions[0].sha1); | |
463acbe1 SP |
1045 | t->entries[t->entry_count++] = e; |
1046 | if (slash1) { | |
1047 | e->tree = new_tree_content(8); | |
4cabf858 | 1048 | e->versions[1].mode = S_IFDIR; |
b54d6422 | 1049 | hashclr(e->versions[1].sha1); |
463acbe1 SP |
1050 | tree_content_set(e, slash1 + 1, sha1, mode); |
1051 | } else { | |
1052 | e->tree = NULL; | |
4cabf858 SP |
1053 | e->versions[1].mode = mode; |
1054 | hashcpy(e->versions[1].sha1, sha1); | |
463acbe1 | 1055 | } |
4cabf858 | 1056 | hashclr(root->versions[1].sha1); |
463acbe1 SP |
1057 | return 1; |
1058 | } | |
1059 | ||
1060 | static int tree_content_remove(struct tree_entry *root, const char *p) | |
1061 | { | |
1062 | struct tree_content *t = root->tree; | |
1063 | const char *slash1; | |
1064 | unsigned int i, n; | |
1065 | struct tree_entry *e; | |
1066 | ||
1067 | slash1 = strchr(p, '/'); | |
1068 | if (slash1) | |
1069 | n = slash1 - p; | |
1070 | else | |
1071 | n = strlen(p); | |
1072 | ||
1073 | for (i = 0; i < t->entry_count; i++) { | |
1074 | e = t->entries[i]; | |
1075 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
4cabf858 | 1076 | if (!slash1 || !S_ISDIR(e->versions[1].mode)) |
463acbe1 SP |
1077 | goto del_entry; |
1078 | if (!e->tree) | |
1079 | load_tree(e); | |
1080 | if (tree_content_remove(e, slash1 + 1)) { | |
b54d6422 SP |
1081 | for (n = 0; n < e->tree->entry_count; n++) { |
1082 | if (e->tree->entries[n]->versions[1].mode) { | |
1083 | hashclr(root->versions[1].sha1); | |
1084 | return 1; | |
1085 | } | |
1086 | } | |
1087 | goto del_entry; | |
463acbe1 SP |
1088 | } |
1089 | return 0; | |
1090 | } | |
1091 | } | |
1092 | return 0; | |
1093 | ||
1094 | del_entry: | |
4cabf858 SP |
1095 | if (e->tree) { |
1096 | release_tree_content_recursive(e->tree); | |
1097 | e->tree = NULL; | |
1098 | } | |
1099 | e->versions[1].mode = 0; | |
1100 | hashclr(e->versions[1].sha1); | |
1101 | hashclr(root->versions[1].sha1); | |
ac47a738 | 1102 | return 1; |
db5e523f SP |
1103 | } |
1104 | ||
8bcce301 | 1105 | static void init_pack_header() |
db5e523f | 1106 | { |
c90be46a SP |
1107 | struct pack_header hdr; |
1108 | ||
1109 | hdr.hdr_signature = htonl(PACK_SIGNATURE); | |
1110 | hdr.hdr_version = htonl(2); | |
1111 | hdr.hdr_entries = 0; | |
1112 | ||
23bc886c | 1113 | write_or_die(pack_fd, &hdr, sizeof(hdr)); |
41e5257f | 1114 | pack_size = sizeof(hdr); |
db5e523f SP |
1115 | } |
1116 | ||
8bcce301 | 1117 | static void fixup_header_footer() |
db5e523f SP |
1118 | { |
1119 | SHA_CTX c; | |
1120 | char hdr[8]; | |
db5e523f SP |
1121 | unsigned long cnt; |
1122 | char *buf; | |
1123 | size_t n; | |
1124 | ||
ac47a738 | 1125 | if (lseek(pack_fd, 0, SEEK_SET) != 0) |
db5e523f SP |
1126 | die("Failed seeking to start: %s", strerror(errno)); |
1127 | ||
1128 | SHA1_Init(&c); | |
463acbe1 | 1129 | yread(pack_fd, hdr, 8); |
db5e523f SP |
1130 | SHA1_Update(&c, hdr, 8); |
1131 | ||
db5e523f SP |
1132 | cnt = htonl(object_count); |
1133 | SHA1_Update(&c, &cnt, 4); | |
23bc886c | 1134 | write_or_die(pack_fd, &cnt, 4); |
db5e523f SP |
1135 | |
1136 | buf = xmalloc(128 * 1024); | |
1137 | for (;;) { | |
ac47a738 | 1138 | n = xread(pack_fd, buf, 128 * 1024); |
db5e523f SP |
1139 | if (n <= 0) |
1140 | break; | |
1141 | SHA1_Update(&c, buf, n); | |
1142 | } | |
1143 | free(buf); | |
1144 | ||
ac47a738 | 1145 | SHA1_Final(pack_sha1, &c); |
23bc886c | 1146 | write_or_die(pack_fd, pack_sha1, sizeof(pack_sha1)); |
db5e523f SP |
1147 | } |
1148 | ||
8bcce301 | 1149 | static int oecmp (const void *_a, const void *_b) |
db5e523f | 1150 | { |
8bcce301 SP |
1151 | struct object_entry *a = *((struct object_entry**)_a); |
1152 | struct object_entry *b = *((struct object_entry**)_b); | |
445b8599 | 1153 | return hashcmp(a->sha1, b->sha1); |
8bcce301 SP |
1154 | } |
1155 | ||
1156 | static void write_index(const char *idx_name) | |
1157 | { | |
1158 | struct sha1file *f; | |
1159 | struct object_entry **idx, **c, **last; | |
1160 | struct object_entry *e; | |
463acbe1 | 1161 | struct object_entry_pool *o; |
8bcce301 SP |
1162 | unsigned int array[256]; |
1163 | int i; | |
1164 | ||
1165 | /* Build the sorted table of object IDs. */ | |
1166 | idx = xmalloc(object_count * sizeof(struct object_entry*)); | |
1167 | c = idx; | |
463acbe1 | 1168 | for (o = blocks; o; o = o->next_pool) |
27d6d290 SP |
1169 | for (e = o->entries; e != o->next_free; e++) |
1170 | *c++ = e; | |
8bcce301 SP |
1171 | last = idx + object_count; |
1172 | qsort(idx, object_count, sizeof(struct object_entry*), oecmp); | |
1173 | ||
1174 | /* Generate the fan-out array. */ | |
1175 | c = idx; | |
1176 | for (i = 0; i < 256; i++) { | |
1177 | struct object_entry **next = c;; | |
1178 | while (next < last) { | |
1179 | if ((*next)->sha1[0] != i) | |
1180 | break; | |
1181 | next++; | |
1182 | } | |
1183 | array[i] = htonl(next - idx); | |
1184 | c = next; | |
1185 | } | |
1186 | ||
1187 | f = sha1create("%s", idx_name); | |
1188 | sha1write(f, array, 256 * sizeof(int)); | |
1189 | for (c = idx; c != last; c++) { | |
1190 | unsigned int offset = htonl((*c)->offset); | |
1191 | sha1write(f, &offset, 4); | |
1192 | sha1write(f, (*c)->sha1, sizeof((*c)->sha1)); | |
1193 | } | |
ac47a738 | 1194 | sha1write(f, pack_sha1, sizeof(pack_sha1)); |
8bcce301 SP |
1195 | sha1close(f, NULL, 1); |
1196 | free(idx); | |
1197 | } | |
1198 | ||
463acbe1 SP |
1199 | static void dump_branches() |
1200 | { | |
1201 | static const char *msg = "fast-import"; | |
1202 | unsigned int i; | |
1203 | struct branch *b; | |
1204 | struct ref_lock *lock; | |
1205 | ||
1206 | for (i = 0; i < branch_table_sz; i++) { | |
1207 | for (b = branch_table[i]; b; b = b->table_next_branch) { | |
1208 | lock = lock_any_ref_for_update(b->name, NULL, 0); | |
1209 | if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0) | |
1210 | die("Can't write %s", b->name); | |
1211 | } | |
1212 | } | |
1213 | } | |
1214 | ||
72303d44 SP |
1215 | static void dump_tags() |
1216 | { | |
1217 | static const char *msg = "fast-import"; | |
1218 | struct tag *t; | |
1219 | struct ref_lock *lock; | |
1220 | char path[PATH_MAX]; | |
1221 | ||
1222 | for (t = first_tag; t; t = t->next_tag) { | |
1223 | sprintf(path, "refs/tags/%s", t->name); | |
1224 | lock = lock_any_ref_for_update(path, NULL, 0); | |
1225 | if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0) | |
1226 | die("Can't write %s", path); | |
1227 | } | |
1228 | } | |
1229 | ||
a6a1a831 SP |
1230 | static void dump_marks_helper(FILE *f, |
1231 | unsigned long base, | |
1232 | struct mark_set *m) | |
1233 | { | |
1234 | int k; | |
1235 | if (m->shift) { | |
1236 | for (k = 0; k < 1024; k++) { | |
1237 | if (m->data.sets[k]) | |
1238 | dump_marks_helper(f, (base + k) << m->shift, | |
1239 | m->data.sets[k]); | |
1240 | } | |
1241 | } else { | |
1242 | for (k = 0; k < 1024; k++) { | |
1243 | if (m->data.marked[k]) | |
264244a0 | 1244 | fprintf(f, ":%lu %s\n", base + k, |
a6a1a831 SP |
1245 | sha1_to_hex(m->data.marked[k]->sha1)); |
1246 | } | |
1247 | } | |
1248 | } | |
1249 | ||
1250 | static void dump_marks() | |
1251 | { | |
1252 | if (mark_file) | |
1253 | { | |
1254 | FILE *f = fopen(mark_file, "w"); | |
1255 | dump_marks_helper(f, 0, marks); | |
1256 | fclose(f); | |
1257 | } | |
1258 | } | |
1259 | ||
c44cdc7e SP |
1260 | static void read_next_command() |
1261 | { | |
1262 | read_line(&command_buf, stdin, '\n'); | |
1263 | } | |
1264 | ||
1265 | static void cmd_mark() | |
1266 | { | |
1267 | if (!strncmp("mark :", command_buf.buf, 6)) { | |
d8397168 | 1268 | next_mark = strtoul(command_buf.buf + 6, NULL, 10); |
c44cdc7e SP |
1269 | read_next_command(); |
1270 | } | |
1271 | else | |
d8397168 | 1272 | next_mark = 0; |
c44cdc7e SP |
1273 | } |
1274 | ||
1275 | static void* cmd_data (size_t *size) | |
1276 | { | |
1277 | size_t n = 0; | |
1278 | void *buffer; | |
1279 | size_t length; | |
1280 | ||
1281 | if (strncmp("data ", command_buf.buf, 5)) | |
1282 | die("Expected 'data n' command, found: %s", command_buf.buf); | |
1283 | ||
1284 | length = strtoul(command_buf.buf + 5, NULL, 10); | |
1285 | buffer = xmalloc(length); | |
1286 | ||
1287 | while (n < length) { | |
1288 | size_t s = fread((char*)buffer + n, 1, length - n, stdin); | |
1289 | if (!s && feof(stdin)) | |
1290 | die("EOF in data (%lu bytes remaining)", length - n); | |
1291 | n += s; | |
1292 | } | |
1293 | ||
1294 | if (fgetc(stdin) != '\n') | |
1295 | die("An lf did not trail the binary data as expected."); | |
1296 | ||
1297 | *size = length; | |
1298 | return buffer; | |
1299 | } | |
1300 | ||
463acbe1 | 1301 | static void cmd_new_blob() |
6143f064 | 1302 | { |
d8397168 SP |
1303 | size_t l; |
1304 | void *d; | |
c44cdc7e SP |
1305 | |
1306 | read_next_command(); | |
1307 | cmd_mark(); | |
d8397168 | 1308 | d = cmd_data(&l); |
6143f064 | 1309 | |
d8397168 SP |
1310 | if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark)) |
1311 | free(d); | |
6143f064 SP |
1312 | } |
1313 | ||
463acbe1 | 1314 | static void unload_one_branch() |
6bb5b329 | 1315 | { |
41e5257f SP |
1316 | while (cur_active_branches |
1317 | && cur_active_branches >= max_active_branches) { | |
463acbe1 SP |
1318 | unsigned long min_commit = ULONG_MAX; |
1319 | struct branch *e, *l = NULL, *p = NULL; | |
1320 | ||
1321 | for (e = active_branches; e; e = e->active_next_branch) { | |
1322 | if (e->last_commit < min_commit) { | |
1323 | p = l; | |
1324 | min_commit = e->last_commit; | |
1325 | } | |
1326 | l = e; | |
1327 | } | |
1328 | ||
1329 | if (p) { | |
1330 | e = p->active_next_branch; | |
1331 | p->active_next_branch = e->active_next_branch; | |
1332 | } else { | |
1333 | e = active_branches; | |
1334 | active_branches = e->active_next_branch; | |
1335 | } | |
1336 | e->active_next_branch = NULL; | |
1337 | if (e->branch_tree.tree) { | |
afde8dd9 | 1338 | release_tree_content_recursive(e->branch_tree.tree); |
463acbe1 SP |
1339 | e->branch_tree.tree = NULL; |
1340 | } | |
1341 | cur_active_branches--; | |
6bb5b329 | 1342 | } |
6bb5b329 SP |
1343 | } |
1344 | ||
463acbe1 | 1345 | static void load_branch(struct branch *b) |
6bb5b329 | 1346 | { |
463acbe1 SP |
1347 | load_tree(&b->branch_tree); |
1348 | b->active_next_branch = active_branches; | |
1349 | active_branches = b; | |
1350 | cur_active_branches++; | |
d6c7eb2c | 1351 | branch_load_count++; |
6bb5b329 SP |
1352 | } |
1353 | ||
463acbe1 | 1354 | static void file_change_m(struct branch *b) |
6bb5b329 | 1355 | { |
c44cdc7e SP |
1356 | const char *p = command_buf.buf + 2; |
1357 | char *p_uq; | |
1358 | const char *endp; | |
7111feed | 1359 | struct object_entry *oe; |
463acbe1 | 1360 | unsigned char sha1[20]; |
c44cdc7e | 1361 | unsigned int mode; |
7111feed | 1362 | char type[20]; |
6bb5b329 | 1363 | |
c44cdc7e SP |
1364 | p = get_mode(p, &mode); |
1365 | if (!p) | |
1366 | die("Corrupt mode: %s", command_buf.buf); | |
1367 | switch (mode) { | |
1368 | case S_IFREG | 0644: | |
1369 | case S_IFREG | 0755: | |
ace4a9d1 | 1370 | case S_IFLNK: |
c44cdc7e SP |
1371 | case 0644: |
1372 | case 0755: | |
1373 | /* ok */ | |
1374 | break; | |
1375 | default: | |
1376 | die("Corrupt mode: %s", command_buf.buf); | |
1377 | } | |
1378 | ||
d8397168 SP |
1379 | if (*p == ':') { |
1380 | char *x; | |
1381 | oe = find_mark(strtoul(p + 1, &x, 10)); | |
1382 | p = x; | |
1383 | } else { | |
1384 | if (get_sha1_hex(p, sha1)) | |
1385 | die("Invalid SHA1: %s", command_buf.buf); | |
1386 | oe = find_object(sha1); | |
1387 | p += 40; | |
1388 | } | |
c44cdc7e SP |
1389 | if (*p++ != ' ') |
1390 | die("Missing space after SHA1: %s", command_buf.buf); | |
1391 | ||
1392 | p_uq = unquote_c_style(p, &endp); | |
1393 | if (p_uq) { | |
1394 | if (*endp) | |
1395 | die("Garbage after path in: %s", command_buf.buf); | |
1396 | p = p_uq; | |
1397 | } | |
6bb5b329 | 1398 | |
7111feed SP |
1399 | if (oe) { |
1400 | if (oe->type != OBJ_BLOB) | |
c44cdc7e SP |
1401 | die("Not a blob (actually a %s): %s", |
1402 | command_buf.buf, type_names[oe->type]); | |
7111feed SP |
1403 | } else { |
1404 | if (sha1_object_info(sha1, type, NULL)) | |
c44cdc7e | 1405 | die("Blob not found: %s", command_buf.buf); |
7111feed | 1406 | if (strcmp(blob_type, type)) |
c44cdc7e SP |
1407 | die("Not a blob (actually a %s): %s", |
1408 | command_buf.buf, type); | |
7111feed | 1409 | } |
6bb5b329 | 1410 | |
c44cdc7e SP |
1411 | tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode); |
1412 | ||
1413 | if (p_uq) | |
1414 | free(p_uq); | |
463acbe1 | 1415 | } |
6bb5b329 | 1416 | |
463acbe1 SP |
1417 | static void file_change_d(struct branch *b) |
1418 | { | |
c44cdc7e SP |
1419 | const char *p = command_buf.buf + 2; |
1420 | char *p_uq; | |
1421 | const char *endp; | |
1422 | ||
1423 | p_uq = unquote_c_style(p, &endp); | |
1424 | if (p_uq) { | |
1425 | if (*endp) | |
1426 | die("Garbage after path in: %s", command_buf.buf); | |
1427 | p = p_uq; | |
1428 | } | |
1429 | tree_content_remove(&b->branch_tree, p); | |
1430 | if (p_uq) | |
1431 | free(p_uq); | |
6bb5b329 SP |
1432 | } |
1433 | ||
00e2b884 SP |
1434 | static void cmd_from(struct branch *b) |
1435 | { | |
1436 | const char *from, *endp; | |
1437 | char *str_uq; | |
1438 | struct branch *s; | |
1439 | ||
1440 | if (strncmp("from ", command_buf.buf, 5)) | |
1441 | return; | |
1442 | ||
1443 | if (b->last_commit) | |
1444 | die("Can't reinitailize branch %s", b->name); | |
1445 | ||
1446 | from = strchr(command_buf.buf, ' ') + 1; | |
1447 | str_uq = unquote_c_style(from, &endp); | |
1448 | if (str_uq) { | |
1449 | if (*endp) | |
1450 | die("Garbage after string in: %s", command_buf.buf); | |
1451 | from = str_uq; | |
1452 | } | |
1453 | ||
1454 | s = lookup_branch(from); | |
1455 | if (b == s) | |
1456 | die("Can't create a branch from itself: %s", b->name); | |
1457 | else if (s) { | |
4cabf858 | 1458 | unsigned char *t = s->branch_tree.versions[1].sha1; |
445b8599 | 1459 | hashcpy(b->sha1, s->sha1); |
4cabf858 SP |
1460 | hashcpy(b->branch_tree.versions[0].sha1, t); |
1461 | hashcpy(b->branch_tree.versions[1].sha1, t); | |
00e2b884 SP |
1462 | } else if (*from == ':') { |
1463 | unsigned long idnum = strtoul(from + 1, NULL, 10); | |
1464 | struct object_entry *oe = find_mark(idnum); | |
1465 | unsigned long size; | |
4cabf858 | 1466 | unsigned int depth; |
00e2b884 SP |
1467 | char *buf; |
1468 | if (oe->type != OBJ_COMMIT) | |
1469 | die("Mark :%lu not a commit", idnum); | |
445b8599 | 1470 | hashcpy(b->sha1, oe->sha1); |
4cabf858 | 1471 | buf = unpack_entry(oe->offset, &size, &depth); |
00e2b884 SP |
1472 | if (!buf || size < 46) |
1473 | die("Not a valid commit: %s", from); | |
1474 | if (memcmp("tree ", buf, 5) | |
4cabf858 | 1475 | || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1)) |
00e2b884 SP |
1476 | die("The commit %s is corrupt", sha1_to_hex(b->sha1)); |
1477 | free(buf); | |
4cabf858 SP |
1478 | hashcpy(b->branch_tree.versions[0].sha1, |
1479 | b->branch_tree.versions[1].sha1); | |
00e2b884 | 1480 | } else if (!get_sha1(from, b->sha1)) { |
4cabf858 SP |
1481 | if (is_null_sha1(b->sha1)) { |
1482 | hashclr(b->branch_tree.versions[0].sha1); | |
1483 | hashclr(b->branch_tree.versions[1].sha1); | |
1484 | } else { | |
00e2b884 SP |
1485 | unsigned long size; |
1486 | char *buf; | |
1487 | ||
1488 | buf = read_object_with_reference(b->sha1, | |
1489 | type_names[OBJ_COMMIT], &size, b->sha1); | |
1490 | if (!buf || size < 46) | |
1491 | die("Not a valid commit: %s", from); | |
1492 | if (memcmp("tree ", buf, 5) | |
4cabf858 | 1493 | || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1)) |
00e2b884 SP |
1494 | die("The commit %s is corrupt", sha1_to_hex(b->sha1)); |
1495 | free(buf); | |
4cabf858 SP |
1496 | hashcpy(b->branch_tree.versions[0].sha1, |
1497 | b->branch_tree.versions[1].sha1); | |
00e2b884 SP |
1498 | } |
1499 | } else | |
1500 | die("Invalid ref name or SHA1 expression: %s", from); | |
1501 | ||
1502 | read_next_command(); | |
1503 | } | |
1504 | ||
463acbe1 | 1505 | static void cmd_new_commit() |
6bb5b329 | 1506 | { |
c44cdc7e SP |
1507 | struct branch *b; |
1508 | void *msg; | |
1509 | size_t msglen; | |
1510 | char *str_uq; | |
1511 | const char *endp; | |
1512 | char *sp; | |
1513 | char *author = NULL; | |
1514 | char *committer = NULL; | |
c44cdc7e SP |
1515 | |
1516 | /* Obtain the branch name from the rest of our command */ | |
1517 | sp = strchr(command_buf.buf, ' ') + 1; | |
1518 | str_uq = unquote_c_style(sp, &endp); | |
1519 | if (str_uq) { | |
1520 | if (*endp) | |
1521 | die("Garbage after ref in: %s", command_buf.buf); | |
1522 | sp = str_uq; | |
1523 | } | |
1524 | b = lookup_branch(sp); | |
463acbe1 | 1525 | if (!b) |
00e2b884 | 1526 | b = new_branch(sp); |
c44cdc7e SP |
1527 | if (str_uq) |
1528 | free(str_uq); | |
1529 | ||
1530 | read_next_command(); | |
1531 | cmd_mark(); | |
1532 | if (!strncmp("author ", command_buf.buf, 7)) { | |
1533 | author = strdup(command_buf.buf); | |
1534 | read_next_command(); | |
1535 | } | |
1536 | if (!strncmp("committer ", command_buf.buf, 10)) { | |
1537 | committer = strdup(command_buf.buf); | |
1538 | read_next_command(); | |
1539 | } | |
1540 | if (!committer) | |
1541 | die("Expected committer but didn't get one"); | |
1542 | msg = cmd_data(&msglen); | |
02f3389d SP |
1543 | read_next_command(); |
1544 | cmd_from(b); | |
c44cdc7e SP |
1545 | |
1546 | /* ensure the branch is active/loaded */ | |
41e5257f | 1547 | if (!b->branch_tree.tree || !max_active_branches) { |
463acbe1 SP |
1548 | unload_one_branch(); |
1549 | load_branch(b); | |
1550 | } | |
6bb5b329 | 1551 | |
463acbe1 SP |
1552 | /* file_change* */ |
1553 | for (;;) { | |
c44cdc7e | 1554 | if (1 == command_buf.len) |
463acbe1 | 1555 | break; |
c44cdc7e | 1556 | else if (!strncmp("M ", command_buf.buf, 2)) |
463acbe1 | 1557 | file_change_m(b); |
c44cdc7e | 1558 | else if (!strncmp("D ", command_buf.buf, 2)) |
463acbe1 SP |
1559 | file_change_d(b); |
1560 | else | |
c44cdc7e | 1561 | die("Unsupported file_change: %s", command_buf.buf); |
02f3389d | 1562 | read_next_command(); |
6bb5b329 | 1563 | } |
6bb5b329 | 1564 | |
c44cdc7e | 1565 | /* build the tree and the commit */ |
463acbe1 | 1566 | store_tree(&b->branch_tree); |
243f801d | 1567 | size_dbuf(&new_data, 97 + msglen |
c44cdc7e SP |
1568 | + (author |
1569 | ? strlen(author) + strlen(committer) | |
1570 | : 2 * strlen(committer))); | |
243f801d | 1571 | sp = new_data.buffer; |
4cabf858 SP |
1572 | sp += sprintf(sp, "tree %s\n", |
1573 | sha1_to_hex(b->branch_tree.versions[1].sha1)); | |
445b8599 | 1574 | if (!is_null_sha1(b->sha1)) |
c44cdc7e SP |
1575 | sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1)); |
1576 | if (author) | |
1577 | sp += sprintf(sp, "%s\n", author); | |
1578 | else | |
1579 | sp += sprintf(sp, "author %s\n", committer + 10); | |
1580 | sp += sprintf(sp, "%s\n\n", committer); | |
1581 | memcpy(sp, msg, msglen); | |
1582 | sp += msglen; | |
1583 | if (author) | |
1584 | free(author); | |
1585 | free(committer); | |
1586 | free(msg); | |
1587 | ||
243f801d SP |
1588 | store_object(OBJ_COMMIT, |
1589 | new_data.buffer, sp - (char*)new_data.buffer, | |
1590 | NULL, b->sha1, next_mark); | |
463acbe1 | 1591 | b->last_commit = object_count_by_type[OBJ_COMMIT]; |
264244a0 SP |
1592 | |
1593 | if (branch_log) { | |
1594 | int need_dq = quote_c_style(b->name, NULL, NULL, 0); | |
1595 | fprintf(branch_log, "commit "); | |
1596 | if (need_dq) { | |
1597 | fputc('"', branch_log); | |
1598 | quote_c_style(b->name, NULL, branch_log, 0); | |
1599 | fputc('"', branch_log); | |
1600 | } else | |
1601 | fprintf(branch_log, "%s", b->name); | |
1602 | fprintf(branch_log," :%lu %s\n",next_mark,sha1_to_hex(b->sha1)); | |
1603 | } | |
6bb5b329 SP |
1604 | } |
1605 | ||
72303d44 SP |
1606 | static void cmd_new_tag() |
1607 | { | |
1608 | char *str_uq; | |
1609 | const char *endp; | |
1610 | char *sp; | |
1611 | const char *from; | |
1612 | char *tagger; | |
1613 | struct branch *s; | |
1614 | void *msg; | |
1615 | size_t msglen; | |
72303d44 | 1616 | struct tag *t; |
264244a0 | 1617 | unsigned long from_mark = 0; |
72303d44 SP |
1618 | unsigned char sha1[20]; |
1619 | ||
1620 | /* Obtain the new tag name from the rest of our command */ | |
1621 | sp = strchr(command_buf.buf, ' ') + 1; | |
1622 | str_uq = unquote_c_style(sp, &endp); | |
1623 | if (str_uq) { | |
1624 | if (*endp) | |
1625 | die("Garbage after tag name in: %s", command_buf.buf); | |
1626 | sp = str_uq; | |
1627 | } | |
1628 | t = pool_alloc(sizeof(struct tag)); | |
1629 | t->next_tag = NULL; | |
1630 | t->name = pool_strdup(sp); | |
1631 | if (last_tag) | |
1632 | last_tag->next_tag = t; | |
1633 | else | |
1634 | first_tag = t; | |
1635 | last_tag = t; | |
1636 | if (str_uq) | |
1637 | free(str_uq); | |
1638 | read_next_command(); | |
1639 | ||
1640 | /* from ... */ | |
1641 | if (strncmp("from ", command_buf.buf, 5)) | |
1642 | die("Expected from command, got %s", command_buf.buf); | |
1643 | ||
1644 | from = strchr(command_buf.buf, ' ') + 1; | |
1645 | str_uq = unquote_c_style(from, &endp); | |
1646 | if (str_uq) { | |
1647 | if (*endp) | |
1648 | die("Garbage after string in: %s", command_buf.buf); | |
1649 | from = str_uq; | |
1650 | } | |
1651 | ||
1652 | s = lookup_branch(from); | |
1653 | if (s) { | |
445b8599 | 1654 | hashcpy(sha1, s->sha1); |
72303d44 | 1655 | } else if (*from == ':') { |
264244a0 SP |
1656 | from_mark = strtoul(from + 1, NULL, 10); |
1657 | struct object_entry *oe = find_mark(from_mark); | |
72303d44 | 1658 | if (oe->type != OBJ_COMMIT) |
264244a0 | 1659 | die("Mark :%lu not a commit", from_mark); |
445b8599 | 1660 | hashcpy(sha1, oe->sha1); |
72303d44 SP |
1661 | } else if (!get_sha1(from, sha1)) { |
1662 | unsigned long size; | |
1663 | char *buf; | |
1664 | ||
1665 | buf = read_object_with_reference(sha1, | |
1666 | type_names[OBJ_COMMIT], &size, sha1); | |
1667 | if (!buf || size < 46) | |
1668 | die("Not a valid commit: %s", from); | |
1669 | free(buf); | |
1670 | } else | |
1671 | die("Invalid ref name or SHA1 expression: %s", from); | |
1672 | ||
1673 | if (str_uq) | |
1674 | free(str_uq); | |
1675 | read_next_command(); | |
1676 | ||
1677 | /* tagger ... */ | |
1678 | if (strncmp("tagger ", command_buf.buf, 7)) | |
1679 | die("Expected tagger command, got %s", command_buf.buf); | |
1680 | tagger = strdup(command_buf.buf); | |
1681 | ||
1682 | /* tag payload/message */ | |
1683 | read_next_command(); | |
1684 | msg = cmd_data(&msglen); | |
1685 | ||
1686 | /* build the tag object */ | |
243f801d SP |
1687 | size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen); |
1688 | sp = new_data.buffer; | |
72303d44 SP |
1689 | sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1)); |
1690 | sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]); | |
1691 | sp += sprintf(sp, "tag %s\n", t->name); | |
1692 | sp += sprintf(sp, "%s\n\n", tagger); | |
1693 | memcpy(sp, msg, msglen); | |
1694 | sp += msglen; | |
1695 | free(tagger); | |
1696 | free(msg); | |
1697 | ||
243f801d SP |
1698 | store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer, |
1699 | NULL, t->sha1, 0); | |
264244a0 SP |
1700 | |
1701 | if (branch_log) { | |
1702 | int need_dq = quote_c_style(t->name, NULL, NULL, 0); | |
1703 | fprintf(branch_log, "tag "); | |
1704 | if (need_dq) { | |
1705 | fputc('"', branch_log); | |
1706 | quote_c_style(t->name, NULL, branch_log, 0); | |
1707 | fputc('"', branch_log); | |
1708 | } else | |
1709 | fprintf(branch_log, "%s", t->name); | |
1710 | fprintf(branch_log," :%lu %s\n",from_mark,sha1_to_hex(t->sha1)); | |
1711 | } | |
72303d44 SP |
1712 | } |
1713 | ||
5fced8dc SP |
1714 | static void cmd_reset_branch() |
1715 | { | |
1716 | struct branch *b; | |
1717 | char *str_uq; | |
1718 | const char *endp; | |
1719 | char *sp; | |
1720 | ||
1721 | /* Obtain the branch name from the rest of our command */ | |
1722 | sp = strchr(command_buf.buf, ' ') + 1; | |
1723 | str_uq = unquote_c_style(sp, &endp); | |
1724 | if (str_uq) { | |
1725 | if (*endp) | |
1726 | die("Garbage after ref in: %s", command_buf.buf); | |
1727 | sp = str_uq; | |
1728 | } | |
1729 | b = lookup_branch(sp); | |
1730 | if (b) { | |
1731 | b->last_commit = 0; | |
1732 | if (b->branch_tree.tree) { | |
1733 | release_tree_content_recursive(b->branch_tree.tree); | |
1734 | b->branch_tree.tree = NULL; | |
1735 | } | |
1736 | } | |
1737 | if (str_uq) | |
1738 | free(str_uq); | |
1739 | } | |
1740 | ||
d5c57b28 | 1741 | static const char fast_import_usage[] = |
264244a0 | 1742 | "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack"; |
d5c57b28 | 1743 | |
8bcce301 SP |
1744 | int main(int argc, const char **argv) |
1745 | { | |
d5c57b28 SP |
1746 | const char *base_name; |
1747 | int i; | |
243f801d | 1748 | unsigned long est_obj_cnt = object_entry_alloc; |
8bcce301 SP |
1749 | char *pack_name; |
1750 | char *idx_name; | |
6143f064 | 1751 | struct stat sb; |
8bcce301 | 1752 | |
463acbe1 SP |
1753 | setup_ident(); |
1754 | git_config(git_default_config); | |
53dbce78 | 1755 | page_size = getpagesize(); |
463acbe1 | 1756 | |
d5c57b28 SP |
1757 | for (i = 1; i < argc; i++) { |
1758 | const char *a = argv[i]; | |
1759 | ||
1760 | if (*a != '-' || !strcmp(a, "--")) | |
1761 | break; | |
1762 | else if (!strncmp(a, "--objects=", 10)) | |
1763 | est_obj_cnt = strtoul(a + 10, NULL, 0); | |
1764 | else if (!strncmp(a, "--depth=", 8)) | |
1765 | max_depth = strtoul(a + 8, NULL, 0); | |
1766 | else if (!strncmp(a, "--active-branches=", 18)) | |
1767 | max_active_branches = strtoul(a + 18, NULL, 0); | |
a6a1a831 SP |
1768 | else if (!strncmp(a, "--export-marks=", 15)) |
1769 | mark_file = a + 15; | |
264244a0 SP |
1770 | else if (!strncmp(a, "--branch-log=", 13)) { |
1771 | branch_log = fopen(a + 13, "w"); | |
1772 | if (!branch_log) | |
1773 | die("Can't create %s: %s", a + 13, strerror(errno)); | |
1774 | } | |
d5c57b28 SP |
1775 | else |
1776 | die("unknown option %s", a); | |
1777 | } | |
1778 | if ((i+1) != argc) | |
1779 | usage(fast_import_usage); | |
1780 | base_name = argv[i]; | |
1781 | ||
8bcce301 SP |
1782 | pack_name = xmalloc(strlen(base_name) + 6); |
1783 | sprintf(pack_name, "%s.pack", base_name); | |
1784 | idx_name = xmalloc(strlen(base_name) + 5); | |
1785 | sprintf(idx_name, "%s.idx", base_name); | |
1786 | ||
ac47a738 SP |
1787 | pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666); |
1788 | if (pack_fd < 0) | |
6143f064 | 1789 | die("Can't create %s: %s", pack_name, strerror(errno)); |
8bcce301 | 1790 | |
c44cdc7e | 1791 | init_pack_header(); |
27d6d290 | 1792 | alloc_objects(est_obj_cnt); |
c44cdc7e | 1793 | strbuf_init(&command_buf); |
463acbe1 SP |
1794 | |
1795 | atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*)); | |
1796 | branch_table = xcalloc(branch_table_sz, sizeof(struct branch*)); | |
1797 | avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*)); | |
d8397168 | 1798 | marks = pool_calloc(1, sizeof(struct mark_set)); |
463acbe1 | 1799 | |
db5e523f | 1800 | for (;;) { |
c44cdc7e SP |
1801 | read_next_command(); |
1802 | if (command_buf.eof) | |
db5e523f | 1803 | break; |
c44cdc7e SP |
1804 | else if (!strcmp("blob", command_buf.buf)) |
1805 | cmd_new_blob(); | |
c44cdc7e SP |
1806 | else if (!strncmp("commit ", command_buf.buf, 7)) |
1807 | cmd_new_commit(); | |
72303d44 SP |
1808 | else if (!strncmp("tag ", command_buf.buf, 4)) |
1809 | cmd_new_tag(); | |
5fced8dc SP |
1810 | else if (!strncmp("reset ", command_buf.buf, 6)) |
1811 | cmd_reset_branch(); | |
c44cdc7e SP |
1812 | else |
1813 | die("Unsupported command: %s", command_buf.buf); | |
db5e523f | 1814 | } |
c44cdc7e | 1815 | |
db5e523f | 1816 | fixup_header_footer(); |
ac47a738 | 1817 | close(pack_fd); |
8bcce301 | 1818 | write_index(idx_name); |
463acbe1 | 1819 | dump_branches(); |
72303d44 | 1820 | dump_tags(); |
a6a1a831 | 1821 | dump_marks(); |
08d7e892 SP |
1822 | if (branch_log) |
1823 | fclose(branch_log); | |
8bcce301 | 1824 | |
6143f064 SP |
1825 | fprintf(stderr, "%s statistics:\n", argv[0]); |
1826 | fprintf(stderr, "---------------------------------------------------\n"); | |
1827 | fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt); | |
1828 | fprintf(stderr, "Total objects: %10lu (%10lu duplicates)\n", object_count, duplicate_count); | |
4cabf858 SP |
1829 | 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]); |
1830 | 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]); | |
1831 | 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]); | |
1832 | 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 | 1833 | fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count); |
d8397168 | 1834 | fprintf(stderr, " marks: %10u (%10lu unique )\n", (1 << marks->shift) * 1024, marks_set_count); |
d6c7eb2c | 1835 | fprintf(stderr, " atoms: %10u\n", atom_cnt); |
7111feed SP |
1836 | fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024); |
1837 | fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024); | |
1838 | fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024); | |
41e5257f | 1839 | fprintf(stderr, "Pack remaps: %10lu\n", remap_count); |
6143f064 SP |
1840 | fprintf(stderr, "---------------------------------------------------\n"); |
1841 | ||
1842 | stat(pack_name, &sb); | |
1843 | fprintf(stderr, "Pack size: %10lu KiB\n", (unsigned long)(sb.st_size/1024)); | |
1844 | stat(idx_name, &sb); | |
1845 | fprintf(stderr, "Index size: %10lu KiB\n", (unsigned long)(sb.st_size/1024)); | |
1846 | ||
1847 | fprintf(stderr, "\n"); | |
db5e523f SP |
1848 | |
1849 | return 0; | |
1850 | } |