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