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