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