]>
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]; | |
8a8c55ea SP |
481 | b->branch_tree.versions[0].mode = S_IFDIR; |
482 | b->branch_tree.versions[1].mode = S_IFDIR; | |
463acbe1 SP |
483 | branch_table[hc] = b; |
484 | branch_count++; | |
485 | return b; | |
486 | } | |
487 | ||
488 | static unsigned int hc_entries(unsigned int cnt) | |
489 | { | |
490 | cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8; | |
491 | return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1; | |
492 | } | |
493 | ||
494 | static struct tree_content* new_tree_content(unsigned int cnt) | |
495 | { | |
496 | struct avail_tree_content *f, *l = NULL; | |
497 | struct tree_content *t; | |
498 | unsigned int hc = hc_entries(cnt); | |
499 | ||
500 | for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail) | |
501 | if (f->entry_capacity >= cnt) | |
502 | break; | |
503 | ||
504 | if (f) { | |
505 | if (l) | |
506 | l->next_avail = f->next_avail; | |
507 | else | |
508 | avail_tree_table[hc] = f->next_avail; | |
509 | } else { | |
510 | cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt; | |
511 | f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt); | |
512 | f->entry_capacity = cnt; | |
513 | } | |
514 | ||
515 | t = (struct tree_content*)f; | |
516 | t->entry_count = 0; | |
4cabf858 | 517 | t->delta_depth = 0; |
463acbe1 SP |
518 | return t; |
519 | } | |
520 | ||
521 | static void release_tree_entry(struct tree_entry *e); | |
522 | static void release_tree_content(struct tree_content *t) | |
523 | { | |
524 | struct avail_tree_content *f = (struct avail_tree_content*)t; | |
525 | unsigned int hc = hc_entries(f->entry_capacity); | |
afde8dd9 SP |
526 | f->next_avail = avail_tree_table[hc]; |
527 | avail_tree_table[hc] = f; | |
528 | } | |
529 | ||
530 | static void release_tree_content_recursive(struct tree_content *t) | |
531 | { | |
463acbe1 SP |
532 | unsigned int i; |
533 | for (i = 0; i < t->entry_count; i++) | |
534 | release_tree_entry(t->entries[i]); | |
afde8dd9 | 535 | release_tree_content(t); |
463acbe1 SP |
536 | } |
537 | ||
538 | static struct tree_content* grow_tree_content( | |
539 | struct tree_content *t, | |
540 | int amt) | |
541 | { | |
542 | struct tree_content *r = new_tree_content(t->entry_count + amt); | |
543 | r->entry_count = t->entry_count; | |
4cabf858 | 544 | r->delta_depth = t->delta_depth; |
463acbe1 SP |
545 | memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0])); |
546 | release_tree_content(t); | |
547 | return r; | |
548 | } | |
549 | ||
550 | static struct tree_entry* new_tree_entry() | |
551 | { | |
552 | struct tree_entry *e; | |
553 | ||
554 | if (!avail_tree_entry) { | |
555 | unsigned int n = tree_entry_alloc; | |
8435a9cb | 556 | total_allocd += n * sizeof(struct tree_entry); |
463acbe1 | 557 | avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry)); |
2eb26d84 | 558 | while (n-- > 1) { |
463acbe1 SP |
559 | *((void**)e) = e + 1; |
560 | e++; | |
561 | } | |
35ef237c | 562 | *((void**)e) = NULL; |
463acbe1 SP |
563 | } |
564 | ||
565 | e = avail_tree_entry; | |
566 | avail_tree_entry = *((void**)e); | |
567 | return e; | |
568 | } | |
569 | ||
570 | static void release_tree_entry(struct tree_entry *e) | |
571 | { | |
572 | if (e->tree) | |
afde8dd9 | 573 | release_tree_content_recursive(e->tree); |
463acbe1 SP |
574 | *((void**)e) = avail_tree_entry; |
575 | avail_tree_entry = e; | |
576 | } | |
577 | ||
578 | static void yread(int fd, void *buffer, size_t length) | |
db5e523f SP |
579 | { |
580 | ssize_t ret = 0; | |
581 | while (ret < length) { | |
582 | ssize_t size = xread(fd, (char *) buffer + ret, length - ret); | |
463acbe1 SP |
583 | if (!size) |
584 | die("Read from descriptor %i: end of stream", fd); | |
585 | if (size < 0) | |
586 | die("Read from descriptor %i: %s", fd, strerror(errno)); | |
587 | ret += size; | |
588 | } | |
589 | } | |
590 | ||
c44cdc7e | 591 | static size_t encode_header( |
ac47a738 | 592 | enum object_type type, |
c44cdc7e | 593 | size_t size, |
ac47a738 | 594 | unsigned char *hdr) |
db5e523f SP |
595 | { |
596 | int n = 1; | |
597 | unsigned char c; | |
598 | ||
599 | if (type < OBJ_COMMIT || type > OBJ_DELTA) | |
600 | die("bad type %d", type); | |
601 | ||
602 | c = (type << 4) | (size & 15); | |
603 | size >>= 4; | |
604 | while (size) { | |
605 | *hdr++ = c | 0x80; | |
606 | c = size & 0x7f; | |
607 | size >>= 7; | |
608 | n++; | |
609 | } | |
610 | *hdr = c; | |
611 | return n; | |
612 | } | |
613 | ||
ac47a738 SP |
614 | static int store_object( |
615 | enum object_type type, | |
616 | void *dat, | |
c44cdc7e | 617 | size_t datlen, |
6bb5b329 | 618 | struct last_object *last, |
d8397168 SP |
619 | unsigned char *sha1out, |
620 | unsigned long mark) | |
db5e523f | 621 | { |
db5e523f | 622 | void *out, *delta; |
ac47a738 SP |
623 | struct object_entry *e; |
624 | unsigned char hdr[96]; | |
625 | unsigned char sha1[20]; | |
db5e523f | 626 | unsigned long hdrlen, deltalen; |
ac47a738 SP |
627 | SHA_CTX c; |
628 | z_stream s; | |
629 | ||
630 | hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1; | |
631 | SHA1_Init(&c); | |
632 | SHA1_Update(&c, hdr, hdrlen); | |
633 | SHA1_Update(&c, dat, datlen); | |
634 | SHA1_Final(sha1, &c); | |
6bb5b329 | 635 | if (sha1out) |
445b8599 | 636 | hashcpy(sha1out, sha1); |
ac47a738 SP |
637 | |
638 | e = insert_object(sha1); | |
d8397168 SP |
639 | if (mark) |
640 | insert_mark(mark, e); | |
ac47a738 SP |
641 | if (e->offset) { |
642 | duplicate_count++; | |
6143f064 | 643 | duplicate_count_by_type[type]++; |
463acbe1 | 644 | return 1; |
ac47a738 | 645 | } |
7111feed | 646 | e->type = type; |
41e5257f | 647 | e->offset = pack_size; |
ac47a738 | 648 | object_count++; |
6143f064 | 649 | object_count_by_type[type]++; |
db5e523f | 650 | |
463acbe1 | 651 | if (last && last->data && last->depth < max_depth) |
ac47a738 | 652 | delta = diff_delta(last->data, last->len, |
db5e523f SP |
653 | dat, datlen, |
654 | &deltalen, 0); | |
ac47a738 | 655 | else |
db5e523f SP |
656 | delta = 0; |
657 | ||
658 | memset(&s, 0, sizeof(s)); | |
659 | deflateInit(&s, zlib_compression_level); | |
660 | ||
661 | if (delta) { | |
4cabf858 | 662 | delta_count_by_type[type]++; |
ac47a738 | 663 | last->depth++; |
db5e523f SP |
664 | s.next_in = delta; |
665 | s.avail_in = deltalen; | |
666 | hdrlen = encode_header(OBJ_DELTA, deltalen, hdr); | |
23bc886c SP |
667 | write_or_die(pack_fd, hdr, hdrlen); |
668 | write_or_die(pack_fd, last->sha1, sizeof(sha1)); | |
41e5257f | 669 | pack_size += hdrlen + sizeof(sha1); |
db5e523f | 670 | } else { |
463acbe1 SP |
671 | if (last) |
672 | last->depth = 0; | |
db5e523f SP |
673 | s.next_in = dat; |
674 | s.avail_in = datlen; | |
ac47a738 | 675 | hdrlen = encode_header(type, datlen, hdr); |
23bc886c | 676 | write_or_die(pack_fd, hdr, hdrlen); |
41e5257f | 677 | pack_size += hdrlen; |
db5e523f SP |
678 | } |
679 | ||
680 | s.avail_out = deflateBound(&s, s.avail_in); | |
681 | s.next_out = out = xmalloc(s.avail_out); | |
682 | while (deflate(&s, Z_FINISH) == Z_OK) | |
683 | /* nothing */; | |
684 | deflateEnd(&s); | |
685 | ||
23bc886c | 686 | write_or_die(pack_fd, out, s.total_out); |
41e5257f | 687 | pack_size += s.total_out; |
db5e523f SP |
688 | |
689 | free(out); | |
690 | if (delta) | |
691 | free(delta); | |
463acbe1 | 692 | if (last) { |
e2eb469d | 693 | if (last->data && !last->no_free) |
463acbe1 SP |
694 | free(last->data); |
695 | last->data = dat; | |
696 | last->len = datlen; | |
445b8599 | 697 | hashcpy(last->sha1, sha1); |
463acbe1 SP |
698 | } |
699 | return 0; | |
700 | } | |
701 | ||
53dbce78 | 702 | static unsigned char* map_pack(unsigned long offset, unsigned int *left) |
41e5257f SP |
703 | { |
704 | if (offset >= pack_size) | |
705 | die("object offset outside of pack file"); | |
53dbce78 SP |
706 | if (!pack_base |
707 | || offset < pack_moff | |
708 | || (offset + 20) >= (pack_moff + pack_mlen)) { | |
41e5257f SP |
709 | if (pack_base) |
710 | munmap(pack_base, pack_mlen); | |
53dbce78 SP |
711 | pack_moff = (offset / page_size) * page_size; |
712 | pack_base = mmap(NULL,pack_mlen,PROT_READ,MAP_SHARED, | |
713 | pack_fd,pack_moff); | |
41e5257f SP |
714 | if (pack_base == MAP_FAILED) |
715 | die("Failed to map generated pack: %s", strerror(errno)); | |
716 | remap_count++; | |
717 | } | |
53dbce78 SP |
718 | offset -= pack_moff; |
719 | if (left) | |
720 | *left = pack_mlen - offset; | |
721 | return pack_base + offset; | |
41e5257f SP |
722 | } |
723 | ||
724 | static unsigned long unpack_object_header(unsigned long offset, | |
725 | enum object_type *type, | |
726 | unsigned long *sizep) | |
727 | { | |
728 | unsigned shift; | |
729 | unsigned char c; | |
730 | unsigned long size; | |
731 | ||
53dbce78 | 732 | c = *map_pack(offset++, NULL); |
41e5257f SP |
733 | *type = (c >> 4) & 7; |
734 | size = c & 15; | |
735 | shift = 4; | |
736 | while (c & 0x80) { | |
53dbce78 | 737 | c = *map_pack(offset++, NULL); |
41e5257f SP |
738 | size += (c & 0x7f) << shift; |
739 | shift += 7; | |
740 | } | |
741 | *sizep = size; | |
742 | return offset; | |
743 | } | |
744 | ||
745 | static void *unpack_non_delta_entry(unsigned long o, unsigned long sz) | |
746 | { | |
747 | z_stream stream; | |
748 | unsigned char *result; | |
749 | ||
750 | result = xmalloc(sz + 1); | |
751 | result[sz] = 0; | |
752 | ||
753 | memset(&stream, 0, sizeof(stream)); | |
53dbce78 | 754 | stream.next_in = map_pack(o, &stream.avail_in); |
41e5257f SP |
755 | stream.next_out = result; |
756 | stream.avail_out = sz; | |
757 | ||
758 | inflateInit(&stream); | |
759 | for (;;) { | |
760 | int st = inflate(&stream, Z_FINISH); | |
761 | if (st == Z_STREAM_END) | |
762 | break; | |
53dbce78 SP |
763 | if (st == Z_OK || st == Z_BUF_ERROR) { |
764 | o = stream.next_in - pack_base + pack_moff; | |
765 | stream.next_in = map_pack(o, &stream.avail_in); | |
41e5257f SP |
766 | continue; |
767 | } | |
53dbce78 | 768 | die("Error %i from zlib during inflate.", st); |
41e5257f SP |
769 | } |
770 | inflateEnd(&stream); | |
771 | if (stream.total_out != sz) | |
772 | die("Error after inflate: sizes mismatch"); | |
773 | return result; | |
774 | } | |
775 | ||
4cabf858 SP |
776 | static void *unpack_entry(unsigned long offset, |
777 | unsigned long *sizep, | |
778 | unsigned int *delta_depth); | |
41e5257f SP |
779 | |
780 | static void *unpack_delta_entry(unsigned long offset, | |
781 | unsigned long delta_size, | |
4cabf858 SP |
782 | unsigned long *sizep, |
783 | unsigned int *delta_depth) | |
41e5257f SP |
784 | { |
785 | struct object_entry *base_oe; | |
786 | unsigned char *base_sha1; | |
787 | void *delta_data, *base, *result; | |
788 | unsigned long base_size, result_size; | |
789 | ||
53dbce78 | 790 | base_sha1 = map_pack(offset, NULL); |
41e5257f SP |
791 | base_oe = find_object(base_sha1); |
792 | if (!base_oe) | |
793 | die("I'm broken; I can't find a base I know must be here."); | |
4cabf858 | 794 | base = unpack_entry(base_oe->offset, &base_size, delta_depth); |
41e5257f SP |
795 | delta_data = unpack_non_delta_entry(offset + 20, delta_size); |
796 | result = patch_delta(base, base_size, | |
797 | delta_data, delta_size, | |
798 | &result_size); | |
799 | if (!result) | |
800 | die("failed to apply delta"); | |
801 | free(delta_data); | |
802 | free(base); | |
803 | *sizep = result_size; | |
4cabf858 | 804 | (*delta_depth)++; |
41e5257f SP |
805 | return result; |
806 | } | |
807 | ||
4cabf858 SP |
808 | static void *unpack_entry(unsigned long offset, |
809 | unsigned long *sizep, | |
810 | unsigned int *delta_depth) | |
41e5257f SP |
811 | { |
812 | unsigned long size; | |
813 | enum object_type kind; | |
814 | ||
815 | offset = unpack_object_header(offset, &kind, &size); | |
816 | switch (kind) { | |
817 | case OBJ_DELTA: | |
4cabf858 | 818 | return unpack_delta_entry(offset, size, sizep, delta_depth); |
41e5257f SP |
819 | case OBJ_COMMIT: |
820 | case OBJ_TREE: | |
821 | case OBJ_BLOB: | |
822 | case OBJ_TAG: | |
823 | *sizep = size; | |
4cabf858 | 824 | *delta_depth = 0; |
41e5257f SP |
825 | return unpack_non_delta_entry(offset, size); |
826 | default: | |
827 | die("I created an object I can't read!"); | |
828 | } | |
829 | } | |
830 | ||
463acbe1 SP |
831 | static const char *get_mode(const char *str, unsigned int *modep) |
832 | { | |
833 | unsigned char c; | |
834 | unsigned int mode = 0; | |
835 | ||
836 | while ((c = *str++) != ' ') { | |
837 | if (c < '0' || c > '7') | |
838 | return NULL; | |
839 | mode = (mode << 3) + (c - '0'); | |
840 | } | |
841 | *modep = mode; | |
842 | return str; | |
843 | } | |
844 | ||
845 | static void load_tree(struct tree_entry *root) | |
846 | { | |
4cabf858 | 847 | unsigned char* sha1 = root->versions[1].sha1; |
463acbe1 SP |
848 | struct object_entry *myoe; |
849 | struct tree_content *t; | |
850 | unsigned long size; | |
851 | char *buf; | |
852 | const char *c; | |
463acbe1 SP |
853 | |
854 | root->tree = t = new_tree_content(8); | |
4cabf858 | 855 | if (is_null_sha1(sha1)) |
463acbe1 SP |
856 | return; |
857 | ||
4cabf858 | 858 | myoe = find_object(sha1); |
463acbe1 | 859 | if (myoe) { |
41e5257f | 860 | if (myoe->type != OBJ_TREE) |
4cabf858 SP |
861 | die("Not a tree: %s", sha1_to_hex(sha1)); |
862 | buf = unpack_entry(myoe->offset, &size, &t->delta_depth); | |
463acbe1 | 863 | } else { |
41e5257f | 864 | char type[20]; |
4cabf858 | 865 | buf = read_sha1_file(sha1, type, &size); |
00e2b884 | 866 | if (!buf || strcmp(type, tree_type)) |
4cabf858 | 867 | die("Can't load tree %s", sha1_to_hex(sha1)); |
463acbe1 SP |
868 | } |
869 | ||
870 | c = buf; | |
871 | while (c != (buf + size)) { | |
872 | struct tree_entry *e = new_tree_entry(); | |
873 | ||
874 | if (t->entry_count == t->entry_capacity) | |
875 | root->tree = t = grow_tree_content(t, 8); | |
876 | t->entries[t->entry_count++] = e; | |
877 | ||
878 | e->tree = NULL; | |
4cabf858 | 879 | c = get_mode(c, &e->versions[1].mode); |
463acbe1 | 880 | if (!c) |
4cabf858 SP |
881 | die("Corrupt mode in %s", sha1_to_hex(sha1)); |
882 | e->versions[0].mode = e->versions[1].mode; | |
463acbe1 SP |
883 | e->name = to_atom(c, strlen(c)); |
884 | c += e->name->str_len + 1; | |
4cabf858 SP |
885 | hashcpy(e->versions[0].sha1, (unsigned char*)c); |
886 | hashcpy(e->versions[1].sha1, (unsigned char*)c); | |
463acbe1 SP |
887 | c += 20; |
888 | } | |
889 | free(buf); | |
890 | } | |
891 | ||
4cabf858 | 892 | static int tecmp0 (const void *_a, const void *_b) |
463acbe1 SP |
893 | { |
894 | struct tree_entry *a = *((struct tree_entry**)_a); | |
895 | struct tree_entry *b = *((struct tree_entry**)_b); | |
896 | return base_name_compare( | |
4cabf858 SP |
897 | a->name->str_dat, a->name->str_len, a->versions[0].mode, |
898 | b->name->str_dat, b->name->str_len, b->versions[0].mode); | |
463acbe1 SP |
899 | } |
900 | ||
4cabf858 | 901 | static int tecmp1 (const void *_a, const void *_b) |
463acbe1 | 902 | { |
4cabf858 SP |
903 | struct tree_entry *a = *((struct tree_entry**)_a); |
904 | struct tree_entry *b = *((struct tree_entry**)_b); | |
905 | return base_name_compare( | |
906 | a->name->str_dat, a->name->str_len, a->versions[1].mode, | |
907 | b->name->str_dat, b->name->str_len, b->versions[1].mode); | |
908 | } | |
909 | ||
e2eb469d SP |
910 | static void mktree(struct tree_content *t, |
911 | int v, | |
912 | unsigned long *szp, | |
913 | struct dbuf *b) | |
4cabf858 SP |
914 | { |
915 | size_t maxlen = 0; | |
463acbe1 | 916 | unsigned int i; |
e2eb469d | 917 | char *c; |
463acbe1 | 918 | |
4cabf858 SP |
919 | if (!v) |
920 | qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0); | |
921 | else | |
922 | qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1); | |
463acbe1 | 923 | |
463acbe1 | 924 | for (i = 0; i < t->entry_count; i++) { |
4cabf858 SP |
925 | if (t->entries[i]->versions[v].mode) |
926 | maxlen += t->entries[i]->name->str_len + 34; | |
463acbe1 SP |
927 | } |
928 | ||
243f801d | 929 | size_dbuf(b, maxlen); |
e2eb469d | 930 | c = b->buffer; |
463acbe1 SP |
931 | for (i = 0; i < t->entry_count; i++) { |
932 | struct tree_entry *e = t->entries[i]; | |
4cabf858 SP |
933 | if (!e->versions[v].mode) |
934 | continue; | |
935 | c += sprintf(c, "%o", e->versions[v].mode); | |
463acbe1 SP |
936 | *c++ = ' '; |
937 | strcpy(c, e->name->str_dat); | |
938 | c += e->name->str_len + 1; | |
4cabf858 | 939 | hashcpy((unsigned char*)c, e->versions[v].sha1); |
463acbe1 SP |
940 | c += 20; |
941 | } | |
e2eb469d | 942 | *szp = c - (char*)b->buffer; |
4cabf858 SP |
943 | } |
944 | ||
945 | static void store_tree(struct tree_entry *root) | |
946 | { | |
947 | struct tree_content *t = root->tree; | |
948 | unsigned int i, j, del; | |
e2eb469d | 949 | unsigned long new_len; |
4cabf858 SP |
950 | struct last_object lo; |
951 | ||
952 | if (!is_null_sha1(root->versions[1].sha1)) | |
953 | return; | |
954 | ||
955 | for (i = 0; i < t->entry_count; i++) { | |
956 | if (t->entries[i]->tree) | |
957 | store_tree(t->entries[i]); | |
958 | } | |
959 | ||
8a8c55ea SP |
960 | if (!S_ISDIR(root->versions[0].mode) |
961 | || is_null_sha1(root->versions[0].sha1) | |
962 | || !find_object(root->versions[0].sha1)) { | |
4cabf858 SP |
963 | lo.data = NULL; |
964 | lo.depth = 0; | |
965 | } else { | |
e2eb469d SP |
966 | mktree(t, 0, &lo.len, &old_tree); |
967 | lo.data = old_tree.buffer; | |
4cabf858 | 968 | lo.depth = t->delta_depth; |
e2eb469d | 969 | lo.no_free = 1; |
4cabf858 SP |
970 | hashcpy(lo.sha1, root->versions[0].sha1); |
971 | } | |
4cabf858 | 972 | |
8a8c55ea | 973 | mktree(t, 1, &new_len, &new_tree); |
e2eb469d | 974 | store_object(OBJ_TREE, new_tree.buffer, new_len, |
4cabf858 | 975 | &lo, root->versions[1].sha1, 0); |
4cabf858 SP |
976 | |
977 | t->delta_depth = lo.depth; | |
4cabf858 SP |
978 | for (i = 0, j = 0, del = 0; i < t->entry_count; i++) { |
979 | struct tree_entry *e = t->entries[i]; | |
980 | if (e->versions[1].mode) { | |
981 | e->versions[0].mode = e->versions[1].mode; | |
982 | hashcpy(e->versions[0].sha1, e->versions[1].sha1); | |
983 | t->entries[j++] = e; | |
984 | } else { | |
985 | release_tree_entry(e); | |
986 | del++; | |
987 | } | |
988 | } | |
989 | t->entry_count -= del; | |
463acbe1 SP |
990 | } |
991 | ||
992 | static int tree_content_set( | |
993 | struct tree_entry *root, | |
994 | const char *p, | |
995 | const unsigned char *sha1, | |
996 | const unsigned int mode) | |
997 | { | |
998 | struct tree_content *t = root->tree; | |
999 | const char *slash1; | |
1000 | unsigned int i, n; | |
1001 | struct tree_entry *e; | |
1002 | ||
1003 | slash1 = strchr(p, '/'); | |
1004 | if (slash1) | |
1005 | n = slash1 - p; | |
1006 | else | |
1007 | n = strlen(p); | |
1008 | ||
1009 | for (i = 0; i < t->entry_count; i++) { | |
1010 | e = t->entries[i]; | |
1011 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
1012 | if (!slash1) { | |
4cabf858 SP |
1013 | if (e->versions[1].mode == mode |
1014 | && !hashcmp(e->versions[1].sha1, sha1)) | |
463acbe1 | 1015 | return 0; |
4cabf858 SP |
1016 | e->versions[1].mode = mode; |
1017 | hashcpy(e->versions[1].sha1, sha1); | |
463acbe1 | 1018 | if (e->tree) { |
afde8dd9 | 1019 | release_tree_content_recursive(e->tree); |
463acbe1 SP |
1020 | e->tree = NULL; |
1021 | } | |
4cabf858 | 1022 | hashclr(root->versions[1].sha1); |
463acbe1 SP |
1023 | return 1; |
1024 | } | |
4cabf858 | 1025 | if (!S_ISDIR(e->versions[1].mode)) { |
463acbe1 | 1026 | e->tree = new_tree_content(8); |
4cabf858 | 1027 | e->versions[1].mode = S_IFDIR; |
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; |
463acbe1 SP |
1049 | tree_content_set(e, slash1 + 1, sha1, mode); |
1050 | } else { | |
1051 | e->tree = NULL; | |
4cabf858 SP |
1052 | e->versions[1].mode = mode; |
1053 | hashcpy(e->versions[1].sha1, sha1); | |
463acbe1 | 1054 | } |
4cabf858 | 1055 | hashclr(root->versions[1].sha1); |
463acbe1 SP |
1056 | return 1; |
1057 | } | |
1058 | ||
1059 | static int tree_content_remove(struct tree_entry *root, const char *p) | |
1060 | { | |
1061 | struct tree_content *t = root->tree; | |
1062 | const char *slash1; | |
1063 | unsigned int i, n; | |
1064 | struct tree_entry *e; | |
1065 | ||
1066 | slash1 = strchr(p, '/'); | |
1067 | if (slash1) | |
1068 | n = slash1 - p; | |
1069 | else | |
1070 | n = strlen(p); | |
1071 | ||
1072 | for (i = 0; i < t->entry_count; i++) { | |
1073 | e = t->entries[i]; | |
1074 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
4cabf858 | 1075 | if (!slash1 || !S_ISDIR(e->versions[1].mode)) |
463acbe1 SP |
1076 | goto del_entry; |
1077 | if (!e->tree) | |
1078 | load_tree(e); | |
1079 | if (tree_content_remove(e, slash1 + 1)) { | |
b54d6422 SP |
1080 | for (n = 0; n < e->tree->entry_count; n++) { |
1081 | if (e->tree->entries[n]->versions[1].mode) { | |
1082 | hashclr(root->versions[1].sha1); | |
1083 | return 1; | |
1084 | } | |
1085 | } | |
1086 | goto del_entry; | |
463acbe1 SP |
1087 | } |
1088 | return 0; | |
1089 | } | |
1090 | } | |
1091 | return 0; | |
1092 | ||
1093 | del_entry: | |
4cabf858 SP |
1094 | if (e->tree) { |
1095 | release_tree_content_recursive(e->tree); | |
1096 | e->tree = NULL; | |
1097 | } | |
1098 | e->versions[1].mode = 0; | |
1099 | hashclr(e->versions[1].sha1); | |
1100 | hashclr(root->versions[1].sha1); | |
ac47a738 | 1101 | return 1; |
db5e523f SP |
1102 | } |
1103 | ||
8bcce301 | 1104 | static void init_pack_header() |
db5e523f | 1105 | { |
c90be46a SP |
1106 | struct pack_header hdr; |
1107 | ||
1108 | hdr.hdr_signature = htonl(PACK_SIGNATURE); | |
1109 | hdr.hdr_version = htonl(2); | |
1110 | hdr.hdr_entries = 0; | |
1111 | ||
23bc886c | 1112 | write_or_die(pack_fd, &hdr, sizeof(hdr)); |
41e5257f | 1113 | pack_size = sizeof(hdr); |
db5e523f SP |
1114 | } |
1115 | ||
8bcce301 | 1116 | static void fixup_header_footer() |
db5e523f SP |
1117 | { |
1118 | SHA_CTX c; | |
1119 | char hdr[8]; | |
db5e523f SP |
1120 | unsigned long cnt; |
1121 | char *buf; | |
1122 | size_t n; | |
1123 | ||
ac47a738 | 1124 | if (lseek(pack_fd, 0, SEEK_SET) != 0) |
db5e523f SP |
1125 | die("Failed seeking to start: %s", strerror(errno)); |
1126 | ||
1127 | SHA1_Init(&c); | |
463acbe1 | 1128 | yread(pack_fd, hdr, 8); |
db5e523f SP |
1129 | SHA1_Update(&c, hdr, 8); |
1130 | ||
db5e523f SP |
1131 | cnt = htonl(object_count); |
1132 | SHA1_Update(&c, &cnt, 4); | |
23bc886c | 1133 | write_or_die(pack_fd, &cnt, 4); |
db5e523f SP |
1134 | |
1135 | buf = xmalloc(128 * 1024); | |
1136 | for (;;) { | |
ac47a738 | 1137 | n = xread(pack_fd, buf, 128 * 1024); |
db5e523f SP |
1138 | if (n <= 0) |
1139 | break; | |
1140 | SHA1_Update(&c, buf, n); | |
1141 | } | |
1142 | free(buf); | |
1143 | ||
ac47a738 | 1144 | SHA1_Final(pack_sha1, &c); |
23bc886c | 1145 | write_or_die(pack_fd, pack_sha1, sizeof(pack_sha1)); |
db5e523f SP |
1146 | } |
1147 | ||
8bcce301 | 1148 | static int oecmp (const void *_a, const void *_b) |
db5e523f | 1149 | { |
8bcce301 SP |
1150 | struct object_entry *a = *((struct object_entry**)_a); |
1151 | struct object_entry *b = *((struct object_entry**)_b); | |
445b8599 | 1152 | return hashcmp(a->sha1, b->sha1); |
8bcce301 SP |
1153 | } |
1154 | ||
1155 | static void write_index(const char *idx_name) | |
1156 | { | |
1157 | struct sha1file *f; | |
1158 | struct object_entry **idx, **c, **last; | |
1159 | struct object_entry *e; | |
463acbe1 | 1160 | struct object_entry_pool *o; |
8bcce301 SP |
1161 | unsigned int array[256]; |
1162 | int i; | |
1163 | ||
1164 | /* Build the sorted table of object IDs. */ | |
1165 | idx = xmalloc(object_count * sizeof(struct object_entry*)); | |
1166 | c = idx; | |
463acbe1 | 1167 | for (o = blocks; o; o = o->next_pool) |
27d6d290 SP |
1168 | for (e = o->entries; e != o->next_free; e++) |
1169 | *c++ = e; | |
8bcce301 SP |
1170 | last = idx + object_count; |
1171 | qsort(idx, object_count, sizeof(struct object_entry*), oecmp); | |
1172 | ||
1173 | /* Generate the fan-out array. */ | |
1174 | c = idx; | |
1175 | for (i = 0; i < 256; i++) { | |
1176 | struct object_entry **next = c;; | |
1177 | while (next < last) { | |
1178 | if ((*next)->sha1[0] != i) | |
1179 | break; | |
1180 | next++; | |
1181 | } | |
1182 | array[i] = htonl(next - idx); | |
1183 | c = next; | |
1184 | } | |
1185 | ||
1186 | f = sha1create("%s", idx_name); | |
1187 | sha1write(f, array, 256 * sizeof(int)); | |
1188 | for (c = idx; c != last; c++) { | |
1189 | unsigned int offset = htonl((*c)->offset); | |
1190 | sha1write(f, &offset, 4); | |
1191 | sha1write(f, (*c)->sha1, sizeof((*c)->sha1)); | |
1192 | } | |
ac47a738 | 1193 | sha1write(f, pack_sha1, sizeof(pack_sha1)); |
8bcce301 SP |
1194 | sha1close(f, NULL, 1); |
1195 | free(idx); | |
1196 | } | |
1197 | ||
463acbe1 SP |
1198 | static void dump_branches() |
1199 | { | |
1200 | static const char *msg = "fast-import"; | |
1201 | unsigned int i; | |
1202 | struct branch *b; | |
1203 | struct ref_lock *lock; | |
1204 | ||
1205 | for (i = 0; i < branch_table_sz; i++) { | |
1206 | for (b = branch_table[i]; b; b = b->table_next_branch) { | |
1207 | lock = lock_any_ref_for_update(b->name, NULL, 0); | |
1208 | if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0) | |
1209 | die("Can't write %s", b->name); | |
1210 | } | |
1211 | } | |
1212 | } | |
1213 | ||
72303d44 SP |
1214 | static void dump_tags() |
1215 | { | |
1216 | static const char *msg = "fast-import"; | |
1217 | struct tag *t; | |
1218 | struct ref_lock *lock; | |
1219 | char path[PATH_MAX]; | |
1220 | ||
1221 | for (t = first_tag; t; t = t->next_tag) { | |
1222 | sprintf(path, "refs/tags/%s", t->name); | |
1223 | lock = lock_any_ref_for_update(path, NULL, 0); | |
1224 | if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0) | |
1225 | die("Can't write %s", path); | |
1226 | } | |
1227 | } | |
1228 | ||
a6a1a831 SP |
1229 | static void dump_marks_helper(FILE *f, |
1230 | unsigned long base, | |
1231 | struct mark_set *m) | |
1232 | { | |
1233 | int k; | |
1234 | if (m->shift) { | |
1235 | for (k = 0; k < 1024; k++) { | |
1236 | if (m->data.sets[k]) | |
1237 | dump_marks_helper(f, (base + k) << m->shift, | |
1238 | m->data.sets[k]); | |
1239 | } | |
1240 | } else { | |
1241 | for (k = 0; k < 1024; k++) { | |
1242 | if (m->data.marked[k]) | |
264244a0 | 1243 | fprintf(f, ":%lu %s\n", base + k, |
a6a1a831 SP |
1244 | sha1_to_hex(m->data.marked[k]->sha1)); |
1245 | } | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | static void dump_marks() | |
1250 | { | |
1251 | if (mark_file) | |
1252 | { | |
1253 | FILE *f = fopen(mark_file, "w"); | |
1254 | dump_marks_helper(f, 0, marks); | |
1255 | fclose(f); | |
1256 | } | |
1257 | } | |
1258 | ||
c44cdc7e SP |
1259 | static void read_next_command() |
1260 | { | |
1261 | read_line(&command_buf, stdin, '\n'); | |
1262 | } | |
1263 | ||
1264 | static void cmd_mark() | |
1265 | { | |
1266 | if (!strncmp("mark :", command_buf.buf, 6)) { | |
d8397168 | 1267 | next_mark = strtoul(command_buf.buf + 6, NULL, 10); |
c44cdc7e SP |
1268 | read_next_command(); |
1269 | } | |
1270 | else | |
d8397168 | 1271 | next_mark = 0; |
c44cdc7e SP |
1272 | } |
1273 | ||
1274 | static void* cmd_data (size_t *size) | |
1275 | { | |
1276 | size_t n = 0; | |
1277 | void *buffer; | |
1278 | size_t length; | |
1279 | ||
1280 | if (strncmp("data ", command_buf.buf, 5)) | |
1281 | die("Expected 'data n' command, found: %s", command_buf.buf); | |
1282 | ||
1283 | length = strtoul(command_buf.buf + 5, NULL, 10); | |
1284 | buffer = xmalloc(length); | |
1285 | ||
1286 | while (n < length) { | |
1287 | size_t s = fread((char*)buffer + n, 1, length - n, stdin); | |
1288 | if (!s && feof(stdin)) | |
1289 | die("EOF in data (%lu bytes remaining)", length - n); | |
1290 | n += s; | |
1291 | } | |
1292 | ||
1293 | if (fgetc(stdin) != '\n') | |
1294 | die("An lf did not trail the binary data as expected."); | |
1295 | ||
1296 | *size = length; | |
1297 | return buffer; | |
1298 | } | |
1299 | ||
463acbe1 | 1300 | static void cmd_new_blob() |
6143f064 | 1301 | { |
d8397168 SP |
1302 | size_t l; |
1303 | void *d; | |
c44cdc7e SP |
1304 | |
1305 | read_next_command(); | |
1306 | cmd_mark(); | |
d8397168 | 1307 | d = cmd_data(&l); |
6143f064 | 1308 | |
d8397168 SP |
1309 | if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark)) |
1310 | free(d); | |
6143f064 SP |
1311 | } |
1312 | ||
463acbe1 | 1313 | static void unload_one_branch() |
6bb5b329 | 1314 | { |
41e5257f SP |
1315 | while (cur_active_branches |
1316 | && cur_active_branches >= max_active_branches) { | |
463acbe1 SP |
1317 | unsigned long min_commit = ULONG_MAX; |
1318 | struct branch *e, *l = NULL, *p = NULL; | |
1319 | ||
1320 | for (e = active_branches; e; e = e->active_next_branch) { | |
1321 | if (e->last_commit < min_commit) { | |
1322 | p = l; | |
1323 | min_commit = e->last_commit; | |
1324 | } | |
1325 | l = e; | |
1326 | } | |
1327 | ||
1328 | if (p) { | |
1329 | e = p->active_next_branch; | |
1330 | p->active_next_branch = e->active_next_branch; | |
1331 | } else { | |
1332 | e = active_branches; | |
1333 | active_branches = e->active_next_branch; | |
1334 | } | |
1335 | e->active_next_branch = NULL; | |
1336 | if (e->branch_tree.tree) { | |
afde8dd9 | 1337 | release_tree_content_recursive(e->branch_tree.tree); |
463acbe1 SP |
1338 | e->branch_tree.tree = NULL; |
1339 | } | |
1340 | cur_active_branches--; | |
6bb5b329 | 1341 | } |
6bb5b329 SP |
1342 | } |
1343 | ||
463acbe1 | 1344 | static void load_branch(struct branch *b) |
6bb5b329 | 1345 | { |
463acbe1 SP |
1346 | load_tree(&b->branch_tree); |
1347 | b->active_next_branch = active_branches; | |
1348 | active_branches = b; | |
1349 | cur_active_branches++; | |
d6c7eb2c | 1350 | branch_load_count++; |
6bb5b329 SP |
1351 | } |
1352 | ||
463acbe1 | 1353 | static void file_change_m(struct branch *b) |
6bb5b329 | 1354 | { |
c44cdc7e SP |
1355 | const char *p = command_buf.buf + 2; |
1356 | char *p_uq; | |
1357 | const char *endp; | |
7111feed | 1358 | struct object_entry *oe; |
463acbe1 | 1359 | unsigned char sha1[20]; |
c44cdc7e | 1360 | unsigned int mode; |
7111feed | 1361 | char type[20]; |
6bb5b329 | 1362 | |
c44cdc7e SP |
1363 | p = get_mode(p, &mode); |
1364 | if (!p) | |
1365 | die("Corrupt mode: %s", command_buf.buf); | |
1366 | switch (mode) { | |
1367 | case S_IFREG | 0644: | |
1368 | case S_IFREG | 0755: | |
ace4a9d1 | 1369 | case S_IFLNK: |
c44cdc7e SP |
1370 | case 0644: |
1371 | case 0755: | |
1372 | /* ok */ | |
1373 | break; | |
1374 | default: | |
1375 | die("Corrupt mode: %s", command_buf.buf); | |
1376 | } | |
1377 | ||
d8397168 SP |
1378 | if (*p == ':') { |
1379 | char *x; | |
1380 | oe = find_mark(strtoul(p + 1, &x, 10)); | |
cacbdd0a | 1381 | hashcpy(sha1, oe->sha1); |
d8397168 SP |
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); |
8a8c55ea SP |
1567 | hashcpy(b->branch_tree.versions[0].sha1, |
1568 | b->branch_tree.versions[1].sha1); | |
243f801d | 1569 | size_dbuf(&new_data, 97 + msglen |
c44cdc7e SP |
1570 | + (author |
1571 | ? strlen(author) + strlen(committer) | |
1572 | : 2 * strlen(committer))); | |
243f801d | 1573 | sp = new_data.buffer; |
4cabf858 SP |
1574 | sp += sprintf(sp, "tree %s\n", |
1575 | sha1_to_hex(b->branch_tree.versions[1].sha1)); | |
445b8599 | 1576 | if (!is_null_sha1(b->sha1)) |
c44cdc7e SP |
1577 | sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1)); |
1578 | if (author) | |
1579 | sp += sprintf(sp, "%s\n", author); | |
1580 | else | |
1581 | sp += sprintf(sp, "author %s\n", committer + 10); | |
1582 | sp += sprintf(sp, "%s\n\n", committer); | |
1583 | memcpy(sp, msg, msglen); | |
1584 | sp += msglen; | |
1585 | if (author) | |
1586 | free(author); | |
1587 | free(committer); | |
1588 | free(msg); | |
1589 | ||
243f801d SP |
1590 | store_object(OBJ_COMMIT, |
1591 | new_data.buffer, sp - (char*)new_data.buffer, | |
1592 | NULL, b->sha1, next_mark); | |
463acbe1 | 1593 | b->last_commit = object_count_by_type[OBJ_COMMIT]; |
264244a0 SP |
1594 | |
1595 | if (branch_log) { | |
1596 | int need_dq = quote_c_style(b->name, NULL, NULL, 0); | |
1597 | fprintf(branch_log, "commit "); | |
1598 | if (need_dq) { | |
1599 | fputc('"', branch_log); | |
1600 | quote_c_style(b->name, NULL, branch_log, 0); | |
1601 | fputc('"', branch_log); | |
1602 | } else | |
1603 | fprintf(branch_log, "%s", b->name); | |
1604 | fprintf(branch_log," :%lu %s\n",next_mark,sha1_to_hex(b->sha1)); | |
1605 | } | |
6bb5b329 SP |
1606 | } |
1607 | ||
72303d44 SP |
1608 | static void cmd_new_tag() |
1609 | { | |
1610 | char *str_uq; | |
1611 | const char *endp; | |
1612 | char *sp; | |
1613 | const char *from; | |
1614 | char *tagger; | |
1615 | struct branch *s; | |
1616 | void *msg; | |
1617 | size_t msglen; | |
72303d44 | 1618 | struct tag *t; |
264244a0 | 1619 | unsigned long from_mark = 0; |
72303d44 SP |
1620 | unsigned char sha1[20]; |
1621 | ||
1622 | /* Obtain the new tag name from the rest of our command */ | |
1623 | sp = strchr(command_buf.buf, ' ') + 1; | |
1624 | str_uq = unquote_c_style(sp, &endp); | |
1625 | if (str_uq) { | |
1626 | if (*endp) | |
1627 | die("Garbage after tag name in: %s", command_buf.buf); | |
1628 | sp = str_uq; | |
1629 | } | |
1630 | t = pool_alloc(sizeof(struct tag)); | |
1631 | t->next_tag = NULL; | |
1632 | t->name = pool_strdup(sp); | |
1633 | if (last_tag) | |
1634 | last_tag->next_tag = t; | |
1635 | else | |
1636 | first_tag = t; | |
1637 | last_tag = t; | |
1638 | if (str_uq) | |
1639 | free(str_uq); | |
1640 | read_next_command(); | |
1641 | ||
1642 | /* from ... */ | |
1643 | if (strncmp("from ", command_buf.buf, 5)) | |
1644 | die("Expected from command, got %s", command_buf.buf); | |
1645 | ||
1646 | from = strchr(command_buf.buf, ' ') + 1; | |
1647 | str_uq = unquote_c_style(from, &endp); | |
1648 | if (str_uq) { | |
1649 | if (*endp) | |
1650 | die("Garbage after string in: %s", command_buf.buf); | |
1651 | from = str_uq; | |
1652 | } | |
1653 | ||
1654 | s = lookup_branch(from); | |
1655 | if (s) { | |
445b8599 | 1656 | hashcpy(sha1, s->sha1); |
72303d44 | 1657 | } else if (*from == ':') { |
264244a0 SP |
1658 | from_mark = strtoul(from + 1, NULL, 10); |
1659 | struct object_entry *oe = find_mark(from_mark); | |
72303d44 | 1660 | if (oe->type != OBJ_COMMIT) |
264244a0 | 1661 | die("Mark :%lu not a commit", from_mark); |
445b8599 | 1662 | hashcpy(sha1, oe->sha1); |
72303d44 SP |
1663 | } else if (!get_sha1(from, sha1)) { |
1664 | unsigned long size; | |
1665 | char *buf; | |
1666 | ||
1667 | buf = read_object_with_reference(sha1, | |
1668 | type_names[OBJ_COMMIT], &size, sha1); | |
1669 | if (!buf || size < 46) | |
1670 | die("Not a valid commit: %s", from); | |
1671 | free(buf); | |
1672 | } else | |
1673 | die("Invalid ref name or SHA1 expression: %s", from); | |
1674 | ||
1675 | if (str_uq) | |
1676 | free(str_uq); | |
1677 | read_next_command(); | |
1678 | ||
1679 | /* tagger ... */ | |
1680 | if (strncmp("tagger ", command_buf.buf, 7)) | |
1681 | die("Expected tagger command, got %s", command_buf.buf); | |
1682 | tagger = strdup(command_buf.buf); | |
1683 | ||
1684 | /* tag payload/message */ | |
1685 | read_next_command(); | |
1686 | msg = cmd_data(&msglen); | |
1687 | ||
1688 | /* build the tag object */ | |
243f801d SP |
1689 | size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen); |
1690 | sp = new_data.buffer; | |
72303d44 SP |
1691 | sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1)); |
1692 | sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]); | |
1693 | sp += sprintf(sp, "tag %s\n", t->name); | |
1694 | sp += sprintf(sp, "%s\n\n", tagger); | |
1695 | memcpy(sp, msg, msglen); | |
1696 | sp += msglen; | |
1697 | free(tagger); | |
1698 | free(msg); | |
1699 | ||
243f801d SP |
1700 | store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer, |
1701 | NULL, t->sha1, 0); | |
264244a0 SP |
1702 | |
1703 | if (branch_log) { | |
1704 | int need_dq = quote_c_style(t->name, NULL, NULL, 0); | |
1705 | fprintf(branch_log, "tag "); | |
1706 | if (need_dq) { | |
1707 | fputc('"', branch_log); | |
1708 | quote_c_style(t->name, NULL, branch_log, 0); | |
1709 | fputc('"', branch_log); | |
1710 | } else | |
1711 | fprintf(branch_log, "%s", t->name); | |
1712 | fprintf(branch_log," :%lu %s\n",from_mark,sha1_to_hex(t->sha1)); | |
1713 | } | |
72303d44 SP |
1714 | } |
1715 | ||
5fced8dc SP |
1716 | static void cmd_reset_branch() |
1717 | { | |
1718 | struct branch *b; | |
1719 | char *str_uq; | |
1720 | const char *endp; | |
1721 | char *sp; | |
1722 | ||
1723 | /* Obtain the branch name from the rest of our command */ | |
1724 | sp = strchr(command_buf.buf, ' ') + 1; | |
1725 | str_uq = unquote_c_style(sp, &endp); | |
1726 | if (str_uq) { | |
1727 | if (*endp) | |
1728 | die("Garbage after ref in: %s", command_buf.buf); | |
1729 | sp = str_uq; | |
1730 | } | |
1731 | b = lookup_branch(sp); | |
1732 | if (b) { | |
1733 | b->last_commit = 0; | |
1734 | if (b->branch_tree.tree) { | |
1735 | release_tree_content_recursive(b->branch_tree.tree); | |
1736 | b->branch_tree.tree = NULL; | |
1737 | } | |
1738 | } | |
1739 | if (str_uq) | |
1740 | free(str_uq); | |
1741 | } | |
1742 | ||
d5c57b28 | 1743 | static const char fast_import_usage[] = |
264244a0 | 1744 | "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack"; |
d5c57b28 | 1745 | |
8bcce301 SP |
1746 | int main(int argc, const char **argv) |
1747 | { | |
d5c57b28 SP |
1748 | const char *base_name; |
1749 | int i; | |
243f801d | 1750 | unsigned long est_obj_cnt = object_entry_alloc; |
8bcce301 SP |
1751 | char *pack_name; |
1752 | char *idx_name; | |
6143f064 | 1753 | struct stat sb; |
8bcce301 | 1754 | |
463acbe1 SP |
1755 | setup_ident(); |
1756 | git_config(git_default_config); | |
53dbce78 | 1757 | page_size = getpagesize(); |
463acbe1 | 1758 | |
d5c57b28 SP |
1759 | for (i = 1; i < argc; i++) { |
1760 | const char *a = argv[i]; | |
1761 | ||
1762 | if (*a != '-' || !strcmp(a, "--")) | |
1763 | break; | |
1764 | else if (!strncmp(a, "--objects=", 10)) | |
1765 | est_obj_cnt = strtoul(a + 10, NULL, 0); | |
1766 | else if (!strncmp(a, "--depth=", 8)) | |
1767 | max_depth = strtoul(a + 8, NULL, 0); | |
1768 | else if (!strncmp(a, "--active-branches=", 18)) | |
1769 | max_active_branches = strtoul(a + 18, NULL, 0); | |
a6a1a831 SP |
1770 | else if (!strncmp(a, "--export-marks=", 15)) |
1771 | mark_file = a + 15; | |
264244a0 SP |
1772 | else if (!strncmp(a, "--branch-log=", 13)) { |
1773 | branch_log = fopen(a + 13, "w"); | |
1774 | if (!branch_log) | |
1775 | die("Can't create %s: %s", a + 13, strerror(errno)); | |
1776 | } | |
d5c57b28 SP |
1777 | else |
1778 | die("unknown option %s", a); | |
1779 | } | |
1780 | if ((i+1) != argc) | |
1781 | usage(fast_import_usage); | |
1782 | base_name = argv[i]; | |
1783 | ||
8bcce301 SP |
1784 | pack_name = xmalloc(strlen(base_name) + 6); |
1785 | sprintf(pack_name, "%s.pack", base_name); | |
1786 | idx_name = xmalloc(strlen(base_name) + 5); | |
1787 | sprintf(idx_name, "%s.idx", base_name); | |
1788 | ||
ac47a738 SP |
1789 | pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666); |
1790 | if (pack_fd < 0) | |
6143f064 | 1791 | die("Can't create %s: %s", pack_name, strerror(errno)); |
8bcce301 | 1792 | |
c44cdc7e | 1793 | init_pack_header(); |
27d6d290 | 1794 | alloc_objects(est_obj_cnt); |
c44cdc7e | 1795 | strbuf_init(&command_buf); |
463acbe1 SP |
1796 | |
1797 | atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*)); | |
1798 | branch_table = xcalloc(branch_table_sz, sizeof(struct branch*)); | |
1799 | avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*)); | |
d8397168 | 1800 | marks = pool_calloc(1, sizeof(struct mark_set)); |
463acbe1 | 1801 | |
db5e523f | 1802 | for (;;) { |
c44cdc7e SP |
1803 | read_next_command(); |
1804 | if (command_buf.eof) | |
db5e523f | 1805 | break; |
c44cdc7e SP |
1806 | else if (!strcmp("blob", command_buf.buf)) |
1807 | cmd_new_blob(); | |
c44cdc7e SP |
1808 | else if (!strncmp("commit ", command_buf.buf, 7)) |
1809 | cmd_new_commit(); | |
72303d44 SP |
1810 | else if (!strncmp("tag ", command_buf.buf, 4)) |
1811 | cmd_new_tag(); | |
5fced8dc SP |
1812 | else if (!strncmp("reset ", command_buf.buf, 6)) |
1813 | cmd_reset_branch(); | |
c44cdc7e SP |
1814 | else |
1815 | die("Unsupported command: %s", command_buf.buf); | |
db5e523f | 1816 | } |
c44cdc7e | 1817 | |
db5e523f | 1818 | fixup_header_footer(); |
ac47a738 | 1819 | close(pack_fd); |
8bcce301 | 1820 | write_index(idx_name); |
463acbe1 | 1821 | dump_branches(); |
72303d44 | 1822 | dump_tags(); |
a6a1a831 | 1823 | dump_marks(); |
08d7e892 SP |
1824 | if (branch_log) |
1825 | fclose(branch_log); | |
8bcce301 | 1826 | |
6143f064 | 1827 | fprintf(stderr, "%s statistics:\n", argv[0]); |
8a8c55ea | 1828 | fprintf(stderr, "---------------------------------------------------------------------\n"); |
6143f064 | 1829 | fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt); |
8a8c55ea | 1830 | fprintf(stderr, "Total objects: %10lu (%10lu duplicates )\n", object_count, duplicate_count); |
4cabf858 SP |
1831 | 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]); |
1832 | 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]); | |
1833 | 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]); | |
1834 | 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 | 1835 | fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count); |
d8397168 | 1836 | fprintf(stderr, " marks: %10u (%10lu unique )\n", (1 << marks->shift) * 1024, marks_set_count); |
d6c7eb2c | 1837 | fprintf(stderr, " atoms: %10u\n", atom_cnt); |
7111feed SP |
1838 | fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024); |
1839 | fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024); | |
1840 | fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024); | |
41e5257f | 1841 | fprintf(stderr, "Pack remaps: %10lu\n", remap_count); |
6143f064 SP |
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)); | |
8a8c55ea | 1846 | fprintf(stderr, "---------------------------------------------------------------------\n"); |
6143f064 SP |
1847 | |
1848 | fprintf(stderr, "\n"); | |
db5e523f SP |
1849 | |
1850 | return 0; | |
1851 | } |