]>
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 |
8c1f22da | 10 | | checkpoint |
463acbe1 SP |
11 | ; |
12 | ||
c44cdc7e | 13 | new_blob ::= 'blob' lf |
c905e090 | 14 | mark? |
c44cdc7e SP |
15 | file_content; |
16 | file_content ::= data; | |
463acbe1 | 17 | |
c44cdc7e | 18 | new_commit ::= 'commit' sp ref_str lf |
00e2b884 | 19 | mark? |
63e0c8b3 SP |
20 | ('author' sp name '<' email '>' when lf)? |
21 | 'committer' sp name '<' email '>' when lf | |
00e2b884 | 22 | commit_msg |
02f3389d | 23 | ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)? |
62b6f483 | 24 | ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)* |
c44cdc7e SP |
25 | file_change* |
26 | lf; | |
27 | commit_msg ::= data; | |
463acbe1 | 28 | |
b6f3481b SP |
29 | file_change ::= file_clr |
30 | | file_del | |
31 | | file_rnm | |
32 | | file_cpy | |
33 | | file_obm | |
34 | | file_inm; | |
825769a8 | 35 | file_clr ::= 'deleteall' lf; |
b715cfbb | 36 | file_del ::= 'D' sp path_str lf; |
f39a946a | 37 | file_rnm ::= 'R' sp path_str sp path_str lf; |
b6f3481b | 38 | file_cpy ::= 'C' sp path_str sp path_str lf; |
b715cfbb SP |
39 | file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf; |
40 | file_inm ::= 'M' sp mode sp 'inline' sp path_str lf | |
41 | data; | |
c44cdc7e SP |
42 | |
43 | new_tag ::= 'tag' sp tag_str lf | |
44 | 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf | |
c905e090 | 45 | 'tagger' sp name '<' email '>' when lf |
c44cdc7e SP |
46 | tag_msg; |
47 | tag_msg ::= data; | |
48 | ||
9938ffc5 SP |
49 | reset_branch ::= 'reset' sp ref_str lf |
50 | ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)? | |
51 | lf; | |
5fced8dc | 52 | |
7bfe6e26 SP |
53 | checkpoint ::= 'checkpoint' lf |
54 | lf; | |
55 | ||
c44cdc7e SP |
56 | # note: the first idnum in a stream should be 1 and subsequent |
57 | # idnums should not have gaps between values as this will cause | |
58 | # the stream parser to reserve space for the gapped values. An | |
c905e090 | 59 | # idnum can be updated in the future to a new object by issuing |
c44cdc7e | 60 | # a new mark directive with the old idnum. |
c905e090 | 61 | # |
c44cdc7e | 62 | mark ::= 'mark' sp idnum lf; |
3b4dce02 | 63 | data ::= (delimited_data | exact_data) |
2c570cde | 64 | lf?; |
3b4dce02 SP |
65 | |
66 | # note: delim may be any string but must not contain lf. | |
67 | # data_line may contain any data but must not be exactly | |
68 | # delim. | |
69 | delimited_data ::= 'data' sp '<<' delim lf | |
70 | (data_line lf)* | |
c905e090 | 71 | delim lf; |
c44cdc7e SP |
72 | |
73 | # note: declen indicates the length of binary_data in bytes. | |
3b4dce02 | 74 | # declen does not include the lf preceeding the binary data. |
c44cdc7e | 75 | # |
3b4dce02 SP |
76 | exact_data ::= 'data' sp declen lf |
77 | binary_data; | |
c44cdc7e SP |
78 | |
79 | # note: quoted strings are C-style quoting supporting \c for | |
80 | # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn | |
c905e090 | 81 | # is the signed byte value in octal. Note that the only |
c44cdc7e SP |
82 | # characters which must actually be escaped to protect the |
83 | # stream formatting is: \, " and LF. Otherwise these values | |
c905e090 | 84 | # are UTF8. |
c44cdc7e | 85 | # |
6c3aac1c SP |
86 | ref_str ::= ref; |
87 | sha1exp_str ::= sha1exp; | |
88 | tag_str ::= tag; | |
c44cdc7e | 89 | path_str ::= path | '"' quoted(path) '"' ; |
b715cfbb SP |
90 | mode ::= '100644' | '644' |
91 | | '100755' | '755' | |
9981b6d9 | 92 | | '120000' |
b715cfbb | 93 | ; |
c44cdc7e SP |
94 | |
95 | declen ::= # unsigned 32 bit value, ascii base10 notation; | |
2104838b | 96 | bigint ::= # unsigned integer value, ascii base10 notation; |
463acbe1 | 97 | binary_data ::= # file content, not interpreted; |
c44cdc7e | 98 | |
63e0c8b3 SP |
99 | when ::= raw_when | rfc2822_when; |
100 | raw_when ::= ts sp tz; | |
101 | rfc2822_when ::= # Valid RFC 2822 date and time; | |
102 | ||
463acbe1 SP |
103 | sp ::= # ASCII space character; |
104 | lf ::= # ASCII newline (LF) character; | |
c44cdc7e SP |
105 | |
106 | # note: a colon (':') must precede the numerical value assigned to | |
c905e090 | 107 | # an idnum. This is to distinguish it from a ref or tag name as |
c44cdc7e | 108 | # GIT does not permit ':' in ref or tag strings. |
c905e090 | 109 | # |
2104838b | 110 | idnum ::= ':' bigint; |
c44cdc7e SP |
111 | path ::= # GIT style file path, e.g. "a/b/c"; |
112 | ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT"; | |
113 | tag ::= # GIT tag name, e.g. "FIREFOX_1_5"; | |
463acbe1 SP |
114 | sha1exp ::= # Any valid GIT SHA1 expression; |
115 | hexsha1 ::= # SHA1 in hexadecimal format; | |
c44cdc7e SP |
116 | |
117 | # note: name and email are UTF8 strings, however name must not | |
c905e090 | 118 | # contain '<' or lf and email must not contain any of the |
c44cdc7e | 119 | # following: '<', '>', lf. |
c905e090 | 120 | # |
c44cdc7e | 121 | name ::= # valid GIT author/committer name; |
463acbe1 | 122 | email ::= # valid GIT author/committer email; |
c44cdc7e SP |
123 | ts ::= # time since the epoch in seconds, ascii base10 notation; |
124 | tz ::= # GIT style timezone; | |
401d53fa SP |
125 | |
126 | # note: comments may appear anywhere in the input, except | |
127 | # within a data command. Any form of the data command | |
128 | # always escapes the related input from comment processing. | |
129 | # | |
130 | # In case it is not clear, the '#' that starts the comment | |
131 | # must be the first character on that the line (an lf have | |
132 | # preceeded it). | |
133 | # | |
134 | comment ::= '#' not_lf* lf; | |
135 | not_lf ::= # Any byte that is not ASCII newline (LF); | |
463acbe1 SP |
136 | */ |
137 | ||
db5e523f SP |
138 | #include "builtin.h" |
139 | #include "cache.h" | |
140 | #include "object.h" | |
141 | #include "blob.h" | |
463acbe1 | 142 | #include "tree.h" |
7073e69e | 143 | #include "commit.h" |
db5e523f SP |
144 | #include "delta.h" |
145 | #include "pack.h" | |
463acbe1 | 146 | #include "refs.h" |
db5e523f | 147 | #include "csum-file.h" |
c44cdc7e SP |
148 | #include "strbuf.h" |
149 | #include "quote.h" | |
db5e523f | 150 | |
69e74e74 SP |
151 | #define PACK_ID_BITS 16 |
152 | #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1) | |
153 | ||
27d6d290 SP |
154 | struct object_entry |
155 | { | |
156 | struct object_entry *next; | |
10831c55 | 157 | uint32_t offset; |
03842d8e | 158 | unsigned type : TYPE_BITS; |
69e74e74 | 159 | unsigned pack_id : PACK_ID_BITS; |
27d6d290 SP |
160 | unsigned char sha1[20]; |
161 | }; | |
162 | ||
463acbe1 | 163 | struct object_entry_pool |
27d6d290 | 164 | { |
463acbe1 | 165 | struct object_entry_pool *next_pool; |
27d6d290 SP |
166 | struct object_entry *next_free; |
167 | struct object_entry *end; | |
ac47a738 | 168 | struct object_entry entries[FLEX_ARRAY]; /* more */ |
27d6d290 SP |
169 | }; |
170 | ||
d8397168 SP |
171 | struct mark_set |
172 | { | |
d8397168 SP |
173 | union { |
174 | struct object_entry *marked[1024]; | |
175 | struct mark_set *sets[1024]; | |
176 | } data; | |
6f64f6d9 | 177 | unsigned int shift; |
d8397168 SP |
178 | }; |
179 | ||
ac47a738 SP |
180 | struct last_object |
181 | { | |
182 | void *data; | |
4cabf858 | 183 | unsigned long len; |
10831c55 | 184 | uint32_t offset; |
6bb5b329 | 185 | unsigned int depth; |
d489bc14 | 186 | unsigned no_free:1; |
ac47a738 SP |
187 | }; |
188 | ||
463acbe1 SP |
189 | struct mem_pool |
190 | { | |
191 | struct mem_pool *next_pool; | |
192 | char *next_free; | |
193 | char *end; | |
194 | char space[FLEX_ARRAY]; /* more */ | |
195 | }; | |
196 | ||
197 | struct atom_str | |
198 | { | |
199 | struct atom_str *next_atom; | |
10831c55 | 200 | unsigned short str_len; |
463acbe1 SP |
201 | char str_dat[FLEX_ARRAY]; /* more */ |
202 | }; | |
203 | ||
204 | struct tree_content; | |
6bb5b329 SP |
205 | struct tree_entry |
206 | { | |
463acbe1 SP |
207 | struct tree_content *tree; |
208 | struct atom_str* name; | |
4cabf858 SP |
209 | struct tree_entry_ms |
210 | { | |
10831c55 | 211 | uint16_t mode; |
4cabf858 SP |
212 | unsigned char sha1[20]; |
213 | } versions[2]; | |
6bb5b329 SP |
214 | }; |
215 | ||
463acbe1 | 216 | struct tree_content |
6bb5b329 | 217 | { |
463acbe1 SP |
218 | unsigned int entry_capacity; /* must match avail_tree_content */ |
219 | unsigned int entry_count; | |
4cabf858 | 220 | unsigned int delta_depth; |
463acbe1 SP |
221 | struct tree_entry *entries[FLEX_ARRAY]; /* more */ |
222 | }; | |
223 | ||
224 | struct avail_tree_content | |
225 | { | |
226 | unsigned int entry_capacity; /* must match tree_content */ | |
227 | struct avail_tree_content *next_avail; | |
6bb5b329 SP |
228 | }; |
229 | ||
230 | struct branch | |
231 | { | |
463acbe1 SP |
232 | struct branch *table_next_branch; |
233 | struct branch *active_next_branch; | |
6bb5b329 | 234 | const char *name; |
463acbe1 | 235 | struct tree_entry branch_tree; |
69e74e74 | 236 | uintmax_t last_commit; |
734c91f9 SP |
237 | unsigned active : 1; |
238 | unsigned pack_id : PACK_ID_BITS; | |
463acbe1 | 239 | unsigned char sha1[20]; |
6bb5b329 SP |
240 | }; |
241 | ||
72303d44 SP |
242 | struct tag |
243 | { | |
244 | struct tag *next_tag; | |
245 | const char *name; | |
2369ed79 | 246 | unsigned int pack_id; |
72303d44 SP |
247 | unsigned char sha1[20]; |
248 | }; | |
249 | ||
e2eb469d SP |
250 | struct dbuf |
251 | { | |
252 | void *buffer; | |
253 | size_t capacity; | |
254 | }; | |
255 | ||
62b6f483 SP |
256 | struct hash_list |
257 | { | |
258 | struct hash_list *next; | |
259 | unsigned char sha1[20]; | |
260 | }; | |
463acbe1 | 261 | |
63e0c8b3 SP |
262 | typedef enum { |
263 | WHENSPEC_RAW = 1, | |
264 | WHENSPEC_RFC2822, | |
265 | WHENSPEC_NOW, | |
266 | } whenspec_type; | |
267 | ||
0ea9f045 | 268 | /* Configured limits on output */ |
d5c57b28 | 269 | static unsigned long max_depth = 10; |
6777a59f | 270 | static off_t max_packsize = (1LL << 32) - 1; |
7073e69e | 271 | static int force_update; |
0ea9f045 SP |
272 | |
273 | /* Stats and misc. counters */ | |
274 | static uintmax_t alloc_count; | |
0ea9f045 SP |
275 | static uintmax_t marks_set_count; |
276 | static uintmax_t object_count_by_type[1 << TYPE_BITS]; | |
277 | static uintmax_t duplicate_count_by_type[1 << TYPE_BITS]; | |
278 | static uintmax_t delta_count_by_type[1 << TYPE_BITS]; | |
a7ddc487 | 279 | static unsigned long object_count; |
6bb5b329 | 280 | static unsigned long branch_count; |
d6c7eb2c | 281 | static unsigned long branch_load_count; |
7073e69e | 282 | static int failure; |
bdf1c06d | 283 | static FILE *pack_edges; |
ac47a738 | 284 | |
463acbe1 SP |
285 | /* Memory pools */ |
286 | static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool); | |
287 | static size_t total_allocd; | |
288 | static struct mem_pool *mem_pool; | |
289 | ||
c44cdc7e | 290 | /* Atom management */ |
463acbe1 SP |
291 | static unsigned int atom_table_sz = 4451; |
292 | static unsigned int atom_cnt; | |
293 | static struct atom_str **atom_table; | |
294 | ||
295 | /* The .pack file being generated */ | |
7bfe6e26 | 296 | static unsigned int pack_id; |
d489bc14 | 297 | static struct packed_git *pack_data; |
7bfe6e26 | 298 | static struct packed_git **all_packs; |
41e5257f | 299 | static unsigned long pack_size; |
ac47a738 SP |
300 | |
301 | /* Table of objects we've written. */ | |
4cabf858 | 302 | static unsigned int object_entry_alloc = 5000; |
463acbe1 SP |
303 | static struct object_entry_pool *blocks; |
304 | static struct object_entry *object_table[1 << 16]; | |
d8397168 | 305 | static struct mark_set *marks; |
a6a1a831 | 306 | static const char* mark_file; |
ac47a738 SP |
307 | |
308 | /* Our last blob */ | |
463acbe1 SP |
309 | static struct last_object last_blob; |
310 | ||
311 | /* Tree management */ | |
312 | static unsigned int tree_entry_alloc = 1000; | |
313 | static void *avail_tree_entry; | |
314 | static unsigned int avail_tree_table_sz = 100; | |
315 | static struct avail_tree_content **avail_tree_table; | |
e2eb469d SP |
316 | static struct dbuf old_tree; |
317 | static struct dbuf new_tree; | |
8bcce301 | 318 | |
6bb5b329 | 319 | /* Branch data */ |
d5c57b28 SP |
320 | static unsigned long max_active_branches = 5; |
321 | static unsigned long cur_active_branches; | |
322 | static unsigned long branch_table_sz = 1039; | |
463acbe1 SP |
323 | static struct branch **branch_table; |
324 | static struct branch *active_branches; | |
325 | ||
72303d44 SP |
326 | /* Tag data */ |
327 | static struct tag *first_tag; | |
328 | static struct tag *last_tag; | |
329 | ||
c44cdc7e | 330 | /* Input stream parsing */ |
63e0c8b3 | 331 | static whenspec_type whenspec = WHENSPEC_RAW; |
c44cdc7e | 332 | static struct strbuf command_buf; |
0ea9f045 | 333 | static uintmax_t next_mark; |
243f801d | 334 | static struct dbuf new_data; |
c44cdc7e | 335 | |
6bb5b329 | 336 | |
03842d8e | 337 | static void alloc_objects(unsigned int cnt) |
8bcce301 | 338 | { |
463acbe1 | 339 | struct object_entry_pool *b; |
27d6d290 | 340 | |
463acbe1 | 341 | b = xmalloc(sizeof(struct object_entry_pool) |
27d6d290 | 342 | + cnt * sizeof(struct object_entry)); |
463acbe1 | 343 | b->next_pool = blocks; |
27d6d290 SP |
344 | b->next_free = b->entries; |
345 | b->end = b->entries + cnt; | |
346 | blocks = b; | |
347 | alloc_count += cnt; | |
348 | } | |
8bcce301 | 349 | |
e5b1444b | 350 | static struct object_entry *new_object(unsigned char *sha1) |
8bcce301 | 351 | { |
27d6d290 | 352 | struct object_entry *e; |
8bcce301 | 353 | |
27d6d290 | 354 | if (blocks->next_free == blocks->end) |
463acbe1 | 355 | alloc_objects(object_entry_alloc); |
8bcce301 | 356 | |
27d6d290 | 357 | e = blocks->next_free++; |
445b8599 | 358 | hashcpy(e->sha1, sha1); |
27d6d290 | 359 | return e; |
8bcce301 SP |
360 | } |
361 | ||
e5b1444b | 362 | static struct object_entry *find_object(unsigned char *sha1) |
463acbe1 SP |
363 | { |
364 | unsigned int h = sha1[0] << 8 | sha1[1]; | |
365 | struct object_entry *e; | |
366 | for (e = object_table[h]; e; e = e->next) | |
445b8599 | 367 | if (!hashcmp(sha1, e->sha1)) |
463acbe1 SP |
368 | return e; |
369 | return NULL; | |
370 | } | |
371 | ||
e5b1444b | 372 | static struct object_entry *insert_object(unsigned char *sha1) |
8bcce301 SP |
373 | { |
374 | unsigned int h = sha1[0] << 8 | sha1[1]; | |
27d6d290 | 375 | struct object_entry *e = object_table[h]; |
463acbe1 | 376 | struct object_entry *p = NULL; |
8bcce301 SP |
377 | |
378 | while (e) { | |
445b8599 | 379 | if (!hashcmp(sha1, e->sha1)) |
8bcce301 SP |
380 | return e; |
381 | p = e; | |
382 | e = e->next; | |
383 | } | |
384 | ||
385 | e = new_object(sha1); | |
463acbe1 | 386 | e->next = NULL; |
8bcce301 SP |
387 | e->offset = 0; |
388 | if (p) | |
389 | p->next = e; | |
390 | else | |
27d6d290 | 391 | object_table[h] = e; |
8bcce301 SP |
392 | return e; |
393 | } | |
db5e523f | 394 | |
463acbe1 SP |
395 | static unsigned int hc_str(const char *s, size_t len) |
396 | { | |
397 | unsigned int r = 0; | |
398 | while (len-- > 0) | |
399 | r = r * 31 + *s++; | |
400 | return r; | |
401 | } | |
402 | ||
e5b1444b | 403 | static void *pool_alloc(size_t len) |
463acbe1 SP |
404 | { |
405 | struct mem_pool *p; | |
406 | void *r; | |
407 | ||
408 | for (p = mem_pool; p; p = p->next_pool) | |
409 | if ((p->end - p->next_free >= len)) | |
410 | break; | |
411 | ||
412 | if (!p) { | |
413 | if (len >= (mem_pool_alloc/2)) { | |
414 | total_allocd += len; | |
415 | return xmalloc(len); | |
416 | } | |
417 | total_allocd += sizeof(struct mem_pool) + mem_pool_alloc; | |
418 | p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc); | |
419 | p->next_pool = mem_pool; | |
420 | p->next_free = p->space; | |
421 | p->end = p->next_free + mem_pool_alloc; | |
422 | mem_pool = p; | |
423 | } | |
424 | ||
425 | r = p->next_free; | |
8d8928b0 SP |
426 | /* round out to a pointer alignment */ |
427 | if (len & (sizeof(void*) - 1)) | |
428 | len += sizeof(void*) - (len & (sizeof(void*) - 1)); | |
463acbe1 SP |
429 | p->next_free += len; |
430 | return r; | |
431 | } | |
432 | ||
e5b1444b | 433 | static void *pool_calloc(size_t count, size_t size) |
463acbe1 SP |
434 | { |
435 | size_t len = count * size; | |
436 | void *r = pool_alloc(len); | |
437 | memset(r, 0, len); | |
438 | return r; | |
439 | } | |
440 | ||
e5b1444b | 441 | static char *pool_strdup(const char *s) |
463acbe1 SP |
442 | { |
443 | char *r = pool_alloc(strlen(s) + 1); | |
444 | strcpy(r, s); | |
445 | return r; | |
446 | } | |
447 | ||
243f801d SP |
448 | static void size_dbuf(struct dbuf *b, size_t maxlen) |
449 | { | |
450 | if (b->buffer) { | |
451 | if (b->capacity >= maxlen) | |
452 | return; | |
453 | free(b->buffer); | |
454 | } | |
455 | b->capacity = ((maxlen / 1024) + 1) * 1024; | |
456 | b->buffer = xmalloc(b->capacity); | |
457 | } | |
458 | ||
0ea9f045 | 459 | static void insert_mark(uintmax_t idnum, struct object_entry *oe) |
d8397168 SP |
460 | { |
461 | struct mark_set *s = marks; | |
462 | while ((idnum >> s->shift) >= 1024) { | |
463 | s = pool_calloc(1, sizeof(struct mark_set)); | |
464 | s->shift = marks->shift + 10; | |
465 | s->data.sets[0] = marks; | |
466 | marks = s; | |
467 | } | |
468 | while (s->shift) { | |
0ea9f045 | 469 | uintmax_t i = idnum >> s->shift; |
d8397168 SP |
470 | idnum -= i << s->shift; |
471 | if (!s->data.sets[i]) { | |
472 | s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set)); | |
473 | s->data.sets[i]->shift = s->shift - 10; | |
474 | } | |
475 | s = s->data.sets[i]; | |
476 | } | |
477 | if (!s->data.marked[idnum]) | |
478 | marks_set_count++; | |
479 | s->data.marked[idnum] = oe; | |
480 | } | |
481 | ||
e5b1444b | 482 | static struct object_entry *find_mark(uintmax_t idnum) |
d8397168 | 483 | { |
0ea9f045 | 484 | uintmax_t orig_idnum = idnum; |
d8397168 SP |
485 | struct mark_set *s = marks; |
486 | struct object_entry *oe = NULL; | |
487 | if ((idnum >> s->shift) < 1024) { | |
488 | while (s && s->shift) { | |
0ea9f045 | 489 | uintmax_t i = idnum >> s->shift; |
d8397168 SP |
490 | idnum -= i << s->shift; |
491 | s = s->data.sets[i]; | |
492 | } | |
493 | if (s) | |
494 | oe = s->data.marked[idnum]; | |
495 | } | |
496 | if (!oe) | |
3efb1f34 | 497 | die("mark :%" PRIuMAX " not declared", orig_idnum); |
d8397168 SP |
498 | return oe; |
499 | } | |
500 | ||
e5b1444b | 501 | static struct atom_str *to_atom(const char *s, unsigned short len) |
463acbe1 SP |
502 | { |
503 | unsigned int hc = hc_str(s, len) % atom_table_sz; | |
504 | struct atom_str *c; | |
505 | ||
506 | for (c = atom_table[hc]; c; c = c->next_atom) | |
507 | if (c->str_len == len && !strncmp(s, c->str_dat, len)) | |
508 | return c; | |
509 | ||
510 | c = pool_alloc(sizeof(struct atom_str) + len + 1); | |
511 | c->str_len = len; | |
512 | strncpy(c->str_dat, s, len); | |
513 | c->str_dat[len] = 0; | |
514 | c->next_atom = atom_table[hc]; | |
515 | atom_table[hc] = c; | |
516 | atom_cnt++; | |
517 | return c; | |
518 | } | |
519 | ||
e5b1444b | 520 | static struct branch *lookup_branch(const char *name) |
463acbe1 SP |
521 | { |
522 | unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; | |
523 | struct branch *b; | |
524 | ||
525 | for (b = branch_table[hc]; b; b = b->table_next_branch) | |
526 | if (!strcmp(name, b->name)) | |
527 | return b; | |
528 | return NULL; | |
529 | } | |
530 | ||
e5b1444b | 531 | static struct branch *new_branch(const char *name) |
463acbe1 SP |
532 | { |
533 | unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; | |
534 | struct branch* b = lookup_branch(name); | |
535 | ||
536 | if (b) | |
537 | die("Invalid attempt to create duplicate branch: %s", name); | |
ea08a6fd SP |
538 | switch (check_ref_format(name)) { |
539 | case 0: break; /* its valid */ | |
540 | case -2: break; /* valid, but too few '/', allow anyway */ | |
541 | default: | |
c44cdc7e | 542 | die("Branch name doesn't conform to GIT standards: %s", name); |
ea08a6fd | 543 | } |
463acbe1 SP |
544 | |
545 | b = pool_calloc(1, sizeof(struct branch)); | |
546 | b->name = pool_strdup(name); | |
547 | b->table_next_branch = branch_table[hc]; | |
8a8c55ea SP |
548 | b->branch_tree.versions[0].mode = S_IFDIR; |
549 | b->branch_tree.versions[1].mode = S_IFDIR; | |
734c91f9 | 550 | b->active = 0; |
69e74e74 | 551 | b->pack_id = MAX_PACK_ID; |
463acbe1 SP |
552 | branch_table[hc] = b; |
553 | branch_count++; | |
554 | return b; | |
555 | } | |
556 | ||
557 | static unsigned int hc_entries(unsigned int cnt) | |
558 | { | |
559 | cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8; | |
560 | return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1; | |
561 | } | |
562 | ||
e5b1444b | 563 | static struct tree_content *new_tree_content(unsigned int cnt) |
463acbe1 SP |
564 | { |
565 | struct avail_tree_content *f, *l = NULL; | |
566 | struct tree_content *t; | |
567 | unsigned int hc = hc_entries(cnt); | |
568 | ||
569 | for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail) | |
570 | if (f->entry_capacity >= cnt) | |
571 | break; | |
572 | ||
573 | if (f) { | |
574 | if (l) | |
575 | l->next_avail = f->next_avail; | |
576 | else | |
577 | avail_tree_table[hc] = f->next_avail; | |
578 | } else { | |
579 | cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt; | |
580 | f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt); | |
581 | f->entry_capacity = cnt; | |
582 | } | |
583 | ||
584 | t = (struct tree_content*)f; | |
585 | t->entry_count = 0; | |
4cabf858 | 586 | t->delta_depth = 0; |
463acbe1 SP |
587 | return t; |
588 | } | |
589 | ||
590 | static void release_tree_entry(struct tree_entry *e); | |
591 | static void release_tree_content(struct tree_content *t) | |
592 | { | |
593 | struct avail_tree_content *f = (struct avail_tree_content*)t; | |
594 | unsigned int hc = hc_entries(f->entry_capacity); | |
afde8dd9 SP |
595 | f->next_avail = avail_tree_table[hc]; |
596 | avail_tree_table[hc] = f; | |
597 | } | |
598 | ||
599 | static void release_tree_content_recursive(struct tree_content *t) | |
600 | { | |
463acbe1 SP |
601 | unsigned int i; |
602 | for (i = 0; i < t->entry_count; i++) | |
603 | release_tree_entry(t->entries[i]); | |
afde8dd9 | 604 | release_tree_content(t); |
463acbe1 SP |
605 | } |
606 | ||
e5b1444b | 607 | static struct tree_content *grow_tree_content( |
463acbe1 SP |
608 | struct tree_content *t, |
609 | int amt) | |
610 | { | |
611 | struct tree_content *r = new_tree_content(t->entry_count + amt); | |
612 | r->entry_count = t->entry_count; | |
4cabf858 | 613 | r->delta_depth = t->delta_depth; |
463acbe1 SP |
614 | memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0])); |
615 | release_tree_content(t); | |
616 | return r; | |
617 | } | |
618 | ||
e5b1444b | 619 | static struct tree_entry *new_tree_entry(void) |
463acbe1 SP |
620 | { |
621 | struct tree_entry *e; | |
622 | ||
623 | if (!avail_tree_entry) { | |
624 | unsigned int n = tree_entry_alloc; | |
8435a9cb | 625 | total_allocd += n * sizeof(struct tree_entry); |
463acbe1 | 626 | avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry)); |
2eb26d84 | 627 | while (n-- > 1) { |
463acbe1 SP |
628 | *((void**)e) = e + 1; |
629 | e++; | |
630 | } | |
35ef237c | 631 | *((void**)e) = NULL; |
463acbe1 SP |
632 | } |
633 | ||
634 | e = avail_tree_entry; | |
635 | avail_tree_entry = *((void**)e); | |
636 | return e; | |
637 | } | |
638 | ||
639 | static void release_tree_entry(struct tree_entry *e) | |
640 | { | |
641 | if (e->tree) | |
afde8dd9 | 642 | release_tree_content_recursive(e->tree); |
463acbe1 SP |
643 | *((void**)e) = avail_tree_entry; |
644 | avail_tree_entry = e; | |
645 | } | |
646 | ||
b6f3481b SP |
647 | static struct tree_content *dup_tree_content(struct tree_content *s) |
648 | { | |
649 | struct tree_content *d; | |
650 | struct tree_entry *a, *b; | |
651 | unsigned int i; | |
652 | ||
653 | if (!s) | |
654 | return NULL; | |
655 | d = new_tree_content(s->entry_count); | |
656 | for (i = 0; i < s->entry_count; i++) { | |
657 | a = s->entries[i]; | |
658 | b = new_tree_entry(); | |
659 | memcpy(b, a, sizeof(*a)); | |
660 | if (a->tree && is_null_sha1(b->versions[1].sha1)) | |
661 | b->tree = dup_tree_content(a->tree); | |
662 | else | |
663 | b->tree = NULL; | |
664 | d->entries[i] = b; | |
665 | } | |
666 | d->entry_count = s->entry_count; | |
667 | d->delta_depth = s->delta_depth; | |
668 | ||
669 | return d; | |
670 | } | |
671 | ||
fd99224e | 672 | static void start_packfile(void) |
f70b6534 | 673 | { |
8455e484 | 674 | static char tmpfile[PATH_MAX]; |
7bfe6e26 | 675 | struct packed_git *p; |
f70b6534 | 676 | struct pack_header hdr; |
0fcbcae7 | 677 | int pack_fd; |
f70b6534 | 678 | |
8455e484 | 679 | snprintf(tmpfile, sizeof(tmpfile), |
0e55181f | 680 | "%s/tmp_pack_XXXXXX", get_object_directory()); |
7647b17f | 681 | pack_fd = xmkstemp(tmpfile); |
8455e484 SP |
682 | p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2); |
683 | strcpy(p->pack_name, tmpfile); | |
7bfe6e26 | 684 | p->pack_fd = pack_fd; |
f70b6534 SP |
685 | |
686 | hdr.hdr_signature = htonl(PACK_SIGNATURE); | |
687 | hdr.hdr_version = htonl(2); | |
688 | hdr.hdr_entries = 0; | |
0fcbcae7 | 689 | write_or_die(p->pack_fd, &hdr, sizeof(hdr)); |
7bfe6e26 SP |
690 | |
691 | pack_data = p; | |
f70b6534 SP |
692 | pack_size = sizeof(hdr); |
693 | object_count = 0; | |
7bfe6e26 SP |
694 | |
695 | all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1)); | |
696 | all_packs[pack_id] = p; | |
f70b6534 SP |
697 | } |
698 | ||
f70b6534 SP |
699 | static int oecmp (const void *a_, const void *b_) |
700 | { | |
701 | struct object_entry *a = *((struct object_entry**)a_); | |
702 | struct object_entry *b = *((struct object_entry**)b_); | |
703 | return hashcmp(a->sha1, b->sha1); | |
704 | } | |
705 | ||
e5b1444b | 706 | static char *create_index(void) |
f70b6534 | 707 | { |
8455e484 SP |
708 | static char tmpfile[PATH_MAX]; |
709 | SHA_CTX ctx; | |
f70b6534 SP |
710 | struct sha1file *f; |
711 | struct object_entry **idx, **c, **last, *e; | |
712 | struct object_entry_pool *o; | |
ebea9dd4 | 713 | uint32_t array[256]; |
8455e484 | 714 | int i, idx_fd; |
f70b6534 SP |
715 | |
716 | /* Build the sorted table of object IDs. */ | |
717 | idx = xmalloc(object_count * sizeof(struct object_entry*)); | |
718 | c = idx; | |
719 | for (o = blocks; o; o = o->next_pool) | |
d9ee53ce SP |
720 | for (e = o->next_free; e-- != o->entries;) |
721 | if (pack_id == e->pack_id) | |
722 | *c++ = e; | |
f70b6534 | 723 | last = idx + object_count; |
2fce1f3c SP |
724 | if (c != last) |
725 | die("internal consistency error creating the index"); | |
f70b6534 SP |
726 | qsort(idx, object_count, sizeof(struct object_entry*), oecmp); |
727 | ||
728 | /* Generate the fan-out array. */ | |
729 | c = idx; | |
730 | for (i = 0; i < 256; i++) { | |
731 | struct object_entry **next = c;; | |
732 | while (next < last) { | |
733 | if ((*next)->sha1[0] != i) | |
734 | break; | |
735 | next++; | |
736 | } | |
737 | array[i] = htonl(next - idx); | |
738 | c = next; | |
739 | } | |
740 | ||
8455e484 | 741 | snprintf(tmpfile, sizeof(tmpfile), |
0e55181f | 742 | "%s/tmp_idx_XXXXXX", get_object_directory()); |
7647b17f | 743 | idx_fd = xmkstemp(tmpfile); |
8455e484 | 744 | f = sha1fd(idx_fd, tmpfile); |
f70b6534 | 745 | sha1write(f, array, 256 * sizeof(int)); |
8455e484 | 746 | SHA1_Init(&ctx); |
f70b6534 | 747 | for (c = idx; c != last; c++) { |
ebea9dd4 | 748 | uint32_t offset = htonl((*c)->offset); |
f70b6534 SP |
749 | sha1write(f, &offset, 4); |
750 | sha1write(f, (*c)->sha1, sizeof((*c)->sha1)); | |
8455e484 | 751 | SHA1_Update(&ctx, (*c)->sha1, 20); |
f70b6534 | 752 | } |
09543c96 | 753 | sha1write(f, pack_data->sha1, sizeof(pack_data->sha1)); |
8455e484 | 754 | sha1close(f, NULL, 1); |
f70b6534 | 755 | free(idx); |
8455e484 SP |
756 | SHA1_Final(pack_data->sha1, &ctx); |
757 | return tmpfile; | |
758 | } | |
759 | ||
e5b1444b | 760 | static char *keep_pack(char *curr_index_name) |
8455e484 SP |
761 | { |
762 | static char name[PATH_MAX]; | |
3a55602e | 763 | static const char *keep_msg = "fast-import"; |
8455e484 SP |
764 | int keep_fd; |
765 | ||
766 | chmod(pack_data->pack_name, 0444); | |
767 | chmod(curr_index_name, 0444); | |
768 | ||
769 | snprintf(name, sizeof(name), "%s/pack/pack-%s.keep", | |
770 | get_object_directory(), sha1_to_hex(pack_data->sha1)); | |
771 | keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600); | |
772 | if (keep_fd < 0) | |
773 | die("cannot create keep file"); | |
774 | write(keep_fd, keep_msg, strlen(keep_msg)); | |
775 | close(keep_fd); | |
776 | ||
777 | snprintf(name, sizeof(name), "%s/pack/pack-%s.pack", | |
778 | get_object_directory(), sha1_to_hex(pack_data->sha1)); | |
779 | if (move_temp_to_file(pack_data->pack_name, name)) | |
780 | die("cannot store pack file"); | |
8455e484 SP |
781 | |
782 | snprintf(name, sizeof(name), "%s/pack/pack-%s.idx", | |
783 | get_object_directory(), sha1_to_hex(pack_data->sha1)); | |
784 | if (move_temp_to_file(curr_index_name, name)) | |
785 | die("cannot store index file"); | |
786 | return name; | |
787 | } | |
788 | ||
fd99224e | 789 | static void unkeep_all_packs(void) |
8455e484 SP |
790 | { |
791 | static char name[PATH_MAX]; | |
792 | int k; | |
793 | ||
794 | for (k = 0; k < pack_id; k++) { | |
795 | struct packed_git *p = all_packs[k]; | |
796 | snprintf(name, sizeof(name), "%s/pack/pack-%s.keep", | |
797 | get_object_directory(), sha1_to_hex(p->sha1)); | |
798 | unlink(name); | |
799 | } | |
f70b6534 SP |
800 | } |
801 | ||
fd99224e | 802 | static void end_packfile(void) |
f70b6534 | 803 | { |
7bfe6e26 SP |
804 | struct packed_git *old_p = pack_data, *new_p; |
805 | ||
3e005baf | 806 | if (object_count) { |
8455e484 | 807 | char *idx_name; |
2369ed79 SP |
808 | int i; |
809 | struct branch *b; | |
810 | struct tag *t; | |
8455e484 | 811 | |
8b0eca7c DH |
812 | fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1, |
813 | pack_data->pack_name, object_count); | |
814 | close(pack_data->pack_fd); | |
8455e484 | 815 | idx_name = keep_pack(create_index()); |
3e005baf SP |
816 | |
817 | /* Register the packfile with core git's machinary. */ | |
818 | new_p = add_packed_git(idx_name, strlen(idx_name), 1); | |
819 | if (!new_p) | |
820 | die("core git rejected index %s", idx_name); | |
821 | new_p->windows = old_p->windows; | |
2369ed79 | 822 | all_packs[pack_id] = new_p; |
3e005baf | 823 | install_packed_git(new_p); |
2369ed79 SP |
824 | |
825 | /* Print the boundary */ | |
bdf1c06d SP |
826 | if (pack_edges) { |
827 | fprintf(pack_edges, "%s:", new_p->pack_name); | |
828 | for (i = 0; i < branch_table_sz; i++) { | |
829 | for (b = branch_table[i]; b; b = b->table_next_branch) { | |
830 | if (b->pack_id == pack_id) | |
831 | fprintf(pack_edges, " %s", sha1_to_hex(b->sha1)); | |
832 | } | |
2369ed79 | 833 | } |
bdf1c06d SP |
834 | for (t = first_tag; t; t = t->next_tag) { |
835 | if (t->pack_id == pack_id) | |
836 | fprintf(pack_edges, " %s", sha1_to_hex(t->sha1)); | |
837 | } | |
838 | fputc('\n', pack_edges); | |
839 | fflush(pack_edges); | |
2369ed79 | 840 | } |
2369ed79 SP |
841 | |
842 | pack_id++; | |
3e005baf | 843 | } |
12801587 | 844 | else |
3e005baf | 845 | unlink(old_p->pack_name); |
7bfe6e26 | 846 | free(old_p); |
7bfe6e26 SP |
847 | |
848 | /* We can't carry a delta across packfiles. */ | |
849 | free(last_blob.data); | |
850 | last_blob.data = NULL; | |
851 | last_blob.len = 0; | |
852 | last_blob.offset = 0; | |
853 | last_blob.depth = 0; | |
f70b6534 SP |
854 | } |
855 | ||
820b9310 | 856 | static void cycle_packfile(void) |
d9ee53ce SP |
857 | { |
858 | end_packfile(); | |
859 | start_packfile(); | |
860 | } | |
861 | ||
c44cdc7e | 862 | static size_t encode_header( |
ac47a738 | 863 | enum object_type type, |
c44cdc7e | 864 | size_t size, |
ac47a738 | 865 | unsigned char *hdr) |
db5e523f SP |
866 | { |
867 | int n = 1; | |
868 | unsigned char c; | |
869 | ||
1fcdd62a | 870 | if (type < OBJ_COMMIT || type > OBJ_REF_DELTA) |
db5e523f SP |
871 | die("bad type %d", type); |
872 | ||
873 | c = (type << 4) | (size & 15); | |
874 | size >>= 4; | |
875 | while (size) { | |
876 | *hdr++ = c | 0x80; | |
877 | c = size & 0x7f; | |
878 | size >>= 7; | |
879 | n++; | |
880 | } | |
881 | *hdr = c; | |
882 | return n; | |
883 | } | |
884 | ||
ac47a738 SP |
885 | static int store_object( |
886 | enum object_type type, | |
887 | void *dat, | |
c44cdc7e | 888 | size_t datlen, |
6bb5b329 | 889 | struct last_object *last, |
d8397168 | 890 | unsigned char *sha1out, |
0ea9f045 | 891 | uintmax_t mark) |
db5e523f | 892 | { |
db5e523f | 893 | void *out, *delta; |
ac47a738 SP |
894 | struct object_entry *e; |
895 | unsigned char hdr[96]; | |
896 | unsigned char sha1[20]; | |
db5e523f | 897 | unsigned long hdrlen, deltalen; |
ac47a738 SP |
898 | SHA_CTX c; |
899 | z_stream s; | |
900 | ||
df843662 | 901 | hdrlen = sprintf((char*)hdr,"%s %lu", typename(type), |
40db58b8 | 902 | (unsigned long)datlen) + 1; |
ac47a738 SP |
903 | SHA1_Init(&c); |
904 | SHA1_Update(&c, hdr, hdrlen); | |
905 | SHA1_Update(&c, dat, datlen); | |
906 | SHA1_Final(sha1, &c); | |
6bb5b329 | 907 | if (sha1out) |
445b8599 | 908 | hashcpy(sha1out, sha1); |
ac47a738 SP |
909 | |
910 | e = insert_object(sha1); | |
d8397168 SP |
911 | if (mark) |
912 | insert_mark(mark, e); | |
ac47a738 | 913 | if (e->offset) { |
6143f064 | 914 | duplicate_count_by_type[type]++; |
463acbe1 | 915 | return 1; |
a5c1780a SP |
916 | } else if (find_sha1_pack(sha1, packed_git)) { |
917 | e->type = type; | |
918 | e->pack_id = MAX_PACK_ID; | |
919 | e->offset = 1; /* just not zero! */ | |
920 | duplicate_count_by_type[type]++; | |
921 | return 1; | |
ac47a738 | 922 | } |
db5e523f | 923 | |
d9ee53ce | 924 | if (last && last->data && last->depth < max_depth) { |
ac47a738 | 925 | delta = diff_delta(last->data, last->len, |
db5e523f SP |
926 | dat, datlen, |
927 | &deltalen, 0); | |
d9ee53ce SP |
928 | if (delta && deltalen >= datlen) { |
929 | free(delta); | |
930 | delta = NULL; | |
931 | } | |
932 | } else | |
933 | delta = NULL; | |
db5e523f SP |
934 | |
935 | memset(&s, 0, sizeof(s)); | |
936 | deflateInit(&s, zlib_compression_level); | |
d9ee53ce SP |
937 | if (delta) { |
938 | s.next_in = delta; | |
939 | s.avail_in = deltalen; | |
940 | } else { | |
941 | s.next_in = dat; | |
942 | s.avail_in = datlen; | |
943 | } | |
944 | s.avail_out = deflateBound(&s, s.avail_in); | |
945 | s.next_out = out = xmalloc(s.avail_out); | |
946 | while (deflate(&s, Z_FINISH) == Z_OK) | |
947 | /* nothing */; | |
948 | deflateEnd(&s); | |
949 | ||
950 | /* Determine if we should auto-checkpoint. */ | |
e5808826 | 951 | if ((pack_size + 60 + s.total_out) > max_packsize |
d9ee53ce SP |
952 | || (pack_size + 60 + s.total_out) < pack_size) { |
953 | ||
954 | /* This new object needs to *not* have the current pack_id. */ | |
955 | e->pack_id = pack_id + 1; | |
820b9310 | 956 | cycle_packfile(); |
d9ee53ce SP |
957 | |
958 | /* We cannot carry a delta into the new pack. */ | |
959 | if (delta) { | |
960 | free(delta); | |
961 | delta = NULL; | |
5d6f3ef6 SP |
962 | |
963 | memset(&s, 0, sizeof(s)); | |
964 | deflateInit(&s, zlib_compression_level); | |
965 | s.next_in = dat; | |
966 | s.avail_in = datlen; | |
967 | s.avail_out = deflateBound(&s, s.avail_in); | |
968 | s.next_out = out = xrealloc(out, s.avail_out); | |
969 | while (deflate(&s, Z_FINISH) == Z_OK) | |
970 | /* nothing */; | |
971 | deflateEnd(&s); | |
d9ee53ce | 972 | } |
d9ee53ce SP |
973 | } |
974 | ||
975 | e->type = type; | |
976 | e->pack_id = pack_id; | |
977 | e->offset = pack_size; | |
978 | object_count++; | |
979 | object_count_by_type[type]++; | |
db5e523f SP |
980 | |
981 | if (delta) { | |
d489bc14 SP |
982 | unsigned long ofs = e->offset - last->offset; |
983 | unsigned pos = sizeof(hdr) - 1; | |
984 | ||
4cabf858 | 985 | delta_count_by_type[type]++; |
ac47a738 | 986 | last->depth++; |
d489bc14 SP |
987 | |
988 | hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr); | |
0fcbcae7 | 989 | write_or_die(pack_data->pack_fd, hdr, hdrlen); |
d489bc14 SP |
990 | pack_size += hdrlen; |
991 | ||
992 | hdr[pos] = ofs & 127; | |
993 | while (ofs >>= 7) | |
994 | hdr[--pos] = 128 | (--ofs & 127); | |
0fcbcae7 | 995 | write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos); |
d489bc14 | 996 | pack_size += sizeof(hdr) - pos; |
db5e523f | 997 | } else { |
463acbe1 SP |
998 | if (last) |
999 | last->depth = 0; | |
ac47a738 | 1000 | hdrlen = encode_header(type, datlen, hdr); |
0fcbcae7 | 1001 | write_or_die(pack_data->pack_fd, hdr, hdrlen); |
41e5257f | 1002 | pack_size += hdrlen; |
db5e523f SP |
1003 | } |
1004 | ||
0fcbcae7 | 1005 | write_or_die(pack_data->pack_fd, out, s.total_out); |
41e5257f | 1006 | pack_size += s.total_out; |
db5e523f SP |
1007 | |
1008 | free(out); | |
e7d06a4b | 1009 | free(delta); |
463acbe1 | 1010 | if (last) { |
e7d06a4b | 1011 | if (!last->no_free) |
463acbe1 SP |
1012 | free(last->data); |
1013 | last->data = dat; | |
d489bc14 | 1014 | last->offset = e->offset; |
463acbe1 | 1015 | last->len = datlen; |
463acbe1 SP |
1016 | } |
1017 | return 0; | |
1018 | } | |
1019 | ||
7bfe6e26 SP |
1020 | static void *gfi_unpack_entry( |
1021 | struct object_entry *oe, | |
1022 | unsigned long *sizep) | |
41e5257f | 1023 | { |
21666f1a | 1024 | enum object_type type; |
7bfe6e26 SP |
1025 | struct packed_git *p = all_packs[oe->pack_id]; |
1026 | if (p == pack_data) | |
1027 | p->pack_size = pack_size + 20; | |
21666f1a | 1028 | return unpack_entry(p, oe->offset, &type, sizep); |
41e5257f SP |
1029 | } |
1030 | ||
10831c55 | 1031 | static const char *get_mode(const char *str, uint16_t *modep) |
463acbe1 SP |
1032 | { |
1033 | unsigned char c; | |
10831c55 | 1034 | uint16_t mode = 0; |
463acbe1 SP |
1035 | |
1036 | while ((c = *str++) != ' ') { | |
1037 | if (c < '0' || c > '7') | |
1038 | return NULL; | |
1039 | mode = (mode << 3) + (c - '0'); | |
1040 | } | |
1041 | *modep = mode; | |
1042 | return str; | |
1043 | } | |
1044 | ||
1045 | static void load_tree(struct tree_entry *root) | |
1046 | { | |
4cabf858 | 1047 | unsigned char* sha1 = root->versions[1].sha1; |
463acbe1 SP |
1048 | struct object_entry *myoe; |
1049 | struct tree_content *t; | |
1050 | unsigned long size; | |
1051 | char *buf; | |
1052 | const char *c; | |
463acbe1 SP |
1053 | |
1054 | root->tree = t = new_tree_content(8); | |
4cabf858 | 1055 | if (is_null_sha1(sha1)) |
463acbe1 SP |
1056 | return; |
1057 | ||
4cabf858 | 1058 | myoe = find_object(sha1); |
20f546a8 | 1059 | if (myoe && myoe->pack_id != MAX_PACK_ID) { |
41e5257f | 1060 | if (myoe->type != OBJ_TREE) |
4cabf858 | 1061 | die("Not a tree: %s", sha1_to_hex(sha1)); |
d489bc14 | 1062 | t->delta_depth = 0; |
7bfe6e26 | 1063 | buf = gfi_unpack_entry(myoe, &size); |
463acbe1 | 1064 | } else { |
21666f1a NP |
1065 | enum object_type type; |
1066 | buf = read_sha1_file(sha1, &type, &size); | |
1067 | if (!buf || type != OBJ_TREE) | |
4cabf858 | 1068 | die("Can't load tree %s", sha1_to_hex(sha1)); |
463acbe1 SP |
1069 | } |
1070 | ||
1071 | c = buf; | |
1072 | while (c != (buf + size)) { | |
1073 | struct tree_entry *e = new_tree_entry(); | |
1074 | ||
1075 | if (t->entry_count == t->entry_capacity) | |
f022f85f | 1076 | root->tree = t = grow_tree_content(t, t->entry_count); |
463acbe1 SP |
1077 | t->entries[t->entry_count++] = e; |
1078 | ||
1079 | e->tree = NULL; | |
4cabf858 | 1080 | c = get_mode(c, &e->versions[1].mode); |
463acbe1 | 1081 | if (!c) |
4cabf858 SP |
1082 | die("Corrupt mode in %s", sha1_to_hex(sha1)); |
1083 | e->versions[0].mode = e->versions[1].mode; | |
061e35c5 | 1084 | e->name = to_atom(c, strlen(c)); |
463acbe1 | 1085 | c += e->name->str_len + 1; |
4cabf858 SP |
1086 | hashcpy(e->versions[0].sha1, (unsigned char*)c); |
1087 | hashcpy(e->versions[1].sha1, (unsigned char*)c); | |
463acbe1 SP |
1088 | c += 20; |
1089 | } | |
1090 | free(buf); | |
1091 | } | |
1092 | ||
4cabf858 | 1093 | static int tecmp0 (const void *_a, const void *_b) |
463acbe1 SP |
1094 | { |
1095 | struct tree_entry *a = *((struct tree_entry**)_a); | |
1096 | struct tree_entry *b = *((struct tree_entry**)_b); | |
1097 | return base_name_compare( | |
4cabf858 SP |
1098 | a->name->str_dat, a->name->str_len, a->versions[0].mode, |
1099 | b->name->str_dat, b->name->str_len, b->versions[0].mode); | |
463acbe1 SP |
1100 | } |
1101 | ||
4cabf858 | 1102 | static int tecmp1 (const void *_a, const void *_b) |
463acbe1 | 1103 | { |
4cabf858 SP |
1104 | struct tree_entry *a = *((struct tree_entry**)_a); |
1105 | struct tree_entry *b = *((struct tree_entry**)_b); | |
1106 | return base_name_compare( | |
1107 | a->name->str_dat, a->name->str_len, a->versions[1].mode, | |
1108 | b->name->str_dat, b->name->str_len, b->versions[1].mode); | |
1109 | } | |
1110 | ||
e2eb469d SP |
1111 | static void mktree(struct tree_content *t, |
1112 | int v, | |
1113 | unsigned long *szp, | |
1114 | struct dbuf *b) | |
4cabf858 SP |
1115 | { |
1116 | size_t maxlen = 0; | |
463acbe1 | 1117 | unsigned int i; |
e2eb469d | 1118 | char *c; |
463acbe1 | 1119 | |
4cabf858 SP |
1120 | if (!v) |
1121 | qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0); | |
1122 | else | |
1123 | qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1); | |
463acbe1 | 1124 | |
463acbe1 | 1125 | for (i = 0; i < t->entry_count; i++) { |
4cabf858 SP |
1126 | if (t->entries[i]->versions[v].mode) |
1127 | maxlen += t->entries[i]->name->str_len + 34; | |
463acbe1 SP |
1128 | } |
1129 | ||
243f801d | 1130 | size_dbuf(b, maxlen); |
e2eb469d | 1131 | c = b->buffer; |
463acbe1 SP |
1132 | for (i = 0; i < t->entry_count; i++) { |
1133 | struct tree_entry *e = t->entries[i]; | |
4cabf858 SP |
1134 | if (!e->versions[v].mode) |
1135 | continue; | |
10831c55 | 1136 | c += sprintf(c, "%o", (unsigned int)e->versions[v].mode); |
463acbe1 SP |
1137 | *c++ = ' '; |
1138 | strcpy(c, e->name->str_dat); | |
1139 | c += e->name->str_len + 1; | |
4cabf858 | 1140 | hashcpy((unsigned char*)c, e->versions[v].sha1); |
463acbe1 SP |
1141 | c += 20; |
1142 | } | |
e2eb469d | 1143 | *szp = c - (char*)b->buffer; |
4cabf858 SP |
1144 | } |
1145 | ||
1146 | static void store_tree(struct tree_entry *root) | |
1147 | { | |
1148 | struct tree_content *t = root->tree; | |
1149 | unsigned int i, j, del; | |
e2eb469d | 1150 | unsigned long new_len; |
4cabf858 | 1151 | struct last_object lo; |
d489bc14 | 1152 | struct object_entry *le; |
4cabf858 SP |
1153 | |
1154 | if (!is_null_sha1(root->versions[1].sha1)) | |
1155 | return; | |
1156 | ||
1157 | for (i = 0; i < t->entry_count; i++) { | |
1158 | if (t->entries[i]->tree) | |
1159 | store_tree(t->entries[i]); | |
1160 | } | |
1161 | ||
d489bc14 | 1162 | le = find_object(root->versions[0].sha1); |
7bfe6e26 SP |
1163 | if (!S_ISDIR(root->versions[0].mode) |
1164 | || !le | |
1165 | || le->pack_id != pack_id) { | |
4cabf858 SP |
1166 | lo.data = NULL; |
1167 | lo.depth = 0; | |
b259157f | 1168 | lo.no_free = 0; |
4cabf858 | 1169 | } else { |
e2eb469d SP |
1170 | mktree(t, 0, &lo.len, &old_tree); |
1171 | lo.data = old_tree.buffer; | |
d489bc14 | 1172 | lo.offset = le->offset; |
4cabf858 | 1173 | lo.depth = t->delta_depth; |
e2eb469d | 1174 | lo.no_free = 1; |
4cabf858 | 1175 | } |
4cabf858 | 1176 | |
8a8c55ea | 1177 | mktree(t, 1, &new_len, &new_tree); |
e2eb469d | 1178 | store_object(OBJ_TREE, new_tree.buffer, new_len, |
4cabf858 | 1179 | &lo, root->versions[1].sha1, 0); |
4cabf858 SP |
1180 | |
1181 | t->delta_depth = lo.depth; | |
4cabf858 SP |
1182 | for (i = 0, j = 0, del = 0; i < t->entry_count; i++) { |
1183 | struct tree_entry *e = t->entries[i]; | |
1184 | if (e->versions[1].mode) { | |
1185 | e->versions[0].mode = e->versions[1].mode; | |
1186 | hashcpy(e->versions[0].sha1, e->versions[1].sha1); | |
1187 | t->entries[j++] = e; | |
1188 | } else { | |
1189 | release_tree_entry(e); | |
1190 | del++; | |
1191 | } | |
1192 | } | |
1193 | t->entry_count -= del; | |
463acbe1 SP |
1194 | } |
1195 | ||
1196 | static int tree_content_set( | |
1197 | struct tree_entry *root, | |
1198 | const char *p, | |
1199 | const unsigned char *sha1, | |
f39a946a SP |
1200 | const uint16_t mode, |
1201 | struct tree_content *subtree) | |
463acbe1 SP |
1202 | { |
1203 | struct tree_content *t = root->tree; | |
1204 | const char *slash1; | |
1205 | unsigned int i, n; | |
1206 | struct tree_entry *e; | |
1207 | ||
1208 | slash1 = strchr(p, '/'); | |
1209 | if (slash1) | |
1210 | n = slash1 - p; | |
1211 | else | |
1212 | n = strlen(p); | |
475d1b33 SP |
1213 | if (!n) |
1214 | die("Empty path component found in input"); | |
f39a946a SP |
1215 | if (!slash1 && !S_ISDIR(mode) && subtree) |
1216 | die("Non-directories cannot have subtrees"); | |
463acbe1 SP |
1217 | |
1218 | for (i = 0; i < t->entry_count; i++) { | |
1219 | e = t->entries[i]; | |
1220 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
1221 | if (!slash1) { | |
f39a946a SP |
1222 | if (!S_ISDIR(mode) |
1223 | && e->versions[1].mode == mode | |
4cabf858 | 1224 | && !hashcmp(e->versions[1].sha1, sha1)) |
463acbe1 | 1225 | return 0; |
4cabf858 SP |
1226 | e->versions[1].mode = mode; |
1227 | hashcpy(e->versions[1].sha1, sha1); | |
f39a946a | 1228 | if (e->tree) |
afde8dd9 | 1229 | release_tree_content_recursive(e->tree); |
f39a946a | 1230 | e->tree = subtree; |
4cabf858 | 1231 | hashclr(root->versions[1].sha1); |
463acbe1 SP |
1232 | return 1; |
1233 | } | |
4cabf858 | 1234 | if (!S_ISDIR(e->versions[1].mode)) { |
463acbe1 | 1235 | e->tree = new_tree_content(8); |
4cabf858 | 1236 | e->versions[1].mode = S_IFDIR; |
463acbe1 SP |
1237 | } |
1238 | if (!e->tree) | |
1239 | load_tree(e); | |
f39a946a | 1240 | if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) { |
4cabf858 | 1241 | hashclr(root->versions[1].sha1); |
463acbe1 SP |
1242 | return 1; |
1243 | } | |
1244 | return 0; | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | if (t->entry_count == t->entry_capacity) | |
f022f85f | 1249 | root->tree = t = grow_tree_content(t, t->entry_count); |
463acbe1 | 1250 | e = new_tree_entry(); |
061e35c5 | 1251 | e->name = to_atom(p, n); |
4cabf858 SP |
1252 | e->versions[0].mode = 0; |
1253 | hashclr(e->versions[0].sha1); | |
463acbe1 SP |
1254 | t->entries[t->entry_count++] = e; |
1255 | if (slash1) { | |
1256 | e->tree = new_tree_content(8); | |
4cabf858 | 1257 | e->versions[1].mode = S_IFDIR; |
f39a946a | 1258 | tree_content_set(e, slash1 + 1, sha1, mode, subtree); |
463acbe1 | 1259 | } else { |
f39a946a | 1260 | e->tree = subtree; |
4cabf858 SP |
1261 | e->versions[1].mode = mode; |
1262 | hashcpy(e->versions[1].sha1, sha1); | |
463acbe1 | 1263 | } |
4cabf858 | 1264 | hashclr(root->versions[1].sha1); |
463acbe1 SP |
1265 | return 1; |
1266 | } | |
1267 | ||
f39a946a SP |
1268 | static int tree_content_remove( |
1269 | struct tree_entry *root, | |
1270 | const char *p, | |
1271 | struct tree_entry *backup_leaf) | |
463acbe1 SP |
1272 | { |
1273 | struct tree_content *t = root->tree; | |
1274 | const char *slash1; | |
1275 | unsigned int i, n; | |
1276 | struct tree_entry *e; | |
1277 | ||
1278 | slash1 = strchr(p, '/'); | |
1279 | if (slash1) | |
1280 | n = slash1 - p; | |
1281 | else | |
1282 | n = strlen(p); | |
1283 | ||
1284 | for (i = 0; i < t->entry_count; i++) { | |
1285 | e = t->entries[i]; | |
1286 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
4cabf858 | 1287 | if (!slash1 || !S_ISDIR(e->versions[1].mode)) |
463acbe1 SP |
1288 | goto del_entry; |
1289 | if (!e->tree) | |
1290 | load_tree(e); | |
f39a946a | 1291 | if (tree_content_remove(e, slash1 + 1, backup_leaf)) { |
b54d6422 SP |
1292 | for (n = 0; n < e->tree->entry_count; n++) { |
1293 | if (e->tree->entries[n]->versions[1].mode) { | |
1294 | hashclr(root->versions[1].sha1); | |
1295 | return 1; | |
1296 | } | |
1297 | } | |
f39a946a | 1298 | backup_leaf = NULL; |
b54d6422 | 1299 | goto del_entry; |
463acbe1 SP |
1300 | } |
1301 | return 0; | |
1302 | } | |
1303 | } | |
1304 | return 0; | |
1305 | ||
1306 | del_entry: | |
f39a946a SP |
1307 | if (backup_leaf) |
1308 | memcpy(backup_leaf, e, sizeof(*backup_leaf)); | |
1309 | else if (e->tree) | |
4cabf858 | 1310 | release_tree_content_recursive(e->tree); |
f39a946a | 1311 | e->tree = NULL; |
4cabf858 SP |
1312 | e->versions[1].mode = 0; |
1313 | hashclr(e->versions[1].sha1); | |
1314 | hashclr(root->versions[1].sha1); | |
ac47a738 | 1315 | return 1; |
db5e523f SP |
1316 | } |
1317 | ||
b6f3481b SP |
1318 | static int tree_content_get( |
1319 | struct tree_entry *root, | |
1320 | const char *p, | |
1321 | struct tree_entry *leaf) | |
1322 | { | |
1323 | struct tree_content *t = root->tree; | |
1324 | const char *slash1; | |
1325 | unsigned int i, n; | |
1326 | struct tree_entry *e; | |
1327 | ||
1328 | slash1 = strchr(p, '/'); | |
1329 | if (slash1) | |
1330 | n = slash1 - p; | |
1331 | else | |
1332 | n = strlen(p); | |
1333 | ||
1334 | for (i = 0; i < t->entry_count; i++) { | |
1335 | e = t->entries[i]; | |
1336 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
1337 | if (!slash1) { | |
1338 | memcpy(leaf, e, sizeof(*leaf)); | |
1339 | if (e->tree && is_null_sha1(e->versions[1].sha1)) | |
1340 | leaf->tree = dup_tree_content(e->tree); | |
1341 | else | |
1342 | leaf->tree = NULL; | |
1343 | return 1; | |
1344 | } | |
1345 | if (!S_ISDIR(e->versions[1].mode)) | |
1346 | return 0; | |
1347 | if (!e->tree) | |
1348 | load_tree(e); | |
1349 | return tree_content_get(e, slash1 + 1, leaf); | |
1350 | } | |
1351 | } | |
1352 | return 0; | |
1353 | } | |
1354 | ||
7073e69e | 1355 | static int update_branch(struct branch *b) |
463acbe1 SP |
1356 | { |
1357 | static const char *msg = "fast-import"; | |
7073e69e SP |
1358 | struct ref_lock *lock; |
1359 | unsigned char old_sha1[20]; | |
1360 | ||
1361 | if (read_ref(b->name, old_sha1)) | |
1362 | hashclr(old_sha1); | |
68db31cc | 1363 | lock = lock_any_ref_for_update(b->name, old_sha1, 0); |
7073e69e SP |
1364 | if (!lock) |
1365 | return error("Unable to lock %s", b->name); | |
1366 | if (!force_update && !is_null_sha1(old_sha1)) { | |
1367 | struct commit *old_cmit, *new_cmit; | |
1368 | ||
1369 | old_cmit = lookup_commit_reference_gently(old_sha1, 0); | |
1370 | new_cmit = lookup_commit_reference_gently(b->sha1, 0); | |
1371 | if (!old_cmit || !new_cmit) { | |
1372 | unlock_ref(lock); | |
1373 | return error("Branch %s is missing commits.", b->name); | |
1374 | } | |
1375 | ||
4a164d48 | 1376 | if (!in_merge_bases(old_cmit, &new_cmit, 1)) { |
7073e69e | 1377 | unlock_ref(lock); |
46efd2d9 | 1378 | warning("Not updating %s" |
7073e69e SP |
1379 | " (new tip %s does not contain %s)", |
1380 | b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1)); | |
1381 | return -1; | |
1382 | } | |
1383 | } | |
1384 | if (write_ref_sha1(lock, b->sha1, msg) < 0) | |
1385 | return error("Unable to update %s", b->name); | |
1386 | return 0; | |
1387 | } | |
1388 | ||
1389 | static void dump_branches(void) | |
1390 | { | |
463acbe1 SP |
1391 | unsigned int i; |
1392 | struct branch *b; | |
463acbe1 SP |
1393 | |
1394 | for (i = 0; i < branch_table_sz; i++) { | |
7073e69e SP |
1395 | for (b = branch_table[i]; b; b = b->table_next_branch) |
1396 | failure |= update_branch(b); | |
463acbe1 SP |
1397 | } |
1398 | } | |
1399 | ||
fd99224e | 1400 | static void dump_tags(void) |
72303d44 SP |
1401 | { |
1402 | static const char *msg = "fast-import"; | |
1403 | struct tag *t; | |
1404 | struct ref_lock *lock; | |
7073e69e | 1405 | char ref_name[PATH_MAX]; |
72303d44 SP |
1406 | |
1407 | for (t = first_tag; t; t = t->next_tag) { | |
7073e69e SP |
1408 | sprintf(ref_name, "tags/%s", t->name); |
1409 | lock = lock_ref_sha1(ref_name, NULL); | |
72303d44 | 1410 | if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0) |
7073e69e | 1411 | failure |= error("Unable to update %s", ref_name); |
72303d44 SP |
1412 | } |
1413 | } | |
1414 | ||
a6a1a831 | 1415 | static void dump_marks_helper(FILE *f, |
0ea9f045 | 1416 | uintmax_t base, |
a6a1a831 SP |
1417 | struct mark_set *m) |
1418 | { | |
0ea9f045 | 1419 | uintmax_t k; |
a6a1a831 SP |
1420 | if (m->shift) { |
1421 | for (k = 0; k < 1024; k++) { | |
1422 | if (m->data.sets[k]) | |
1423 | dump_marks_helper(f, (base + k) << m->shift, | |
1424 | m->data.sets[k]); | |
1425 | } | |
1426 | } else { | |
1427 | for (k = 0; k < 1024; k++) { | |
1428 | if (m->data.marked[k]) | |
3efb1f34 | 1429 | fprintf(f, ":%" PRIuMAX " %s\n", base + k, |
a6a1a831 SP |
1430 | sha1_to_hex(m->data.marked[k]->sha1)); |
1431 | } | |
1432 | } | |
1433 | } | |
1434 | ||
fd99224e | 1435 | static void dump_marks(void) |
a6a1a831 | 1436 | { |
60b9004c SP |
1437 | static struct lock_file mark_lock; |
1438 | int mark_fd; | |
1439 | FILE *f; | |
1440 | ||
1441 | if (!mark_file) | |
1442 | return; | |
1443 | ||
1444 | mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0); | |
1445 | if (mark_fd < 0) { | |
1446 | failure |= error("Unable to write marks file %s: %s", | |
1447 | mark_file, strerror(errno)); | |
1448 | return; | |
a6a1a831 | 1449 | } |
60b9004c SP |
1450 | |
1451 | f = fdopen(mark_fd, "w"); | |
1452 | if (!f) { | |
1453 | rollback_lock_file(&mark_lock); | |
1454 | failure |= error("Unable to write marks file %s: %s", | |
1455 | mark_file, strerror(errno)); | |
1456 | return; | |
a6a1a831 | 1457 | } |
60b9004c SP |
1458 | |
1459 | dump_marks_helper(f, 0, marks); | |
1460 | fclose(f); | |
1461 | if (commit_lock_file(&mark_lock)) | |
1462 | failure |= error("Unable to write marks file %s: %s", | |
1463 | mark_file, strerror(errno)); | |
a6a1a831 SP |
1464 | } |
1465 | ||
fd99224e | 1466 | static void read_next_command(void) |
c44cdc7e | 1467 | { |
401d53fa SP |
1468 | do { |
1469 | read_line(&command_buf, stdin, '\n'); | |
1470 | } while (!command_buf.eof && command_buf.buf[0] == '#'); | |
c44cdc7e SP |
1471 | } |
1472 | ||
2c570cde SP |
1473 | static void skip_optional_lf() |
1474 | { | |
1475 | int term_char = fgetc(stdin); | |
1476 | if (term_char != '\n' && term_char != EOF) | |
1477 | ungetc(term_char, stdin); | |
1478 | } | |
1479 | ||
fd99224e | 1480 | static void cmd_mark(void) |
c44cdc7e | 1481 | { |
599065a3 | 1482 | if (!prefixcmp(command_buf.buf, "mark :")) { |
0ea9f045 | 1483 | next_mark = strtoumax(command_buf.buf + 6, NULL, 10); |
c44cdc7e SP |
1484 | read_next_command(); |
1485 | } | |
1486 | else | |
d8397168 | 1487 | next_mark = 0; |
c44cdc7e SP |
1488 | } |
1489 | ||
e5b1444b | 1490 | static void *cmd_data (size_t *size) |
c44cdc7e | 1491 | { |
c44cdc7e | 1492 | size_t length; |
3b4dce02 | 1493 | char *buffer; |
c44cdc7e | 1494 | |
599065a3 | 1495 | if (prefixcmp(command_buf.buf, "data ")) |
c44cdc7e SP |
1496 | die("Expected 'data n' command, found: %s", command_buf.buf); |
1497 | ||
599065a3 | 1498 | if (!prefixcmp(command_buf.buf + 5, "<<")) { |
3b4dce02 SP |
1499 | char *term = xstrdup(command_buf.buf + 5 + 2); |
1500 | size_t sz = 8192, term_len = command_buf.len - 5 - 2; | |
1501 | length = 0; | |
1502 | buffer = xmalloc(sz); | |
1503 | for (;;) { | |
401d53fa | 1504 | read_line(&command_buf, stdin, '\n'); |
3b4dce02 SP |
1505 | if (command_buf.eof) |
1506 | die("EOF in data (terminator '%s' not found)", term); | |
1507 | if (term_len == command_buf.len | |
1508 | && !strcmp(term, command_buf.buf)) | |
1509 | break; | |
31490074 | 1510 | ALLOC_GROW(buffer, length + command_buf.len, sz); |
3b4dce02 SP |
1511 | memcpy(buffer + length, |
1512 | command_buf.buf, | |
1513 | command_buf.len - 1); | |
1514 | length += command_buf.len - 1; | |
1515 | buffer[length++] = '\n'; | |
1516 | } | |
1517 | free(term); | |
1518 | } | |
1519 | else { | |
1520 | size_t n = 0; | |
1521 | length = strtoul(command_buf.buf + 5, NULL, 10); | |
1522 | buffer = xmalloc(length); | |
1523 | while (n < length) { | |
1524 | size_t s = fread(buffer + n, 1, length - n, stdin); | |
1525 | if (!s && feof(stdin)) | |
40db58b8 JS |
1526 | die("EOF in data (%lu bytes remaining)", |
1527 | (unsigned long)(length - n)); | |
3b4dce02 SP |
1528 | n += s; |
1529 | } | |
c44cdc7e SP |
1530 | } |
1531 | ||
2c570cde | 1532 | skip_optional_lf(); |
c44cdc7e SP |
1533 | *size = length; |
1534 | return buffer; | |
1535 | } | |
1536 | ||
63e0c8b3 SP |
1537 | static int validate_raw_date(const char *src, char *result, int maxlen) |
1538 | { | |
1539 | const char *orig_src = src; | |
1540 | char *endp, sign; | |
1541 | ||
1542 | strtoul(src, &endp, 10); | |
1543 | if (endp == src || *endp != ' ') | |
1544 | return -1; | |
1545 | ||
1546 | src = endp + 1; | |
1547 | if (*src != '-' && *src != '+') | |
1548 | return -1; | |
1549 | sign = *src; | |
1550 | ||
1551 | strtoul(src + 1, &endp, 10); | |
1552 | if (endp == src || *endp || (endp - orig_src) >= maxlen) | |
1553 | return -1; | |
1554 | ||
1555 | strcpy(result, orig_src); | |
1556 | return 0; | |
1557 | } | |
1558 | ||
1559 | static char *parse_ident(const char *buf) | |
1560 | { | |
1561 | const char *gt; | |
1562 | size_t name_len; | |
1563 | char *ident; | |
1564 | ||
1565 | gt = strrchr(buf, '>'); | |
1566 | if (!gt) | |
1567 | die("Missing > in ident string: %s", buf); | |
1568 | gt++; | |
1569 | if (*gt != ' ') | |
1570 | die("Missing space after > in ident string: %s", buf); | |
1571 | gt++; | |
1572 | name_len = gt - buf; | |
1573 | ident = xmalloc(name_len + 24); | |
1574 | strncpy(ident, buf, name_len); | |
1575 | ||
1576 | switch (whenspec) { | |
1577 | case WHENSPEC_RAW: | |
1578 | if (validate_raw_date(gt, ident + name_len, 24) < 0) | |
1579 | die("Invalid raw date \"%s\" in ident: %s", gt, buf); | |
1580 | break; | |
1581 | case WHENSPEC_RFC2822: | |
1582 | if (parse_date(gt, ident + name_len, 24) < 0) | |
1583 | die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf); | |
1584 | break; | |
1585 | case WHENSPEC_NOW: | |
1586 | if (strcmp("now", gt)) | |
1587 | die("Date in ident must be 'now': %s", buf); | |
1588 | datestamp(ident + name_len, 24); | |
1589 | break; | |
1590 | } | |
1591 | ||
1592 | return ident; | |
1593 | } | |
1594 | ||
fd99224e | 1595 | static void cmd_new_blob(void) |
6143f064 | 1596 | { |
d8397168 SP |
1597 | size_t l; |
1598 | void *d; | |
c44cdc7e SP |
1599 | |
1600 | read_next_command(); | |
1601 | cmd_mark(); | |
d8397168 | 1602 | d = cmd_data(&l); |
6143f064 | 1603 | |
d8397168 SP |
1604 | if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark)) |
1605 | free(d); | |
6143f064 SP |
1606 | } |
1607 | ||
fd99224e | 1608 | static void unload_one_branch(void) |
6bb5b329 | 1609 | { |
41e5257f SP |
1610 | while (cur_active_branches |
1611 | && cur_active_branches >= max_active_branches) { | |
6777a59f | 1612 | uintmax_t min_commit = ULONG_MAX; |
463acbe1 SP |
1613 | struct branch *e, *l = NULL, *p = NULL; |
1614 | ||
1615 | for (e = active_branches; e; e = e->active_next_branch) { | |
1616 | if (e->last_commit < min_commit) { | |
1617 | p = l; | |
1618 | min_commit = e->last_commit; | |
1619 | } | |
1620 | l = e; | |
1621 | } | |
1622 | ||
1623 | if (p) { | |
1624 | e = p->active_next_branch; | |
1625 | p->active_next_branch = e->active_next_branch; | |
1626 | } else { | |
1627 | e = active_branches; | |
1628 | active_branches = e->active_next_branch; | |
1629 | } | |
734c91f9 | 1630 | e->active = 0; |
463acbe1 SP |
1631 | e->active_next_branch = NULL; |
1632 | if (e->branch_tree.tree) { | |
afde8dd9 | 1633 | release_tree_content_recursive(e->branch_tree.tree); |
463acbe1 SP |
1634 | e->branch_tree.tree = NULL; |
1635 | } | |
1636 | cur_active_branches--; | |
6bb5b329 | 1637 | } |
6bb5b329 SP |
1638 | } |
1639 | ||
463acbe1 | 1640 | static void load_branch(struct branch *b) |
6bb5b329 | 1641 | { |
463acbe1 | 1642 | load_tree(&b->branch_tree); |
734c91f9 SP |
1643 | if (!b->active) { |
1644 | b->active = 1; | |
1645 | b->active_next_branch = active_branches; | |
1646 | active_branches = b; | |
1647 | cur_active_branches++; | |
1648 | branch_load_count++; | |
1649 | } | |
6bb5b329 SP |
1650 | } |
1651 | ||
463acbe1 | 1652 | static void file_change_m(struct branch *b) |
6bb5b329 | 1653 | { |
c44cdc7e SP |
1654 | const char *p = command_buf.buf + 2; |
1655 | char *p_uq; | |
1656 | const char *endp; | |
10e8d688 | 1657 | struct object_entry *oe = oe; |
463acbe1 | 1658 | unsigned char sha1[20]; |
10831c55 | 1659 | uint16_t mode, inline_data = 0; |
6bb5b329 | 1660 | |
c44cdc7e SP |
1661 | p = get_mode(p, &mode); |
1662 | if (!p) | |
1663 | die("Corrupt mode: %s", command_buf.buf); | |
1664 | switch (mode) { | |
1665 | case S_IFREG | 0644: | |
1666 | case S_IFREG | 0755: | |
ace4a9d1 | 1667 | case S_IFLNK: |
c44cdc7e SP |
1668 | case 0644: |
1669 | case 0755: | |
1670 | /* ok */ | |
1671 | break; | |
1672 | default: | |
1673 | die("Corrupt mode: %s", command_buf.buf); | |
1674 | } | |
1675 | ||
d8397168 SP |
1676 | if (*p == ':') { |
1677 | char *x; | |
0ea9f045 | 1678 | oe = find_mark(strtoumax(p + 1, &x, 10)); |
cacbdd0a | 1679 | hashcpy(sha1, oe->sha1); |
d8397168 | 1680 | p = x; |
599065a3 | 1681 | } else if (!prefixcmp(p, "inline")) { |
b715cfbb SP |
1682 | inline_data = 1; |
1683 | p += 6; | |
d8397168 SP |
1684 | } else { |
1685 | if (get_sha1_hex(p, sha1)) | |
1686 | die("Invalid SHA1: %s", command_buf.buf); | |
1687 | oe = find_object(sha1); | |
1688 | p += 40; | |
1689 | } | |
c44cdc7e SP |
1690 | if (*p++ != ' ') |
1691 | die("Missing space after SHA1: %s", command_buf.buf); | |
1692 | ||
1693 | p_uq = unquote_c_style(p, &endp); | |
1694 | if (p_uq) { | |
1695 | if (*endp) | |
1696 | die("Garbage after path in: %s", command_buf.buf); | |
1697 | p = p_uq; | |
1698 | } | |
6bb5b329 | 1699 | |
b715cfbb SP |
1700 | if (inline_data) { |
1701 | size_t l; | |
1702 | void *d; | |
1703 | if (!p_uq) | |
1704 | p = p_uq = xstrdup(p); | |
1705 | read_next_command(); | |
1706 | d = cmd_data(&l); | |
1707 | if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0)) | |
1708 | free(d); | |
1709 | } else if (oe) { | |
7111feed | 1710 | if (oe->type != OBJ_BLOB) |
c44cdc7e | 1711 | die("Not a blob (actually a %s): %s", |
df843662 | 1712 | command_buf.buf, typename(oe->type)); |
7111feed | 1713 | } else { |
21666f1a NP |
1714 | enum object_type type = sha1_object_info(sha1, NULL); |
1715 | if (type < 0) | |
c44cdc7e | 1716 | die("Blob not found: %s", command_buf.buf); |
21666f1a | 1717 | if (type != OBJ_BLOB) |
c44cdc7e | 1718 | die("Not a blob (actually a %s): %s", |
21666f1a | 1719 | typename(type), command_buf.buf); |
7111feed | 1720 | } |
6bb5b329 | 1721 | |
f39a946a | 1722 | tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL); |
e7d06a4b | 1723 | free(p_uq); |
463acbe1 | 1724 | } |
6bb5b329 | 1725 | |
463acbe1 SP |
1726 | static void file_change_d(struct branch *b) |
1727 | { | |
c44cdc7e SP |
1728 | const char *p = command_buf.buf + 2; |
1729 | char *p_uq; | |
1730 | const char *endp; | |
1731 | ||
1732 | p_uq = unquote_c_style(p, &endp); | |
1733 | if (p_uq) { | |
1734 | if (*endp) | |
1735 | die("Garbage after path in: %s", command_buf.buf); | |
1736 | p = p_uq; | |
1737 | } | |
f39a946a | 1738 | tree_content_remove(&b->branch_tree, p, NULL); |
e7d06a4b | 1739 | free(p_uq); |
6bb5b329 SP |
1740 | } |
1741 | ||
b6f3481b | 1742 | static void file_change_cr(struct branch *b, int rename) |
f39a946a SP |
1743 | { |
1744 | const char *s, *d; | |
1745 | char *s_uq, *d_uq; | |
1746 | const char *endp; | |
1747 | struct tree_entry leaf; | |
1748 | ||
1749 | s = command_buf.buf + 2; | |
1750 | s_uq = unquote_c_style(s, &endp); | |
1751 | if (s_uq) { | |
1752 | if (*endp != ' ') | |
1753 | die("Missing space after source: %s", command_buf.buf); | |
1754 | } | |
1755 | else { | |
1756 | endp = strchr(s, ' '); | |
1757 | if (!endp) | |
1758 | die("Missing space after source: %s", command_buf.buf); | |
1759 | s_uq = xmalloc(endp - s + 1); | |
1760 | memcpy(s_uq, s, endp - s); | |
1761 | s_uq[endp - s] = 0; | |
1762 | } | |
1763 | s = s_uq; | |
1764 | ||
1765 | endp++; | |
1766 | if (!*endp) | |
1767 | die("Missing dest: %s", command_buf.buf); | |
1768 | ||
1769 | d = endp; | |
1770 | d_uq = unquote_c_style(d, &endp); | |
1771 | if (d_uq) { | |
1772 | if (*endp) | |
1773 | die("Garbage after dest in: %s", command_buf.buf); | |
1774 | d = d_uq; | |
1775 | } | |
1776 | ||
1777 | memset(&leaf, 0, sizeof(leaf)); | |
b6f3481b SP |
1778 | if (rename) |
1779 | tree_content_remove(&b->branch_tree, s, &leaf); | |
1780 | else | |
1781 | tree_content_get(&b->branch_tree, s, &leaf); | |
f39a946a SP |
1782 | if (!leaf.versions[1].mode) |
1783 | die("Path %s not in branch", s); | |
1784 | tree_content_set(&b->branch_tree, d, | |
1785 | leaf.versions[1].sha1, | |
1786 | leaf.versions[1].mode, | |
1787 | leaf.tree); | |
1788 | ||
1789 | free(s_uq); | |
1790 | free(d_uq); | |
1791 | } | |
1792 | ||
825769a8 SP |
1793 | static void file_change_deleteall(struct branch *b) |
1794 | { | |
1795 | release_tree_content_recursive(b->branch_tree.tree); | |
1796 | hashclr(b->branch_tree.versions[0].sha1); | |
1797 | hashclr(b->branch_tree.versions[1].sha1); | |
1798 | load_tree(&b->branch_tree); | |
1799 | } | |
1800 | ||
654aaa37 SP |
1801 | static void cmd_from_commit(struct branch *b, char *buf, unsigned long size) |
1802 | { | |
1803 | if (!buf || size < 46) | |
1804 | die("Not a valid commit: %s", sha1_to_hex(b->sha1)); | |
1805 | if (memcmp("tree ", buf, 5) | |
1806 | || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1)) | |
1807 | die("The commit %s is corrupt", sha1_to_hex(b->sha1)); | |
1808 | hashcpy(b->branch_tree.versions[0].sha1, | |
1809 | b->branch_tree.versions[1].sha1); | |
1810 | } | |
1811 | ||
1812 | static void cmd_from_existing(struct branch *b) | |
1813 | { | |
1814 | if (is_null_sha1(b->sha1)) { | |
1815 | hashclr(b->branch_tree.versions[0].sha1); | |
1816 | hashclr(b->branch_tree.versions[1].sha1); | |
1817 | } else { | |
1818 | unsigned long size; | |
1819 | char *buf; | |
1820 | ||
1821 | buf = read_object_with_reference(b->sha1, | |
1822 | commit_type, &size, b->sha1); | |
1823 | cmd_from_commit(b, buf, size); | |
1824 | free(buf); | |
1825 | } | |
1826 | } | |
1827 | ||
00e2b884 SP |
1828 | static void cmd_from(struct branch *b) |
1829 | { | |
6c3aac1c | 1830 | const char *from; |
00e2b884 SP |
1831 | struct branch *s; |
1832 | ||
599065a3 | 1833 | if (prefixcmp(command_buf.buf, "from ")) |
00e2b884 SP |
1834 | return; |
1835 | ||
ea5e370a SP |
1836 | if (b->branch_tree.tree) { |
1837 | release_tree_content_recursive(b->branch_tree.tree); | |
1838 | b->branch_tree.tree = NULL; | |
1839 | } | |
00e2b884 SP |
1840 | |
1841 | from = strchr(command_buf.buf, ' ') + 1; | |
00e2b884 SP |
1842 | s = lookup_branch(from); |
1843 | if (b == s) | |
1844 | die("Can't create a branch from itself: %s", b->name); | |
1845 | else if (s) { | |
4cabf858 | 1846 | unsigned char *t = s->branch_tree.versions[1].sha1; |
445b8599 | 1847 | hashcpy(b->sha1, s->sha1); |
4cabf858 SP |
1848 | hashcpy(b->branch_tree.versions[0].sha1, t); |
1849 | hashcpy(b->branch_tree.versions[1].sha1, t); | |
00e2b884 | 1850 | } else if (*from == ':') { |
0ea9f045 | 1851 | uintmax_t idnum = strtoumax(from + 1, NULL, 10); |
00e2b884 | 1852 | struct object_entry *oe = find_mark(idnum); |
00e2b884 | 1853 | if (oe->type != OBJ_COMMIT) |
3efb1f34 | 1854 | die("Mark :%" PRIuMAX " not a commit", idnum); |
445b8599 | 1855 | hashcpy(b->sha1, oe->sha1); |
aac65ed1 | 1856 | if (oe->pack_id != MAX_PACK_ID) { |
00e2b884 | 1857 | unsigned long size; |
aac65ed1 SP |
1858 | char *buf = gfi_unpack_entry(oe, &size); |
1859 | cmd_from_commit(b, buf, size); | |
00e2b884 | 1860 | free(buf); |
aac65ed1 SP |
1861 | } else |
1862 | cmd_from_existing(b); | |
654aaa37 SP |
1863 | } else if (!get_sha1(from, b->sha1)) |
1864 | cmd_from_existing(b); | |
1865 | else | |
00e2b884 SP |
1866 | die("Invalid ref name or SHA1 expression: %s", from); |
1867 | ||
1868 | read_next_command(); | |
1869 | } | |
1870 | ||
e5b1444b | 1871 | static struct hash_list *cmd_merge(unsigned int *count) |
62b6f483 | 1872 | { |
10e8d688 | 1873 | struct hash_list *list = NULL, *n, *e = e; |
6c3aac1c | 1874 | const char *from; |
62b6f483 SP |
1875 | struct branch *s; |
1876 | ||
1877 | *count = 0; | |
599065a3 | 1878 | while (!prefixcmp(command_buf.buf, "merge ")) { |
62b6f483 | 1879 | from = strchr(command_buf.buf, ' ') + 1; |
62b6f483 SP |
1880 | n = xmalloc(sizeof(*n)); |
1881 | s = lookup_branch(from); | |
1882 | if (s) | |
1883 | hashcpy(n->sha1, s->sha1); | |
1884 | else if (*from == ':') { | |
0ea9f045 | 1885 | uintmax_t idnum = strtoumax(from + 1, NULL, 10); |
62b6f483 SP |
1886 | struct object_entry *oe = find_mark(idnum); |
1887 | if (oe->type != OBJ_COMMIT) | |
3efb1f34 | 1888 | die("Mark :%" PRIuMAX " not a commit", idnum); |
62b6f483 | 1889 | hashcpy(n->sha1, oe->sha1); |
2f6dc35d SP |
1890 | } else if (!get_sha1(from, n->sha1)) { |
1891 | unsigned long size; | |
1892 | char *buf = read_object_with_reference(n->sha1, | |
6b4318e6 | 1893 | commit_type, &size, n->sha1); |
2f6dc35d SP |
1894 | if (!buf || size < 46) |
1895 | die("Not a valid commit: %s", from); | |
1896 | free(buf); | |
1897 | } else | |
62b6f483 SP |
1898 | die("Invalid ref name or SHA1 expression: %s", from); |
1899 | ||
1900 | n->next = NULL; | |
1901 | if (list) | |
1902 | e->next = n; | |
1903 | else | |
1904 | list = n; | |
1905 | e = n; | |
10e8d688 | 1906 | (*count)++; |
62b6f483 SP |
1907 | read_next_command(); |
1908 | } | |
1909 | return list; | |
1910 | } | |
1911 | ||
fd99224e | 1912 | static void cmd_new_commit(void) |
6bb5b329 | 1913 | { |
c44cdc7e SP |
1914 | struct branch *b; |
1915 | void *msg; | |
1916 | size_t msglen; | |
c44cdc7e SP |
1917 | char *sp; |
1918 | char *author = NULL; | |
1919 | char *committer = NULL; | |
62b6f483 SP |
1920 | struct hash_list *merge_list = NULL; |
1921 | unsigned int merge_count; | |
c44cdc7e SP |
1922 | |
1923 | /* Obtain the branch name from the rest of our command */ | |
1924 | sp = strchr(command_buf.buf, ' ') + 1; | |
c44cdc7e | 1925 | b = lookup_branch(sp); |
463acbe1 | 1926 | if (!b) |
00e2b884 | 1927 | b = new_branch(sp); |
c44cdc7e SP |
1928 | |
1929 | read_next_command(); | |
1930 | cmd_mark(); | |
599065a3 | 1931 | if (!prefixcmp(command_buf.buf, "author ")) { |
63e0c8b3 | 1932 | author = parse_ident(command_buf.buf + 7); |
c44cdc7e SP |
1933 | read_next_command(); |
1934 | } | |
599065a3 | 1935 | if (!prefixcmp(command_buf.buf, "committer ")) { |
63e0c8b3 | 1936 | committer = parse_ident(command_buf.buf + 10); |
c44cdc7e SP |
1937 | read_next_command(); |
1938 | } | |
1939 | if (!committer) | |
1940 | die("Expected committer but didn't get one"); | |
1941 | msg = cmd_data(&msglen); | |
02f3389d SP |
1942 | read_next_command(); |
1943 | cmd_from(b); | |
62b6f483 | 1944 | merge_list = cmd_merge(&merge_count); |
c44cdc7e SP |
1945 | |
1946 | /* ensure the branch is active/loaded */ | |
41e5257f | 1947 | if (!b->branch_tree.tree || !max_active_branches) { |
463acbe1 SP |
1948 | unload_one_branch(); |
1949 | load_branch(b); | |
1950 | } | |
6bb5b329 | 1951 | |
463acbe1 SP |
1952 | /* file_change* */ |
1953 | for (;;) { | |
c44cdc7e | 1954 | if (1 == command_buf.len) |
463acbe1 | 1955 | break; |
599065a3 | 1956 | else if (!prefixcmp(command_buf.buf, "M ")) |
463acbe1 | 1957 | file_change_m(b); |
599065a3 | 1958 | else if (!prefixcmp(command_buf.buf, "D ")) |
463acbe1 | 1959 | file_change_d(b); |
f39a946a | 1960 | else if (!prefixcmp(command_buf.buf, "R ")) |
b6f3481b SP |
1961 | file_change_cr(b, 1); |
1962 | else if (!prefixcmp(command_buf.buf, "C ")) | |
1963 | file_change_cr(b, 0); | |
825769a8 SP |
1964 | else if (!strcmp("deleteall", command_buf.buf)) |
1965 | file_change_deleteall(b); | |
463acbe1 | 1966 | else |
c44cdc7e | 1967 | die("Unsupported file_change: %s", command_buf.buf); |
02f3389d | 1968 | read_next_command(); |
6bb5b329 | 1969 | } |
6bb5b329 | 1970 | |
c44cdc7e | 1971 | /* build the tree and the commit */ |
463acbe1 | 1972 | store_tree(&b->branch_tree); |
8a8c55ea SP |
1973 | hashcpy(b->branch_tree.versions[0].sha1, |
1974 | b->branch_tree.versions[1].sha1); | |
63e0c8b3 | 1975 | size_dbuf(&new_data, 114 + msglen |
62b6f483 | 1976 | + merge_count * 49 |
c44cdc7e SP |
1977 | + (author |
1978 | ? strlen(author) + strlen(committer) | |
1979 | : 2 * strlen(committer))); | |
243f801d | 1980 | sp = new_data.buffer; |
4cabf858 SP |
1981 | sp += sprintf(sp, "tree %s\n", |
1982 | sha1_to_hex(b->branch_tree.versions[1].sha1)); | |
445b8599 | 1983 | if (!is_null_sha1(b->sha1)) |
c44cdc7e | 1984 | sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1)); |
62b6f483 SP |
1985 | while (merge_list) { |
1986 | struct hash_list *next = merge_list->next; | |
1987 | sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1)); | |
1988 | free(merge_list); | |
1989 | merge_list = next; | |
1990 | } | |
63e0c8b3 SP |
1991 | sp += sprintf(sp, "author %s\n", author ? author : committer); |
1992 | sp += sprintf(sp, "committer %s\n", committer); | |
1993 | *sp++ = '\n'; | |
c44cdc7e SP |
1994 | memcpy(sp, msg, msglen); |
1995 | sp += msglen; | |
e7d06a4b | 1996 | free(author); |
c44cdc7e SP |
1997 | free(committer); |
1998 | free(msg); | |
1999 | ||
69e74e74 | 2000 | if (!store_object(OBJ_COMMIT, |
243f801d | 2001 | new_data.buffer, sp - (char*)new_data.buffer, |
69e74e74 SP |
2002 | NULL, b->sha1, next_mark)) |
2003 | b->pack_id = pack_id; | |
463acbe1 | 2004 | b->last_commit = object_count_by_type[OBJ_COMMIT]; |
6bb5b329 SP |
2005 | } |
2006 | ||
fd99224e | 2007 | static void cmd_new_tag(void) |
72303d44 | 2008 | { |
72303d44 SP |
2009 | char *sp; |
2010 | const char *from; | |
2011 | char *tagger; | |
2012 | struct branch *s; | |
2013 | void *msg; | |
2014 | size_t msglen; | |
72303d44 | 2015 | struct tag *t; |
0ea9f045 | 2016 | uintmax_t from_mark = 0; |
72303d44 SP |
2017 | unsigned char sha1[20]; |
2018 | ||
2019 | /* Obtain the new tag name from the rest of our command */ | |
2020 | sp = strchr(command_buf.buf, ' ') + 1; | |
72303d44 SP |
2021 | t = pool_alloc(sizeof(struct tag)); |
2022 | t->next_tag = NULL; | |
2023 | t->name = pool_strdup(sp); | |
2024 | if (last_tag) | |
2025 | last_tag->next_tag = t; | |
2026 | else | |
2027 | first_tag = t; | |
2028 | last_tag = t; | |
72303d44 SP |
2029 | read_next_command(); |
2030 | ||
2031 | /* from ... */ | |
599065a3 | 2032 | if (prefixcmp(command_buf.buf, "from ")) |
72303d44 | 2033 | die("Expected from command, got %s", command_buf.buf); |
72303d44 | 2034 | from = strchr(command_buf.buf, ' ') + 1; |
72303d44 SP |
2035 | s = lookup_branch(from); |
2036 | if (s) { | |
445b8599 | 2037 | hashcpy(sha1, s->sha1); |
72303d44 | 2038 | } else if (*from == ':') { |
10e8d688 | 2039 | struct object_entry *oe; |
0ea9f045 | 2040 | from_mark = strtoumax(from + 1, NULL, 10); |
10e8d688 | 2041 | oe = find_mark(from_mark); |
72303d44 | 2042 | if (oe->type != OBJ_COMMIT) |
3efb1f34 | 2043 | die("Mark :%" PRIuMAX " not a commit", from_mark); |
445b8599 | 2044 | hashcpy(sha1, oe->sha1); |
72303d44 SP |
2045 | } else if (!get_sha1(from, sha1)) { |
2046 | unsigned long size; | |
2047 | char *buf; | |
2048 | ||
2049 | buf = read_object_with_reference(sha1, | |
df843662 | 2050 | commit_type, &size, sha1); |
72303d44 SP |
2051 | if (!buf || size < 46) |
2052 | die("Not a valid commit: %s", from); | |
2053 | free(buf); | |
2054 | } else | |
2055 | die("Invalid ref name or SHA1 expression: %s", from); | |
72303d44 SP |
2056 | read_next_command(); |
2057 | ||
2058 | /* tagger ... */ | |
599065a3 | 2059 | if (prefixcmp(command_buf.buf, "tagger ")) |
72303d44 | 2060 | die("Expected tagger command, got %s", command_buf.buf); |
63e0c8b3 | 2061 | tagger = parse_ident(command_buf.buf + 7); |
72303d44 SP |
2062 | |
2063 | /* tag payload/message */ | |
2064 | read_next_command(); | |
2065 | msg = cmd_data(&msglen); | |
2066 | ||
2067 | /* build the tag object */ | |
243f801d SP |
2068 | size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen); |
2069 | sp = new_data.buffer; | |
72303d44 | 2070 | sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1)); |
df843662 | 2071 | sp += sprintf(sp, "type %s\n", commit_type); |
72303d44 | 2072 | sp += sprintf(sp, "tag %s\n", t->name); |
63e0c8b3 SP |
2073 | sp += sprintf(sp, "tagger %s\n", tagger); |
2074 | *sp++ = '\n'; | |
72303d44 SP |
2075 | memcpy(sp, msg, msglen); |
2076 | sp += msglen; | |
2077 | free(tagger); | |
2078 | free(msg); | |
2079 | ||
69e74e74 SP |
2080 | if (store_object(OBJ_TAG, new_data.buffer, |
2081 | sp - (char*)new_data.buffer, | |
2082 | NULL, t->sha1, 0)) | |
2083 | t->pack_id = MAX_PACK_ID; | |
2084 | else | |
2085 | t->pack_id = pack_id; | |
72303d44 SP |
2086 | } |
2087 | ||
fd99224e | 2088 | static void cmd_reset_branch(void) |
5fced8dc SP |
2089 | { |
2090 | struct branch *b; | |
5fced8dc SP |
2091 | char *sp; |
2092 | ||
2093 | /* Obtain the branch name from the rest of our command */ | |
2094 | sp = strchr(command_buf.buf, ' ') + 1; | |
5fced8dc SP |
2095 | b = lookup_branch(sp); |
2096 | if (b) { | |
ea5e370a SP |
2097 | hashclr(b->sha1); |
2098 | hashclr(b->branch_tree.versions[0].sha1); | |
2099 | hashclr(b->branch_tree.versions[1].sha1); | |
5fced8dc SP |
2100 | if (b->branch_tree.tree) { |
2101 | release_tree_content_recursive(b->branch_tree.tree); | |
2102 | b->branch_tree.tree = NULL; | |
2103 | } | |
2104 | } | |
9938ffc5 SP |
2105 | else |
2106 | b = new_branch(sp); | |
9938ffc5 SP |
2107 | read_next_command(); |
2108 | cmd_from(b); | |
5fced8dc SP |
2109 | } |
2110 | ||
fd99224e | 2111 | static void cmd_checkpoint(void) |
7bfe6e26 | 2112 | { |
820b9310 SP |
2113 | if (object_count) { |
2114 | cycle_packfile(); | |
2115 | dump_branches(); | |
2116 | dump_tags(); | |
2117 | dump_marks(); | |
2118 | } | |
7bfe6e26 SP |
2119 | read_next_command(); |
2120 | } | |
2121 | ||
e8438420 SP |
2122 | static void import_marks(const char *input_file) |
2123 | { | |
2124 | char line[512]; | |
2125 | FILE *f = fopen(input_file, "r"); | |
2126 | if (!f) | |
2127 | die("cannot read %s: %s", input_file, strerror(errno)); | |
2128 | while (fgets(line, sizeof(line), f)) { | |
2129 | uintmax_t mark; | |
2130 | char *end; | |
2131 | unsigned char sha1[20]; | |
2132 | struct object_entry *e; | |
2133 | ||
2134 | end = strchr(line, '\n'); | |
2135 | if (line[0] != ':' || !end) | |
2136 | die("corrupt mark line: %s", line); | |
2137 | *end = 0; | |
2138 | mark = strtoumax(line + 1, &end, 10); | |
2139 | if (!mark || end == line + 1 | |
2140 | || *end != ' ' || get_sha1(end + 1, sha1)) | |
2141 | die("corrupt mark line: %s", line); | |
2142 | e = find_object(sha1); | |
2143 | if (!e) { | |
2144 | enum object_type type = sha1_object_info(sha1, NULL); | |
2145 | if (type < 0) | |
2146 | die("object not found: %s", sha1_to_hex(sha1)); | |
2147 | e = insert_object(sha1); | |
2148 | e->type = type; | |
2149 | e->pack_id = MAX_PACK_ID; | |
a5c1780a | 2150 | e->offset = 1; /* just not zero! */ |
e8438420 SP |
2151 | } |
2152 | insert_mark(mark, e); | |
2153 | } | |
2154 | fclose(f); | |
2155 | } | |
2156 | ||
d5c57b28 | 2157 | static const char fast_import_usage[] = |
63e0c8b3 | 2158 | "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]"; |
d5c57b28 | 2159 | |
8bcce301 SP |
2160 | int main(int argc, const char **argv) |
2161 | { | |
c499d768 | 2162 | int i, show_stats = 1; |
8bcce301 | 2163 | |
463acbe1 | 2164 | git_config(git_default_config); |
93e72d8d SP |
2165 | alloc_objects(object_entry_alloc); |
2166 | strbuf_init(&command_buf); | |
2167 | atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*)); | |
2168 | branch_table = xcalloc(branch_table_sz, sizeof(struct branch*)); | |
2169 | avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*)); | |
2170 | marks = pool_calloc(1, sizeof(struct mark_set)); | |
463acbe1 | 2171 | |
d5c57b28 SP |
2172 | for (i = 1; i < argc; i++) { |
2173 | const char *a = argv[i]; | |
2174 | ||
2175 | if (*a != '-' || !strcmp(a, "--")) | |
2176 | break; | |
cc44c765 | 2177 | else if (!prefixcmp(a, "--date-format=")) { |
63e0c8b3 SP |
2178 | const char *fmt = a + 14; |
2179 | if (!strcmp(fmt, "raw")) | |
2180 | whenspec = WHENSPEC_RAW; | |
2181 | else if (!strcmp(fmt, "rfc2822")) | |
2182 | whenspec = WHENSPEC_RFC2822; | |
2183 | else if (!strcmp(fmt, "now")) | |
2184 | whenspec = WHENSPEC_NOW; | |
2185 | else | |
2186 | die("unknown --date-format argument %s", fmt); | |
2187 | } | |
cc44c765 | 2188 | else if (!prefixcmp(a, "--max-pack-size=")) |
0ea9f045 | 2189 | max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024; |
cc44c765 | 2190 | else if (!prefixcmp(a, "--depth=")) |
d5c57b28 | 2191 | max_depth = strtoul(a + 8, NULL, 0); |
cc44c765 | 2192 | else if (!prefixcmp(a, "--active-branches=")) |
d5c57b28 | 2193 | max_active_branches = strtoul(a + 18, NULL, 0); |
e8438420 SP |
2194 | else if (!prefixcmp(a, "--import-marks=")) |
2195 | import_marks(a + 15); | |
cc44c765 | 2196 | else if (!prefixcmp(a, "--export-marks=")) |
a6a1a831 | 2197 | mark_file = a + 15; |
cc44c765 | 2198 | else if (!prefixcmp(a, "--export-pack-edges=")) { |
bdf1c06d SP |
2199 | if (pack_edges) |
2200 | fclose(pack_edges); | |
2201 | pack_edges = fopen(a + 20, "a"); | |
2202 | if (!pack_edges) | |
2203 | die("Cannot open %s: %s", a + 20, strerror(errno)); | |
2204 | } else if (!strcmp(a, "--force")) | |
7073e69e | 2205 | force_update = 1; |
c499d768 SP |
2206 | else if (!strcmp(a, "--quiet")) |
2207 | show_stats = 0; | |
2208 | else if (!strcmp(a, "--stats")) | |
2209 | show_stats = 1; | |
d5c57b28 SP |
2210 | else |
2211 | die("unknown option %s", a); | |
2212 | } | |
8455e484 | 2213 | if (i != argc) |
d5c57b28 | 2214 | usage(fast_import_usage); |
d5c57b28 | 2215 | |
a5c1780a | 2216 | prepare_packed_git(); |
f70b6534 | 2217 | start_packfile(); |
db5e523f | 2218 | for (;;) { |
c44cdc7e SP |
2219 | read_next_command(); |
2220 | if (command_buf.eof) | |
db5e523f | 2221 | break; |
c44cdc7e SP |
2222 | else if (!strcmp("blob", command_buf.buf)) |
2223 | cmd_new_blob(); | |
599065a3 | 2224 | else if (!prefixcmp(command_buf.buf, "commit ")) |
c44cdc7e | 2225 | cmd_new_commit(); |
599065a3 | 2226 | else if (!prefixcmp(command_buf.buf, "tag ")) |
72303d44 | 2227 | cmd_new_tag(); |
599065a3 | 2228 | else if (!prefixcmp(command_buf.buf, "reset ")) |
5fced8dc | 2229 | cmd_reset_branch(); |
7bfe6e26 SP |
2230 | else if (!strcmp("checkpoint", command_buf.buf)) |
2231 | cmd_checkpoint(); | |
c44cdc7e SP |
2232 | else |
2233 | die("Unsupported command: %s", command_buf.buf); | |
db5e523f | 2234 | } |
f70b6534 | 2235 | end_packfile(); |
c44cdc7e | 2236 | |
463acbe1 | 2237 | dump_branches(); |
72303d44 | 2238 | dump_tags(); |
8455e484 | 2239 | unkeep_all_packs(); |
a6a1a831 | 2240 | dump_marks(); |
8bcce301 | 2241 | |
bdf1c06d SP |
2242 | if (pack_edges) |
2243 | fclose(pack_edges); | |
2244 | ||
c499d768 SP |
2245 | if (show_stats) { |
2246 | uintmax_t total_count = 0, duplicate_count = 0; | |
2247 | for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++) | |
2248 | total_count += object_count_by_type[i]; | |
2249 | for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++) | |
2250 | duplicate_count += duplicate_count_by_type[i]; | |
2251 | ||
2252 | fprintf(stderr, "%s statistics:\n", argv[0]); | |
2253 | fprintf(stderr, "---------------------------------------------------------------------\n"); | |
3efb1f34 JR |
2254 | fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count); |
2255 | fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count); | |
2256 | fprintf(stderr, " blobs : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]); | |
2257 | fprintf(stderr, " trees : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]); | |
2258 | fprintf(stderr, " commits: %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]); | |
2259 | fprintf(stderr, " tags : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]); | |
c499d768 | 2260 | fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count); |
3efb1f34 | 2261 | fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count); |
c499d768 | 2262 | fprintf(stderr, " atoms: %10u\n", atom_cnt); |
3efb1f34 | 2263 | fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024); |
40db58b8 | 2264 | fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024)); |
3efb1f34 | 2265 | fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024); |
c499d768 SP |
2266 | fprintf(stderr, "---------------------------------------------------------------------\n"); |
2267 | pack_report(); | |
2268 | fprintf(stderr, "---------------------------------------------------------------------\n"); | |
2269 | fprintf(stderr, "\n"); | |
2270 | } | |
db5e523f | 2271 | |
7073e69e | 2272 | return failure ? 1 : 0; |
db5e523f | 2273 | } |