]>
Commit | Line | Data |
---|---|---|
463acbe1 SP |
1 | /* |
2 | Format of STDIN stream: | |
3 | ||
4 | stream ::= cmd*; | |
5 | ||
6 | cmd ::= new_blob | |
463acbe1 | 7 | | new_branch |
c44cdc7e | 8 | | new_commit |
463acbe1 SP |
9 | | new_tag |
10 | ; | |
11 | ||
c44cdc7e SP |
12 | new_blob ::= 'blob' lf |
13 | mark? | |
14 | file_content; | |
15 | file_content ::= data; | |
463acbe1 | 16 | |
c44cdc7e SP |
17 | new_branch ::= 'branch' sp ref_str lf |
18 | ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)? | |
19 | lf; | |
463acbe1 | 20 | |
c44cdc7e SP |
21 | new_commit ::= 'commit' sp ref_str lf |
22 | mark? | |
23 | ('author' sp name '<' email '>' ts tz lf)? | |
24 | 'committer' sp name '<' email '>' ts tz lf | |
25 | commit_msg | |
26 | file_change* | |
27 | lf; | |
28 | commit_msg ::= data; | |
463acbe1 | 29 | |
c44cdc7e SP |
30 | file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf |
31 | | 'D' sp path_str lf | |
463acbe1 | 32 | ; |
c44cdc7e SP |
33 | mode ::= '644' | '755'; |
34 | ||
35 | new_tag ::= 'tag' sp tag_str lf | |
36 | 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf | |
37 | 'tagger' sp name '<' email '>' ts tz lf | |
38 | tag_msg; | |
39 | tag_msg ::= data; | |
40 | ||
41 | # note: the first idnum in a stream should be 1 and subsequent | |
42 | # idnums should not have gaps between values as this will cause | |
43 | # the stream parser to reserve space for the gapped values. An | |
44 | # idnum can be updated in the future to a new object by issuing | |
45 | # a new mark directive with the old idnum. | |
46 | # | |
47 | mark ::= 'mark' sp idnum lf; | |
48 | ||
49 | # note: declen indicates the length of binary_data in bytes. | |
50 | # declen does not include the lf preceeding or trailing the | |
51 | # binary data. | |
52 | # | |
53 | data ::= 'data' sp declen lf | |
54 | binary_data | |
55 | lf; | |
56 | ||
57 | # note: quoted strings are C-style quoting supporting \c for | |
58 | # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn | |
59 | # is the signed byte value in octal. Note that the only | |
60 | # characters which must actually be escaped to protect the | |
61 | # stream formatting is: \, " and LF. Otherwise these values | |
62 | # are UTF8. | |
63 | # | |
64 | ref_str ::= ref | '"' quoted(ref) '"' ; | |
65 | sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ; | |
66 | tag_str ::= tag | '"' quoted(tag) '"' ; | |
67 | path_str ::= path | '"' quoted(path) '"' ; | |
68 | ||
69 | declen ::= # unsigned 32 bit value, ascii base10 notation; | |
463acbe1 | 70 | binary_data ::= # file content, not interpreted; |
c44cdc7e | 71 | |
463acbe1 SP |
72 | sp ::= # ASCII space character; |
73 | lf ::= # ASCII newline (LF) character; | |
c44cdc7e SP |
74 | |
75 | # note: a colon (':') must precede the numerical value assigned to | |
76 | # an idnum. This is to distinguish it from a ref or tag name as | |
77 | # GIT does not permit ':' in ref or tag strings. | |
78 | # | |
79 | idnum ::= ':' declen; | |
80 | path ::= # GIT style file path, e.g. "a/b/c"; | |
81 | ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT"; | |
82 | tag ::= # GIT tag name, e.g. "FIREFOX_1_5"; | |
463acbe1 SP |
83 | sha1exp ::= # Any valid GIT SHA1 expression; |
84 | hexsha1 ::= # SHA1 in hexadecimal format; | |
c44cdc7e SP |
85 | |
86 | # note: name and email are UTF8 strings, however name must not | |
87 | # contain '<' or lf and email must not contain any of the | |
88 | # following: '<', '>', lf. | |
89 | # | |
90 | name ::= # valid GIT author/committer name; | |
463acbe1 | 91 | email ::= # valid GIT author/committer email; |
c44cdc7e SP |
92 | ts ::= # time since the epoch in seconds, ascii base10 notation; |
93 | tz ::= # GIT style timezone; | |
463acbe1 SP |
94 | */ |
95 | ||
db5e523f SP |
96 | #include "builtin.h" |
97 | #include "cache.h" | |
98 | #include "object.h" | |
99 | #include "blob.h" | |
463acbe1 | 100 | #include "tree.h" |
db5e523f SP |
101 | #include "delta.h" |
102 | #include "pack.h" | |
463acbe1 | 103 | #include "refs.h" |
db5e523f | 104 | #include "csum-file.h" |
c44cdc7e SP |
105 | #include "strbuf.h" |
106 | #include "quote.h" | |
db5e523f | 107 | |
27d6d290 SP |
108 | struct object_entry |
109 | { | |
110 | struct object_entry *next; | |
7111feed | 111 | enum object_type type; |
27d6d290 SP |
112 | unsigned long offset; |
113 | unsigned char sha1[20]; | |
114 | }; | |
115 | ||
463acbe1 | 116 | struct object_entry_pool |
27d6d290 | 117 | { |
463acbe1 | 118 | struct object_entry_pool *next_pool; |
27d6d290 SP |
119 | struct object_entry *next_free; |
120 | struct object_entry *end; | |
ac47a738 | 121 | struct object_entry entries[FLEX_ARRAY]; /* more */ |
27d6d290 SP |
122 | }; |
123 | ||
d8397168 SP |
124 | struct mark_set |
125 | { | |
126 | int shift; | |
127 | union { | |
128 | struct object_entry *marked[1024]; | |
129 | struct mark_set *sets[1024]; | |
130 | } data; | |
131 | }; | |
132 | ||
ac47a738 SP |
133 | struct last_object |
134 | { | |
135 | void *data; | |
6bb5b329 SP |
136 | unsigned int len; |
137 | unsigned int depth; | |
ac47a738 SP |
138 | unsigned char sha1[20]; |
139 | }; | |
140 | ||
463acbe1 SP |
141 | struct mem_pool |
142 | { | |
143 | struct mem_pool *next_pool; | |
144 | char *next_free; | |
145 | char *end; | |
146 | char space[FLEX_ARRAY]; /* more */ | |
147 | }; | |
148 | ||
149 | struct atom_str | |
150 | { | |
151 | struct atom_str *next_atom; | |
152 | int str_len; | |
153 | char str_dat[FLEX_ARRAY]; /* more */ | |
154 | }; | |
155 | ||
156 | struct tree_content; | |
6bb5b329 SP |
157 | struct tree_entry |
158 | { | |
463acbe1 SP |
159 | struct tree_content *tree; |
160 | struct atom_str* name; | |
161 | unsigned int mode; | |
6bb5b329 | 162 | unsigned char sha1[20]; |
6bb5b329 SP |
163 | }; |
164 | ||
463acbe1 | 165 | struct tree_content |
6bb5b329 | 166 | { |
463acbe1 SP |
167 | unsigned int entry_capacity; /* must match avail_tree_content */ |
168 | unsigned int entry_count; | |
169 | struct tree_entry *entries[FLEX_ARRAY]; /* more */ | |
170 | }; | |
171 | ||
172 | struct avail_tree_content | |
173 | { | |
174 | unsigned int entry_capacity; /* must match tree_content */ | |
175 | struct avail_tree_content *next_avail; | |
6bb5b329 SP |
176 | }; |
177 | ||
178 | struct branch | |
179 | { | |
463acbe1 SP |
180 | struct branch *table_next_branch; |
181 | struct branch *active_next_branch; | |
6bb5b329 | 182 | const char *name; |
463acbe1 SP |
183 | unsigned long last_commit; |
184 | struct tree_entry branch_tree; | |
185 | unsigned char sha1[20]; | |
6bb5b329 SP |
186 | }; |
187 | ||
463acbe1 SP |
188 | |
189 | /* Stats and misc. counters */ | |
d5c57b28 | 190 | static unsigned long max_depth = 10; |
27d6d290 | 191 | static unsigned long alloc_count; |
6bb5b329 | 192 | static unsigned long branch_count; |
db5e523f | 193 | static unsigned long object_count; |
8bcce301 | 194 | static unsigned long duplicate_count; |
d8397168 | 195 | static unsigned long marks_set_count; |
6143f064 SP |
196 | static unsigned long object_count_by_type[9]; |
197 | static unsigned long duplicate_count_by_type[9]; | |
ac47a738 | 198 | |
463acbe1 SP |
199 | /* Memory pools */ |
200 | static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool); | |
201 | static size_t total_allocd; | |
202 | static struct mem_pool *mem_pool; | |
203 | ||
c44cdc7e | 204 | /* Atom management */ |
463acbe1 SP |
205 | static unsigned int atom_table_sz = 4451; |
206 | static unsigned int atom_cnt; | |
207 | static struct atom_str **atom_table; | |
208 | ||
209 | /* The .pack file being generated */ | |
ac47a738 SP |
210 | static int pack_fd; |
211 | static unsigned long pack_offset; | |
212 | static unsigned char pack_sha1[20]; | |
213 | ||
214 | /* Table of objects we've written. */ | |
463acbe1 SP |
215 | static unsigned int object_entry_alloc = 1000; |
216 | static struct object_entry_pool *blocks; | |
217 | static struct object_entry *object_table[1 << 16]; | |
d8397168 | 218 | static struct mark_set *marks; |
ac47a738 SP |
219 | |
220 | /* Our last blob */ | |
463acbe1 SP |
221 | static struct last_object last_blob; |
222 | ||
223 | /* Tree management */ | |
224 | static unsigned int tree_entry_alloc = 1000; | |
225 | static void *avail_tree_entry; | |
226 | static unsigned int avail_tree_table_sz = 100; | |
227 | static struct avail_tree_content **avail_tree_table; | |
8bcce301 | 228 | |
6bb5b329 | 229 | /* Branch data */ |
d5c57b28 SP |
230 | static unsigned long max_active_branches = 5; |
231 | static unsigned long cur_active_branches; | |
232 | static unsigned long branch_table_sz = 1039; | |
463acbe1 SP |
233 | static struct branch **branch_table; |
234 | static struct branch *active_branches; | |
235 | ||
c44cdc7e SP |
236 | /* Input stream parsing */ |
237 | static struct strbuf command_buf; | |
d8397168 | 238 | static unsigned long next_mark; |
c44cdc7e | 239 | |
6bb5b329 | 240 | |
27d6d290 | 241 | static void alloc_objects(int cnt) |
8bcce301 | 242 | { |
463acbe1 | 243 | struct object_entry_pool *b; |
27d6d290 | 244 | |
463acbe1 | 245 | b = xmalloc(sizeof(struct object_entry_pool) |
27d6d290 | 246 | + cnt * sizeof(struct object_entry)); |
463acbe1 | 247 | b->next_pool = blocks; |
27d6d290 SP |
248 | b->next_free = b->entries; |
249 | b->end = b->entries + cnt; | |
250 | blocks = b; | |
251 | alloc_count += cnt; | |
252 | } | |
8bcce301 | 253 | |
27d6d290 | 254 | static struct object_entry* new_object(unsigned char *sha1) |
8bcce301 | 255 | { |
27d6d290 | 256 | struct object_entry *e; |
8bcce301 | 257 | |
27d6d290 | 258 | if (blocks->next_free == blocks->end) |
463acbe1 | 259 | alloc_objects(object_entry_alloc); |
8bcce301 | 260 | |
27d6d290 SP |
261 | e = blocks->next_free++; |
262 | memcpy(e->sha1, sha1, sizeof(e->sha1)); | |
263 | return e; | |
8bcce301 SP |
264 | } |
265 | ||
463acbe1 SP |
266 | static struct object_entry* find_object(unsigned char *sha1) |
267 | { | |
268 | unsigned int h = sha1[0] << 8 | sha1[1]; | |
269 | struct object_entry *e; | |
270 | for (e = object_table[h]; e; e = e->next) | |
271 | if (!memcmp(sha1, e->sha1, sizeof(e->sha1))) | |
272 | return e; | |
273 | return NULL; | |
274 | } | |
275 | ||
8bcce301 SP |
276 | static struct object_entry* insert_object(unsigned char *sha1) |
277 | { | |
278 | unsigned int h = sha1[0] << 8 | sha1[1]; | |
27d6d290 | 279 | struct object_entry *e = object_table[h]; |
463acbe1 | 280 | struct object_entry *p = NULL; |
8bcce301 SP |
281 | |
282 | while (e) { | |
283 | if (!memcmp(sha1, e->sha1, sizeof(e->sha1))) | |
284 | return e; | |
285 | p = e; | |
286 | e = e->next; | |
287 | } | |
288 | ||
289 | e = new_object(sha1); | |
463acbe1 | 290 | e->next = NULL; |
8bcce301 SP |
291 | e->offset = 0; |
292 | if (p) | |
293 | p->next = e; | |
294 | else | |
27d6d290 | 295 | object_table[h] = e; |
8bcce301 SP |
296 | return e; |
297 | } | |
db5e523f | 298 | |
463acbe1 SP |
299 | static unsigned int hc_str(const char *s, size_t len) |
300 | { | |
301 | unsigned int r = 0; | |
302 | while (len-- > 0) | |
303 | r = r * 31 + *s++; | |
304 | return r; | |
305 | } | |
306 | ||
307 | static void* pool_alloc(size_t len) | |
308 | { | |
309 | struct mem_pool *p; | |
310 | void *r; | |
311 | ||
312 | for (p = mem_pool; p; p = p->next_pool) | |
313 | if ((p->end - p->next_free >= len)) | |
314 | break; | |
315 | ||
316 | if (!p) { | |
317 | if (len >= (mem_pool_alloc/2)) { | |
318 | total_allocd += len; | |
319 | return xmalloc(len); | |
320 | } | |
321 | total_allocd += sizeof(struct mem_pool) + mem_pool_alloc; | |
322 | p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc); | |
323 | p->next_pool = mem_pool; | |
324 | p->next_free = p->space; | |
325 | p->end = p->next_free + mem_pool_alloc; | |
326 | mem_pool = p; | |
327 | } | |
328 | ||
329 | r = p->next_free; | |
330 | p->next_free += len; | |
331 | return r; | |
332 | } | |
333 | ||
334 | static void* pool_calloc(size_t count, size_t size) | |
335 | { | |
336 | size_t len = count * size; | |
337 | void *r = pool_alloc(len); | |
338 | memset(r, 0, len); | |
339 | return r; | |
340 | } | |
341 | ||
342 | static char* pool_strdup(const char *s) | |
343 | { | |
344 | char *r = pool_alloc(strlen(s) + 1); | |
345 | strcpy(r, s); | |
346 | return r; | |
347 | } | |
348 | ||
d8397168 SP |
349 | static void insert_mark(unsigned long idnum, struct object_entry *oe) |
350 | { | |
351 | struct mark_set *s = marks; | |
352 | while ((idnum >> s->shift) >= 1024) { | |
353 | s = pool_calloc(1, sizeof(struct mark_set)); | |
354 | s->shift = marks->shift + 10; | |
355 | s->data.sets[0] = marks; | |
356 | marks = s; | |
357 | } | |
358 | while (s->shift) { | |
359 | unsigned long i = idnum >> s->shift; | |
360 | idnum -= i << s->shift; | |
361 | if (!s->data.sets[i]) { | |
362 | s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set)); | |
363 | s->data.sets[i]->shift = s->shift - 10; | |
364 | } | |
365 | s = s->data.sets[i]; | |
366 | } | |
367 | if (!s->data.marked[idnum]) | |
368 | marks_set_count++; | |
369 | s->data.marked[idnum] = oe; | |
370 | } | |
371 | ||
372 | static struct object_entry* find_mark(unsigned long idnum) | |
373 | { | |
374 | unsigned long orig_idnum = idnum; | |
375 | struct mark_set *s = marks; | |
376 | struct object_entry *oe = NULL; | |
377 | if ((idnum >> s->shift) < 1024) { | |
378 | while (s && s->shift) { | |
379 | unsigned long i = idnum >> s->shift; | |
380 | idnum -= i << s->shift; | |
381 | s = s->data.sets[i]; | |
382 | } | |
383 | if (s) | |
384 | oe = s->data.marked[idnum]; | |
385 | } | |
386 | if (!oe) | |
387 | die("mark :%lu not declared", orig_idnum); | |
388 | return oe; | |
389 | } | |
390 | ||
463acbe1 SP |
391 | static struct atom_str* to_atom(const char *s, size_t len) |
392 | { | |
393 | unsigned int hc = hc_str(s, len) % atom_table_sz; | |
394 | struct atom_str *c; | |
395 | ||
396 | for (c = atom_table[hc]; c; c = c->next_atom) | |
397 | if (c->str_len == len && !strncmp(s, c->str_dat, len)) | |
398 | return c; | |
399 | ||
400 | c = pool_alloc(sizeof(struct atom_str) + len + 1); | |
401 | c->str_len = len; | |
402 | strncpy(c->str_dat, s, len); | |
403 | c->str_dat[len] = 0; | |
404 | c->next_atom = atom_table[hc]; | |
405 | atom_table[hc] = c; | |
406 | atom_cnt++; | |
407 | return c; | |
408 | } | |
409 | ||
410 | static struct branch* lookup_branch(const char *name) | |
411 | { | |
412 | unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; | |
413 | struct branch *b; | |
414 | ||
415 | for (b = branch_table[hc]; b; b = b->table_next_branch) | |
416 | if (!strcmp(name, b->name)) | |
417 | return b; | |
418 | return NULL; | |
419 | } | |
420 | ||
421 | static struct branch* new_branch(const char *name) | |
422 | { | |
423 | unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; | |
424 | struct branch* b = lookup_branch(name); | |
425 | ||
426 | if (b) | |
427 | die("Invalid attempt to create duplicate branch: %s", name); | |
c44cdc7e SP |
428 | if (check_ref_format(name)) |
429 | die("Branch name doesn't conform to GIT standards: %s", name); | |
463acbe1 SP |
430 | |
431 | b = pool_calloc(1, sizeof(struct branch)); | |
432 | b->name = pool_strdup(name); | |
433 | b->table_next_branch = branch_table[hc]; | |
434 | branch_table[hc] = b; | |
435 | branch_count++; | |
436 | return b; | |
437 | } | |
438 | ||
439 | static unsigned int hc_entries(unsigned int cnt) | |
440 | { | |
441 | cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8; | |
442 | return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1; | |
443 | } | |
444 | ||
445 | static struct tree_content* new_tree_content(unsigned int cnt) | |
446 | { | |
447 | struct avail_tree_content *f, *l = NULL; | |
448 | struct tree_content *t; | |
449 | unsigned int hc = hc_entries(cnt); | |
450 | ||
451 | for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail) | |
452 | if (f->entry_capacity >= cnt) | |
453 | break; | |
454 | ||
455 | if (f) { | |
456 | if (l) | |
457 | l->next_avail = f->next_avail; | |
458 | else | |
459 | avail_tree_table[hc] = f->next_avail; | |
460 | } else { | |
461 | cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt; | |
462 | f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt); | |
463 | f->entry_capacity = cnt; | |
464 | } | |
465 | ||
466 | t = (struct tree_content*)f; | |
467 | t->entry_count = 0; | |
468 | return t; | |
469 | } | |
470 | ||
471 | static void release_tree_entry(struct tree_entry *e); | |
472 | static void release_tree_content(struct tree_content *t) | |
473 | { | |
474 | struct avail_tree_content *f = (struct avail_tree_content*)t; | |
475 | unsigned int hc = hc_entries(f->entry_capacity); | |
afde8dd9 SP |
476 | f->next_avail = avail_tree_table[hc]; |
477 | avail_tree_table[hc] = f; | |
478 | } | |
479 | ||
480 | static void release_tree_content_recursive(struct tree_content *t) | |
481 | { | |
463acbe1 SP |
482 | unsigned int i; |
483 | for (i = 0; i < t->entry_count; i++) | |
484 | release_tree_entry(t->entries[i]); | |
afde8dd9 | 485 | release_tree_content(t); |
463acbe1 SP |
486 | } |
487 | ||
488 | static struct tree_content* grow_tree_content( | |
489 | struct tree_content *t, | |
490 | int amt) | |
491 | { | |
492 | struct tree_content *r = new_tree_content(t->entry_count + amt); | |
493 | r->entry_count = t->entry_count; | |
494 | memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0])); | |
495 | release_tree_content(t); | |
496 | return r; | |
497 | } | |
498 | ||
499 | static struct tree_entry* new_tree_entry() | |
500 | { | |
501 | struct tree_entry *e; | |
502 | ||
503 | if (!avail_tree_entry) { | |
504 | unsigned int n = tree_entry_alloc; | |
505 | avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry)); | |
506 | while (n--) { | |
507 | *((void**)e) = e + 1; | |
508 | e++; | |
509 | } | |
510 | } | |
511 | ||
512 | e = avail_tree_entry; | |
513 | avail_tree_entry = *((void**)e); | |
514 | return e; | |
515 | } | |
516 | ||
517 | static void release_tree_entry(struct tree_entry *e) | |
518 | { | |
519 | if (e->tree) | |
afde8dd9 | 520 | release_tree_content_recursive(e->tree); |
463acbe1 SP |
521 | *((void**)e) = avail_tree_entry; |
522 | avail_tree_entry = e; | |
523 | } | |
524 | ||
525 | static void yread(int fd, void *buffer, size_t length) | |
db5e523f SP |
526 | { |
527 | ssize_t ret = 0; | |
528 | while (ret < length) { | |
529 | ssize_t size = xread(fd, (char *) buffer + ret, length - ret); | |
463acbe1 SP |
530 | if (!size) |
531 | die("Read from descriptor %i: end of stream", fd); | |
532 | if (size < 0) | |
533 | die("Read from descriptor %i: %s", fd, strerror(errno)); | |
534 | ret += size; | |
535 | } | |
536 | } | |
537 | ||
463acbe1 | 538 | static void ywrite(int fd, void *buffer, size_t length) |
db5e523f SP |
539 | { |
540 | ssize_t ret = 0; | |
541 | while (ret < length) { | |
542 | ssize_t size = xwrite(fd, (char *) buffer + ret, length - ret); | |
463acbe1 SP |
543 | if (!size) |
544 | die("Write to descriptor %i: end of file", fd); | |
545 | if (size < 0) | |
546 | die("Write to descriptor %i: %s", fd, strerror(errno)); | |
db5e523f SP |
547 | ret += size; |
548 | } | |
db5e523f SP |
549 | } |
550 | ||
c44cdc7e | 551 | static size_t encode_header( |
ac47a738 | 552 | enum object_type type, |
c44cdc7e | 553 | size_t size, |
ac47a738 | 554 | unsigned char *hdr) |
db5e523f SP |
555 | { |
556 | int n = 1; | |
557 | unsigned char c; | |
558 | ||
559 | if (type < OBJ_COMMIT || type > OBJ_DELTA) | |
560 | die("bad type %d", type); | |
561 | ||
562 | c = (type << 4) | (size & 15); | |
563 | size >>= 4; | |
564 | while (size) { | |
565 | *hdr++ = c | 0x80; | |
566 | c = size & 0x7f; | |
567 | size >>= 7; | |
568 | n++; | |
569 | } | |
570 | *hdr = c; | |
571 | return n; | |
572 | } | |
573 | ||
ac47a738 SP |
574 | static int store_object( |
575 | enum object_type type, | |
576 | void *dat, | |
c44cdc7e | 577 | size_t datlen, |
6bb5b329 | 578 | struct last_object *last, |
d8397168 SP |
579 | unsigned char *sha1out, |
580 | unsigned long mark) | |
db5e523f | 581 | { |
db5e523f | 582 | void *out, *delta; |
ac47a738 SP |
583 | struct object_entry *e; |
584 | unsigned char hdr[96]; | |
585 | unsigned char sha1[20]; | |
db5e523f | 586 | unsigned long hdrlen, deltalen; |
ac47a738 SP |
587 | SHA_CTX c; |
588 | z_stream s; | |
589 | ||
590 | hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1; | |
591 | SHA1_Init(&c); | |
592 | SHA1_Update(&c, hdr, hdrlen); | |
593 | SHA1_Update(&c, dat, datlen); | |
594 | SHA1_Final(sha1, &c); | |
6bb5b329 SP |
595 | if (sha1out) |
596 | memcpy(sha1out, sha1, sizeof(sha1)); | |
ac47a738 SP |
597 | |
598 | e = insert_object(sha1); | |
d8397168 SP |
599 | if (mark) |
600 | insert_mark(mark, e); | |
ac47a738 SP |
601 | if (e->offset) { |
602 | duplicate_count++; | |
6143f064 | 603 | duplicate_count_by_type[type]++; |
463acbe1 | 604 | return 1; |
ac47a738 | 605 | } |
7111feed | 606 | e->type = type; |
ac47a738 SP |
607 | e->offset = pack_offset; |
608 | object_count++; | |
6143f064 | 609 | object_count_by_type[type]++; |
db5e523f | 610 | |
463acbe1 | 611 | if (last && last->data && last->depth < max_depth) |
ac47a738 | 612 | delta = diff_delta(last->data, last->len, |
db5e523f SP |
613 | dat, datlen, |
614 | &deltalen, 0); | |
ac47a738 | 615 | else |
db5e523f SP |
616 | delta = 0; |
617 | ||
618 | memset(&s, 0, sizeof(s)); | |
619 | deflateInit(&s, zlib_compression_level); | |
620 | ||
621 | if (delta) { | |
ac47a738 | 622 | last->depth++; |
db5e523f SP |
623 | s.next_in = delta; |
624 | s.avail_in = deltalen; | |
625 | hdrlen = encode_header(OBJ_DELTA, deltalen, hdr); | |
463acbe1 SP |
626 | ywrite(pack_fd, hdr, hdrlen); |
627 | ywrite(pack_fd, last->sha1, sizeof(sha1)); | |
ac47a738 | 628 | pack_offset += hdrlen + sizeof(sha1); |
db5e523f | 629 | } else { |
463acbe1 SP |
630 | if (last) |
631 | last->depth = 0; | |
db5e523f SP |
632 | s.next_in = dat; |
633 | s.avail_in = datlen; | |
ac47a738 | 634 | hdrlen = encode_header(type, datlen, hdr); |
463acbe1 | 635 | ywrite(pack_fd, hdr, hdrlen); |
ac47a738 | 636 | pack_offset += hdrlen; |
db5e523f SP |
637 | } |
638 | ||
639 | s.avail_out = deflateBound(&s, s.avail_in); | |
640 | s.next_out = out = xmalloc(s.avail_out); | |
641 | while (deflate(&s, Z_FINISH) == Z_OK) | |
642 | /* nothing */; | |
643 | deflateEnd(&s); | |
644 | ||
463acbe1 | 645 | ywrite(pack_fd, out, s.total_out); |
ac47a738 | 646 | pack_offset += s.total_out; |
db5e523f SP |
647 | |
648 | free(out); | |
649 | if (delta) | |
650 | free(delta); | |
463acbe1 SP |
651 | if (last) { |
652 | if (last->data) | |
653 | free(last->data); | |
654 | last->data = dat; | |
655 | last->len = datlen; | |
656 | memcpy(last->sha1, sha1, sizeof(sha1)); | |
657 | } | |
658 | return 0; | |
659 | } | |
660 | ||
661 | static const char *get_mode(const char *str, unsigned int *modep) | |
662 | { | |
663 | unsigned char c; | |
664 | unsigned int mode = 0; | |
665 | ||
666 | while ((c = *str++) != ' ') { | |
667 | if (c < '0' || c > '7') | |
668 | return NULL; | |
669 | mode = (mode << 3) + (c - '0'); | |
670 | } | |
671 | *modep = mode; | |
672 | return str; | |
673 | } | |
674 | ||
675 | static void load_tree(struct tree_entry *root) | |
676 | { | |
677 | struct object_entry *myoe; | |
678 | struct tree_content *t; | |
679 | unsigned long size; | |
680 | char *buf; | |
681 | const char *c; | |
682 | char type[20]; | |
683 | ||
684 | root->tree = t = new_tree_content(8); | |
685 | if (!memcmp(root->sha1, null_sha1, 20)) | |
686 | return; | |
687 | ||
688 | myoe = find_object(root->sha1); | |
689 | if (myoe) { | |
690 | die("FIXME"); | |
691 | } else { | |
692 | buf = read_sha1_file(root->sha1, type, &size); | |
693 | if (!buf || strcmp(type, tree_type)) | |
694 | die("Can't load existing tree %s", sha1_to_hex(root->sha1)); | |
695 | } | |
696 | ||
697 | c = buf; | |
698 | while (c != (buf + size)) { | |
699 | struct tree_entry *e = new_tree_entry(); | |
700 | ||
701 | if (t->entry_count == t->entry_capacity) | |
702 | root->tree = t = grow_tree_content(t, 8); | |
703 | t->entries[t->entry_count++] = e; | |
704 | ||
705 | e->tree = NULL; | |
706 | c = get_mode(c, &e->mode); | |
707 | if (!c) | |
708 | die("Corrupt mode in %s", sha1_to_hex(root->sha1)); | |
709 | e->name = to_atom(c, strlen(c)); | |
710 | c += e->name->str_len + 1; | |
711 | memcpy(e->sha1, c, sizeof(e->sha1)); | |
712 | c += 20; | |
713 | } | |
714 | free(buf); | |
715 | } | |
716 | ||
717 | static int tecmp (const void *_a, const void *_b) | |
718 | { | |
719 | struct tree_entry *a = *((struct tree_entry**)_a); | |
720 | struct tree_entry *b = *((struct tree_entry**)_b); | |
721 | return base_name_compare( | |
722 | a->name->str_dat, a->name->str_len, a->mode, | |
723 | b->name->str_dat, b->name->str_len, b->mode); | |
724 | } | |
725 | ||
726 | static void store_tree(struct tree_entry *root) | |
727 | { | |
728 | struct tree_content *t = root->tree; | |
729 | unsigned int i; | |
730 | size_t maxlen; | |
731 | char *buf, *c; | |
732 | ||
733 | if (memcmp(root->sha1, null_sha1, 20)) | |
734 | return; | |
735 | ||
736 | maxlen = 0; | |
737 | for (i = 0; i < t->entry_count; i++) { | |
738 | maxlen += t->entries[i]->name->str_len + 34; | |
739 | if (t->entries[i]->tree) | |
740 | store_tree(t->entries[i]); | |
741 | } | |
742 | ||
743 | qsort(t->entries, t->entry_count, sizeof(t->entries[0]), tecmp); | |
744 | buf = c = xmalloc(maxlen); | |
745 | for (i = 0; i < t->entry_count; i++) { | |
746 | struct tree_entry *e = t->entries[i]; | |
747 | c += sprintf(c, "%o", e->mode); | |
748 | *c++ = ' '; | |
749 | strcpy(c, e->name->str_dat); | |
750 | c += e->name->str_len + 1; | |
751 | memcpy(c, e->sha1, 20); | |
752 | c += 20; | |
753 | } | |
d8397168 | 754 | store_object(OBJ_TREE, buf, c - buf, NULL, root->sha1, 0); |
463acbe1 SP |
755 | free(buf); |
756 | } | |
757 | ||
758 | static int tree_content_set( | |
759 | struct tree_entry *root, | |
760 | const char *p, | |
761 | const unsigned char *sha1, | |
762 | const unsigned int mode) | |
763 | { | |
764 | struct tree_content *t = root->tree; | |
765 | const char *slash1; | |
766 | unsigned int i, n; | |
767 | struct tree_entry *e; | |
768 | ||
769 | slash1 = strchr(p, '/'); | |
770 | if (slash1) | |
771 | n = slash1 - p; | |
772 | else | |
773 | n = strlen(p); | |
774 | ||
775 | for (i = 0; i < t->entry_count; i++) { | |
776 | e = t->entries[i]; | |
777 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
778 | if (!slash1) { | |
779 | if (e->mode == mode && !memcmp(e->sha1, sha1, 20)) | |
780 | return 0; | |
781 | e->mode = mode; | |
782 | memcpy(e->sha1, sha1, 20); | |
783 | if (e->tree) { | |
afde8dd9 | 784 | release_tree_content_recursive(e->tree); |
463acbe1 SP |
785 | e->tree = NULL; |
786 | } | |
787 | memcpy(root->sha1, null_sha1, 20); | |
788 | return 1; | |
789 | } | |
790 | if (!S_ISDIR(e->mode)) { | |
791 | e->tree = new_tree_content(8); | |
7111feed | 792 | e->mode = S_IFDIR; |
463acbe1 SP |
793 | } |
794 | if (!e->tree) | |
795 | load_tree(e); | |
796 | if (tree_content_set(e, slash1 + 1, sha1, mode)) { | |
797 | memcpy(root->sha1, null_sha1, 20); | |
798 | return 1; | |
799 | } | |
800 | return 0; | |
801 | } | |
802 | } | |
803 | ||
804 | if (t->entry_count == t->entry_capacity) | |
805 | root->tree = t = grow_tree_content(t, 8); | |
806 | e = new_tree_entry(); | |
807 | e->name = to_atom(p, n); | |
808 | t->entries[t->entry_count++] = e; | |
809 | if (slash1) { | |
810 | e->tree = new_tree_content(8); | |
7111feed | 811 | e->mode = S_IFDIR; |
463acbe1 SP |
812 | tree_content_set(e, slash1 + 1, sha1, mode); |
813 | } else { | |
814 | e->tree = NULL; | |
815 | e->mode = mode; | |
816 | memcpy(e->sha1, sha1, 20); | |
817 | } | |
818 | memcpy(root->sha1, null_sha1, 20); | |
819 | return 1; | |
820 | } | |
821 | ||
822 | static int tree_content_remove(struct tree_entry *root, const char *p) | |
823 | { | |
824 | struct tree_content *t = root->tree; | |
825 | const char *slash1; | |
826 | unsigned int i, n; | |
827 | struct tree_entry *e; | |
828 | ||
829 | slash1 = strchr(p, '/'); | |
830 | if (slash1) | |
831 | n = slash1 - p; | |
832 | else | |
833 | n = strlen(p); | |
834 | ||
835 | for (i = 0; i < t->entry_count; i++) { | |
836 | e = t->entries[i]; | |
837 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
838 | if (!slash1 || !S_ISDIR(e->mode)) | |
839 | goto del_entry; | |
840 | if (!e->tree) | |
841 | load_tree(e); | |
842 | if (tree_content_remove(e, slash1 + 1)) { | |
843 | if (!e->tree->entry_count) | |
844 | goto del_entry; | |
845 | memcpy(root->sha1, null_sha1, 20); | |
846 | return 1; | |
847 | } | |
848 | return 0; | |
849 | } | |
850 | } | |
851 | return 0; | |
852 | ||
853 | del_entry: | |
854 | for (i++; i < t->entry_count; i++) | |
855 | t->entries[i-1] = t->entries[i]; | |
856 | t->entry_count--; | |
857 | release_tree_entry(e); | |
858 | memcpy(root->sha1, null_sha1, 20); | |
ac47a738 | 859 | return 1; |
db5e523f SP |
860 | } |
861 | ||
8bcce301 | 862 | static void init_pack_header() |
db5e523f | 863 | { |
c90be46a SP |
864 | struct pack_header hdr; |
865 | ||
866 | hdr.hdr_signature = htonl(PACK_SIGNATURE); | |
867 | hdr.hdr_version = htonl(2); | |
868 | hdr.hdr_entries = 0; | |
869 | ||
870 | ywrite(pack_fd, &hdr, sizeof(hdr)); | |
871 | pack_offset = sizeof(hdr); | |
db5e523f SP |
872 | } |
873 | ||
8bcce301 | 874 | static void fixup_header_footer() |
db5e523f SP |
875 | { |
876 | SHA_CTX c; | |
877 | char hdr[8]; | |
db5e523f SP |
878 | unsigned long cnt; |
879 | char *buf; | |
880 | size_t n; | |
881 | ||
ac47a738 | 882 | if (lseek(pack_fd, 0, SEEK_SET) != 0) |
db5e523f SP |
883 | die("Failed seeking to start: %s", strerror(errno)); |
884 | ||
885 | SHA1_Init(&c); | |
463acbe1 | 886 | yread(pack_fd, hdr, 8); |
db5e523f SP |
887 | SHA1_Update(&c, hdr, 8); |
888 | ||
db5e523f SP |
889 | cnt = htonl(object_count); |
890 | SHA1_Update(&c, &cnt, 4); | |
463acbe1 | 891 | ywrite(pack_fd, &cnt, 4); |
db5e523f SP |
892 | |
893 | buf = xmalloc(128 * 1024); | |
894 | for (;;) { | |
ac47a738 | 895 | n = xread(pack_fd, buf, 128 * 1024); |
db5e523f SP |
896 | if (n <= 0) |
897 | break; | |
898 | SHA1_Update(&c, buf, n); | |
899 | } | |
900 | free(buf); | |
901 | ||
ac47a738 | 902 | SHA1_Final(pack_sha1, &c); |
463acbe1 | 903 | ywrite(pack_fd, pack_sha1, sizeof(pack_sha1)); |
db5e523f SP |
904 | } |
905 | ||
8bcce301 | 906 | static int oecmp (const void *_a, const void *_b) |
db5e523f | 907 | { |
8bcce301 SP |
908 | struct object_entry *a = *((struct object_entry**)_a); |
909 | struct object_entry *b = *((struct object_entry**)_b); | |
910 | return memcmp(a->sha1, b->sha1, sizeof(a->sha1)); | |
911 | } | |
912 | ||
913 | static void write_index(const char *idx_name) | |
914 | { | |
915 | struct sha1file *f; | |
916 | struct object_entry **idx, **c, **last; | |
917 | struct object_entry *e; | |
463acbe1 | 918 | struct object_entry_pool *o; |
8bcce301 SP |
919 | unsigned int array[256]; |
920 | int i; | |
921 | ||
922 | /* Build the sorted table of object IDs. */ | |
923 | idx = xmalloc(object_count * sizeof(struct object_entry*)); | |
924 | c = idx; | |
463acbe1 | 925 | for (o = blocks; o; o = o->next_pool) |
27d6d290 SP |
926 | for (e = o->entries; e != o->next_free; e++) |
927 | *c++ = e; | |
8bcce301 SP |
928 | last = idx + object_count; |
929 | qsort(idx, object_count, sizeof(struct object_entry*), oecmp); | |
930 | ||
931 | /* Generate the fan-out array. */ | |
932 | c = idx; | |
933 | for (i = 0; i < 256; i++) { | |
934 | struct object_entry **next = c;; | |
935 | while (next < last) { | |
936 | if ((*next)->sha1[0] != i) | |
937 | break; | |
938 | next++; | |
939 | } | |
940 | array[i] = htonl(next - idx); | |
941 | c = next; | |
942 | } | |
943 | ||
944 | f = sha1create("%s", idx_name); | |
945 | sha1write(f, array, 256 * sizeof(int)); | |
946 | for (c = idx; c != last; c++) { | |
947 | unsigned int offset = htonl((*c)->offset); | |
948 | sha1write(f, &offset, 4); | |
949 | sha1write(f, (*c)->sha1, sizeof((*c)->sha1)); | |
950 | } | |
ac47a738 | 951 | sha1write(f, pack_sha1, sizeof(pack_sha1)); |
8bcce301 SP |
952 | sha1close(f, NULL, 1); |
953 | free(idx); | |
954 | } | |
955 | ||
463acbe1 SP |
956 | static void dump_branches() |
957 | { | |
958 | static const char *msg = "fast-import"; | |
959 | unsigned int i; | |
960 | struct branch *b; | |
961 | struct ref_lock *lock; | |
962 | ||
963 | for (i = 0; i < branch_table_sz; i++) { | |
964 | for (b = branch_table[i]; b; b = b->table_next_branch) { | |
965 | lock = lock_any_ref_for_update(b->name, NULL, 0); | |
966 | if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0) | |
967 | die("Can't write %s", b->name); | |
968 | } | |
969 | } | |
970 | } | |
971 | ||
c44cdc7e SP |
972 | static void read_next_command() |
973 | { | |
974 | read_line(&command_buf, stdin, '\n'); | |
975 | } | |
976 | ||
977 | static void cmd_mark() | |
978 | { | |
979 | if (!strncmp("mark :", command_buf.buf, 6)) { | |
d8397168 | 980 | next_mark = strtoul(command_buf.buf + 6, NULL, 10); |
c44cdc7e SP |
981 | read_next_command(); |
982 | } | |
983 | else | |
d8397168 | 984 | next_mark = 0; |
c44cdc7e SP |
985 | } |
986 | ||
987 | static void* cmd_data (size_t *size) | |
988 | { | |
989 | size_t n = 0; | |
990 | void *buffer; | |
991 | size_t length; | |
992 | ||
993 | if (strncmp("data ", command_buf.buf, 5)) | |
994 | die("Expected 'data n' command, found: %s", command_buf.buf); | |
995 | ||
996 | length = strtoul(command_buf.buf + 5, NULL, 10); | |
997 | buffer = xmalloc(length); | |
998 | ||
999 | while (n < length) { | |
1000 | size_t s = fread((char*)buffer + n, 1, length - n, stdin); | |
1001 | if (!s && feof(stdin)) | |
1002 | die("EOF in data (%lu bytes remaining)", length - n); | |
1003 | n += s; | |
1004 | } | |
1005 | ||
1006 | if (fgetc(stdin) != '\n') | |
1007 | die("An lf did not trail the binary data as expected."); | |
1008 | ||
1009 | *size = length; | |
1010 | return buffer; | |
1011 | } | |
1012 | ||
463acbe1 | 1013 | static void cmd_new_blob() |
6143f064 | 1014 | { |
d8397168 SP |
1015 | size_t l; |
1016 | void *d; | |
c44cdc7e SP |
1017 | |
1018 | read_next_command(); | |
1019 | cmd_mark(); | |
d8397168 | 1020 | d = cmd_data(&l); |
6143f064 | 1021 | |
d8397168 SP |
1022 | if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark)) |
1023 | free(d); | |
6143f064 SP |
1024 | } |
1025 | ||
463acbe1 | 1026 | static void unload_one_branch() |
6bb5b329 | 1027 | { |
463acbe1 SP |
1028 | while (cur_active_branches >= max_active_branches) { |
1029 | unsigned long min_commit = ULONG_MAX; | |
1030 | struct branch *e, *l = NULL, *p = NULL; | |
1031 | ||
1032 | for (e = active_branches; e; e = e->active_next_branch) { | |
1033 | if (e->last_commit < min_commit) { | |
1034 | p = l; | |
1035 | min_commit = e->last_commit; | |
1036 | } | |
1037 | l = e; | |
1038 | } | |
1039 | ||
1040 | if (p) { | |
1041 | e = p->active_next_branch; | |
1042 | p->active_next_branch = e->active_next_branch; | |
1043 | } else { | |
1044 | e = active_branches; | |
1045 | active_branches = e->active_next_branch; | |
1046 | } | |
1047 | e->active_next_branch = NULL; | |
1048 | if (e->branch_tree.tree) { | |
afde8dd9 | 1049 | release_tree_content_recursive(e->branch_tree.tree); |
463acbe1 SP |
1050 | e->branch_tree.tree = NULL; |
1051 | } | |
1052 | cur_active_branches--; | |
6bb5b329 | 1053 | } |
6bb5b329 SP |
1054 | } |
1055 | ||
463acbe1 | 1056 | static void load_branch(struct branch *b) |
6bb5b329 | 1057 | { |
463acbe1 SP |
1058 | load_tree(&b->branch_tree); |
1059 | b->active_next_branch = active_branches; | |
1060 | active_branches = b; | |
1061 | cur_active_branches++; | |
6bb5b329 SP |
1062 | } |
1063 | ||
463acbe1 | 1064 | static void file_change_m(struct branch *b) |
6bb5b329 | 1065 | { |
c44cdc7e SP |
1066 | const char *p = command_buf.buf + 2; |
1067 | char *p_uq; | |
1068 | const char *endp; | |
7111feed | 1069 | struct object_entry *oe; |
463acbe1 | 1070 | unsigned char sha1[20]; |
c44cdc7e | 1071 | unsigned int mode; |
7111feed | 1072 | char type[20]; |
6bb5b329 | 1073 | |
c44cdc7e SP |
1074 | p = get_mode(p, &mode); |
1075 | if (!p) | |
1076 | die("Corrupt mode: %s", command_buf.buf); | |
1077 | switch (mode) { | |
1078 | case S_IFREG | 0644: | |
1079 | case S_IFREG | 0755: | |
ace4a9d1 | 1080 | case S_IFLNK: |
c44cdc7e SP |
1081 | case 0644: |
1082 | case 0755: | |
1083 | /* ok */ | |
1084 | break; | |
1085 | default: | |
1086 | die("Corrupt mode: %s", command_buf.buf); | |
1087 | } | |
1088 | ||
d8397168 SP |
1089 | if (*p == ':') { |
1090 | char *x; | |
1091 | oe = find_mark(strtoul(p + 1, &x, 10)); | |
1092 | p = x; | |
1093 | } else { | |
1094 | if (get_sha1_hex(p, sha1)) | |
1095 | die("Invalid SHA1: %s", command_buf.buf); | |
1096 | oe = find_object(sha1); | |
1097 | p += 40; | |
1098 | } | |
c44cdc7e SP |
1099 | if (*p++ != ' ') |
1100 | die("Missing space after SHA1: %s", command_buf.buf); | |
1101 | ||
1102 | p_uq = unquote_c_style(p, &endp); | |
1103 | if (p_uq) { | |
1104 | if (*endp) | |
1105 | die("Garbage after path in: %s", command_buf.buf); | |
1106 | p = p_uq; | |
1107 | } | |
6bb5b329 | 1108 | |
7111feed SP |
1109 | if (oe) { |
1110 | if (oe->type != OBJ_BLOB) | |
c44cdc7e SP |
1111 | die("Not a blob (actually a %s): %s", |
1112 | command_buf.buf, type_names[oe->type]); | |
7111feed SP |
1113 | } else { |
1114 | if (sha1_object_info(sha1, type, NULL)) | |
c44cdc7e | 1115 | die("Blob not found: %s", command_buf.buf); |
7111feed | 1116 | if (strcmp(blob_type, type)) |
c44cdc7e SP |
1117 | die("Not a blob (actually a %s): %s", |
1118 | command_buf.buf, type); | |
7111feed | 1119 | } |
6bb5b329 | 1120 | |
c44cdc7e SP |
1121 | tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode); |
1122 | ||
1123 | if (p_uq) | |
1124 | free(p_uq); | |
463acbe1 | 1125 | } |
6bb5b329 | 1126 | |
463acbe1 SP |
1127 | static void file_change_d(struct branch *b) |
1128 | { | |
c44cdc7e SP |
1129 | const char *p = command_buf.buf + 2; |
1130 | char *p_uq; | |
1131 | const char *endp; | |
1132 | ||
1133 | p_uq = unquote_c_style(p, &endp); | |
1134 | if (p_uq) { | |
1135 | if (*endp) | |
1136 | die("Garbage after path in: %s", command_buf.buf); | |
1137 | p = p_uq; | |
1138 | } | |
1139 | tree_content_remove(&b->branch_tree, p); | |
1140 | if (p_uq) | |
1141 | free(p_uq); | |
6bb5b329 SP |
1142 | } |
1143 | ||
463acbe1 | 1144 | static void cmd_new_commit() |
6bb5b329 | 1145 | { |
c44cdc7e SP |
1146 | struct branch *b; |
1147 | void *msg; | |
1148 | size_t msglen; | |
1149 | char *str_uq; | |
1150 | const char *endp; | |
1151 | char *sp; | |
1152 | char *author = NULL; | |
1153 | char *committer = NULL; | |
1154 | char *body; | |
1155 | ||
1156 | /* Obtain the branch name from the rest of our command */ | |
1157 | sp = strchr(command_buf.buf, ' ') + 1; | |
1158 | str_uq = unquote_c_style(sp, &endp); | |
1159 | if (str_uq) { | |
1160 | if (*endp) | |
1161 | die("Garbage after ref in: %s", command_buf.buf); | |
1162 | sp = str_uq; | |
1163 | } | |
1164 | b = lookup_branch(sp); | |
463acbe1 | 1165 | if (!b) |
c44cdc7e SP |
1166 | die("Branch not declared: %s", sp); |
1167 | if (str_uq) | |
1168 | free(str_uq); | |
1169 | ||
1170 | read_next_command(); | |
1171 | cmd_mark(); | |
1172 | if (!strncmp("author ", command_buf.buf, 7)) { | |
1173 | author = strdup(command_buf.buf); | |
1174 | read_next_command(); | |
1175 | } | |
1176 | if (!strncmp("committer ", command_buf.buf, 10)) { | |
1177 | committer = strdup(command_buf.buf); | |
1178 | read_next_command(); | |
1179 | } | |
1180 | if (!committer) | |
1181 | die("Expected committer but didn't get one"); | |
1182 | msg = cmd_data(&msglen); | |
1183 | ||
1184 | /* ensure the branch is active/loaded */ | |
463acbe1 SP |
1185 | if (!b->branch_tree.tree) { |
1186 | unload_one_branch(); | |
1187 | load_branch(b); | |
1188 | } | |
6bb5b329 | 1189 | |
463acbe1 SP |
1190 | /* file_change* */ |
1191 | for (;;) { | |
c44cdc7e SP |
1192 | read_next_command(); |
1193 | if (1 == command_buf.len) | |
463acbe1 | 1194 | break; |
c44cdc7e | 1195 | else if (!strncmp("M ", command_buf.buf, 2)) |
463acbe1 | 1196 | file_change_m(b); |
c44cdc7e | 1197 | else if (!strncmp("D ", command_buf.buf, 2)) |
463acbe1 SP |
1198 | file_change_d(b); |
1199 | else | |
c44cdc7e | 1200 | die("Unsupported file_change: %s", command_buf.buf); |
6bb5b329 | 1201 | } |
6bb5b329 | 1202 | |
c44cdc7e | 1203 | /* build the tree and the commit */ |
463acbe1 | 1204 | store_tree(&b->branch_tree); |
c44cdc7e SP |
1205 | body = xmalloc(97 + msglen |
1206 | + (author | |
1207 | ? strlen(author) + strlen(committer) | |
1208 | : 2 * strlen(committer))); | |
1209 | sp = body; | |
1210 | sp += sprintf(sp, "tree %s\n", sha1_to_hex(b->branch_tree.sha1)); | |
1211 | if (memcmp(b->sha1, null_sha1, 20)) | |
1212 | sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1)); | |
1213 | if (author) | |
1214 | sp += sprintf(sp, "%s\n", author); | |
1215 | else | |
1216 | sp += sprintf(sp, "author %s\n", committer + 10); | |
1217 | sp += sprintf(sp, "%s\n\n", committer); | |
1218 | memcpy(sp, msg, msglen); | |
1219 | sp += msglen; | |
1220 | if (author) | |
1221 | free(author); | |
1222 | free(committer); | |
1223 | free(msg); | |
1224 | ||
d8397168 | 1225 | store_object(OBJ_COMMIT, body, sp - body, NULL, b->sha1, next_mark); |
463acbe1 SP |
1226 | free(body); |
1227 | b->last_commit = object_count_by_type[OBJ_COMMIT]; | |
6bb5b329 SP |
1228 | } |
1229 | ||
463acbe1 | 1230 | static void cmd_new_branch() |
6bb5b329 | 1231 | { |
c44cdc7e SP |
1232 | struct branch *b; |
1233 | char *str_uq; | |
1234 | const char *endp; | |
1235 | char *sp; | |
1236 | ||
1237 | /* Obtain the new branch name from the rest of our command */ | |
1238 | sp = strchr(command_buf.buf, ' ') + 1; | |
1239 | str_uq = unquote_c_style(sp, &endp); | |
1240 | if (str_uq) { | |
1241 | if (*endp) | |
1242 | die("Garbage after ref in: %s", command_buf.buf); | |
1243 | sp = str_uq; | |
463acbe1 | 1244 | } |
c44cdc7e SP |
1245 | b = new_branch(sp); |
1246 | if (str_uq) | |
1247 | free(str_uq); | |
1248 | read_next_command(); | |
1249 | ||
1250 | /* from ... */ | |
1251 | if (!strncmp("from ", command_buf.buf, 5)) { | |
1252 | const char *from; | |
1253 | struct branch *s; | |
1254 | ||
1255 | from = strchr(command_buf.buf, ' ') + 1; | |
1256 | str_uq = unquote_c_style(from, &endp); | |
1257 | if (str_uq) { | |
1258 | if (*endp) | |
1259 | die("Garbage after string in: %s", command_buf.buf); | |
1260 | from = str_uq; | |
463acbe1 | 1261 | } |
c44cdc7e SP |
1262 | |
1263 | s = lookup_branch(from); | |
1264 | if (b == s) | |
1265 | die("Can't create a branch from itself: %s", b->name); | |
1266 | else if (s) { | |
1267 | memcpy(b->sha1, s->sha1, 20); | |
1268 | memcpy(b->branch_tree.sha1, s->branch_tree.sha1, 20); | |
d8397168 SP |
1269 | } else if (*from == ':') { |
1270 | unsigned long idnum = strtoul(from + 1, NULL, 10); | |
1271 | struct object_entry *oe = find_mark(idnum); | |
1272 | if (oe->type != OBJ_COMMIT) | |
1273 | die("Mark :%lu not a commit", idnum); | |
1274 | memcpy(b->sha1, oe->sha1, 20); | |
1275 | memcpy(b->branch_tree.sha1, null_sha1, 20); | |
c44cdc7e SP |
1276 | } else if (!get_sha1(from, b->sha1)) { |
1277 | if (!memcmp(b->sha1, null_sha1, 20)) | |
1278 | memcpy(b->branch_tree.sha1, null_sha1, 20); | |
1279 | else { | |
1280 | unsigned long size; | |
1281 | char *buf; | |
1282 | ||
1283 | buf = read_object_with_reference(b->sha1, | |
1284 | type_names[OBJ_COMMIT], &size, b->sha1); | |
1285 | if (!buf || size < 46) | |
1286 | die("Not a valid commit: %s", from); | |
1287 | if (memcmp("tree ", buf, 5) | |
1288 | || get_sha1_hex(buf + 5, b->branch_tree.sha1)) | |
1289 | die("The commit %s is corrupt", sha1_to_hex(b->sha1)); | |
1290 | free(buf); | |
1291 | } | |
1292 | } else | |
1293 | die("Invalid ref name or SHA1 expression: %s", from); | |
1294 | ||
1295 | if (str_uq) | |
1296 | free(str_uq); | |
1297 | read_next_command(); | |
1298 | } else { | |
1299 | memcpy(b->sha1, null_sha1, 20); | |
1300 | memcpy(b->branch_tree.sha1, null_sha1, 20); | |
1301 | } | |
1302 | ||
1303 | if (command_buf.eof || command_buf.len > 1) | |
1304 | die("An lf did not terminate the branch command as expected."); | |
6bb5b329 SP |
1305 | } |
1306 | ||
d5c57b28 SP |
1307 | static const char fast_import_usage[] = |
1308 | "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] temp.pack"; | |
1309 | ||
8bcce301 SP |
1310 | int main(int argc, const char **argv) |
1311 | { | |
d5c57b28 SP |
1312 | const char *base_name; |
1313 | int i; | |
1314 | unsigned long est_obj_cnt = 1000; | |
8bcce301 SP |
1315 | char *pack_name; |
1316 | char *idx_name; | |
6143f064 | 1317 | struct stat sb; |
8bcce301 | 1318 | |
463acbe1 SP |
1319 | setup_ident(); |
1320 | git_config(git_default_config); | |
1321 | ||
d5c57b28 SP |
1322 | for (i = 1; i < argc; i++) { |
1323 | const char *a = argv[i]; | |
1324 | ||
1325 | if (*a != '-' || !strcmp(a, "--")) | |
1326 | break; | |
1327 | else if (!strncmp(a, "--objects=", 10)) | |
1328 | est_obj_cnt = strtoul(a + 10, NULL, 0); | |
1329 | else if (!strncmp(a, "--depth=", 8)) | |
1330 | max_depth = strtoul(a + 8, NULL, 0); | |
1331 | else if (!strncmp(a, "--active-branches=", 18)) | |
1332 | max_active_branches = strtoul(a + 18, NULL, 0); | |
1333 | else | |
1334 | die("unknown option %s", a); | |
1335 | } | |
1336 | if ((i+1) != argc) | |
1337 | usage(fast_import_usage); | |
1338 | base_name = argv[i]; | |
1339 | ||
8bcce301 SP |
1340 | pack_name = xmalloc(strlen(base_name) + 6); |
1341 | sprintf(pack_name, "%s.pack", base_name); | |
1342 | idx_name = xmalloc(strlen(base_name) + 5); | |
1343 | sprintf(idx_name, "%s.idx", base_name); | |
1344 | ||
ac47a738 SP |
1345 | pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666); |
1346 | if (pack_fd < 0) | |
6143f064 | 1347 | die("Can't create %s: %s", pack_name, strerror(errno)); |
8bcce301 | 1348 | |
c44cdc7e | 1349 | init_pack_header(); |
27d6d290 | 1350 | alloc_objects(est_obj_cnt); |
c44cdc7e | 1351 | strbuf_init(&command_buf); |
463acbe1 SP |
1352 | |
1353 | atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*)); | |
1354 | branch_table = xcalloc(branch_table_sz, sizeof(struct branch*)); | |
1355 | avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*)); | |
d8397168 | 1356 | marks = pool_calloc(1, sizeof(struct mark_set)); |
463acbe1 | 1357 | |
db5e523f | 1358 | for (;;) { |
c44cdc7e SP |
1359 | read_next_command(); |
1360 | if (command_buf.eof) | |
db5e523f | 1361 | break; |
c44cdc7e SP |
1362 | else if (!strcmp("blob", command_buf.buf)) |
1363 | cmd_new_blob(); | |
1364 | else if (!strncmp("branch ", command_buf.buf, 7)) | |
1365 | cmd_new_branch(); | |
1366 | else if (!strncmp("commit ", command_buf.buf, 7)) | |
1367 | cmd_new_commit(); | |
1368 | else | |
1369 | die("Unsupported command: %s", command_buf.buf); | |
db5e523f | 1370 | } |
c44cdc7e | 1371 | |
db5e523f | 1372 | fixup_header_footer(); |
ac47a738 | 1373 | close(pack_fd); |
8bcce301 | 1374 | write_index(idx_name); |
463acbe1 | 1375 | dump_branches(); |
8bcce301 | 1376 | |
6143f064 SP |
1377 | fprintf(stderr, "%s statistics:\n", argv[0]); |
1378 | fprintf(stderr, "---------------------------------------------------\n"); | |
1379 | fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt); | |
1380 | fprintf(stderr, "Total objects: %10lu (%10lu duplicates)\n", object_count, duplicate_count); | |
1381 | fprintf(stderr, " blobs : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB]); | |
1382 | fprintf(stderr, " trees : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE]); | |
1383 | fprintf(stderr, " commits: %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT]); | |
1384 | fprintf(stderr, " tags : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG]); | |
6bb5b329 | 1385 | fprintf(stderr, "Total branches: %10lu\n", branch_count); |
d8397168 SP |
1386 | fprintf(stderr, " atoms: %10u\n", atom_cnt); |
1387 | fprintf(stderr, " marks: %10u (%10lu unique )\n", (1 << marks->shift) * 1024, marks_set_count); | |
7111feed SP |
1388 | fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024); |
1389 | fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024); | |
1390 | fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024); | |
6143f064 SP |
1391 | fprintf(stderr, "---------------------------------------------------\n"); |
1392 | ||
1393 | stat(pack_name, &sb); | |
1394 | fprintf(stderr, "Pack size: %10lu KiB\n", (unsigned long)(sb.st_size/1024)); | |
1395 | stat(idx_name, &sb); | |
1396 | fprintf(stderr, "Index size: %10lu KiB\n", (unsigned long)(sb.st_size/1024)); | |
1397 | ||
1398 | fprintf(stderr, "\n"); | |
db5e523f SP |
1399 | |
1400 | return 0; | |
1401 | } |