]>
Commit | Line | Data |
---|---|---|
db5e523f SP |
1 | #include "builtin.h" |
2 | #include "cache.h" | |
a80d72db | 3 | #include "repository.h" |
b2141fc1 | 4 | #include "config.h" |
697cc8ef | 5 | #include "lockfile.h" |
db5e523f SP |
6 | #include "object.h" |
7 | #include "blob.h" | |
463acbe1 | 8 | #include "tree.h" |
7073e69e | 9 | #include "commit.h" |
db5e523f SP |
10 | #include "delta.h" |
11 | #include "pack.h" | |
463acbe1 | 12 | #include "refs.h" |
db5e523f | 13 | #include "csum-file.h" |
c44cdc7e | 14 | #include "quote.h" |
50906e04 | 15 | #include "dir.h" |
d9545c7f | 16 | #include "run-command.h" |
4f39cd82 | 17 | #include "packfile.h" |
a80d72db | 18 | #include "object-store.h" |
065feab4 | 19 | #include "mem-pool.h" |
64043556 | 20 | #include "commit-reach.h" |
db5e523f | 21 | |
69e74e74 SP |
22 | #define PACK_ID_BITS 16 |
23 | #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1) | |
436e7a74 SP |
24 | #define DEPTH_BITS 13 |
25 | #define MAX_DEPTH ((1<<DEPTH_BITS)-1) | |
69e74e74 | 26 | |
8fb3ad76 DI |
27 | /* |
28 | * We abuse the setuid bit on directories to mean "do not delta". | |
29 | */ | |
30 | #define NO_DELTA S_ISUID | |
31 | ||
28d055bd | 32 | /* |
33 | * The amount of additional space required in order to write an object into the | |
34 | * current pack. This is the hash lengths at the end of the pack, plus the | |
35 | * length of one object ID. | |
36 | */ | |
37 | #define PACK_SIZE_THRESHOLD (the_hash_algo->rawsz * 3) | |
38 | ||
9cba13ca | 39 | struct object_entry { |
3fc366bd | 40 | struct pack_idx_entry idx; |
27d6d290 | 41 | struct object_entry *next; |
436e7a74 SP |
42 | uint32_t type : TYPE_BITS, |
43 | pack_id : PACK_ID_BITS, | |
44 | depth : DEPTH_BITS; | |
27d6d290 SP |
45 | }; |
46 | ||
9cba13ca | 47 | struct object_entry_pool { |
463acbe1 | 48 | struct object_entry_pool *next_pool; |
27d6d290 SP |
49 | struct object_entry *next_free; |
50 | struct object_entry *end; | |
ac47a738 | 51 | struct object_entry entries[FLEX_ARRAY]; /* more */ |
27d6d290 SP |
52 | }; |
53 | ||
9cba13ca | 54 | struct mark_set { |
d8397168 SP |
55 | union { |
56 | struct object_entry *marked[1024]; | |
57 | struct mark_set *sets[1024]; | |
58 | } data; | |
6f64f6d9 | 59 | unsigned int shift; |
d8397168 SP |
60 | }; |
61 | ||
9cba13ca | 62 | struct last_object { |
05576569 | 63 | struct strbuf data; |
89e0a3a1 | 64 | off_t offset; |
6bb5b329 | 65 | unsigned int depth; |
05576569 | 66 | unsigned no_swap : 1; |
ac47a738 SP |
67 | }; |
68 | ||
9cba13ca | 69 | struct atom_str { |
463acbe1 | 70 | struct atom_str *next_atom; |
10831c55 | 71 | unsigned short str_len; |
463acbe1 SP |
72 | char str_dat[FLEX_ARRAY]; /* more */ |
73 | }; | |
74 | ||
75 | struct tree_content; | |
9cba13ca | 76 | struct tree_entry { |
463acbe1 | 77 | struct tree_content *tree; |
4b25d091 | 78 | struct atom_str *name; |
9cba13ca | 79 | struct tree_entry_ms { |
10831c55 | 80 | uint16_t mode; |
d7e6b6a8 | 81 | struct object_id oid; |
4cabf858 | 82 | } versions[2]; |
6bb5b329 SP |
83 | }; |
84 | ||
9cba13ca | 85 | struct tree_content { |
463acbe1 SP |
86 | unsigned int entry_capacity; /* must match avail_tree_content */ |
87 | unsigned int entry_count; | |
4cabf858 | 88 | unsigned int delta_depth; |
463acbe1 SP |
89 | struct tree_entry *entries[FLEX_ARRAY]; /* more */ |
90 | }; | |
91 | ||
9cba13ca | 92 | struct avail_tree_content { |
463acbe1 SP |
93 | unsigned int entry_capacity; /* must match tree_content */ |
94 | struct avail_tree_content *next_avail; | |
6bb5b329 SP |
95 | }; |
96 | ||
9cba13ca | 97 | struct branch { |
463acbe1 SP |
98 | struct branch *table_next_branch; |
99 | struct branch *active_next_branch; | |
6bb5b329 | 100 | const char *name; |
463acbe1 | 101 | struct tree_entry branch_tree; |
69e74e74 | 102 | uintmax_t last_commit; |
2a113aee | 103 | uintmax_t num_notes; |
734c91f9 | 104 | unsigned active : 1; |
4ee1b225 | 105 | unsigned delete : 1; |
734c91f9 | 106 | unsigned pack_id : PACK_ID_BITS; |
d7e6b6a8 | 107 | struct object_id oid; |
6bb5b329 SP |
108 | }; |
109 | ||
9cba13ca | 110 | struct tag { |
72303d44 SP |
111 | struct tag *next_tag; |
112 | const char *name; | |
2369ed79 | 113 | unsigned int pack_id; |
d7e6b6a8 | 114 | struct object_id oid; |
72303d44 SP |
115 | }; |
116 | ||
9cba13ca | 117 | struct hash_list { |
62b6f483 | 118 | struct hash_list *next; |
d7e6b6a8 | 119 | struct object_id oid; |
62b6f483 | 120 | }; |
463acbe1 | 121 | |
63e0c8b3 SP |
122 | typedef enum { |
123 | WHENSPEC_RAW = 1, | |
124 | WHENSPEC_RFC2822, | |
4b05548f | 125 | WHENSPEC_NOW |
63e0c8b3 SP |
126 | } whenspec_type; |
127 | ||
9cba13ca | 128 | struct recent_command { |
904b1941 SP |
129 | struct recent_command *prev; |
130 | struct recent_command *next; | |
131 | char *buf; | |
132 | }; | |
133 | ||
0ea9f045 | 134 | /* Configured limits on output */ |
4f2220e6 | 135 | static unsigned long max_depth = 50; |
89e0a3a1 | 136 | static off_t max_packsize; |
d9545c7f | 137 | static int unpack_limit = 100; |
7073e69e | 138 | static int force_update; |
0ea9f045 SP |
139 | |
140 | /* Stats and misc. counters */ | |
141 | static uintmax_t alloc_count; | |
0ea9f045 SP |
142 | static uintmax_t marks_set_count; |
143 | static uintmax_t object_count_by_type[1 << TYPE_BITS]; | |
144 | static uintmax_t duplicate_count_by_type[1 << TYPE_BITS]; | |
145 | static uintmax_t delta_count_by_type[1 << TYPE_BITS]; | |
94c3b482 | 146 | static uintmax_t delta_count_attempts_by_type[1 << TYPE_BITS]; |
a7ddc487 | 147 | static unsigned long object_count; |
6bb5b329 | 148 | static unsigned long branch_count; |
d6c7eb2c | 149 | static unsigned long branch_load_count; |
7073e69e | 150 | static int failure; |
bdf1c06d | 151 | static FILE *pack_edges; |
0f6927c2 | 152 | static unsigned int show_stats = 1; |
9c8398f0 | 153 | static int global_argc; |
3f2e2297 | 154 | static const char **global_argv; |
ac47a738 | 155 | |
463acbe1 | 156 | /* Memory pools */ |
96c47d14 JM |
157 | static struct mem_pool fi_mem_pool = {NULL, 2*1024*1024 - |
158 | sizeof(struct mp_block), 0 }; | |
463acbe1 | 159 | |
c44cdc7e | 160 | /* Atom management */ |
463acbe1 SP |
161 | static unsigned int atom_table_sz = 4451; |
162 | static unsigned int atom_cnt; | |
163 | static struct atom_str **atom_table; | |
164 | ||
165 | /* The .pack file being generated */ | |
ebcfb379 | 166 | static struct pack_idx_option pack_idx_opts; |
7bfe6e26 | 167 | static unsigned int pack_id; |
98a3beab | 168 | static struct hashfile *pack_file; |
d489bc14 | 169 | static struct packed_git *pack_data; |
7bfe6e26 | 170 | static struct packed_git **all_packs; |
89e0a3a1 | 171 | static off_t pack_size; |
ac47a738 SP |
172 | |
173 | /* Table of objects we've written. */ | |
4cabf858 | 174 | static unsigned int object_entry_alloc = 5000; |
463acbe1 SP |
175 | static struct object_entry_pool *blocks; |
176 | static struct object_entry *object_table[1 << 16]; | |
d8397168 | 177 | static struct mark_set *marks; |
07cd9328 SR |
178 | static const char *export_marks_file; |
179 | static const char *import_marks_file; | |
081751c8 | 180 | static int import_marks_file_from_stream; |
dded4f12 | 181 | static int import_marks_file_ignore_missing; |
f4beed60 | 182 | static int import_marks_file_done; |
bc3c79ae | 183 | static int relative_marks_paths; |
ac47a738 SP |
184 | |
185 | /* Our last blob */ | |
05576569 | 186 | static struct last_object last_blob = { STRBUF_INIT, 0, 0, 0 }; |
463acbe1 SP |
187 | |
188 | /* Tree management */ | |
189 | static unsigned int tree_entry_alloc = 1000; | |
190 | static void *avail_tree_entry; | |
191 | static unsigned int avail_tree_table_sz = 100; | |
192 | static struct avail_tree_content **avail_tree_table; | |
96c47d14 | 193 | static size_t tree_entry_allocd; |
eec813cf PH |
194 | static struct strbuf old_tree = STRBUF_INIT; |
195 | static struct strbuf new_tree = STRBUF_INIT; | |
8bcce301 | 196 | |
6bb5b329 | 197 | /* Branch data */ |
d5c57b28 SP |
198 | static unsigned long max_active_branches = 5; |
199 | static unsigned long cur_active_branches; | |
200 | static unsigned long branch_table_sz = 1039; | |
463acbe1 SP |
201 | static struct branch **branch_table; |
202 | static struct branch *active_branches; | |
203 | ||
72303d44 SP |
204 | /* Tag data */ |
205 | static struct tag *first_tag; | |
206 | static struct tag *last_tag; | |
207 | ||
c44cdc7e | 208 | /* Input stream parsing */ |
63e0c8b3 | 209 | static whenspec_type whenspec = WHENSPEC_RAW; |
4a241d79 | 210 | static struct strbuf command_buf = STRBUF_INIT; |
1fdb649c | 211 | static int unread_command_buf; |
904b1941 SP |
212 | static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL}; |
213 | static struct recent_command *cmd_tail = &cmd_hist; | |
214 | static struct recent_command *rc_free; | |
215 | static unsigned int cmd_save = 100; | |
0ea9f045 | 216 | static uintmax_t next_mark; |
eec813cf | 217 | static struct strbuf new_data = STRBUF_INIT; |
f963bd5d | 218 | static int seen_data_command; |
be56862f | 219 | static int require_explicit_termination; |
68061e34 | 220 | static int allow_unsafe_features; |
c44cdc7e | 221 | |
dc01f59d JN |
222 | /* Signal handling */ |
223 | static volatile sig_atomic_t checkpoint_requested; | |
224 | ||
85c62395 DB |
225 | /* Where to write output of cat-blob commands */ |
226 | static int cat_blob_fd = STDOUT_FILENO; | |
227 | ||
9c8398f0 | 228 | static void parse_argv(void); |
28c7b1f7 | 229 | static void parse_get_mark(const char *p); |
97313bef JK |
230 | static void parse_cat_blob(const char *p); |
231 | static void parse_ls(const char *p, struct branch *b); | |
c44cdc7e | 232 | |
8acb3297 SP |
233 | static void write_branch_report(FILE *rpt, struct branch *b) |
234 | { | |
235 | fprintf(rpt, "%s:\n", b->name); | |
236 | ||
237 | fprintf(rpt, " status :"); | |
238 | if (b->active) | |
239 | fputs(" active", rpt); | |
240 | if (b->branch_tree.tree) | |
241 | fputs(" loaded", rpt); | |
d7e6b6a8 | 242 | if (is_null_oid(&b->branch_tree.versions[1].oid)) |
8acb3297 SP |
243 | fputs(" dirty", rpt); |
244 | fputc('\n', rpt); | |
245 | ||
d7e6b6a8 | 246 | fprintf(rpt, " tip commit : %s\n", oid_to_hex(&b->oid)); |
247 | fprintf(rpt, " old tree : %s\n", | |
248 | oid_to_hex(&b->branch_tree.versions[0].oid)); | |
249 | fprintf(rpt, " cur tree : %s\n", | |
250 | oid_to_hex(&b->branch_tree.versions[1].oid)); | |
8acb3297 SP |
251 | fprintf(rpt, " commit clock: %" PRIuMAX "\n", b->last_commit); |
252 | ||
253 | fputs(" last pack : ", rpt); | |
254 | if (b->pack_id < MAX_PACK_ID) | |
255 | fprintf(rpt, "%u", b->pack_id); | |
256 | fputc('\n', rpt); | |
257 | ||
258 | fputc('\n', rpt); | |
259 | } | |
260 | ||
3b08e5b8 SP |
261 | static void dump_marks_helper(FILE *, uintmax_t, struct mark_set *); |
262 | ||
4bf53833 | 263 | static void write_crash_report(const char *err) |
8acb3297 | 264 | { |
fcd12db6 | 265 | char *loc = git_pathdup("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid()); |
8acb3297 SP |
266 | FILE *rpt = fopen(loc, "w"); |
267 | struct branch *b; | |
268 | unsigned long lu; | |
904b1941 | 269 | struct recent_command *rc; |
8acb3297 SP |
270 | |
271 | if (!rpt) { | |
6c223e49 | 272 | error_errno("can't write crash report %s", loc); |
fcd12db6 | 273 | free(loc); |
8acb3297 SP |
274 | return; |
275 | } | |
276 | ||
277 | fprintf(stderr, "fast-import: dumping crash report to %s\n", loc); | |
278 | ||
279 | fprintf(rpt, "fast-import crash report:\n"); | |
85e72830 DSP |
280 | fprintf(rpt, " fast-import process: %"PRIuMAX"\n", (uintmax_t) getpid()); |
281 | fprintf(rpt, " parent process : %"PRIuMAX"\n", (uintmax_t) getppid()); | |
547ed716 | 282 | fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_MODE(ISO8601))); |
8acb3297 SP |
283 | fputc('\n', rpt); |
284 | ||
285 | fputs("fatal: ", rpt); | |
4bf53833 | 286 | fputs(err, rpt); |
8acb3297 SP |
287 | fputc('\n', rpt); |
288 | ||
904b1941 SP |
289 | fputc('\n', rpt); |
290 | fputs("Most Recent Commands Before Crash\n", rpt); | |
291 | fputs("---------------------------------\n", rpt); | |
292 | for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) { | |
293 | if (rc->next == &cmd_hist) | |
294 | fputs("* ", rpt); | |
295 | else | |
296 | fputs(" ", rpt); | |
297 | fputs(rc->buf, rpt); | |
298 | fputc('\n', rpt); | |
299 | } | |
300 | ||
8acb3297 SP |
301 | fputc('\n', rpt); |
302 | fputs("Active Branch LRU\n", rpt); | |
303 | fputs("-----------------\n", rpt); | |
304 | fprintf(rpt, " active_branches = %lu cur, %lu max\n", | |
305 | cur_active_branches, | |
306 | max_active_branches); | |
307 | fputc('\n', rpt); | |
308 | fputs(" pos clock name\n", rpt); | |
309 | fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt); | |
310 | for (b = active_branches, lu = 0; b; b = b->active_next_branch) | |
311 | fprintf(rpt, " %2lu) %6" PRIuMAX" %s\n", | |
312 | ++lu, b->last_commit, b->name); | |
313 | ||
314 | fputc('\n', rpt); | |
315 | fputs("Inactive Branches\n", rpt); | |
316 | fputs("-----------------\n", rpt); | |
317 | for (lu = 0; lu < branch_table_sz; lu++) { | |
318 | for (b = branch_table[lu]; b; b = b->table_next_branch) | |
319 | write_branch_report(rpt, b); | |
320 | } | |
321 | ||
fbc63ea6 SP |
322 | if (first_tag) { |
323 | struct tag *tg; | |
324 | fputc('\n', rpt); | |
325 | fputs("Annotated Tags\n", rpt); | |
326 | fputs("--------------\n", rpt); | |
327 | for (tg = first_tag; tg; tg = tg->next_tag) { | |
d7e6b6a8 | 328 | fputs(oid_to_hex(&tg->oid), rpt); |
fbc63ea6 SP |
329 | fputc(' ', rpt); |
330 | fputs(tg->name, rpt); | |
331 | fputc('\n', rpt); | |
332 | } | |
333 | } | |
334 | ||
3b08e5b8 SP |
335 | fputc('\n', rpt); |
336 | fputs("Marks\n", rpt); | |
337 | fputs("-----\n", rpt); | |
07cd9328 SR |
338 | if (export_marks_file) |
339 | fprintf(rpt, " exported to %s\n", export_marks_file); | |
3b08e5b8 SP |
340 | else |
341 | dump_marks_helper(rpt, 0, marks); | |
342 | ||
8acb3297 SP |
343 | fputc('\n', rpt); |
344 | fputs("-------------------\n", rpt); | |
345 | fputs("END OF CRASH REPORT\n", rpt); | |
346 | fclose(rpt); | |
fcd12db6 | 347 | free(loc); |
8acb3297 SP |
348 | } |
349 | ||
118805b9 SP |
350 | static void end_packfile(void); |
351 | static void unkeep_all_packs(void); | |
352 | static void dump_marks(void); | |
353 | ||
8acb3297 SP |
354 | static NORETURN void die_nicely(const char *err, va_list params) |
355 | { | |
356 | static int zombie; | |
4bf53833 | 357 | char message[2 * PATH_MAX]; |
8acb3297 | 358 | |
4bf53833 | 359 | vsnprintf(message, sizeof(message), err, params); |
8acb3297 | 360 | fputs("fatal: ", stderr); |
4bf53833 | 361 | fputs(message, stderr); |
8acb3297 SP |
362 | fputc('\n', stderr); |
363 | ||
364 | if (!zombie) { | |
365 | zombie = 1; | |
4bf53833 | 366 | write_crash_report(message); |
118805b9 SP |
367 | end_packfile(); |
368 | unkeep_all_packs(); | |
369 | dump_marks(); | |
8acb3297 | 370 | } |
8acb3297 SP |
371 | exit(128); |
372 | } | |
6bb5b329 | 373 | |
dc01f59d JN |
374 | #ifndef SIGUSR1 /* Windows, for example */ |
375 | ||
376 | static void set_checkpoint_signal(void) | |
377 | { | |
378 | } | |
379 | ||
380 | #else | |
381 | ||
382 | static void checkpoint_signal(int signo) | |
383 | { | |
384 | checkpoint_requested = 1; | |
385 | } | |
386 | ||
387 | static void set_checkpoint_signal(void) | |
388 | { | |
389 | struct sigaction sa; | |
390 | ||
391 | memset(&sa, 0, sizeof(sa)); | |
392 | sa.sa_handler = checkpoint_signal; | |
393 | sigemptyset(&sa.sa_mask); | |
394 | sa.sa_flags = SA_RESTART; | |
395 | sigaction(SIGUSR1, &sa, NULL); | |
396 | } | |
397 | ||
398 | #endif | |
399 | ||
03842d8e | 400 | static void alloc_objects(unsigned int cnt) |
8bcce301 | 401 | { |
463acbe1 | 402 | struct object_entry_pool *b; |
27d6d290 | 403 | |
463acbe1 | 404 | b = xmalloc(sizeof(struct object_entry_pool) |
27d6d290 | 405 | + cnt * sizeof(struct object_entry)); |
463acbe1 | 406 | b->next_pool = blocks; |
27d6d290 SP |
407 | b->next_free = b->entries; |
408 | b->end = b->entries + cnt; | |
409 | blocks = b; | |
410 | alloc_count += cnt; | |
411 | } | |
8bcce301 | 412 | |
912c13d5 | 413 | static struct object_entry *new_object(struct object_id *oid) |
8bcce301 | 414 | { |
27d6d290 | 415 | struct object_entry *e; |
8bcce301 | 416 | |
27d6d290 | 417 | if (blocks->next_free == blocks->end) |
463acbe1 | 418 | alloc_objects(object_entry_alloc); |
8bcce301 | 419 | |
27d6d290 | 420 | e = blocks->next_free++; |
e6a492b7 | 421 | oidcpy(&e->idx.oid, oid); |
27d6d290 | 422 | return e; |
8bcce301 SP |
423 | } |
424 | ||
912c13d5 | 425 | static struct object_entry *find_object(struct object_id *oid) |
463acbe1 | 426 | { |
912c13d5 | 427 | unsigned int h = oid->hash[0] << 8 | oid->hash[1]; |
463acbe1 SP |
428 | struct object_entry *e; |
429 | for (e = object_table[h]; e; e = e->next) | |
4a7e27e9 | 430 | if (oideq(oid, &e->idx.oid)) |
463acbe1 SP |
431 | return e; |
432 | return NULL; | |
433 | } | |
434 | ||
912c13d5 | 435 | static struct object_entry *insert_object(struct object_id *oid) |
8bcce301 | 436 | { |
912c13d5 | 437 | unsigned int h = oid->hash[0] << 8 | oid->hash[1]; |
27d6d290 | 438 | struct object_entry *e = object_table[h]; |
8bcce301 SP |
439 | |
440 | while (e) { | |
4a7e27e9 | 441 | if (oideq(oid, &e->idx.oid)) |
8bcce301 | 442 | return e; |
8bcce301 SP |
443 | e = e->next; |
444 | } | |
445 | ||
912c13d5 | 446 | e = new_object(oid); |
b7c1ce4f | 447 | e->next = object_table[h]; |
3fc366bd | 448 | e->idx.offset = 0; |
b7c1ce4f | 449 | object_table[h] = e; |
8bcce301 SP |
450 | return e; |
451 | } | |
db5e523f | 452 | |
d2986d0f EW |
453 | static void invalidate_pack_id(unsigned int id) |
454 | { | |
455 | unsigned int h; | |
456 | unsigned long lu; | |
457 | struct tag *t; | |
458 | ||
459 | for (h = 0; h < ARRAY_SIZE(object_table); h++) { | |
460 | struct object_entry *e; | |
461 | ||
462 | for (e = object_table[h]; e; e = e->next) | |
463 | if (e->pack_id == id) | |
464 | e->pack_id = MAX_PACK_ID; | |
465 | } | |
466 | ||
467 | for (lu = 0; lu < branch_table_sz; lu++) { | |
468 | struct branch *b; | |
469 | ||
470 | for (b = branch_table[lu]; b; b = b->table_next_branch) | |
471 | if (b->pack_id == id) | |
472 | b->pack_id = MAX_PACK_ID; | |
473 | } | |
474 | ||
475 | for (t = first_tag; t; t = t->next_tag) | |
476 | if (t->pack_id == id) | |
477 | t->pack_id = MAX_PACK_ID; | |
478 | } | |
479 | ||
463acbe1 SP |
480 | static unsigned int hc_str(const char *s, size_t len) |
481 | { | |
482 | unsigned int r = 0; | |
483 | while (len-- > 0) | |
484 | r = r * 31 + *s++; | |
485 | return r; | |
486 | } | |
487 | ||
e5b1444b | 488 | static char *pool_strdup(const char *s) |
463acbe1 | 489 | { |
34fa79a6 | 490 | size_t len = strlen(s) + 1; |
96c47d14 | 491 | char *r = mem_pool_alloc(&fi_mem_pool, len); |
34fa79a6 | 492 | memcpy(r, s, len); |
463acbe1 SP |
493 | return r; |
494 | } | |
495 | ||
0ea9f045 | 496 | static void insert_mark(uintmax_t idnum, struct object_entry *oe) |
d8397168 SP |
497 | { |
498 | struct mark_set *s = marks; | |
499 | while ((idnum >> s->shift) >= 1024) { | |
96c47d14 | 500 | s = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set)); |
d8397168 SP |
501 | s->shift = marks->shift + 10; |
502 | s->data.sets[0] = marks; | |
503 | marks = s; | |
504 | } | |
505 | while (s->shift) { | |
0ea9f045 | 506 | uintmax_t i = idnum >> s->shift; |
d8397168 SP |
507 | idnum -= i << s->shift; |
508 | if (!s->data.sets[i]) { | |
96c47d14 | 509 | s->data.sets[i] = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set)); |
d8397168 SP |
510 | s->data.sets[i]->shift = s->shift - 10; |
511 | } | |
512 | s = s->data.sets[i]; | |
513 | } | |
514 | if (!s->data.marked[idnum]) | |
515 | marks_set_count++; | |
516 | s->data.marked[idnum] = oe; | |
517 | } | |
518 | ||
e5b1444b | 519 | static struct object_entry *find_mark(uintmax_t idnum) |
d8397168 | 520 | { |
0ea9f045 | 521 | uintmax_t orig_idnum = idnum; |
d8397168 SP |
522 | struct mark_set *s = marks; |
523 | struct object_entry *oe = NULL; | |
524 | if ((idnum >> s->shift) < 1024) { | |
525 | while (s && s->shift) { | |
0ea9f045 | 526 | uintmax_t i = idnum >> s->shift; |
d8397168 SP |
527 | idnum -= i << s->shift; |
528 | s = s->data.sets[i]; | |
529 | } | |
530 | if (s) | |
531 | oe = s->data.marked[idnum]; | |
532 | } | |
533 | if (!oe) | |
3efb1f34 | 534 | die("mark :%" PRIuMAX " not declared", orig_idnum); |
d8397168 SP |
535 | return oe; |
536 | } | |
537 | ||
e5b1444b | 538 | static struct atom_str *to_atom(const char *s, unsigned short len) |
463acbe1 SP |
539 | { |
540 | unsigned int hc = hc_str(s, len) % atom_table_sz; | |
541 | struct atom_str *c; | |
542 | ||
543 | for (c = atom_table[hc]; c; c = c->next_atom) | |
544 | if (c->str_len == len && !strncmp(s, c->str_dat, len)) | |
545 | return c; | |
546 | ||
96c47d14 | 547 | c = mem_pool_alloc(&fi_mem_pool, sizeof(struct atom_str) + len + 1); |
463acbe1 | 548 | c->str_len = len; |
eddda371 | 549 | memcpy(c->str_dat, s, len); |
463acbe1 SP |
550 | c->str_dat[len] = 0; |
551 | c->next_atom = atom_table[hc]; | |
552 | atom_table[hc] = c; | |
553 | atom_cnt++; | |
554 | return c; | |
555 | } | |
556 | ||
e5b1444b | 557 | static struct branch *lookup_branch(const char *name) |
463acbe1 SP |
558 | { |
559 | unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; | |
560 | struct branch *b; | |
561 | ||
562 | for (b = branch_table[hc]; b; b = b->table_next_branch) | |
563 | if (!strcmp(name, b->name)) | |
564 | return b; | |
565 | return NULL; | |
566 | } | |
567 | ||
e5b1444b | 568 | static struct branch *new_branch(const char *name) |
463acbe1 SP |
569 | { |
570 | unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; | |
4b25d091 | 571 | struct branch *b = lookup_branch(name); |
463acbe1 SP |
572 | |
573 | if (b) | |
574 | die("Invalid attempt to create duplicate branch: %s", name); | |
8d9c5010 | 575 | if (check_refname_format(name, REFNAME_ALLOW_ONELEVEL)) |
c44cdc7e | 576 | die("Branch name doesn't conform to GIT standards: %s", name); |
463acbe1 | 577 | |
96c47d14 | 578 | b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch)); |
463acbe1 SP |
579 | b->name = pool_strdup(name); |
580 | b->table_next_branch = branch_table[hc]; | |
8a8c55ea SP |
581 | b->branch_tree.versions[0].mode = S_IFDIR; |
582 | b->branch_tree.versions[1].mode = S_IFDIR; | |
2a113aee | 583 | b->num_notes = 0; |
734c91f9 | 584 | b->active = 0; |
69e74e74 | 585 | b->pack_id = MAX_PACK_ID; |
463acbe1 SP |
586 | branch_table[hc] = b; |
587 | branch_count++; | |
588 | return b; | |
589 | } | |
590 | ||
591 | static unsigned int hc_entries(unsigned int cnt) | |
592 | { | |
593 | cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8; | |
594 | return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1; | |
595 | } | |
596 | ||
e5b1444b | 597 | static struct tree_content *new_tree_content(unsigned int cnt) |
463acbe1 SP |
598 | { |
599 | struct avail_tree_content *f, *l = NULL; | |
600 | struct tree_content *t; | |
601 | unsigned int hc = hc_entries(cnt); | |
602 | ||
603 | for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail) | |
604 | if (f->entry_capacity >= cnt) | |
605 | break; | |
606 | ||
607 | if (f) { | |
608 | if (l) | |
609 | l->next_avail = f->next_avail; | |
610 | else | |
611 | avail_tree_table[hc] = f->next_avail; | |
612 | } else { | |
613 | cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt; | |
96c47d14 | 614 | f = mem_pool_alloc(&fi_mem_pool, sizeof(*t) + sizeof(t->entries[0]) * cnt); |
463acbe1 SP |
615 | f->entry_capacity = cnt; |
616 | } | |
617 | ||
618 | t = (struct tree_content*)f; | |
619 | t->entry_count = 0; | |
4cabf858 | 620 | t->delta_depth = 0; |
463acbe1 SP |
621 | return t; |
622 | } | |
623 | ||
624 | static void release_tree_entry(struct tree_entry *e); | |
625 | static void release_tree_content(struct tree_content *t) | |
626 | { | |
627 | struct avail_tree_content *f = (struct avail_tree_content*)t; | |
628 | unsigned int hc = hc_entries(f->entry_capacity); | |
afde8dd9 SP |
629 | f->next_avail = avail_tree_table[hc]; |
630 | avail_tree_table[hc] = f; | |
631 | } | |
632 | ||
633 | static void release_tree_content_recursive(struct tree_content *t) | |
634 | { | |
463acbe1 SP |
635 | unsigned int i; |
636 | for (i = 0; i < t->entry_count; i++) | |
637 | release_tree_entry(t->entries[i]); | |
afde8dd9 | 638 | release_tree_content(t); |
463acbe1 SP |
639 | } |
640 | ||
e5b1444b | 641 | static struct tree_content *grow_tree_content( |
463acbe1 SP |
642 | struct tree_content *t, |
643 | int amt) | |
644 | { | |
645 | struct tree_content *r = new_tree_content(t->entry_count + amt); | |
646 | r->entry_count = t->entry_count; | |
4cabf858 | 647 | r->delta_depth = t->delta_depth; |
921d49be | 648 | COPY_ARRAY(r->entries, t->entries, t->entry_count); |
463acbe1 SP |
649 | release_tree_content(t); |
650 | return r; | |
651 | } | |
652 | ||
e5b1444b | 653 | static struct tree_entry *new_tree_entry(void) |
463acbe1 SP |
654 | { |
655 | struct tree_entry *e; | |
656 | ||
657 | if (!avail_tree_entry) { | |
658 | unsigned int n = tree_entry_alloc; | |
96c47d14 | 659 | tree_entry_allocd += n * sizeof(struct tree_entry); |
b32fa95f JK |
660 | ALLOC_ARRAY(e, n); |
661 | avail_tree_entry = e; | |
2eb26d84 | 662 | while (n-- > 1) { |
463acbe1 SP |
663 | *((void**)e) = e + 1; |
664 | e++; | |
665 | } | |
35ef237c | 666 | *((void**)e) = NULL; |
463acbe1 SP |
667 | } |
668 | ||
669 | e = avail_tree_entry; | |
670 | avail_tree_entry = *((void**)e); | |
671 | return e; | |
672 | } | |
673 | ||
674 | static void release_tree_entry(struct tree_entry *e) | |
675 | { | |
676 | if (e->tree) | |
afde8dd9 | 677 | release_tree_content_recursive(e->tree); |
463acbe1 SP |
678 | *((void**)e) = avail_tree_entry; |
679 | avail_tree_entry = e; | |
680 | } | |
681 | ||
b6f3481b SP |
682 | static struct tree_content *dup_tree_content(struct tree_content *s) |
683 | { | |
684 | struct tree_content *d; | |
685 | struct tree_entry *a, *b; | |
686 | unsigned int i; | |
687 | ||
688 | if (!s) | |
689 | return NULL; | |
690 | d = new_tree_content(s->entry_count); | |
691 | for (i = 0; i < s->entry_count; i++) { | |
692 | a = s->entries[i]; | |
693 | b = new_tree_entry(); | |
694 | memcpy(b, a, sizeof(*a)); | |
d7e6b6a8 | 695 | if (a->tree && is_null_oid(&b->versions[1].oid)) |
b6f3481b SP |
696 | b->tree = dup_tree_content(a->tree); |
697 | else | |
698 | b->tree = NULL; | |
699 | d->entries[i] = b; | |
700 | } | |
701 | d->entry_count = s->entry_count; | |
702 | d->delta_depth = s->delta_depth; | |
703 | ||
704 | return d; | |
705 | } | |
706 | ||
fd99224e | 707 | static void start_packfile(void) |
f70b6534 | 708 | { |
594fa999 | 709 | struct strbuf tmp_file = STRBUF_INIT; |
7bfe6e26 | 710 | struct packed_git *p; |
f70b6534 | 711 | struct pack_header hdr; |
0fcbcae7 | 712 | int pack_fd; |
f70b6534 | 713 | |
594fa999 JK |
714 | pack_fd = odb_mkstemp(&tmp_file, "pack/tmp_pack_XXXXXX"); |
715 | FLEX_ALLOC_STR(p, pack_name, tmp_file.buf); | |
716 | strbuf_release(&tmp_file); | |
717 | ||
7bfe6e26 | 718 | p->pack_fd = pack_fd; |
d131b7af | 719 | p->do_not_close = 1; |
98a3beab | 720 | pack_file = hashfd(pack_fd, p->pack_name); |
f70b6534 SP |
721 | |
722 | hdr.hdr_signature = htonl(PACK_SIGNATURE); | |
723 | hdr.hdr_version = htonl(2); | |
724 | hdr.hdr_entries = 0; | |
98a3beab | 725 | hashwrite(pack_file, &hdr, sizeof(hdr)); |
7bfe6e26 SP |
726 | |
727 | pack_data = p; | |
f70b6534 SP |
728 | pack_size = sizeof(hdr); |
729 | object_count = 0; | |
7bfe6e26 | 730 | |
2756ca43 | 731 | REALLOC_ARRAY(all_packs, pack_id + 1); |
7bfe6e26 | 732 | all_packs[pack_id] = p; |
f70b6534 SP |
733 | } |
734 | ||
427cb22c | 735 | static const char *create_index(void) |
f70b6534 | 736 | { |
427cb22c NP |
737 | const char *tmpfile; |
738 | struct pack_idx_entry **idx, **c, **last; | |
739 | struct object_entry *e; | |
f70b6534 | 740 | struct object_entry_pool *o; |
f70b6534 | 741 | |
427cb22c | 742 | /* Build the table of object IDs. */ |
b32fa95f | 743 | ALLOC_ARRAY(idx, object_count); |
f70b6534 SP |
744 | c = idx; |
745 | for (o = blocks; o; o = o->next_pool) | |
d9ee53ce SP |
746 | for (e = o->next_free; e-- != o->entries;) |
747 | if (pack_id == e->pack_id) | |
427cb22c | 748 | *c++ = &e->idx; |
f70b6534 | 749 | last = idx + object_count; |
2fce1f3c SP |
750 | if (c != last) |
751 | die("internal consistency error creating the index"); | |
f70b6534 | 752 | |
538b1523 | 753 | tmpfile = write_idx_file(NULL, idx, object_count, &pack_idx_opts, |
754 | pack_data->hash); | |
f70b6534 | 755 | free(idx); |
8455e484 SP |
756 | return tmpfile; |
757 | } | |
758 | ||
427cb22c | 759 | static char *keep_pack(const char *curr_index_name) |
8455e484 | 760 | { |
3a55602e | 761 | static const char *keep_msg = "fast-import"; |
ba47a308 | 762 | struct strbuf name = STRBUF_INIT; |
8455e484 SP |
763 | int keep_fd; |
764 | ||
538b1523 | 765 | odb_pack_name(&name, pack_data->hash, "keep"); |
ba47a308 | 766 | keep_fd = odb_pack_keep(name.buf); |
8455e484 | 767 | if (keep_fd < 0) |
0721c314 | 768 | die_errno("cannot create keep file"); |
95693d45 JM |
769 | write_or_die(keep_fd, keep_msg, strlen(keep_msg)); |
770 | if (close(keep_fd)) | |
0721c314 | 771 | die_errno("failed to write keep file"); |
8455e484 | 772 | |
538b1523 | 773 | odb_pack_name(&name, pack_data->hash, "pack"); |
ba47a308 | 774 | if (finalize_object_file(pack_data->pack_name, name.buf)) |
8455e484 | 775 | die("cannot store pack file"); |
8455e484 | 776 | |
538b1523 | 777 | odb_pack_name(&name, pack_data->hash, "idx"); |
ba47a308 | 778 | if (finalize_object_file(curr_index_name, name.buf)) |
8455e484 | 779 | die("cannot store index file"); |
427cb22c | 780 | free((void *)curr_index_name); |
ba47a308 | 781 | return strbuf_detach(&name, NULL); |
8455e484 SP |
782 | } |
783 | ||
fd99224e | 784 | static void unkeep_all_packs(void) |
8455e484 | 785 | { |
ba47a308 | 786 | struct strbuf name = STRBUF_INIT; |
8455e484 SP |
787 | int k; |
788 | ||
789 | for (k = 0; k < pack_id; k++) { | |
790 | struct packed_git *p = all_packs[k]; | |
538b1523 | 791 | odb_pack_name(&name, p->hash, "keep"); |
ba47a308 | 792 | unlink_or_warn(name.buf); |
8455e484 | 793 | } |
ba47a308 | 794 | strbuf_release(&name); |
f70b6534 SP |
795 | } |
796 | ||
d9545c7f EW |
797 | static int loosen_small_pack(const struct packed_git *p) |
798 | { | |
799 | struct child_process unpack = CHILD_PROCESS_INIT; | |
800 | ||
801 | if (lseek(p->pack_fd, 0, SEEK_SET) < 0) | |
802 | die_errno("Failed seeking to start of '%s'", p->pack_name); | |
803 | ||
804 | unpack.in = p->pack_fd; | |
805 | unpack.git_cmd = 1; | |
806 | unpack.stdout_to_stderr = 1; | |
807 | argv_array_push(&unpack.args, "unpack-objects"); | |
808 | if (!show_stats) | |
809 | argv_array_push(&unpack.args, "-q"); | |
810 | ||
811 | return run_command(&unpack); | |
812 | } | |
813 | ||
fd99224e | 814 | static void end_packfile(void) |
f70b6534 | 815 | { |
5e915f30 JK |
816 | static int running; |
817 | ||
818 | if (running || !pack_data) | |
3c078b9c | 819 | return; |
7bfe6e26 | 820 | |
5e915f30 | 821 | running = 1; |
3d20c636 | 822 | clear_delta_base_cache(); |
3e005baf | 823 | if (object_count) { |
3c078b9c | 824 | struct packed_git *new_p; |
912c13d5 | 825 | struct object_id cur_pack_oid; |
8455e484 | 826 | char *idx_name; |
2369ed79 SP |
827 | int i; |
828 | struct branch *b; | |
829 | struct tag *t; | |
8455e484 | 830 | |
c9ced051 | 831 | close_pack_windows(pack_data); |
f2af9f5e | 832 | finalize_hashfile(pack_file, cur_pack_oid.hash, 0); |
538b1523 | 833 | fixup_pack_header_footer(pack_data->pack_fd, pack_data->hash, |
834 | pack_data->pack_name, object_count, | |
835 | cur_pack_oid.hash, pack_size); | |
d9545c7f EW |
836 | |
837 | if (object_count <= unpack_limit) { | |
d2986d0f EW |
838 | if (!loosen_small_pack(pack_data)) { |
839 | invalidate_pack_id(pack_id); | |
d9545c7f | 840 | goto discard_pack; |
d2986d0f | 841 | } |
d9545c7f EW |
842 | } |
843 | ||
8b0eca7c | 844 | close(pack_data->pack_fd); |
8455e484 | 845 | idx_name = keep_pack(create_index()); |
3e005baf | 846 | |
3ea3c215 | 847 | /* Register the packfile with core git's machinery. */ |
3e005baf SP |
848 | new_p = add_packed_git(idx_name, strlen(idx_name), 1); |
849 | if (!new_p) | |
850 | die("core git rejected index %s", idx_name); | |
2369ed79 | 851 | all_packs[pack_id] = new_p; |
5babff16 | 852 | install_packed_git(the_repository, new_p); |
ba47a308 | 853 | free(idx_name); |
2369ed79 SP |
854 | |
855 | /* Print the boundary */ | |
bdf1c06d SP |
856 | if (pack_edges) { |
857 | fprintf(pack_edges, "%s:", new_p->pack_name); | |
858 | for (i = 0; i < branch_table_sz; i++) { | |
859 | for (b = branch_table[i]; b; b = b->table_next_branch) { | |
860 | if (b->pack_id == pack_id) | |
d7e6b6a8 | 861 | fprintf(pack_edges, " %s", |
862 | oid_to_hex(&b->oid)); | |
bdf1c06d | 863 | } |
2369ed79 | 864 | } |
bdf1c06d SP |
865 | for (t = first_tag; t; t = t->next_tag) { |
866 | if (t->pack_id == pack_id) | |
d7e6b6a8 | 867 | fprintf(pack_edges, " %s", |
868 | oid_to_hex(&t->oid)); | |
bdf1c06d SP |
869 | } |
870 | fputc('\n', pack_edges); | |
871 | fflush(pack_edges); | |
2369ed79 | 872 | } |
2369ed79 SP |
873 | |
874 | pack_id++; | |
3e005baf | 875 | } |
87c8a56e | 876 | else { |
d9545c7f | 877 | discard_pack: |
3c078b9c JK |
878 | close(pack_data->pack_fd); |
879 | unlink_or_warn(pack_data->pack_name); | |
87c8a56e | 880 | } |
6a83d902 | 881 | FREE_AND_NULL(pack_data); |
5e915f30 | 882 | running = 0; |
7bfe6e26 SP |
883 | |
884 | /* We can't carry a delta across packfiles. */ | |
05576569 | 885 | strbuf_release(&last_blob.data); |
7bfe6e26 SP |
886 | last_blob.offset = 0; |
887 | last_blob.depth = 0; | |
f70b6534 SP |
888 | } |
889 | ||
820b9310 | 890 | static void cycle_packfile(void) |
d9ee53ce SP |
891 | { |
892 | end_packfile(); | |
893 | start_packfile(); | |
894 | } | |
895 | ||
ac47a738 SP |
896 | static int store_object( |
897 | enum object_type type, | |
eec813cf | 898 | struct strbuf *dat, |
6bb5b329 | 899 | struct last_object *last, |
912c13d5 | 900 | struct object_id *oidout, |
0ea9f045 | 901 | uintmax_t mark) |
db5e523f | 902 | { |
db5e523f | 903 | void *out, *delta; |
ac47a738 SP |
904 | struct object_entry *e; |
905 | unsigned char hdr[96]; | |
912c13d5 | 906 | struct object_id oid; |
db5e523f | 907 | unsigned long hdrlen, deltalen; |
7f89428d | 908 | git_hash_ctx c; |
ef49a7a0 | 909 | git_zstream s; |
ac47a738 | 910 | |
ef1286d3 | 911 | hdrlen = xsnprintf((char *)hdr, sizeof(hdr), "%s %lu", |
debca9d2 | 912 | type_name(type), (unsigned long)dat->len) + 1; |
7f89428d | 913 | the_hash_algo->init_fn(&c); |
914 | the_hash_algo->update_fn(&c, hdr, hdrlen); | |
915 | the_hash_algo->update_fn(&c, dat->buf, dat->len); | |
916 | the_hash_algo->final_fn(oid.hash, &c); | |
912c13d5 | 917 | if (oidout) |
918 | oidcpy(oidout, &oid); | |
ac47a738 | 919 | |
912c13d5 | 920 | e = insert_object(&oid); |
d8397168 SP |
921 | if (mark) |
922 | insert_mark(mark, e); | |
3fc366bd | 923 | if (e->idx.offset) { |
6143f064 | 924 | duplicate_count_by_type[type]++; |
463acbe1 | 925 | return 1; |
a80d72db | 926 | } else if (find_sha1_pack(oid.hash, |
454ea2e4 | 927 | get_all_packs(the_repository))) { |
a5c1780a SP |
928 | e->type = type; |
929 | e->pack_id = MAX_PACK_ID; | |
3fc366bd | 930 | e->idx.offset = 1; /* just not zero! */ |
a5c1780a SP |
931 | duplicate_count_by_type[type]++; |
932 | return 1; | |
ac47a738 | 933 | } |
db5e523f | 934 | |
9d14ecf3 | 935 | if (last && last->data.len && last->data.buf && last->depth < max_depth |
7f89428d | 936 | && dat->len > the_hash_algo->rawsz) { |
937 | ||
94c3b482 | 938 | delta_count_attempts_by_type[type]++; |
05576569 | 939 | delta = diff_delta(last->data.buf, last->data.len, |
eec813cf | 940 | dat->buf, dat->len, |
7f89428d | 941 | &deltalen, dat->len - the_hash_algo->rawsz); |
d9ee53ce SP |
942 | } else |
943 | delta = NULL; | |
db5e523f | 944 | |
55bb5c91 | 945 | git_deflate_init(&s, pack_compression_level); |
d9ee53ce SP |
946 | if (delta) { |
947 | s.next_in = delta; | |
948 | s.avail_in = deltalen; | |
949 | } else { | |
eec813cf PH |
950 | s.next_in = (void *)dat->buf; |
951 | s.avail_in = dat->len; | |
d9ee53ce | 952 | } |
225a6f10 | 953 | s.avail_out = git_deflate_bound(&s, s.avail_in); |
d9ee53ce | 954 | s.next_out = out = xmalloc(s.avail_out); |
55bb5c91 JH |
955 | while (git_deflate(&s, Z_FINISH) == Z_OK) |
956 | ; /* nothing */ | |
957 | git_deflate_end(&s); | |
d9ee53ce SP |
958 | |
959 | /* Determine if we should auto-checkpoint. */ | |
28d055bd | 960 | if ((max_packsize |
961 | && (pack_size + PACK_SIZE_THRESHOLD + s.total_out) > max_packsize) | |
962 | || (pack_size + PACK_SIZE_THRESHOLD + s.total_out) < pack_size) { | |
d9ee53ce SP |
963 | |
964 | /* This new object needs to *not* have the current pack_id. */ | |
965 | e->pack_id = pack_id + 1; | |
820b9310 | 966 | cycle_packfile(); |
d9ee53ce SP |
967 | |
968 | /* We cannot carry a delta into the new pack. */ | |
969 | if (delta) { | |
6a83d902 | 970 | FREE_AND_NULL(delta); |
5d6f3ef6 | 971 | |
55bb5c91 | 972 | git_deflate_init(&s, pack_compression_level); |
eec813cf PH |
973 | s.next_in = (void *)dat->buf; |
974 | s.avail_in = dat->len; | |
225a6f10 | 975 | s.avail_out = git_deflate_bound(&s, s.avail_in); |
5d6f3ef6 | 976 | s.next_out = out = xrealloc(out, s.avail_out); |
55bb5c91 JH |
977 | while (git_deflate(&s, Z_FINISH) == Z_OK) |
978 | ; /* nothing */ | |
979 | git_deflate_end(&s); | |
d9ee53ce | 980 | } |
d9ee53ce SP |
981 | } |
982 | ||
983 | e->type = type; | |
984 | e->pack_id = pack_id; | |
3fc366bd | 985 | e->idx.offset = pack_size; |
d9ee53ce SP |
986 | object_count++; |
987 | object_count_by_type[type]++; | |
db5e523f | 988 | |
427cb22c NP |
989 | crc32_begin(pack_file); |
990 | ||
db5e523f | 991 | if (delta) { |
89e0a3a1 | 992 | off_t ofs = e->idx.offset - last->offset; |
d489bc14 SP |
993 | unsigned pos = sizeof(hdr) - 1; |
994 | ||
4cabf858 | 995 | delta_count_by_type[type]++; |
436e7a74 | 996 | e->depth = last->depth + 1; |
d489bc14 | 997 | |
7202a6fa JK |
998 | hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr), |
999 | OBJ_OFS_DELTA, deltalen); | |
98a3beab | 1000 | hashwrite(pack_file, hdr, hdrlen); |
d489bc14 SP |
1001 | pack_size += hdrlen; |
1002 | ||
1003 | hdr[pos] = ofs & 127; | |
1004 | while (ofs >>= 7) | |
1005 | hdr[--pos] = 128 | (--ofs & 127); | |
98a3beab | 1006 | hashwrite(pack_file, hdr + pos, sizeof(hdr) - pos); |
d489bc14 | 1007 | pack_size += sizeof(hdr) - pos; |
db5e523f | 1008 | } else { |
436e7a74 | 1009 | e->depth = 0; |
7202a6fa JK |
1010 | hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr), |
1011 | type, dat->len); | |
98a3beab | 1012 | hashwrite(pack_file, hdr, hdrlen); |
41e5257f | 1013 | pack_size += hdrlen; |
db5e523f SP |
1014 | } |
1015 | ||
98a3beab | 1016 | hashwrite(pack_file, out, s.total_out); |
41e5257f | 1017 | pack_size += s.total_out; |
db5e523f | 1018 | |
427cb22c NP |
1019 | e->idx.crc32 = crc32_end(pack_file); |
1020 | ||
db5e523f | 1021 | free(out); |
e7d06a4b | 1022 | free(delta); |
463acbe1 | 1023 | if (last) { |
05576569 PH |
1024 | if (last->no_swap) { |
1025 | last->data = *dat; | |
1026 | } else { | |
c76689df | 1027 | strbuf_swap(&last->data, dat); |
05576569 | 1028 | } |
3fc366bd | 1029 | last->offset = e->idx.offset; |
436e7a74 | 1030 | last->depth = e->depth; |
463acbe1 SP |
1031 | } |
1032 | return 0; | |
1033 | } | |
1034 | ||
98a3beab | 1035 | static void truncate_pack(struct hashfile_checkpoint *checkpoint) |
5eef828b | 1036 | { |
98a3beab | 1037 | if (hashfile_truncate(pack_file, checkpoint)) |
5eef828b | 1038 | die_errno("cannot truncate pack to skip duplicate"); |
6c526148 | 1039 | pack_size = checkpoint->offset; |
5eef828b SP |
1040 | } |
1041 | ||
912c13d5 | 1042 | static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark) |
5eef828b SP |
1043 | { |
1044 | size_t in_sz = 64 * 1024, out_sz = 64 * 1024; | |
1045 | unsigned char *in_buf = xmalloc(in_sz); | |
1046 | unsigned char *out_buf = xmalloc(out_sz); | |
1047 | struct object_entry *e; | |
912c13d5 | 1048 | struct object_id oid; |
5eef828b SP |
1049 | unsigned long hdrlen; |
1050 | off_t offset; | |
7f89428d | 1051 | git_hash_ctx c; |
ef49a7a0 | 1052 | git_zstream s; |
98a3beab | 1053 | struct hashfile_checkpoint checkpoint; |
5eef828b SP |
1054 | int status = Z_OK; |
1055 | ||
1056 | /* Determine if we should auto-checkpoint. */ | |
28d055bd | 1057 | if ((max_packsize |
1058 | && (pack_size + PACK_SIZE_THRESHOLD + len) > max_packsize) | |
1059 | || (pack_size + PACK_SIZE_THRESHOLD + len) < pack_size) | |
5eef828b SP |
1060 | cycle_packfile(); |
1061 | ||
98a3beab | 1062 | hashfile_checkpoint(pack_file, &checkpoint); |
6c526148 | 1063 | offset = checkpoint.offset; |
21281816 | 1064 | |
98718242 | 1065 | hdrlen = xsnprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1; |
5eef828b | 1066 | |
7f89428d | 1067 | the_hash_algo->init_fn(&c); |
1068 | the_hash_algo->update_fn(&c, out_buf, hdrlen); | |
5eef828b | 1069 | |
427cb22c NP |
1070 | crc32_begin(pack_file); |
1071 | ||
55bb5c91 | 1072 | git_deflate_init(&s, pack_compression_level); |
5eef828b | 1073 | |
7202a6fa | 1074 | hdrlen = encode_in_pack_object_header(out_buf, out_sz, OBJ_BLOB, len); |
5eef828b SP |
1075 | |
1076 | s.next_out = out_buf + hdrlen; | |
1077 | s.avail_out = out_sz - hdrlen; | |
1078 | ||
1079 | while (status != Z_STREAM_END) { | |
1080 | if (0 < len && !s.avail_in) { | |
1081 | size_t cnt = in_sz < len ? in_sz : (size_t)len; | |
1082 | size_t n = fread(in_buf, 1, cnt, stdin); | |
1083 | if (!n && feof(stdin)) | |
1084 | die("EOF in data (%" PRIuMAX " bytes remaining)", len); | |
1085 | ||
7f89428d | 1086 | the_hash_algo->update_fn(&c, in_buf, n); |
5eef828b SP |
1087 | s.next_in = in_buf; |
1088 | s.avail_in = n; | |
1089 | len -= n; | |
1090 | } | |
1091 | ||
55bb5c91 | 1092 | status = git_deflate(&s, len ? 0 : Z_FINISH); |
5eef828b SP |
1093 | |
1094 | if (!s.avail_out || status == Z_STREAM_END) { | |
1095 | size_t n = s.next_out - out_buf; | |
98a3beab | 1096 | hashwrite(pack_file, out_buf, n); |
5eef828b SP |
1097 | pack_size += n; |
1098 | s.next_out = out_buf; | |
1099 | s.avail_out = out_sz; | |
1100 | } | |
1101 | ||
1102 | switch (status) { | |
1103 | case Z_OK: | |
1104 | case Z_BUF_ERROR: | |
1105 | case Z_STREAM_END: | |
1106 | continue; | |
1107 | default: | |
1108 | die("unexpected deflate failure: %d", status); | |
1109 | } | |
1110 | } | |
55bb5c91 | 1111 | git_deflate_end(&s); |
7f89428d | 1112 | the_hash_algo->final_fn(oid.hash, &c); |
5eef828b | 1113 | |
912c13d5 | 1114 | if (oidout) |
1115 | oidcpy(oidout, &oid); | |
5eef828b | 1116 | |
912c13d5 | 1117 | e = insert_object(&oid); |
5eef828b SP |
1118 | |
1119 | if (mark) | |
1120 | insert_mark(mark, e); | |
1121 | ||
3fc366bd | 1122 | if (e->idx.offset) { |
5eef828b | 1123 | duplicate_count_by_type[OBJ_BLOB]++; |
6c526148 | 1124 | truncate_pack(&checkpoint); |
5eef828b | 1125 | |
a80d72db | 1126 | } else if (find_sha1_pack(oid.hash, |
454ea2e4 | 1127 | get_all_packs(the_repository))) { |
5eef828b SP |
1128 | e->type = OBJ_BLOB; |
1129 | e->pack_id = MAX_PACK_ID; | |
3fc366bd | 1130 | e->idx.offset = 1; /* just not zero! */ |
5eef828b | 1131 | duplicate_count_by_type[OBJ_BLOB]++; |
6c526148 | 1132 | truncate_pack(&checkpoint); |
5eef828b SP |
1133 | |
1134 | } else { | |
1135 | e->depth = 0; | |
1136 | e->type = OBJ_BLOB; | |
1137 | e->pack_id = pack_id; | |
3fc366bd | 1138 | e->idx.offset = offset; |
427cb22c | 1139 | e->idx.crc32 = crc32_end(pack_file); |
5eef828b SP |
1140 | object_count++; |
1141 | object_count_by_type[OBJ_BLOB]++; | |
1142 | } | |
1143 | ||
1144 | free(in_buf); | |
1145 | free(out_buf); | |
1146 | } | |
1147 | ||
7422bac4 SP |
1148 | /* All calls must be guarded by find_object() or find_mark() to |
1149 | * ensure the 'struct object_entry' passed was written by this | |
1150 | * process instance. We unpack the entry by the offset, avoiding | |
1151 | * the need for the corresponding .idx file. This unpacking rule | |
1152 | * works because we only use OBJ_REF_DELTA within the packfiles | |
1153 | * created by fast-import. | |
1154 | * | |
1155 | * oe must not be NULL. Such an oe usually comes from giving | |
1156 | * an unknown SHA-1 to find_object() or an undefined mark to | |
1157 | * find_mark(). Callers must test for this condition and use | |
1158 | * the standard read_sha1_file() when it happens. | |
1159 | * | |
1160 | * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from | |
1161 | * find_mark(), where the mark was reloaded from an existing marks | |
1162 | * file and is referencing an object that this fast-import process | |
1163 | * instance did not write out to a packfile. Callers must test for | |
1164 | * this condition and use read_sha1_file() instead. | |
1165 | */ | |
7bfe6e26 SP |
1166 | static void *gfi_unpack_entry( |
1167 | struct object_entry *oe, | |
1168 | unsigned long *sizep) | |
41e5257f | 1169 | { |
21666f1a | 1170 | enum object_type type; |
7bfe6e26 | 1171 | struct packed_git *p = all_packs[oe->pack_id]; |
7f89428d | 1172 | if (p == pack_data && p->pack_size < (pack_size + the_hash_algo->rawsz)) { |
7422bac4 SP |
1173 | /* The object is stored in the packfile we are writing to |
1174 | * and we have modified it since the last time we scanned | |
1175 | * back to read a previously written object. If an old | |
7f89428d | 1176 | * window covered [p->pack_size, p->pack_size + rawsz) its |
7422bac4 SP |
1177 | * data is stale and is not valid. Closing all windows |
1178 | * and updating the packfile length ensures we can read | |
1179 | * the newly written data. | |
1180 | */ | |
c9ced051 | 1181 | close_pack_windows(p); |
98a3beab | 1182 | hashflush(pack_file); |
7422bac4 | 1183 | |
7f89428d | 1184 | /* We have to offer rawsz bytes additional on the end of |
7422bac4 SP |
1185 | * the packfile as the core unpacker code assumes the |
1186 | * footer is present at the file end and must promise | |
7f89428d | 1187 | * at least rawsz bytes within any window it maps. But |
7422bac4 SP |
1188 | * we don't actually create the footer here. |
1189 | */ | |
7f89428d | 1190 | p->pack_size = pack_size + the_hash_algo->rawsz; |
c9ced051 | 1191 | } |
57a6a500 | 1192 | return unpack_entry(the_repository, p, oe->idx.offset, &type, sizep); |
41e5257f SP |
1193 | } |
1194 | ||
10831c55 | 1195 | static const char *get_mode(const char *str, uint16_t *modep) |
463acbe1 SP |
1196 | { |
1197 | unsigned char c; | |
10831c55 | 1198 | uint16_t mode = 0; |
463acbe1 SP |
1199 | |
1200 | while ((c = *str++) != ' ') { | |
1201 | if (c < '0' || c > '7') | |
1202 | return NULL; | |
1203 | mode = (mode << 3) + (c - '0'); | |
1204 | } | |
1205 | *modep = mode; | |
1206 | return str; | |
1207 | } | |
1208 | ||
1209 | static void load_tree(struct tree_entry *root) | |
1210 | { | |
912c13d5 | 1211 | struct object_id *oid = &root->versions[1].oid; |
463acbe1 SP |
1212 | struct object_entry *myoe; |
1213 | struct tree_content *t; | |
1214 | unsigned long size; | |
1215 | char *buf; | |
1216 | const char *c; | |
463acbe1 SP |
1217 | |
1218 | root->tree = t = new_tree_content(8); | |
912c13d5 | 1219 | if (is_null_oid(oid)) |
463acbe1 SP |
1220 | return; |
1221 | ||
912c13d5 | 1222 | myoe = find_object(oid); |
20f546a8 | 1223 | if (myoe && myoe->pack_id != MAX_PACK_ID) { |
41e5257f | 1224 | if (myoe->type != OBJ_TREE) |
912c13d5 | 1225 | die("Not a tree: %s", oid_to_hex(oid)); |
436e7a74 | 1226 | t->delta_depth = myoe->depth; |
7bfe6e26 | 1227 | buf = gfi_unpack_entry(myoe, &size); |
e8b32e06 | 1228 | if (!buf) |
912c13d5 | 1229 | die("Can't load tree %s", oid_to_hex(oid)); |
463acbe1 | 1230 | } else { |
21666f1a | 1231 | enum object_type type; |
b4f5aca4 | 1232 | buf = read_object_file(oid, &type, &size); |
21666f1a | 1233 | if (!buf || type != OBJ_TREE) |
912c13d5 | 1234 | die("Can't load tree %s", oid_to_hex(oid)); |
463acbe1 SP |
1235 | } |
1236 | ||
1237 | c = buf; | |
1238 | while (c != (buf + size)) { | |
1239 | struct tree_entry *e = new_tree_entry(); | |
1240 | ||
1241 | if (t->entry_count == t->entry_capacity) | |
f022f85f | 1242 | root->tree = t = grow_tree_content(t, t->entry_count); |
463acbe1 SP |
1243 | t->entries[t->entry_count++] = e; |
1244 | ||
1245 | e->tree = NULL; | |
4cabf858 | 1246 | c = get_mode(c, &e->versions[1].mode); |
463acbe1 | 1247 | if (!c) |
912c13d5 | 1248 | die("Corrupt mode in %s", oid_to_hex(oid)); |
4cabf858 | 1249 | e->versions[0].mode = e->versions[1].mode; |
061e35c5 | 1250 | e->name = to_atom(c, strlen(c)); |
463acbe1 | 1251 | c += e->name->str_len + 1; |
d7e6b6a8 | 1252 | hashcpy(e->versions[0].oid.hash, (unsigned char *)c); |
1253 | hashcpy(e->versions[1].oid.hash, (unsigned char *)c); | |
28d055bd | 1254 | c += the_hash_algo->rawsz; |
463acbe1 SP |
1255 | } |
1256 | free(buf); | |
1257 | } | |
1258 | ||
4cabf858 | 1259 | static int tecmp0 (const void *_a, const void *_b) |
463acbe1 SP |
1260 | { |
1261 | struct tree_entry *a = *((struct tree_entry**)_a); | |
1262 | struct tree_entry *b = *((struct tree_entry**)_b); | |
1263 | return base_name_compare( | |
4cabf858 SP |
1264 | a->name->str_dat, a->name->str_len, a->versions[0].mode, |
1265 | b->name->str_dat, b->name->str_len, b->versions[0].mode); | |
463acbe1 SP |
1266 | } |
1267 | ||
4cabf858 | 1268 | static int tecmp1 (const void *_a, const void *_b) |
463acbe1 | 1269 | { |
4cabf858 SP |
1270 | struct tree_entry *a = *((struct tree_entry**)_a); |
1271 | struct tree_entry *b = *((struct tree_entry**)_b); | |
1272 | return base_name_compare( | |
1273 | a->name->str_dat, a->name->str_len, a->versions[1].mode, | |
1274 | b->name->str_dat, b->name->str_len, b->versions[1].mode); | |
1275 | } | |
1276 | ||
eec813cf | 1277 | static void mktree(struct tree_content *t, int v, struct strbuf *b) |
4cabf858 SP |
1278 | { |
1279 | size_t maxlen = 0; | |
463acbe1 | 1280 | unsigned int i; |
463acbe1 | 1281 | |
4cabf858 | 1282 | if (!v) |
9ed0d8d6 | 1283 | QSORT(t->entries, t->entry_count, tecmp0); |
4cabf858 | 1284 | else |
9ed0d8d6 | 1285 | QSORT(t->entries, t->entry_count, tecmp1); |
463acbe1 | 1286 | |
463acbe1 | 1287 | for (i = 0; i < t->entry_count; i++) { |
4cabf858 SP |
1288 | if (t->entries[i]->versions[v].mode) |
1289 | maxlen += t->entries[i]->name->str_len + 34; | |
463acbe1 SP |
1290 | } |
1291 | ||
eec813cf PH |
1292 | strbuf_reset(b); |
1293 | strbuf_grow(b, maxlen); | |
463acbe1 SP |
1294 | for (i = 0; i < t->entry_count; i++) { |
1295 | struct tree_entry *e = t->entries[i]; | |
4cabf858 SP |
1296 | if (!e->versions[v].mode) |
1297 | continue; | |
8fb3ad76 DI |
1298 | strbuf_addf(b, "%o %s%c", |
1299 | (unsigned int)(e->versions[v].mode & ~NO_DELTA), | |
1300 | e->name->str_dat, '\0'); | |
28d055bd | 1301 | strbuf_add(b, e->versions[v].oid.hash, the_hash_algo->rawsz); |
463acbe1 | 1302 | } |
4cabf858 SP |
1303 | } |
1304 | ||
1305 | static void store_tree(struct tree_entry *root) | |
1306 | { | |
2668d692 | 1307 | struct tree_content *t; |
4cabf858 | 1308 | unsigned int i, j, del; |
05576569 | 1309 | struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 }; |
8fb3ad76 | 1310 | struct object_entry *le = NULL; |
4cabf858 | 1311 | |
d7e6b6a8 | 1312 | if (!is_null_oid(&root->versions[1].oid)) |
4cabf858 SP |
1313 | return; |
1314 | ||
2668d692 MB |
1315 | if (!root->tree) |
1316 | load_tree(root); | |
1317 | t = root->tree; | |
1318 | ||
4cabf858 SP |
1319 | for (i = 0; i < t->entry_count; i++) { |
1320 | if (t->entries[i]->tree) | |
1321 | store_tree(t->entries[i]); | |
1322 | } | |
1323 | ||
8fb3ad76 | 1324 | if (!(root->versions[0].mode & NO_DELTA)) |
912c13d5 | 1325 | le = find_object(&root->versions[0].oid); |
05576569 | 1326 | if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) { |
eec813cf | 1327 | mktree(t, 0, &old_tree); |
05576569 | 1328 | lo.data = old_tree; |
3fc366bd | 1329 | lo.offset = le->idx.offset; |
4cabf858 | 1330 | lo.depth = t->delta_depth; |
4cabf858 | 1331 | } |
4cabf858 | 1332 | |
eec813cf | 1333 | mktree(t, 1, &new_tree); |
912c13d5 | 1334 | store_object(OBJ_TREE, &new_tree, &lo, &root->versions[1].oid, 0); |
4cabf858 SP |
1335 | |
1336 | t->delta_depth = lo.depth; | |
4cabf858 SP |
1337 | for (i = 0, j = 0, del = 0; i < t->entry_count; i++) { |
1338 | struct tree_entry *e = t->entries[i]; | |
1339 | if (e->versions[1].mode) { | |
1340 | e->versions[0].mode = e->versions[1].mode; | |
d7e6b6a8 | 1341 | oidcpy(&e->versions[0].oid, &e->versions[1].oid); |
4cabf858 SP |
1342 | t->entries[j++] = e; |
1343 | } else { | |
1344 | release_tree_entry(e); | |
1345 | del++; | |
1346 | } | |
1347 | } | |
1348 | t->entry_count -= del; | |
463acbe1 SP |
1349 | } |
1350 | ||
34215783 JN |
1351 | static void tree_content_replace( |
1352 | struct tree_entry *root, | |
912c13d5 | 1353 | const struct object_id *oid, |
34215783 JN |
1354 | const uint16_t mode, |
1355 | struct tree_content *newtree) | |
1356 | { | |
1357 | if (!S_ISDIR(mode)) | |
1358 | die("Root cannot be a non-directory"); | |
d7e6b6a8 | 1359 | oidclr(&root->versions[0].oid); |
912c13d5 | 1360 | oidcpy(&root->versions[1].oid, oid); |
34215783 JN |
1361 | if (root->tree) |
1362 | release_tree_content_recursive(root->tree); | |
1363 | root->tree = newtree; | |
1364 | } | |
1365 | ||
463acbe1 SP |
1366 | static int tree_content_set( |
1367 | struct tree_entry *root, | |
1368 | const char *p, | |
912c13d5 | 1369 | const struct object_id *oid, |
f39a946a SP |
1370 | const uint16_t mode, |
1371 | struct tree_content *subtree) | |
463acbe1 | 1372 | { |
5edde510 | 1373 | struct tree_content *t; |
463acbe1 SP |
1374 | const char *slash1; |
1375 | unsigned int i, n; | |
1376 | struct tree_entry *e; | |
1377 | ||
2c5495f7 RM |
1378 | slash1 = strchrnul(p, '/'); |
1379 | n = slash1 - p; | |
475d1b33 SP |
1380 | if (!n) |
1381 | die("Empty path component found in input"); | |
2c5495f7 | 1382 | if (!*slash1 && !S_ISDIR(mode) && subtree) |
f39a946a | 1383 | die("Non-directories cannot have subtrees"); |
463acbe1 | 1384 | |
5edde510 JN |
1385 | if (!root->tree) |
1386 | load_tree(root); | |
1387 | t = root->tree; | |
463acbe1 SP |
1388 | for (i = 0; i < t->entry_count; i++) { |
1389 | e = t->entries[i]; | |
ba0897e6 | 1390 | if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) { |
2c5495f7 | 1391 | if (!*slash1) { |
f39a946a SP |
1392 | if (!S_ISDIR(mode) |
1393 | && e->versions[1].mode == mode | |
4a7e27e9 | 1394 | && oideq(&e->versions[1].oid, oid)) |
463acbe1 | 1395 | return 0; |
4cabf858 | 1396 | e->versions[1].mode = mode; |
912c13d5 | 1397 | oidcpy(&e->versions[1].oid, oid); |
f39a946a | 1398 | if (e->tree) |
afde8dd9 | 1399 | release_tree_content_recursive(e->tree); |
f39a946a | 1400 | e->tree = subtree; |
8fb3ad76 DI |
1401 | |
1402 | /* | |
1403 | * We need to leave e->versions[0].sha1 alone | |
1404 | * to avoid modifying the preimage tree used | |
1405 | * when writing out the parent directory. | |
1406 | * But after replacing the subdir with a | |
1407 | * completely different one, it's not a good | |
1408 | * delta base any more, and besides, we've | |
1409 | * thrown away the tree entries needed to | |
1410 | * make a delta against it. | |
1411 | * | |
1412 | * So let's just explicitly disable deltas | |
1413 | * for the subtree. | |
1414 | */ | |
1415 | if (S_ISDIR(e->versions[0].mode)) | |
1416 | e->versions[0].mode |= NO_DELTA; | |
1417 | ||
d7e6b6a8 | 1418 | oidclr(&root->versions[1].oid); |
463acbe1 SP |
1419 | return 1; |
1420 | } | |
4cabf858 | 1421 | if (!S_ISDIR(e->versions[1].mode)) { |
463acbe1 | 1422 | e->tree = new_tree_content(8); |
4cabf858 | 1423 | e->versions[1].mode = S_IFDIR; |
463acbe1 SP |
1424 | } |
1425 | if (!e->tree) | |
1426 | load_tree(e); | |
912c13d5 | 1427 | if (tree_content_set(e, slash1 + 1, oid, mode, subtree)) { |
d7e6b6a8 | 1428 | oidclr(&root->versions[1].oid); |
463acbe1 SP |
1429 | return 1; |
1430 | } | |
1431 | return 0; | |
1432 | } | |
1433 | } | |
1434 | ||
1435 | if (t->entry_count == t->entry_capacity) | |
f022f85f | 1436 | root->tree = t = grow_tree_content(t, t->entry_count); |
463acbe1 | 1437 | e = new_tree_entry(); |
061e35c5 | 1438 | e->name = to_atom(p, n); |
4cabf858 | 1439 | e->versions[0].mode = 0; |
d7e6b6a8 | 1440 | oidclr(&e->versions[0].oid); |
463acbe1 | 1441 | t->entries[t->entry_count++] = e; |
2c5495f7 | 1442 | if (*slash1) { |
463acbe1 | 1443 | e->tree = new_tree_content(8); |
4cabf858 | 1444 | e->versions[1].mode = S_IFDIR; |
912c13d5 | 1445 | tree_content_set(e, slash1 + 1, oid, mode, subtree); |
463acbe1 | 1446 | } else { |
f39a946a | 1447 | e->tree = subtree; |
4cabf858 | 1448 | e->versions[1].mode = mode; |
912c13d5 | 1449 | oidcpy(&e->versions[1].oid, oid); |
463acbe1 | 1450 | } |
d7e6b6a8 | 1451 | oidclr(&root->versions[1].oid); |
463acbe1 SP |
1452 | return 1; |
1453 | } | |
1454 | ||
f39a946a SP |
1455 | static int tree_content_remove( |
1456 | struct tree_entry *root, | |
1457 | const char *p, | |
62bfa11c JK |
1458 | struct tree_entry *backup_leaf, |
1459 | int allow_root) | |
463acbe1 | 1460 | { |
5edde510 | 1461 | struct tree_content *t; |
463acbe1 SP |
1462 | const char *slash1; |
1463 | unsigned int i, n; | |
1464 | struct tree_entry *e; | |
1465 | ||
2c5495f7 RM |
1466 | slash1 = strchrnul(p, '/'); |
1467 | n = slash1 - p; | |
463acbe1 | 1468 | |
5edde510 JN |
1469 | if (!root->tree) |
1470 | load_tree(root); | |
62bfa11c JK |
1471 | |
1472 | if (!*p && allow_root) { | |
1473 | e = root; | |
1474 | goto del_entry; | |
1475 | } | |
1476 | ||
5edde510 | 1477 | t = root->tree; |
463acbe1 SP |
1478 | for (i = 0; i < t->entry_count; i++) { |
1479 | e = t->entries[i]; | |
ba0897e6 | 1480 | if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) { |
2c5495f7 | 1481 | if (*slash1 && !S_ISDIR(e->versions[1].mode)) |
253fb5f8 EN |
1482 | /* |
1483 | * If p names a file in some subdirectory, and a | |
1484 | * file or symlink matching the name of the | |
1485 | * parent directory of p exists, then p cannot | |
1486 | * exist and need not be deleted. | |
1487 | */ | |
1488 | return 1; | |
2c5495f7 | 1489 | if (!*slash1 || !S_ISDIR(e->versions[1].mode)) |
463acbe1 SP |
1490 | goto del_entry; |
1491 | if (!e->tree) | |
1492 | load_tree(e); | |
62bfa11c | 1493 | if (tree_content_remove(e, slash1 + 1, backup_leaf, 0)) { |
b54d6422 SP |
1494 | for (n = 0; n < e->tree->entry_count; n++) { |
1495 | if (e->tree->entries[n]->versions[1].mode) { | |
d7e6b6a8 | 1496 | oidclr(&root->versions[1].oid); |
b54d6422 SP |
1497 | return 1; |
1498 | } | |
1499 | } | |
f39a946a | 1500 | backup_leaf = NULL; |
b54d6422 | 1501 | goto del_entry; |
463acbe1 SP |
1502 | } |
1503 | return 0; | |
1504 | } | |
1505 | } | |
1506 | return 0; | |
1507 | ||
1508 | del_entry: | |
f39a946a SP |
1509 | if (backup_leaf) |
1510 | memcpy(backup_leaf, e, sizeof(*backup_leaf)); | |
1511 | else if (e->tree) | |
4cabf858 | 1512 | release_tree_content_recursive(e->tree); |
f39a946a | 1513 | e->tree = NULL; |
4cabf858 | 1514 | e->versions[1].mode = 0; |
d7e6b6a8 | 1515 | oidclr(&e->versions[1].oid); |
1516 | oidclr(&root->versions[1].oid); | |
ac47a738 | 1517 | return 1; |
db5e523f SP |
1518 | } |
1519 | ||
b6f3481b SP |
1520 | static int tree_content_get( |
1521 | struct tree_entry *root, | |
1522 | const char *p, | |
e0eb6b97 JK |
1523 | struct tree_entry *leaf, |
1524 | int allow_root) | |
b6f3481b | 1525 | { |
5edde510 | 1526 | struct tree_content *t; |
b6f3481b SP |
1527 | const char *slash1; |
1528 | unsigned int i, n; | |
1529 | struct tree_entry *e; | |
1530 | ||
2c5495f7 RM |
1531 | slash1 = strchrnul(p, '/'); |
1532 | n = slash1 - p; | |
e0eb6b97 | 1533 | if (!n && !allow_root) |
178e1dea | 1534 | die("Empty path component found in input"); |
b6f3481b | 1535 | |
5edde510 JN |
1536 | if (!root->tree) |
1537 | load_tree(root); | |
e0eb6b97 JK |
1538 | |
1539 | if (!n) { | |
1540 | e = root; | |
1541 | goto found_entry; | |
1542 | } | |
1543 | ||
5edde510 | 1544 | t = root->tree; |
b6f3481b SP |
1545 | for (i = 0; i < t->entry_count; i++) { |
1546 | e = t->entries[i]; | |
ba0897e6 | 1547 | if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) { |
2c5495f7 | 1548 | if (!*slash1) |
e0eb6b97 | 1549 | goto found_entry; |
b6f3481b SP |
1550 | if (!S_ISDIR(e->versions[1].mode)) |
1551 | return 0; | |
1552 | if (!e->tree) | |
1553 | load_tree(e); | |
e0eb6b97 | 1554 | return tree_content_get(e, slash1 + 1, leaf, 0); |
b6f3481b SP |
1555 | } |
1556 | } | |
1557 | return 0; | |
e0eb6b97 JK |
1558 | |
1559 | found_entry: | |
1560 | memcpy(leaf, e, sizeof(*leaf)); | |
d7e6b6a8 | 1561 | if (e->tree && is_null_oid(&e->versions[1].oid)) |
e0eb6b97 JK |
1562 | leaf->tree = dup_tree_content(e->tree); |
1563 | else | |
1564 | leaf->tree = NULL; | |
1565 | return 1; | |
b6f3481b SP |
1566 | } |
1567 | ||
7073e69e | 1568 | static int update_branch(struct branch *b) |
463acbe1 SP |
1569 | { |
1570 | static const char *msg = "fast-import"; | |
de7e86f5 | 1571 | struct ref_transaction *transaction; |
912c13d5 | 1572 | struct object_id old_oid; |
de7e86f5 | 1573 | struct strbuf err = STRBUF_INIT; |
7073e69e | 1574 | |
d7e6b6a8 | 1575 | if (is_null_oid(&b->oid)) { |
4ee1b225 | 1576 | if (b->delete) |
755b49ae | 1577 | delete_ref(NULL, b->name, NULL, 0); |
4ee1b225 FC |
1578 | return 0; |
1579 | } | |
34c290a6 | 1580 | if (read_ref(b->name, &old_oid)) |
912c13d5 | 1581 | oidclr(&old_oid); |
1582 | if (!force_update && !is_null_oid(&old_oid)) { | |
7073e69e SP |
1583 | struct commit *old_cmit, *new_cmit; |
1584 | ||
21e1ee8f SB |
1585 | old_cmit = lookup_commit_reference_gently(the_repository, |
1586 | &old_oid, 0); | |
1587 | new_cmit = lookup_commit_reference_gently(the_repository, | |
1588 | &b->oid, 0); | |
de7e86f5 | 1589 | if (!old_cmit || !new_cmit) |
7073e69e | 1590 | return error("Branch %s is missing commits.", b->name); |
7073e69e | 1591 | |
a20efee9 | 1592 | if (!in_merge_bases(old_cmit, new_cmit)) { |
46efd2d9 | 1593 | warning("Not updating %s" |
7073e69e | 1594 | " (new tip %s does not contain %s)", |
d7e6b6a8 | 1595 | b->name, oid_to_hex(&b->oid), |
912c13d5 | 1596 | oid_to_hex(&old_oid)); |
7073e69e SP |
1597 | return -1; |
1598 | } | |
1599 | } | |
de7e86f5 RS |
1600 | transaction = ref_transaction_begin(&err); |
1601 | if (!transaction || | |
89f3bbdd | 1602 | ref_transaction_update(transaction, b->name, &b->oid, &old_oid, |
1d147bdf | 1603 | 0, msg, &err) || |
db7516ab | 1604 | ref_transaction_commit(transaction, &err)) { |
de7e86f5 RS |
1605 | ref_transaction_free(transaction); |
1606 | error("%s", err.buf); | |
1607 | strbuf_release(&err); | |
1608 | return -1; | |
1609 | } | |
1610 | ref_transaction_free(transaction); | |
1611 | strbuf_release(&err); | |
7073e69e SP |
1612 | return 0; |
1613 | } | |
1614 | ||
1615 | static void dump_branches(void) | |
1616 | { | |
463acbe1 SP |
1617 | unsigned int i; |
1618 | struct branch *b; | |
463acbe1 SP |
1619 | |
1620 | for (i = 0; i < branch_table_sz; i++) { | |
7073e69e SP |
1621 | for (b = branch_table[i]; b; b = b->table_next_branch) |
1622 | failure |= update_branch(b); | |
463acbe1 SP |
1623 | } |
1624 | } | |
1625 | ||
fd99224e | 1626 | static void dump_tags(void) |
72303d44 SP |
1627 | { |
1628 | static const char *msg = "fast-import"; | |
1629 | struct tag *t; | |
3f09ba75 RS |
1630 | struct strbuf ref_name = STRBUF_INIT; |
1631 | struct strbuf err = STRBUF_INIT; | |
1632 | struct ref_transaction *transaction; | |
72303d44 | 1633 | |
3f09ba75 RS |
1634 | transaction = ref_transaction_begin(&err); |
1635 | if (!transaction) { | |
1636 | failure |= error("%s", err.buf); | |
1637 | goto cleanup; | |
1638 | } | |
72303d44 | 1639 | for (t = first_tag; t; t = t->next_tag) { |
3f09ba75 RS |
1640 | strbuf_reset(&ref_name); |
1641 | strbuf_addf(&ref_name, "refs/tags/%s", t->name); | |
1642 | ||
1d147bdf | 1643 | if (ref_transaction_update(transaction, ref_name.buf, |
89f3bbdd | 1644 | &t->oid, NULL, 0, msg, &err)) { |
3f09ba75 RS |
1645 | failure |= error("%s", err.buf); |
1646 | goto cleanup; | |
1647 | } | |
72303d44 | 1648 | } |
db7516ab | 1649 | if (ref_transaction_commit(transaction, &err)) |
3f09ba75 RS |
1650 | failure |= error("%s", err.buf); |
1651 | ||
1652 | cleanup: | |
1653 | ref_transaction_free(transaction); | |
1654 | strbuf_release(&ref_name); | |
1655 | strbuf_release(&err); | |
72303d44 SP |
1656 | } |
1657 | ||
a6a1a831 | 1658 | static void dump_marks_helper(FILE *f, |
0ea9f045 | 1659 | uintmax_t base, |
a6a1a831 SP |
1660 | struct mark_set *m) |
1661 | { | |
0ea9f045 | 1662 | uintmax_t k; |
a6a1a831 SP |
1663 | if (m->shift) { |
1664 | for (k = 0; k < 1024; k++) { | |
1665 | if (m->data.sets[k]) | |
7e7db5e4 | 1666 | dump_marks_helper(f, base + (k << m->shift), |
a6a1a831 SP |
1667 | m->data.sets[k]); |
1668 | } | |
1669 | } else { | |
1670 | for (k = 0; k < 1024; k++) { | |
1671 | if (m->data.marked[k]) | |
3efb1f34 | 1672 | fprintf(f, ":%" PRIuMAX " %s\n", base + k, |
e6a492b7 | 1673 | oid_to_hex(&m->data.marked[k]->idx.oid)); |
a6a1a831 SP |
1674 | } |
1675 | } | |
1676 | } | |
1677 | ||
fd99224e | 1678 | static void dump_marks(void) |
a6a1a831 | 1679 | { |
b2275868 | 1680 | struct lock_file mark_lock = LOCK_INIT; |
60b9004c SP |
1681 | FILE *f; |
1682 | ||
f4beed60 | 1683 | if (!export_marks_file || (import_marks_file && !import_marks_file_done)) |
60b9004c SP |
1684 | return; |
1685 | ||
01968302 JK |
1686 | if (safe_create_leading_directories_const(export_marks_file)) { |
1687 | failure |= error_errno("unable to create leading directories of %s", | |
1688 | export_marks_file); | |
1689 | return; | |
1690 | } | |
1691 | ||
f70f0565 | 1692 | if (hold_lock_file_for_update(&mark_lock, export_marks_file, 0) < 0) { |
6c223e49 NTND |
1693 | failure |= error_errno("Unable to write marks file %s", |
1694 | export_marks_file); | |
60b9004c | 1695 | return; |
a6a1a831 | 1696 | } |
60b9004c | 1697 | |
f70f0565 | 1698 | f = fdopen_lock_file(&mark_lock, "w"); |
60b9004c | 1699 | if (!f) { |
5a7b1b57 | 1700 | int saved_errno = errno; |
60b9004c SP |
1701 | rollback_lock_file(&mark_lock); |
1702 | failure |= error("Unable to write marks file %s: %s", | |
07cd9328 | 1703 | export_marks_file, strerror(saved_errno)); |
60b9004c | 1704 | return; |
a6a1a831 | 1705 | } |
60b9004c | 1706 | |
fb54abd6 | 1707 | dump_marks_helper(f, 0, marks); |
fb54abd6 | 1708 | if (commit_lock_file(&mark_lock)) { |
6c223e49 NTND |
1709 | failure |= error_errno("Unable to write file %s", |
1710 | export_marks_file); | |
fb54abd6 BC |
1711 | return; |
1712 | } | |
a6a1a831 SP |
1713 | } |
1714 | ||
07cd9328 SR |
1715 | static void read_marks(void) |
1716 | { | |
1717 | char line[512]; | |
1718 | FILE *f = fopen(import_marks_file, "r"); | |
dded4f12 RR |
1719 | if (f) |
1720 | ; | |
1721 | else if (import_marks_file_ignore_missing && errno == ENOENT) | |
f4beed60 | 1722 | goto done; /* Marks file does not exist */ |
dded4f12 | 1723 | else |
07cd9328 SR |
1724 | die_errno("cannot read '%s'", import_marks_file); |
1725 | while (fgets(line, sizeof(line), f)) { | |
1726 | uintmax_t mark; | |
1727 | char *end; | |
912c13d5 | 1728 | struct object_id oid; |
07cd9328 SR |
1729 | struct object_entry *e; |
1730 | ||
1731 | end = strchr(line, '\n'); | |
1732 | if (line[0] != ':' || !end) | |
1733 | die("corrupt mark line: %s", line); | |
1734 | *end = 0; | |
1735 | mark = strtoumax(line + 1, &end, 10); | |
1736 | if (!mark || end == line + 1 | |
912c13d5 | 1737 | || *end != ' ' || get_oid_hex(end + 1, &oid)) |
07cd9328 | 1738 | die("corrupt mark line: %s", line); |
912c13d5 | 1739 | e = find_object(&oid); |
07cd9328 | 1740 | if (!e) { |
0df8e965 SB |
1741 | enum object_type type = oid_object_info(the_repository, |
1742 | &oid, NULL); | |
07cd9328 | 1743 | if (type < 0) |
912c13d5 | 1744 | die("object not found: %s", oid_to_hex(&oid)); |
1745 | e = insert_object(&oid); | |
07cd9328 SR |
1746 | e->type = type; |
1747 | e->pack_id = MAX_PACK_ID; | |
3fc366bd | 1748 | e->idx.offset = 1; /* just not zero! */ |
07cd9328 SR |
1749 | } |
1750 | insert_mark(mark, e); | |
1751 | } | |
1752 | fclose(f); | |
f4beed60 FC |
1753 | done: |
1754 | import_marks_file_done = 1; | |
07cd9328 SR |
1755 | } |
1756 | ||
1757 | ||
e6c019d0 | 1758 | static int read_next_command(void) |
c44cdc7e | 1759 | { |
e6c019d0 PH |
1760 | static int stdin_eof = 0; |
1761 | ||
1762 | if (stdin_eof) { | |
1763 | unread_command_buf = 0; | |
1764 | return EOF; | |
1765 | } | |
1766 | ||
777f80d7 | 1767 | for (;;) { |
904b1941 | 1768 | if (unread_command_buf) { |
1fdb649c | 1769 | unread_command_buf = 0; |
904b1941 SP |
1770 | } else { |
1771 | struct recent_command *rc; | |
1772 | ||
b315c5c0 | 1773 | strbuf_detach(&command_buf, NULL); |
8f309aeb | 1774 | stdin_eof = strbuf_getline_lf(&command_buf, stdin); |
e6c019d0 PH |
1775 | if (stdin_eof) |
1776 | return EOF; | |
904b1941 | 1777 | |
f963bd5d | 1778 | if (!seen_data_command |
59556548 CC |
1779 | && !starts_with(command_buf.buf, "feature ") |
1780 | && !starts_with(command_buf.buf, "option ")) { | |
9c8398f0 | 1781 | parse_argv(); |
f963bd5d SR |
1782 | } |
1783 | ||
904b1941 SP |
1784 | rc = rc_free; |
1785 | if (rc) | |
1786 | rc_free = rc->next; | |
1787 | else { | |
1788 | rc = cmd_hist.next; | |
1789 | cmd_hist.next = rc->next; | |
1790 | cmd_hist.next->prev = &cmd_hist; | |
1791 | free(rc->buf); | |
1792 | } | |
1793 | ||
1794 | rc->buf = command_buf.buf; | |
1795 | rc->prev = cmd_tail; | |
1796 | rc->next = cmd_hist.prev; | |
1797 | rc->prev->next = rc; | |
1798 | cmd_tail = rc; | |
1799 | } | |
777f80d7 JN |
1800 | if (command_buf.buf[0] == '#') |
1801 | continue; | |
1802 | return 0; | |
1803 | } | |
c44cdc7e SP |
1804 | } |
1805 | ||
7e5dcea8 | 1806 | static void skip_optional_lf(void) |
2c570cde SP |
1807 | { |
1808 | int term_char = fgetc(stdin); | |
1809 | if (term_char != '\n' && term_char != EOF) | |
1810 | ungetc(term_char, stdin); | |
1811 | } | |
1812 | ||
b3031781 | 1813 | static void parse_mark(void) |
c44cdc7e | 1814 | { |
ae021d87 JK |
1815 | const char *v; |
1816 | if (skip_prefix(command_buf.buf, "mark :", &v)) { | |
1817 | next_mark = strtoumax(v, NULL, 10); | |
c44cdc7e SP |
1818 | read_next_command(); |
1819 | } | |
1820 | else | |
d8397168 | 1821 | next_mark = 0; |
c44cdc7e SP |
1822 | } |
1823 | ||
a965bb31 EN |
1824 | static void parse_original_identifier(void) |
1825 | { | |
1826 | const char *v; | |
1827 | if (skip_prefix(command_buf.buf, "original-oid ", &v)) | |
1828 | read_next_command(); | |
1829 | } | |
1830 | ||
5eef828b | 1831 | static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res) |
c44cdc7e | 1832 | { |
ae021d87 | 1833 | const char *data; |
eec813cf | 1834 | strbuf_reset(sb); |
c44cdc7e | 1835 | |
ae021d87 | 1836 | if (!skip_prefix(command_buf.buf, "data ", &data)) |
c44cdc7e SP |
1837 | die("Expected 'data n' command, found: %s", command_buf.buf); |
1838 | ||
ae021d87 JK |
1839 | if (skip_prefix(data, "<<", &data)) { |
1840 | char *term = xstrdup(data); | |
1841 | size_t term_len = command_buf.len - (data - command_buf.buf); | |
4a241d79 | 1842 | |
c2e6b6d0 | 1843 | strbuf_detach(&command_buf, NULL); |
3b4dce02 | 1844 | for (;;) { |
8f309aeb | 1845 | if (strbuf_getline_lf(&command_buf, stdin) == EOF) |
3b4dce02 SP |
1846 | die("EOF in data (terminator '%s' not found)", term); |
1847 | if (term_len == command_buf.len | |
1848 | && !strcmp(term, command_buf.buf)) | |
1849 | break; | |
eec813cf PH |
1850 | strbuf_addbuf(sb, &command_buf); |
1851 | strbuf_addch(sb, '\n'); | |
3b4dce02 SP |
1852 | } |
1853 | free(term); | |
1854 | } | |
1855 | else { | |
ae021d87 | 1856 | uintmax_t len = strtoumax(data, NULL, 10); |
5eef828b | 1857 | size_t n = 0, length = (size_t)len; |
4a241d79 | 1858 | |
5eef828b SP |
1859 | if (limit && limit < len) { |
1860 | *len_res = len; | |
1861 | return 0; | |
1862 | } | |
1863 | if (length < len) | |
1864 | die("data is too large to use in this context"); | |
4a241d79 | 1865 | |
3b4dce02 | 1866 | while (n < length) { |
eec813cf | 1867 | size_t s = strbuf_fread(sb, length - n, stdin); |
3b4dce02 | 1868 | if (!s && feof(stdin)) |
40db58b8 JS |
1869 | die("EOF in data (%lu bytes remaining)", |
1870 | (unsigned long)(length - n)); | |
3b4dce02 SP |
1871 | n += s; |
1872 | } | |
c44cdc7e SP |
1873 | } |
1874 | ||
2c570cde | 1875 | skip_optional_lf(); |
5eef828b | 1876 | return 1; |
c44cdc7e SP |
1877 | } |
1878 | ||
c33ddc2e | 1879 | static int validate_raw_date(const char *src, struct strbuf *result) |
63e0c8b3 SP |
1880 | { |
1881 | const char *orig_src = src; | |
eb3a9dd3 | 1882 | char *endp; |
1cd749cc | 1883 | unsigned long num; |
63e0c8b3 | 1884 | |
c55fae43 RS |
1885 | errno = 0; |
1886 | ||
1cd749cc JH |
1887 | num = strtoul(src, &endp, 10); |
1888 | /* NEEDSWORK: perhaps check for reasonable values? */ | |
c55fae43 | 1889 | if (errno || endp == src || *endp != ' ') |
63e0c8b3 SP |
1890 | return -1; |
1891 | ||
1892 | src = endp + 1; | |
1893 | if (*src != '-' && *src != '+') | |
1894 | return -1; | |
63e0c8b3 | 1895 | |
1cd749cc | 1896 | num = strtoul(src + 1, &endp, 10); |
c33ddc2e | 1897 | if (errno || endp == src + 1 || *endp || 1400 < num) |
63e0c8b3 SP |
1898 | return -1; |
1899 | ||
c33ddc2e | 1900 | strbuf_addstr(result, orig_src); |
63e0c8b3 SP |
1901 | return 0; |
1902 | } | |
1903 | ||
1904 | static char *parse_ident(const char *buf) | |
1905 | { | |
4b4963c0 | 1906 | const char *ltgt; |
63e0c8b3 | 1907 | size_t name_len; |
c33ddc2e | 1908 | struct strbuf ident = STRBUF_INIT; |
63e0c8b3 | 1909 | |
17fb0072 DI |
1910 | /* ensure there is a space delimiter even if there is no name */ |
1911 | if (*buf == '<') | |
1912 | --buf; | |
1913 | ||
4b4963c0 DI |
1914 | ltgt = buf + strcspn(buf, "<>"); |
1915 | if (*ltgt != '<') | |
1916 | die("Missing < in ident string: %s", buf); | |
1917 | if (ltgt != buf && ltgt[-1] != ' ') | |
1918 | die("Missing space before < in ident string: %s", buf); | |
1919 | ltgt = ltgt + 1 + strcspn(ltgt + 1, "<>"); | |
1920 | if (*ltgt != '>') | |
63e0c8b3 | 1921 | die("Missing > in ident string: %s", buf); |
4b4963c0 DI |
1922 | ltgt++; |
1923 | if (*ltgt != ' ') | |
63e0c8b3 | 1924 | die("Missing space after > in ident string: %s", buf); |
4b4963c0 DI |
1925 | ltgt++; |
1926 | name_len = ltgt - buf; | |
c33ddc2e | 1927 | strbuf_add(&ident, buf, name_len); |
63e0c8b3 SP |
1928 | |
1929 | switch (whenspec) { | |
1930 | case WHENSPEC_RAW: | |
c33ddc2e | 1931 | if (validate_raw_date(ltgt, &ident) < 0) |
4b4963c0 | 1932 | die("Invalid raw date \"%s\" in ident: %s", ltgt, buf); |
63e0c8b3 SP |
1933 | break; |
1934 | case WHENSPEC_RFC2822: | |
c33ddc2e | 1935 | if (parse_date(ltgt, &ident) < 0) |
4b4963c0 | 1936 | die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt, buf); |
63e0c8b3 SP |
1937 | break; |
1938 | case WHENSPEC_NOW: | |
4b4963c0 | 1939 | if (strcmp("now", ltgt)) |
63e0c8b3 | 1940 | die("Date in ident must be 'now': %s", buf); |
c33ddc2e | 1941 | datestamp(&ident); |
63e0c8b3 SP |
1942 | break; |
1943 | } | |
1944 | ||
c33ddc2e | 1945 | return strbuf_detach(&ident, NULL); |
63e0c8b3 SP |
1946 | } |
1947 | ||
5eef828b SP |
1948 | static void parse_and_store_blob( |
1949 | struct last_object *last, | |
912c13d5 | 1950 | struct object_id *oidout, |
5eef828b | 1951 | uintmax_t mark) |
6143f064 | 1952 | { |
05576569 | 1953 | static struct strbuf buf = STRBUF_INIT; |
5eef828b | 1954 | uintmax_t len; |
c44cdc7e | 1955 | |
5eef828b | 1956 | if (parse_data(&buf, big_file_threshold, &len)) |
912c13d5 | 1957 | store_object(OBJ_BLOB, &buf, last, oidout, mark); |
5eef828b SP |
1958 | else { |
1959 | if (last) { | |
1960 | strbuf_release(&last->data); | |
1961 | last->offset = 0; | |
1962 | last->depth = 0; | |
1963 | } | |
912c13d5 | 1964 | stream_blob(len, oidout, mark); |
5eef828b SP |
1965 | skip_optional_lf(); |
1966 | } | |
1967 | } | |
1968 | ||
1969 | static void parse_new_blob(void) | |
1970 | { | |
c44cdc7e | 1971 | read_next_command(); |
b3031781 | 1972 | parse_mark(); |
a965bb31 | 1973 | parse_original_identifier(); |
5eef828b | 1974 | parse_and_store_blob(&last_blob, NULL, next_mark); |
6143f064 SP |
1975 | } |
1976 | ||
fd99224e | 1977 | static void unload_one_branch(void) |
6bb5b329 | 1978 | { |
41e5257f SP |
1979 | while (cur_active_branches |
1980 | && cur_active_branches >= max_active_branches) { | |
6777a59f | 1981 | uintmax_t min_commit = ULONG_MAX; |
463acbe1 SP |
1982 | struct branch *e, *l = NULL, *p = NULL; |
1983 | ||
1984 | for (e = active_branches; e; e = e->active_next_branch) { | |
1985 | if (e->last_commit < min_commit) { | |
1986 | p = l; | |
1987 | min_commit = e->last_commit; | |
1988 | } | |
1989 | l = e; | |
1990 | } | |
1991 | ||
1992 | if (p) { | |
1993 | e = p->active_next_branch; | |
1994 | p->active_next_branch = e->active_next_branch; | |
1995 | } else { | |
1996 | e = active_branches; | |
1997 | active_branches = e->active_next_branch; | |
1998 | } | |
734c91f9 | 1999 | e->active = 0; |
463acbe1 SP |
2000 | e->active_next_branch = NULL; |
2001 | if (e->branch_tree.tree) { | |
afde8dd9 | 2002 | release_tree_content_recursive(e->branch_tree.tree); |
463acbe1 SP |
2003 | e->branch_tree.tree = NULL; |
2004 | } | |
2005 | cur_active_branches--; | |
6bb5b329 | 2006 | } |
6bb5b329 SP |
2007 | } |
2008 | ||
463acbe1 | 2009 | static void load_branch(struct branch *b) |
6bb5b329 | 2010 | { |
463acbe1 | 2011 | load_tree(&b->branch_tree); |
734c91f9 SP |
2012 | if (!b->active) { |
2013 | b->active = 1; | |
2014 | b->active_next_branch = active_branches; | |
2015 | active_branches = b; | |
2016 | cur_active_branches++; | |
2017 | branch_load_count++; | |
2018 | } | |
6bb5b329 SP |
2019 | } |
2020 | ||
2a113aee JH |
2021 | static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes) |
2022 | { | |
2023 | unsigned char fanout = 0; | |
2024 | while ((num_notes >>= 8)) | |
2025 | fanout++; | |
2026 | return fanout; | |
2027 | } | |
2028 | ||
2029 | static void construct_path_with_fanout(const char *hex_sha1, | |
2030 | unsigned char fanout, char *path) | |
2031 | { | |
2032 | unsigned int i = 0, j = 0; | |
7f89428d | 2033 | if (fanout >= the_hash_algo->rawsz) |
2a113aee JH |
2034 | die("Too large fanout (%u)", fanout); |
2035 | while (fanout) { | |
2036 | path[i++] = hex_sha1[j++]; | |
2037 | path[i++] = hex_sha1[j++]; | |
2038 | path[i++] = '/'; | |
2039 | fanout--; | |
2040 | } | |
7f89428d | 2041 | memcpy(path + i, hex_sha1 + j, the_hash_algo->hexsz - j); |
2042 | path[i + the_hash_algo->hexsz - j] = '\0'; | |
2a113aee JH |
2043 | } |
2044 | ||
2045 | static uintmax_t do_change_note_fanout( | |
2046 | struct tree_entry *orig_root, struct tree_entry *root, | |
912c13d5 | 2047 | char *hex_oid, unsigned int hex_oid_len, |
2a113aee JH |
2048 | char *fullpath, unsigned int fullpath_len, |
2049 | unsigned char fanout) | |
2050 | { | |
405d7f4a | 2051 | struct tree_content *t; |
2a113aee | 2052 | struct tree_entry *e, leaf; |
912c13d5 | 2053 | unsigned int i, tmp_hex_oid_len, tmp_fullpath_len; |
2a113aee | 2054 | uintmax_t num_notes = 0; |
912c13d5 | 2055 | struct object_id oid; |
28d055bd | 2056 | /* hex oid + '/' between each pair of hex digits + NUL */ |
2057 | char realpath[GIT_MAX_HEXSZ + ((GIT_MAX_HEXSZ / 2) - 1) + 1]; | |
2058 | const unsigned hexsz = the_hash_algo->hexsz; | |
2a113aee | 2059 | |
405d7f4a MH |
2060 | if (!root->tree) |
2061 | load_tree(root); | |
2062 | t = root->tree; | |
2063 | ||
2a113aee JH |
2064 | for (i = 0; t && i < t->entry_count; i++) { |
2065 | e = t->entries[i]; | |
912c13d5 | 2066 | tmp_hex_oid_len = hex_oid_len + e->name->str_len; |
2a113aee JH |
2067 | tmp_fullpath_len = fullpath_len; |
2068 | ||
2069 | /* | |
2070 | * We're interested in EITHER existing note entries (entries | |
2071 | * with exactly 40 hex chars in path, not including directory | |
2072 | * separators), OR directory entries that may contain note | |
2073 | * entries (with < 40 hex chars in path). | |
2074 | * Also, each path component in a note entry must be a multiple | |
2075 | * of 2 chars. | |
2076 | */ | |
2077 | if (!e->versions[1].mode || | |
28d055bd | 2078 | tmp_hex_oid_len > hexsz || |
2a113aee JH |
2079 | e->name->str_len % 2) |
2080 | continue; | |
2081 | ||
2082 | /* This _may_ be a note entry, or a subdir containing notes */ | |
912c13d5 | 2083 | memcpy(hex_oid + hex_oid_len, e->name->str_dat, |
2a113aee JH |
2084 | e->name->str_len); |
2085 | if (tmp_fullpath_len) | |
2086 | fullpath[tmp_fullpath_len++] = '/'; | |
2087 | memcpy(fullpath + tmp_fullpath_len, e->name->str_dat, | |
2088 | e->name->str_len); | |
2089 | tmp_fullpath_len += e->name->str_len; | |
2090 | fullpath[tmp_fullpath_len] = '\0'; | |
2091 | ||
28d055bd | 2092 | if (tmp_hex_oid_len == hexsz && !get_oid_hex(hex_oid, &oid)) { |
2a113aee | 2093 | /* This is a note entry */ |
18386857 JH |
2094 | if (fanout == 0xff) { |
2095 | /* Counting mode, no rename */ | |
2096 | num_notes++; | |
2097 | continue; | |
2098 | } | |
912c13d5 | 2099 | construct_path_with_fanout(hex_oid, fanout, realpath); |
2a113aee JH |
2100 | if (!strcmp(fullpath, realpath)) { |
2101 | /* Note entry is in correct location */ | |
2102 | num_notes++; | |
2103 | continue; | |
2104 | } | |
2105 | ||
2106 | /* Rename fullpath to realpath */ | |
62bfa11c | 2107 | if (!tree_content_remove(orig_root, fullpath, &leaf, 0)) |
2a113aee JH |
2108 | die("Failed to remove path %s", fullpath); |
2109 | tree_content_set(orig_root, realpath, | |
912c13d5 | 2110 | &leaf.versions[1].oid, |
2a113aee JH |
2111 | leaf.versions[1].mode, |
2112 | leaf.tree); | |
2113 | } else if (S_ISDIR(e->versions[1].mode)) { | |
2114 | /* This is a subdir that may contain note entries */ | |
2a113aee | 2115 | num_notes += do_change_note_fanout(orig_root, e, |
912c13d5 | 2116 | hex_oid, tmp_hex_oid_len, |
2a113aee JH |
2117 | fullpath, tmp_fullpath_len, fanout); |
2118 | } | |
2119 | ||
2120 | /* The above may have reallocated the current tree_content */ | |
2121 | t = root->tree; | |
2122 | } | |
2123 | return num_notes; | |
2124 | } | |
2125 | ||
2126 | static uintmax_t change_note_fanout(struct tree_entry *root, | |
2127 | unsigned char fanout) | |
2128 | { | |
912c13d5 | 2129 | /* |
2130 | * The size of path is due to one slash between every two hex digits, | |
2131 | * plus the terminating NUL. Note that there is no slash at the end, so | |
2132 | * the number of slashes is one less than half the number of hex | |
2133 | * characters. | |
2134 | */ | |
2135 | char hex_oid[GIT_MAX_HEXSZ], path[GIT_MAX_HEXSZ + (GIT_MAX_HEXSZ / 2) - 1 + 1]; | |
2136 | return do_change_note_fanout(root, root, hex_oid, 0, path, 0, fanout); | |
2a113aee JH |
2137 | } |
2138 | ||
06454cb9 PW |
2139 | /* |
2140 | * Given a pointer into a string, parse a mark reference: | |
2141 | * | |
2142 | * idnum ::= ':' bigint; | |
2143 | * | |
2144 | * Return the first character after the value in *endptr. | |
2145 | * | |
2146 | * Complain if the following character is not what is expected, | |
2147 | * either a space or end of the string. | |
2148 | */ | |
2149 | static uintmax_t parse_mark_ref(const char *p, char **endptr) | |
2150 | { | |
2151 | uintmax_t mark; | |
2152 | ||
2153 | assert(*p == ':'); | |
2154 | p++; | |
2155 | mark = strtoumax(p, endptr, 10); | |
2156 | if (*endptr == p) | |
2157 | die("No value after ':' in mark: %s", command_buf.buf); | |
2158 | return mark; | |
2159 | } | |
2160 | ||
2161 | /* | |
2162 | * Parse the mark reference, and complain if this is not the end of | |
2163 | * the string. | |
2164 | */ | |
2165 | static uintmax_t parse_mark_ref_eol(const char *p) | |
2166 | { | |
2167 | char *end; | |
2168 | uintmax_t mark; | |
2169 | ||
2170 | mark = parse_mark_ref(p, &end); | |
2171 | if (*end != '\0') | |
2172 | die("Garbage after mark: %s", command_buf.buf); | |
2173 | return mark; | |
2174 | } | |
2175 | ||
2176 | /* | |
2177 | * Parse the mark reference, demanding a trailing space. Return a | |
2178 | * pointer to the space. | |
2179 | */ | |
2180 | static uintmax_t parse_mark_ref_space(const char **p) | |
2181 | { | |
2182 | uintmax_t mark; | |
2183 | char *end; | |
2184 | ||
2185 | mark = parse_mark_ref(*p, &end); | |
e814c39c | 2186 | if (*end++ != ' ') |
06454cb9 PW |
2187 | die("Missing space after mark: %s", command_buf.buf); |
2188 | *p = end; | |
2189 | return mark; | |
2190 | } | |
2191 | ||
97313bef | 2192 | static void file_change_m(const char *p, struct branch *b) |
6bb5b329 | 2193 | { |
7fb1011e | 2194 | static struct strbuf uq = STRBUF_INIT; |
c44cdc7e | 2195 | const char *endp; |
3aa99df8 | 2196 | struct object_entry *oe; |
912c13d5 | 2197 | struct object_id oid; |
10831c55 | 2198 | uint16_t mode, inline_data = 0; |
6bb5b329 | 2199 | |
c44cdc7e SP |
2200 | p = get_mode(p, &mode); |
2201 | if (!p) | |
2202 | die("Corrupt mode: %s", command_buf.buf); | |
2203 | switch (mode) { | |
3d1d81eb FC |
2204 | case 0644: |
2205 | case 0755: | |
2206 | mode |= S_IFREG; | |
c44cdc7e SP |
2207 | case S_IFREG | 0644: |
2208 | case S_IFREG | 0755: | |
ace4a9d1 | 2209 | case S_IFLNK: |
334fba65 | 2210 | case S_IFDIR: |
03db4525 | 2211 | case S_IFGITLINK: |
c44cdc7e SP |
2212 | /* ok */ |
2213 | break; | |
2214 | default: | |
2215 | die("Corrupt mode: %s", command_buf.buf); | |
2216 | } | |
2217 | ||
d8397168 | 2218 | if (*p == ':') { |
06454cb9 | 2219 | oe = find_mark(parse_mark_ref_space(&p)); |
e6a492b7 | 2220 | oidcpy(&oid, &oe->idx.oid); |
e814c39c | 2221 | } else if (skip_prefix(p, "inline ", &p)) { |
b715cfbb | 2222 | inline_data = 1; |
3aa99df8 | 2223 | oe = NULL; /* not used with inline_data, but makes gcc happy */ |
d8397168 | 2224 | } else { |
912c13d5 | 2225 | if (parse_oid_hex(p, &oid, &p)) |
06454cb9 | 2226 | die("Invalid dataref: %s", command_buf.buf); |
912c13d5 | 2227 | oe = find_object(&oid); |
e814c39c | 2228 | if (*p++ != ' ') |
06454cb9 | 2229 | die("Missing space after SHA1: %s", command_buf.buf); |
d8397168 | 2230 | } |
c44cdc7e | 2231 | |
7fb1011e PH |
2232 | strbuf_reset(&uq); |
2233 | if (!unquote_c_style(&uq, p, &endp)) { | |
c44cdc7e SP |
2234 | if (*endp) |
2235 | die("Garbage after path in: %s", command_buf.buf); | |
7fb1011e | 2236 | p = uq.buf; |
c44cdc7e | 2237 | } |
6bb5b329 | 2238 | |
8fe533f6 | 2239 | /* Git does not track empty, non-toplevel directories. */ |
912c13d5 | 2240 | if (S_ISDIR(mode) && is_empty_tree_oid(&oid) && *p) { |
62bfa11c | 2241 | tree_content_remove(&b->branch_tree, p, NULL, 0); |
8fe533f6 JN |
2242 | return; |
2243 | } | |
2244 | ||
03db4525 AG |
2245 | if (S_ISGITLINK(mode)) { |
2246 | if (inline_data) | |
2247 | die("Git links cannot be specified 'inline': %s", | |
2248 | command_buf.buf); | |
2249 | else if (oe) { | |
2250 | if (oe->type != OBJ_COMMIT) | |
2251 | die("Not a commit (actually a %s): %s", | |
debca9d2 | 2252 | type_name(oe->type), command_buf.buf); |
03db4525 AG |
2253 | } |
2254 | /* | |
2255 | * Accept the sha1 without checking; it expected to be in | |
2256 | * another repository. | |
2257 | */ | |
2258 | } else if (inline_data) { | |
334fba65 JN |
2259 | if (S_ISDIR(mode)) |
2260 | die("Directories cannot be specified 'inline': %s", | |
2261 | command_buf.buf); | |
7fb1011e PH |
2262 | if (p != uq.buf) { |
2263 | strbuf_addstr(&uq, p); | |
2264 | p = uq.buf; | |
2265 | } | |
7ffde293 EN |
2266 | while (read_next_command() != EOF) { |
2267 | const char *v; | |
2268 | if (skip_prefix(command_buf.buf, "cat-blob ", &v)) | |
2269 | parse_cat_blob(v); | |
2270 | else { | |
2271 | parse_and_store_blob(&last_blob, &oid, 0); | |
2272 | break; | |
2273 | } | |
2274 | } | |
7111feed | 2275 | } else { |
334fba65 JN |
2276 | enum object_type expected = S_ISDIR(mode) ? |
2277 | OBJ_TREE: OBJ_BLOB; | |
2278 | enum object_type type = oe ? oe->type : | |
0df8e965 SB |
2279 | oid_object_info(the_repository, &oid, |
2280 | NULL); | |
21666f1a | 2281 | if (type < 0) |
334fba65 JN |
2282 | die("%s not found: %s", |
2283 | S_ISDIR(mode) ? "Tree" : "Blob", | |
2284 | command_buf.buf); | |
2285 | if (type != expected) | |
2286 | die("Not a %s (actually a %s): %s", | |
debca9d2 | 2287 | type_name(expected), type_name(type), |
334fba65 | 2288 | command_buf.buf); |
7111feed | 2289 | } |
6bb5b329 | 2290 | |
34215783 | 2291 | if (!*p) { |
912c13d5 | 2292 | tree_content_replace(&b->branch_tree, &oid, mode, NULL); |
34215783 JN |
2293 | return; |
2294 | } | |
912c13d5 | 2295 | tree_content_set(&b->branch_tree, p, &oid, mode, NULL); |
463acbe1 | 2296 | } |
6bb5b329 | 2297 | |
97313bef | 2298 | static void file_change_d(const char *p, struct branch *b) |
463acbe1 | 2299 | { |
7fb1011e | 2300 | static struct strbuf uq = STRBUF_INIT; |
c44cdc7e SP |
2301 | const char *endp; |
2302 | ||
7fb1011e PH |
2303 | strbuf_reset(&uq); |
2304 | if (!unquote_c_style(&uq, p, &endp)) { | |
c44cdc7e SP |
2305 | if (*endp) |
2306 | die("Garbage after path in: %s", command_buf.buf); | |
7fb1011e | 2307 | p = uq.buf; |
c44cdc7e | 2308 | } |
62bfa11c | 2309 | tree_content_remove(&b->branch_tree, p, NULL, 1); |
6bb5b329 SP |
2310 | } |
2311 | ||
97313bef | 2312 | static void file_change_cr(const char *s, struct branch *b, int rename) |
f39a946a | 2313 | { |
97313bef | 2314 | const char *d; |
7fb1011e PH |
2315 | static struct strbuf s_uq = STRBUF_INIT; |
2316 | static struct strbuf d_uq = STRBUF_INIT; | |
f39a946a SP |
2317 | const char *endp; |
2318 | struct tree_entry leaf; | |
2319 | ||
7fb1011e PH |
2320 | strbuf_reset(&s_uq); |
2321 | if (!unquote_c_style(&s_uq, s, &endp)) { | |
f39a946a SP |
2322 | if (*endp != ' ') |
2323 | die("Missing space after source: %s", command_buf.buf); | |
7fb1011e | 2324 | } else { |
f39a946a SP |
2325 | endp = strchr(s, ' '); |
2326 | if (!endp) | |
2327 | die("Missing space after source: %s", command_buf.buf); | |
7fb1011e | 2328 | strbuf_add(&s_uq, s, endp - s); |
f39a946a | 2329 | } |
7fb1011e | 2330 | s = s_uq.buf; |
f39a946a SP |
2331 | |
2332 | endp++; | |
2333 | if (!*endp) | |
2334 | die("Missing dest: %s", command_buf.buf); | |
2335 | ||
2336 | d = endp; | |
7fb1011e PH |
2337 | strbuf_reset(&d_uq); |
2338 | if (!unquote_c_style(&d_uq, d, &endp)) { | |
f39a946a SP |
2339 | if (*endp) |
2340 | die("Garbage after dest in: %s", command_buf.buf); | |
7fb1011e | 2341 | d = d_uq.buf; |
f39a946a SP |
2342 | } |
2343 | ||
2344 | memset(&leaf, 0, sizeof(leaf)); | |
b6f3481b | 2345 | if (rename) |
62bfa11c | 2346 | tree_content_remove(&b->branch_tree, s, &leaf, 1); |
b6f3481b | 2347 | else |
e0eb6b97 | 2348 | tree_content_get(&b->branch_tree, s, &leaf, 1); |
f39a946a SP |
2349 | if (!leaf.versions[1].mode) |
2350 | die("Path %s not in branch", s); | |
34215783 JN |
2351 | if (!*d) { /* C "path/to/subdir" "" */ |
2352 | tree_content_replace(&b->branch_tree, | |
912c13d5 | 2353 | &leaf.versions[1].oid, |
34215783 JN |
2354 | leaf.versions[1].mode, |
2355 | leaf.tree); | |
2356 | return; | |
2357 | } | |
f39a946a | 2358 | tree_content_set(&b->branch_tree, d, |
912c13d5 | 2359 | &leaf.versions[1].oid, |
f39a946a SP |
2360 | leaf.versions[1].mode, |
2361 | leaf.tree); | |
f39a946a SP |
2362 | } |
2363 | ||
97313bef | 2364 | static void note_change_n(const char *p, struct branch *b, unsigned char *old_fanout) |
a8dd2e7d | 2365 | { |
a8dd2e7d | 2366 | static struct strbuf uq = STRBUF_INIT; |
cbfd5e1c | 2367 | struct object_entry *oe; |
a8dd2e7d | 2368 | struct branch *s; |
912c13d5 | 2369 | struct object_id oid, commit_oid; |
28d055bd | 2370 | char path[GIT_MAX_RAWSZ * 3]; |
a8dd2e7d | 2371 | uint16_t inline_data = 0; |
2a113aee | 2372 | unsigned char new_fanout; |
a8dd2e7d | 2373 | |
18386857 JH |
2374 | /* |
2375 | * When loading a branch, we don't traverse its tree to count the real | |
2376 | * number of notes (too expensive to do this for all non-note refs). | |
2377 | * This means that recently loaded notes refs might incorrectly have | |
2378 | * b->num_notes == 0, and consequently, old_fanout might be wrong. | |
2379 | * | |
2380 | * Fix this by traversing the tree and counting the number of notes | |
2381 | * when b->num_notes == 0. If the notes tree is truly empty, the | |
2382 | * calculation should not take long. | |
2383 | */ | |
2384 | if (b->num_notes == 0 && *old_fanout == 0) { | |
2385 | /* Invoke change_note_fanout() in "counting mode". */ | |
2386 | b->num_notes = change_note_fanout(&b->branch_tree, 0xff); | |
2387 | *old_fanout = convert_num_notes_to_fanout(b->num_notes); | |
2388 | } | |
2389 | ||
2390 | /* Now parse the notemodify command. */ | |
a8dd2e7d JH |
2391 | /* <dataref> or 'inline' */ |
2392 | if (*p == ':') { | |
06454cb9 | 2393 | oe = find_mark(parse_mark_ref_space(&p)); |
e6a492b7 | 2394 | oidcpy(&oid, &oe->idx.oid); |
e814c39c | 2395 | } else if (skip_prefix(p, "inline ", &p)) { |
a8dd2e7d | 2396 | inline_data = 1; |
0a34594c | 2397 | oe = NULL; /* not used with inline_data, but makes gcc happy */ |
a8dd2e7d | 2398 | } else { |
912c13d5 | 2399 | if (parse_oid_hex(p, &oid, &p)) |
06454cb9 | 2400 | die("Invalid dataref: %s", command_buf.buf); |
912c13d5 | 2401 | oe = find_object(&oid); |
e814c39c | 2402 | if (*p++ != ' ') |
06454cb9 | 2403 | die("Missing space after SHA1: %s", command_buf.buf); |
a8dd2e7d | 2404 | } |
a8dd2e7d | 2405 | |
a8a5406a | 2406 | /* <commit-ish> */ |
a8dd2e7d JH |
2407 | s = lookup_branch(p); |
2408 | if (s) { | |
d7e6b6a8 | 2409 | if (is_null_oid(&s->oid)) |
0bc69881 | 2410 | die("Can't add a note on empty branch."); |
912c13d5 | 2411 | oidcpy(&commit_oid, &s->oid); |
a8dd2e7d | 2412 | } else if (*p == ':') { |
06454cb9 | 2413 | uintmax_t commit_mark = parse_mark_ref_eol(p); |
a8dd2e7d JH |
2414 | struct object_entry *commit_oe = find_mark(commit_mark); |
2415 | if (commit_oe->type != OBJ_COMMIT) | |
2416 | die("Mark :%" PRIuMAX " not a commit", commit_mark); | |
e6a492b7 | 2417 | oidcpy(&commit_oid, &commit_oe->idx.oid); |
912c13d5 | 2418 | } else if (!get_oid(p, &commit_oid)) { |
a8dd2e7d | 2419 | unsigned long size; |
d3b4705a NTND |
2420 | char *buf = read_object_with_reference(the_repository, |
2421 | &commit_oid, | |
02f0547e | 2422 | commit_type, &size, |
2423 | &commit_oid); | |
28d055bd | 2424 | if (!buf || size < the_hash_algo->hexsz + 6) |
a8dd2e7d JH |
2425 | die("Not a valid commit: %s", p); |
2426 | free(buf); | |
2427 | } else | |
2428 | die("Invalid ref name or SHA1 expression: %s", p); | |
2429 | ||
2430 | if (inline_data) { | |
a8dd2e7d JH |
2431 | if (p != uq.buf) { |
2432 | strbuf_addstr(&uq, p); | |
2433 | p = uq.buf; | |
2434 | } | |
2435 | read_next_command(); | |
912c13d5 | 2436 | parse_and_store_blob(&last_blob, &oid, 0); |
a8dd2e7d JH |
2437 | } else if (oe) { |
2438 | if (oe->type != OBJ_BLOB) | |
2439 | die("Not a blob (actually a %s): %s", | |
debca9d2 | 2440 | type_name(oe->type), command_buf.buf); |
912c13d5 | 2441 | } else if (!is_null_oid(&oid)) { |
0df8e965 SB |
2442 | enum object_type type = oid_object_info(the_repository, &oid, |
2443 | NULL); | |
a8dd2e7d JH |
2444 | if (type < 0) |
2445 | die("Blob not found: %s", command_buf.buf); | |
2446 | if (type != OBJ_BLOB) | |
2447 | die("Not a blob (actually a %s): %s", | |
debca9d2 | 2448 | type_name(type), command_buf.buf); |
a8dd2e7d JH |
2449 | } |
2450 | ||
912c13d5 | 2451 | construct_path_with_fanout(oid_to_hex(&commit_oid), *old_fanout, path); |
62bfa11c | 2452 | if (tree_content_remove(&b->branch_tree, path, NULL, 0)) |
2a113aee JH |
2453 | b->num_notes--; |
2454 | ||
912c13d5 | 2455 | if (is_null_oid(&oid)) |
2a113aee JH |
2456 | return; /* nothing to insert */ |
2457 | ||
2458 | b->num_notes++; | |
2459 | new_fanout = convert_num_notes_to_fanout(b->num_notes); | |
912c13d5 | 2460 | construct_path_with_fanout(oid_to_hex(&commit_oid), new_fanout, path); |
2461 | tree_content_set(&b->branch_tree, path, &oid, S_IFREG | 0644, NULL); | |
a8dd2e7d JH |
2462 | } |
2463 | ||
825769a8 SP |
2464 | static void file_change_deleteall(struct branch *b) |
2465 | { | |
2466 | release_tree_content_recursive(b->branch_tree.tree); | |
d7e6b6a8 | 2467 | oidclr(&b->branch_tree.versions[0].oid); |
2468 | oidclr(&b->branch_tree.versions[1].oid); | |
825769a8 | 2469 | load_tree(&b->branch_tree); |
2a113aee | 2470 | b->num_notes = 0; |
825769a8 SP |
2471 | } |
2472 | ||
b3031781 | 2473 | static void parse_from_commit(struct branch *b, char *buf, unsigned long size) |
654aaa37 | 2474 | { |
28d055bd | 2475 | if (!buf || size < the_hash_algo->hexsz + 6) |
d7e6b6a8 | 2476 | die("Not a valid commit: %s", oid_to_hex(&b->oid)); |
654aaa37 | 2477 | if (memcmp("tree ", buf, 5) |
912c13d5 | 2478 | || get_oid_hex(buf + 5, &b->branch_tree.versions[1].oid)) |
d7e6b6a8 | 2479 | die("The commit %s is corrupt", oid_to_hex(&b->oid)); |
2480 | oidcpy(&b->branch_tree.versions[0].oid, | |
2481 | &b->branch_tree.versions[1].oid); | |
654aaa37 SP |
2482 | } |
2483 | ||
b3031781 | 2484 | static void parse_from_existing(struct branch *b) |
654aaa37 | 2485 | { |
d7e6b6a8 | 2486 | if (is_null_oid(&b->oid)) { |
2487 | oidclr(&b->branch_tree.versions[0].oid); | |
2488 | oidclr(&b->branch_tree.versions[1].oid); | |
654aaa37 SP |
2489 | } else { |
2490 | unsigned long size; | |
2491 | char *buf; | |
2492 | ||
d3b4705a NTND |
2493 | buf = read_object_with_reference(the_repository, |
2494 | &b->oid, commit_type, &size, | |
02f0547e | 2495 | &b->oid); |
b3031781 | 2496 | parse_from_commit(b, buf, size); |
654aaa37 SP |
2497 | free(buf); |
2498 | } | |
2499 | } | |
2500 | ||
b3031781 | 2501 | static int parse_from(struct branch *b) |
00e2b884 | 2502 | { |
6c3aac1c | 2503 | const char *from; |
00e2b884 | 2504 | struct branch *s; |
912c13d5 | 2505 | struct object_id oid; |
00e2b884 | 2506 | |
97313bef | 2507 | if (!skip_prefix(command_buf.buf, "from ", &from)) |
1fdb649c | 2508 | return 0; |
00e2b884 | 2509 | |
912c13d5 | 2510 | oidcpy(&oid, &b->branch_tree.versions[1].oid); |
00e2b884 | 2511 | |
00e2b884 SP |
2512 | s = lookup_branch(from); |
2513 | if (b == s) | |
2514 | die("Can't create a branch from itself: %s", b->name); | |
2515 | else if (s) { | |
912c13d5 | 2516 | struct object_id *t = &s->branch_tree.versions[1].oid; |
d7e6b6a8 | 2517 | oidcpy(&b->oid, &s->oid); |
912c13d5 | 2518 | oidcpy(&b->branch_tree.versions[0].oid, t); |
2519 | oidcpy(&b->branch_tree.versions[1].oid, t); | |
00e2b884 | 2520 | } else if (*from == ':') { |
06454cb9 | 2521 | uintmax_t idnum = parse_mark_ref_eol(from); |
00e2b884 | 2522 | struct object_entry *oe = find_mark(idnum); |
00e2b884 | 2523 | if (oe->type != OBJ_COMMIT) |
3efb1f34 | 2524 | die("Mark :%" PRIuMAX " not a commit", idnum); |
9001dc2a | 2525 | if (!oideq(&b->oid, &oe->idx.oid)) { |
e6a492b7 | 2526 | oidcpy(&b->oid, &oe->idx.oid); |
0df32457 MH |
2527 | if (oe->pack_id != MAX_PACK_ID) { |
2528 | unsigned long size; | |
2529 | char *buf = gfi_unpack_entry(oe, &size); | |
2530 | parse_from_commit(b, buf, size); | |
2531 | free(buf); | |
2532 | } else | |
2533 | parse_from_existing(b); | |
2534 | } | |
912c13d5 | 2535 | } else if (!get_oid(from, &b->oid)) { |
b3031781 | 2536 | parse_from_existing(b); |
d7e6b6a8 | 2537 | if (is_null_oid(&b->oid)) |
4ee1b225 FC |
2538 | b->delete = 1; |
2539 | } | |
654aaa37 | 2540 | else |
00e2b884 SP |
2541 | die("Invalid ref name or SHA1 expression: %s", from); |
2542 | ||
9001dc2a | 2543 | if (b->branch_tree.tree && !oideq(&oid, &b->branch_tree.versions[1].oid)) { |
0df32457 MH |
2544 | release_tree_content_recursive(b->branch_tree.tree); |
2545 | b->branch_tree.tree = NULL; | |
2546 | } | |
2547 | ||
00e2b884 | 2548 | read_next_command(); |
1fdb649c | 2549 | return 1; |
00e2b884 SP |
2550 | } |
2551 | ||
b3031781 | 2552 | static struct hash_list *parse_merge(unsigned int *count) |
62b6f483 | 2553 | { |
4db34cc1 | 2554 | struct hash_list *list = NULL, **tail = &list, *n; |
6c3aac1c | 2555 | const char *from; |
62b6f483 SP |
2556 | struct branch *s; |
2557 | ||
2558 | *count = 0; | |
97313bef | 2559 | while (skip_prefix(command_buf.buf, "merge ", &from)) { |
62b6f483 SP |
2560 | n = xmalloc(sizeof(*n)); |
2561 | s = lookup_branch(from); | |
2562 | if (s) | |
d7e6b6a8 | 2563 | oidcpy(&n->oid, &s->oid); |
62b6f483 | 2564 | else if (*from == ':') { |
06454cb9 | 2565 | uintmax_t idnum = parse_mark_ref_eol(from); |
62b6f483 SP |
2566 | struct object_entry *oe = find_mark(idnum); |
2567 | if (oe->type != OBJ_COMMIT) | |
3efb1f34 | 2568 | die("Mark :%" PRIuMAX " not a commit", idnum); |
e6a492b7 | 2569 | oidcpy(&n->oid, &oe->idx.oid); |
912c13d5 | 2570 | } else if (!get_oid(from, &n->oid)) { |
2f6dc35d | 2571 | unsigned long size; |
d3b4705a NTND |
2572 | char *buf = read_object_with_reference(the_repository, |
2573 | &n->oid, | |
02f0547e | 2574 | commit_type, |
2575 | &size, &n->oid); | |
28d055bd | 2576 | if (!buf || size < the_hash_algo->hexsz + 6) |
2f6dc35d SP |
2577 | die("Not a valid commit: %s", from); |
2578 | free(buf); | |
2579 | } else | |
62b6f483 SP |
2580 | die("Invalid ref name or SHA1 expression: %s", from); |
2581 | ||
2582 | n->next = NULL; | |
4db34cc1 JK |
2583 | *tail = n; |
2584 | tail = &n->next; | |
2585 | ||
10e8d688 | 2586 | (*count)++; |
62b6f483 SP |
2587 | read_next_command(); |
2588 | } | |
2589 | return list; | |
2590 | } | |
2591 | ||
97313bef | 2592 | static void parse_new_commit(const char *arg) |
6bb5b329 | 2593 | { |
eec813cf | 2594 | static struct strbuf msg = STRBUF_INIT; |
c44cdc7e | 2595 | struct branch *b; |
c44cdc7e SP |
2596 | char *author = NULL; |
2597 | char *committer = NULL; | |
3edfcc65 | 2598 | const char *encoding = NULL; |
62b6f483 SP |
2599 | struct hash_list *merge_list = NULL; |
2600 | unsigned int merge_count; | |
2a113aee | 2601 | unsigned char prev_fanout, new_fanout; |
ae021d87 | 2602 | const char *v; |
c44cdc7e | 2603 | |
97313bef | 2604 | b = lookup_branch(arg); |
463acbe1 | 2605 | if (!b) |
97313bef | 2606 | b = new_branch(arg); |
c44cdc7e SP |
2607 | |
2608 | read_next_command(); | |
b3031781 | 2609 | parse_mark(); |
a965bb31 | 2610 | parse_original_identifier(); |
ae021d87 JK |
2611 | if (skip_prefix(command_buf.buf, "author ", &v)) { |
2612 | author = parse_ident(v); | |
c44cdc7e SP |
2613 | read_next_command(); |
2614 | } | |
ae021d87 JK |
2615 | if (skip_prefix(command_buf.buf, "committer ", &v)) { |
2616 | committer = parse_ident(v); | |
c44cdc7e SP |
2617 | read_next_command(); |
2618 | } | |
2619 | if (!committer) | |
2620 | die("Expected committer but didn't get one"); | |
3edfcc65 EN |
2621 | if (skip_prefix(command_buf.buf, "encoding ", &encoding)) |
2622 | read_next_command(); | |
5eef828b | 2623 | parse_data(&msg, 0, NULL); |
02f3389d | 2624 | read_next_command(); |
b3031781 MV |
2625 | parse_from(b); |
2626 | merge_list = parse_merge(&merge_count); | |
c44cdc7e SP |
2627 | |
2628 | /* ensure the branch is active/loaded */ | |
41e5257f | 2629 | if (!b->branch_tree.tree || !max_active_branches) { |
463acbe1 SP |
2630 | unload_one_branch(); |
2631 | load_branch(b); | |
2632 | } | |
6bb5b329 | 2633 | |
2a113aee JH |
2634 | prev_fanout = convert_num_notes_to_fanout(b->num_notes); |
2635 | ||
463acbe1 | 2636 | /* file_change* */ |
e6c019d0 | 2637 | while (command_buf.len > 0) { |
97313bef JK |
2638 | if (skip_prefix(command_buf.buf, "M ", &v)) |
2639 | file_change_m(v, b); | |
2640 | else if (skip_prefix(command_buf.buf, "D ", &v)) | |
2641 | file_change_d(v, b); | |
2642 | else if (skip_prefix(command_buf.buf, "R ", &v)) | |
2643 | file_change_cr(v, b, 1); | |
2644 | else if (skip_prefix(command_buf.buf, "C ", &v)) | |
2645 | file_change_cr(v, b, 0); | |
2646 | else if (skip_prefix(command_buf.buf, "N ", &v)) | |
2647 | note_change_n(v, b, &prev_fanout); | |
825769a8 SP |
2648 | else if (!strcmp("deleteall", command_buf.buf)) |
2649 | file_change_deleteall(b); | |
97313bef JK |
2650 | else if (skip_prefix(command_buf.buf, "ls ", &v)) |
2651 | parse_ls(v, b); | |
7ffde293 EN |
2652 | else if (skip_prefix(command_buf.buf, "cat-blob ", &v)) |
2653 | parse_cat_blob(v); | |
1fdb649c SP |
2654 | else { |
2655 | unread_command_buf = 1; | |
2656 | break; | |
2657 | } | |
e6c019d0 PH |
2658 | if (read_next_command() == EOF) |
2659 | break; | |
6bb5b329 | 2660 | } |
6bb5b329 | 2661 | |
2a113aee JH |
2662 | new_fanout = convert_num_notes_to_fanout(b->num_notes); |
2663 | if (new_fanout != prev_fanout) | |
2664 | b->num_notes = change_note_fanout(&b->branch_tree, new_fanout); | |
2665 | ||
c44cdc7e | 2666 | /* build the tree and the commit */ |
463acbe1 | 2667 | store_tree(&b->branch_tree); |
d7e6b6a8 | 2668 | oidcpy(&b->branch_tree.versions[0].oid, |
2669 | &b->branch_tree.versions[1].oid); | |
eec813cf PH |
2670 | |
2671 | strbuf_reset(&new_data); | |
2672 | strbuf_addf(&new_data, "tree %s\n", | |
d7e6b6a8 | 2673 | oid_to_hex(&b->branch_tree.versions[1].oid)); |
2674 | if (!is_null_oid(&b->oid)) | |
2675 | strbuf_addf(&new_data, "parent %s\n", | |
2676 | oid_to_hex(&b->oid)); | |
62b6f483 SP |
2677 | while (merge_list) { |
2678 | struct hash_list *next = merge_list->next; | |
d7e6b6a8 | 2679 | strbuf_addf(&new_data, "parent %s\n", |
2680 | oid_to_hex(&merge_list->oid)); | |
62b6f483 SP |
2681 | free(merge_list); |
2682 | merge_list = next; | |
2683 | } | |
eec813cf PH |
2684 | strbuf_addf(&new_data, |
2685 | "author %s\n" | |
3edfcc65 | 2686 | "committer %s\n", |
eec813cf | 2687 | author ? author : committer, committer); |
3edfcc65 EN |
2688 | if (encoding) |
2689 | strbuf_addf(&new_data, | |
2690 | "encoding %s\n", | |
2691 | encoding); | |
2692 | strbuf_addch(&new_data, '\n'); | |
eec813cf | 2693 | strbuf_addbuf(&new_data, &msg); |
e7d06a4b | 2694 | free(author); |
c44cdc7e | 2695 | free(committer); |
c44cdc7e | 2696 | |
912c13d5 | 2697 | if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark)) |
69e74e74 | 2698 | b->pack_id = pack_id; |
463acbe1 | 2699 | b->last_commit = object_count_by_type[OBJ_COMMIT]; |
6bb5b329 SP |
2700 | } |
2701 | ||
97313bef | 2702 | static void parse_new_tag(const char *arg) |
72303d44 | 2703 | { |
eec813cf | 2704 | static struct strbuf msg = STRBUF_INIT; |
72303d44 SP |
2705 | const char *from; |
2706 | char *tagger; | |
2707 | struct branch *s; | |
72303d44 | 2708 | struct tag *t; |
0ea9f045 | 2709 | uintmax_t from_mark = 0; |
912c13d5 | 2710 | struct object_id oid; |
8db751a8 | 2711 | enum object_type type; |
ae021d87 | 2712 | const char *v; |
72303d44 | 2713 | |
96c47d14 | 2714 | t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag)); |
a8ea1b7a | 2715 | memset(t, 0, sizeof(struct tag)); |
97313bef | 2716 | t->name = pool_strdup(arg); |
72303d44 SP |
2717 | if (last_tag) |
2718 | last_tag->next_tag = t; | |
2719 | else | |
2720 | first_tag = t; | |
2721 | last_tag = t; | |
72303d44 SP |
2722 | read_next_command(); |
2723 | ||
2724 | /* from ... */ | |
97313bef | 2725 | if (!skip_prefix(command_buf.buf, "from ", &from)) |
72303d44 | 2726 | die("Expected from command, got %s", command_buf.buf); |
72303d44 SP |
2727 | s = lookup_branch(from); |
2728 | if (s) { | |
d7e6b6a8 | 2729 | if (is_null_oid(&s->oid)) |
2c9c8ee2 | 2730 | die("Can't tag an empty branch."); |
912c13d5 | 2731 | oidcpy(&oid, &s->oid); |
8db751a8 | 2732 | type = OBJ_COMMIT; |
72303d44 | 2733 | } else if (*from == ':') { |
10e8d688 | 2734 | struct object_entry *oe; |
06454cb9 | 2735 | from_mark = parse_mark_ref_eol(from); |
10e8d688 | 2736 | oe = find_mark(from_mark); |
8db751a8 | 2737 | type = oe->type; |
e6a492b7 | 2738 | oidcpy(&oid, &oe->idx.oid); |
912c13d5 | 2739 | } else if (!get_oid(from, &oid)) { |
2740 | struct object_entry *oe = find_object(&oid); | |
6c447f63 | 2741 | if (!oe) { |
0df8e965 | 2742 | type = oid_object_info(the_repository, &oid, NULL); |
6c447f63 DI |
2743 | if (type < 0) |
2744 | die("Not a valid object: %s", from); | |
2745 | } else | |
2746 | type = oe->type; | |
72303d44 SP |
2747 | } else |
2748 | die("Invalid ref name or SHA1 expression: %s", from); | |
72303d44 SP |
2749 | read_next_command(); |
2750 | ||
a965bb31 EN |
2751 | /* original-oid ... */ |
2752 | parse_original_identifier(); | |
2753 | ||
72303d44 | 2754 | /* tagger ... */ |
ae021d87 JK |
2755 | if (skip_prefix(command_buf.buf, "tagger ", &v)) { |
2756 | tagger = parse_ident(v); | |
88fbf67b JH |
2757 | read_next_command(); |
2758 | } else | |
2759 | tagger = NULL; | |
72303d44 SP |
2760 | |
2761 | /* tag payload/message */ | |
5eef828b | 2762 | parse_data(&msg, 0, NULL); |
72303d44 SP |
2763 | |
2764 | /* build the tag object */ | |
eec813cf | 2765 | strbuf_reset(&new_data); |
88fbf67b | 2766 | |
eec813cf | 2767 | strbuf_addf(&new_data, |
88fbf67b JH |
2768 | "object %s\n" |
2769 | "type %s\n" | |
2770 | "tag %s\n", | |
debca9d2 | 2771 | oid_to_hex(&oid), type_name(type), t->name); |
88fbf67b JH |
2772 | if (tagger) |
2773 | strbuf_addf(&new_data, | |
2774 | "tagger %s\n", tagger); | |
2775 | strbuf_addch(&new_data, '\n'); | |
eec813cf | 2776 | strbuf_addbuf(&new_data, &msg); |
72303d44 | 2777 | free(tagger); |
72303d44 | 2778 | |
912c13d5 | 2779 | if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, 0)) |
69e74e74 SP |
2780 | t->pack_id = MAX_PACK_ID; |
2781 | else | |
2782 | t->pack_id = pack_id; | |
72303d44 SP |
2783 | } |
2784 | ||
97313bef | 2785 | static void parse_reset_branch(const char *arg) |
5fced8dc SP |
2786 | { |
2787 | struct branch *b; | |
5fced8dc | 2788 | |
97313bef | 2789 | b = lookup_branch(arg); |
5fced8dc | 2790 | if (b) { |
d7e6b6a8 | 2791 | oidclr(&b->oid); |
2792 | oidclr(&b->branch_tree.versions[0].oid); | |
2793 | oidclr(&b->branch_tree.versions[1].oid); | |
5fced8dc SP |
2794 | if (b->branch_tree.tree) { |
2795 | release_tree_content_recursive(b->branch_tree.tree); | |
2796 | b->branch_tree.tree = NULL; | |
2797 | } | |
2798 | } | |
9938ffc5 | 2799 | else |
97313bef | 2800 | b = new_branch(arg); |
9938ffc5 | 2801 | read_next_command(); |
b3031781 | 2802 | parse_from(b); |
655e8515 | 2803 | if (command_buf.len > 0) |
1fdb649c | 2804 | unread_command_buf = 1; |
5fced8dc SP |
2805 | } |
2806 | ||
85c62395 DB |
2807 | static void cat_blob_write(const char *buf, unsigned long size) |
2808 | { | |
06f46f23 | 2809 | if (write_in_full(cat_blob_fd, buf, size) < 0) |
85c62395 DB |
2810 | die_errno("Write to frontend failed"); |
2811 | } | |
2812 | ||
912c13d5 | 2813 | static void cat_blob(struct object_entry *oe, struct object_id *oid) |
85c62395 DB |
2814 | { |
2815 | struct strbuf line = STRBUF_INIT; | |
2816 | unsigned long size; | |
2817 | enum object_type type = 0; | |
2818 | char *buf; | |
2819 | ||
2820 | if (!oe || oe->pack_id == MAX_PACK_ID) { | |
b4f5aca4 | 2821 | buf = read_object_file(oid, &type, &size); |
85c62395 DB |
2822 | } else { |
2823 | type = oe->type; | |
2824 | buf = gfi_unpack_entry(oe, &size); | |
2825 | } | |
2826 | ||
2827 | /* | |
2828 | * Output based on batch_one_object() from cat-file.c. | |
2829 | */ | |
2830 | if (type <= 0) { | |
2831 | strbuf_reset(&line); | |
912c13d5 | 2832 | strbuf_addf(&line, "%s missing\n", oid_to_hex(oid)); |
85c62395 DB |
2833 | cat_blob_write(line.buf, line.len); |
2834 | strbuf_release(&line); | |
2835 | free(buf); | |
2836 | return; | |
2837 | } | |
2838 | if (!buf) | |
912c13d5 | 2839 | die("Can't read object %s", oid_to_hex(oid)); |
85c62395 DB |
2840 | if (type != OBJ_BLOB) |
2841 | die("Object %s is a %s but a blob was expected.", | |
debca9d2 | 2842 | oid_to_hex(oid), type_name(type)); |
85c62395 | 2843 | strbuf_reset(&line); |
ca473cef TB |
2844 | strbuf_addf(&line, "%s %s %"PRIuMAX"\n", oid_to_hex(oid), |
2845 | type_name(type), (uintmax_t)size); | |
85c62395 DB |
2846 | cat_blob_write(line.buf, line.len); |
2847 | strbuf_release(&line); | |
2848 | cat_blob_write(buf, size); | |
2849 | cat_blob_write("\n", 1); | |
a7e9c341 DI |
2850 | if (oe && oe->pack_id == pack_id) { |
2851 | last_blob.offset = oe->idx.offset; | |
2852 | strbuf_attach(&last_blob.data, buf, size, size); | |
2853 | last_blob.depth = oe->depth; | |
2854 | } else | |
2855 | free(buf); | |
85c62395 DB |
2856 | } |
2857 | ||
28c7b1f7 MH |
2858 | static void parse_get_mark(const char *p) |
2859 | { | |
156e1782 | 2860 | struct object_entry *oe; |
912c13d5 | 2861 | char output[GIT_MAX_HEXSZ + 2]; |
28c7b1f7 MH |
2862 | |
2863 | /* get-mark SP <object> LF */ | |
2864 | if (*p != ':') | |
2865 | die("Not a mark: %s", p); | |
2866 | ||
2867 | oe = find_mark(parse_mark_ref_eol(p)); | |
2868 | if (!oe) | |
2869 | die("Unknown mark: %s", command_buf.buf); | |
2870 | ||
e6a492b7 | 2871 | xsnprintf(output, sizeof(output), "%s\n", oid_to_hex(&oe->idx.oid)); |
28d055bd | 2872 | cat_blob_write(output, the_hash_algo->hexsz + 1); |
28c7b1f7 MH |
2873 | } |
2874 | ||
97313bef | 2875 | static void parse_cat_blob(const char *p) |
85c62395 | 2876 | { |
156e1782 | 2877 | struct object_entry *oe; |
912c13d5 | 2878 | struct object_id oid; |
85c62395 DB |
2879 | |
2880 | /* cat-blob SP <object> LF */ | |
85c62395 | 2881 | if (*p == ':') { |
06454cb9 | 2882 | oe = find_mark(parse_mark_ref_eol(p)); |
85c62395 DB |
2883 | if (!oe) |
2884 | die("Unknown mark: %s", command_buf.buf); | |
e6a492b7 | 2885 | oidcpy(&oid, &oe->idx.oid); |
85c62395 | 2886 | } else { |
912c13d5 | 2887 | if (parse_oid_hex(p, &oid, &p)) |
06454cb9 | 2888 | die("Invalid dataref: %s", command_buf.buf); |
912c13d5 | 2889 | if (*p) |
85c62395 | 2890 | die("Garbage after SHA1: %s", command_buf.buf); |
912c13d5 | 2891 | oe = find_object(&oid); |
85c62395 DB |
2892 | } |
2893 | ||
912c13d5 | 2894 | cat_blob(oe, &oid); |
85c62395 DB |
2895 | } |
2896 | ||
8dc6a373 | 2897 | static struct object_entry *dereference(struct object_entry *oe, |
912c13d5 | 2898 | struct object_id *oid) |
8dc6a373 DB |
2899 | { |
2900 | unsigned long size; | |
6288e3e1 | 2901 | char *buf = NULL; |
28d055bd | 2902 | const unsigned hexsz = the_hash_algo->hexsz; |
2903 | ||
8dc6a373 | 2904 | if (!oe) { |
0df8e965 SB |
2905 | enum object_type type = oid_object_info(the_repository, oid, |
2906 | NULL); | |
8dc6a373 | 2907 | if (type < 0) |
912c13d5 | 2908 | die("object not found: %s", oid_to_hex(oid)); |
8dc6a373 | 2909 | /* cache it! */ |
912c13d5 | 2910 | oe = insert_object(oid); |
8dc6a373 DB |
2911 | oe->type = type; |
2912 | oe->pack_id = MAX_PACK_ID; | |
2913 | oe->idx.offset = 1; | |
2914 | } | |
2915 | switch (oe->type) { | |
2916 | case OBJ_TREE: /* easy case. */ | |
2917 | return oe; | |
2918 | case OBJ_COMMIT: | |
2919 | case OBJ_TAG: | |
2920 | break; | |
2921 | default: | |
bb8040f9 | 2922 | die("Not a tree-ish: %s", command_buf.buf); |
8dc6a373 DB |
2923 | } |
2924 | ||
2925 | if (oe->pack_id != MAX_PACK_ID) { /* in a pack being written */ | |
2926 | buf = gfi_unpack_entry(oe, &size); | |
2927 | } else { | |
2928 | enum object_type unused; | |
b4f5aca4 | 2929 | buf = read_object_file(oid, &unused, &size); |
8dc6a373 DB |
2930 | } |
2931 | if (!buf) | |
912c13d5 | 2932 | die("Can't load object %s", oid_to_hex(oid)); |
8dc6a373 DB |
2933 | |
2934 | /* Peel one layer. */ | |
2935 | switch (oe->type) { | |
2936 | case OBJ_TAG: | |
28d055bd | 2937 | if (size < hexsz + strlen("object ") || |
912c13d5 | 2938 | get_oid_hex(buf + strlen("object "), oid)) |
8dc6a373 DB |
2939 | die("Invalid SHA1 in tag: %s", command_buf.buf); |
2940 | break; | |
2941 | case OBJ_COMMIT: | |
28d055bd | 2942 | if (size < hexsz + strlen("tree ") || |
912c13d5 | 2943 | get_oid_hex(buf + strlen("tree "), oid)) |
8dc6a373 DB |
2944 | die("Invalid SHA1 in commit: %s", command_buf.buf); |
2945 | } | |
2946 | ||
2947 | free(buf); | |
912c13d5 | 2948 | return find_object(oid); |
8dc6a373 DB |
2949 | } |
2950 | ||
2951 | static struct object_entry *parse_treeish_dataref(const char **p) | |
2952 | { | |
912c13d5 | 2953 | struct object_id oid; |
8dc6a373 DB |
2954 | struct object_entry *e; |
2955 | ||
2956 | if (**p == ':') { /* <mark> */ | |
06454cb9 | 2957 | e = find_mark(parse_mark_ref_space(p)); |
8dc6a373 DB |
2958 | if (!e) |
2959 | die("Unknown mark: %s", command_buf.buf); | |
e6a492b7 | 2960 | oidcpy(&oid, &e->idx.oid); |
8dc6a373 | 2961 | } else { /* <sha1> */ |
912c13d5 | 2962 | if (parse_oid_hex(*p, &oid, p)) |
06454cb9 | 2963 | die("Invalid dataref: %s", command_buf.buf); |
912c13d5 | 2964 | e = find_object(&oid); |
e814c39c JK |
2965 | if (*(*p)++ != ' ') |
2966 | die("Missing space after tree-ish: %s", command_buf.buf); | |
8dc6a373 DB |
2967 | } |
2968 | ||
2969 | while (!e || e->type != OBJ_TREE) | |
912c13d5 | 2970 | e = dereference(e, &oid); |
8dc6a373 DB |
2971 | return e; |
2972 | } | |
2973 | ||
ef479a12 | 2974 | static void print_ls(int mode, const unsigned char *hash, const char *path) |
8dc6a373 DB |
2975 | { |
2976 | static struct strbuf line = STRBUF_INIT; | |
2977 | ||
2978 | /* See show_tree(). */ | |
2979 | const char *type = | |
2980 | S_ISGITLINK(mode) ? commit_type : | |
2981 | S_ISDIR(mode) ? tree_type : | |
2982 | blob_type; | |
2983 | ||
2984 | if (!mode) { | |
2985 | /* missing SP path LF */ | |
2986 | strbuf_reset(&line); | |
2987 | strbuf_addstr(&line, "missing "); | |
2988 | quote_c_style(path, &line, NULL, 0); | |
2989 | strbuf_addch(&line, '\n'); | |
2990 | } else { | |
2991 | /* mode SP type SP object_name TAB path LF */ | |
2992 | strbuf_reset(&line); | |
2993 | strbuf_addf(&line, "%06o %s %s\t", | |
ef479a12 | 2994 | mode & ~NO_DELTA, type, hash_to_hex(hash)); |
8dc6a373 DB |
2995 | quote_c_style(path, &line, NULL, 0); |
2996 | strbuf_addch(&line, '\n'); | |
2997 | } | |
2998 | cat_blob_write(line.buf, line.len); | |
2999 | } | |
3000 | ||
97313bef | 3001 | static void parse_ls(const char *p, struct branch *b) |
8dc6a373 | 3002 | { |
8dc6a373 | 3003 | struct tree_entry *root = NULL; |
c2e86add | 3004 | struct tree_entry leaf = {NULL}; |
8dc6a373 | 3005 | |
bb8040f9 | 3006 | /* ls SP (<tree-ish> SP)? <path> */ |
8dc6a373 DB |
3007 | if (*p == '"') { |
3008 | if (!b) | |
3009 | die("Not in a commit: %s", command_buf.buf); | |
3010 | root = &b->branch_tree; | |
3011 | } else { | |
3012 | struct object_entry *e = parse_treeish_dataref(&p); | |
3013 | root = new_tree_entry(); | |
e6a492b7 | 3014 | oidcpy(&root->versions[1].oid, &e->idx.oid); |
d7e6b6a8 | 3015 | if (!is_null_oid(&root->versions[1].oid)) |
adefdba5 | 3016 | root->versions[1].mode = S_IFDIR; |
8dc6a373 | 3017 | load_tree(root); |
8dc6a373 DB |
3018 | } |
3019 | if (*p == '"') { | |
3020 | static struct strbuf uq = STRBUF_INIT; | |
3021 | const char *endp; | |
3022 | strbuf_reset(&uq); | |
3023 | if (unquote_c_style(&uq, p, &endp)) | |
3024 | die("Invalid path: %s", command_buf.buf); | |
3025 | if (*endp) | |
3026 | die("Garbage after path in: %s", command_buf.buf); | |
3027 | p = uq.buf; | |
3028 | } | |
e0eb6b97 | 3029 | tree_content_get(root, p, &leaf, 1); |
8dc6a373 DB |
3030 | /* |
3031 | * A directory in preparation would have a sha1 of zero | |
3032 | * until it is saved. Save, for simplicity. | |
3033 | */ | |
3034 | if (S_ISDIR(leaf.versions[1].mode)) | |
3035 | store_tree(&leaf); | |
3036 | ||
d7e6b6a8 | 3037 | print_ls(leaf.versions[1].mode, leaf.versions[1].oid.hash, p); |
c27e559d JN |
3038 | if (leaf.tree) |
3039 | release_tree_content_recursive(leaf.tree); | |
8dc6a373 DB |
3040 | if (!b || root != &b->branch_tree) |
3041 | release_tree_entry(root); | |
3042 | } | |
3043 | ||
dc01f59d | 3044 | static void checkpoint(void) |
7bfe6e26 | 3045 | { |
dc01f59d | 3046 | checkpoint_requested = 0; |
820b9310 SP |
3047 | if (object_count) { |
3048 | cycle_packfile(); | |
820b9310 | 3049 | } |
30e215a6 ER |
3050 | dump_branches(); |
3051 | dump_tags(); | |
3052 | dump_marks(); | |
dc01f59d JN |
3053 | } |
3054 | ||
3055 | static void parse_checkpoint(void) | |
3056 | { | |
3057 | checkpoint_requested = 1; | |
1fdb649c | 3058 | skip_optional_lf(); |
7bfe6e26 SP |
3059 | } |
3060 | ||
b3031781 | 3061 | static void parse_progress(void) |
ac053c02 | 3062 | { |
b449f4cf | 3063 | fwrite(command_buf.buf, 1, command_buf.len, stdout); |
ac053c02 SP |
3064 | fputc('\n', stdout); |
3065 | fflush(stdout); | |
3066 | skip_optional_lf(); | |
3067 | } | |
3068 | ||
bc3c79ae | 3069 | static char* make_fast_import_path(const char *path) |
e8438420 | 3070 | { |
bc3c79ae SR |
3071 | if (!relative_marks_paths || is_absolute_path(path)) |
3072 | return xstrdup(path); | |
d9c69644 | 3073 | return git_pathdup("info/fast-import/%s", path); |
bc3c79ae SR |
3074 | } |
3075 | ||
dded4f12 RR |
3076 | static void option_import_marks(const char *marks, |
3077 | int from_stream, int ignore_missing) | |
e8438420 | 3078 | { |
081751c8 SR |
3079 | if (import_marks_file) { |
3080 | if (from_stream) | |
3081 | die("Only one import-marks command allowed per stream"); | |
3082 | ||
3083 | /* read previous mark file */ | |
3084 | if(!import_marks_file_from_stream) | |
3085 | read_marks(); | |
e8438420 | 3086 | } |
081751c8 | 3087 | |
bc3c79ae | 3088 | import_marks_file = make_fast_import_path(marks); |
081751c8 | 3089 | import_marks_file_from_stream = from_stream; |
dded4f12 | 3090 | import_marks_file_ignore_missing = ignore_missing; |
e8438420 SP |
3091 | } |
3092 | ||
0f6927c2 SR |
3093 | static void option_date_format(const char *fmt) |
3094 | { | |
3095 | if (!strcmp(fmt, "raw")) | |
3096 | whenspec = WHENSPEC_RAW; | |
3097 | else if (!strcmp(fmt, "rfc2822")) | |
3098 | whenspec = WHENSPEC_RFC2822; | |
3099 | else if (!strcmp(fmt, "now")) | |
3100 | whenspec = WHENSPEC_NOW; | |
3101 | else | |
3102 | die("unknown --date-format argument %s", fmt); | |
3103 | } | |
3104 | ||
a9ff277e JN |
3105 | static unsigned long ulong_arg(const char *option, const char *arg) |
3106 | { | |
3107 | char *endptr; | |
3108 | unsigned long rv = strtoul(arg, &endptr, 0); | |
3109 | if (strchr(arg, '-') || endptr == arg || *endptr) | |
3110 | die("%s: argument must be a non-negative integer", option); | |
3111 | return rv; | |
3112 | } | |
3113 | ||
0f6927c2 SR |
3114 | static void option_depth(const char *depth) |
3115 | { | |
a9ff277e | 3116 | max_depth = ulong_arg("--depth", depth); |
0f6927c2 SR |
3117 | if (max_depth > MAX_DEPTH) |
3118 | die("--depth cannot exceed %u", MAX_DEPTH); | |
3119 | } | |
3120 | ||
3121 | static void option_active_branches(const char *branches) | |
3122 | { | |
a9ff277e | 3123 | max_active_branches = ulong_arg("--active-branches", branches); |
0f6927c2 SR |
3124 | } |
3125 | ||
3126 | static void option_export_marks(const char *marks) | |
3127 | { | |
bc3c79ae | 3128 | export_marks_file = make_fast_import_path(marks); |
0f6927c2 SR |
3129 | } |
3130 | ||
85c62395 DB |
3131 | static void option_cat_blob_fd(const char *fd) |
3132 | { | |
3133 | unsigned long n = ulong_arg("--cat-blob-fd", fd); | |
3134 | if (n > (unsigned long) INT_MAX) | |
3135 | die("--cat-blob-fd cannot exceed %d", INT_MAX); | |
3136 | cat_blob_fd = (int) n; | |
3137 | } | |
3138 | ||
0f6927c2 SR |
3139 | static void option_export_pack_edges(const char *edges) |
3140 | { | |
3141 | if (pack_edges) | |
3142 | fclose(pack_edges); | |
23a9e071 | 3143 | pack_edges = xfopen(edges, "a"); |
0f6927c2 SR |
3144 | } |
3145 | ||
9c8398f0 | 3146 | static int parse_one_option(const char *option) |
0f6927c2 | 3147 | { |
ae021d87 | 3148 | if (skip_prefix(option, "max-pack-size=", &option)) { |
4d0cc224 | 3149 | unsigned long v; |
ae021d87 | 3150 | if (!git_parse_ulong(option, &v)) |
4d0cc224 JH |
3151 | return 0; |
3152 | if (v < 8192) { | |
3153 | warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v); | |
3154 | v *= 1024 * 1024; | |
3155 | } else if (v < 1024 * 1024) { | |
3156 | warning("minimum max-pack-size is 1 MiB"); | |
3157 | v = 1024 * 1024; | |
3158 | } | |
3159 | max_packsize = v; | |
ae021d87 | 3160 | } else if (skip_prefix(option, "big-file-threshold=", &option)) { |
76ea93cc | 3161 | unsigned long v; |
ae021d87 | 3162 | if (!git_parse_ulong(option, &v)) |
76ea93cc JH |
3163 | return 0; |
3164 | big_file_threshold = v; | |
ae021d87 JK |
3165 | } else if (skip_prefix(option, "depth=", &option)) { |
3166 | option_depth(option); | |
3167 | } else if (skip_prefix(option, "active-branches=", &option)) { | |
3168 | option_active_branches(option); | |
3169 | } else if (skip_prefix(option, "export-pack-edges=", &option)) { | |
3170 | option_export_pack_edges(option); | |
11e934d5 | 3171 | } else if (!strcmp(option, "quiet")) { |
0f6927c2 | 3172 | show_stats = 0; |
11e934d5 | 3173 | } else if (!strcmp(option, "stats")) { |
0f6927c2 | 3174 | show_stats = 1; |
68061e34 JK |
3175 | } else if (!strcmp(option, "allow-unsafe-features")) { |
3176 | ; /* already handled during early option parsing */ | |
0f6927c2 | 3177 | } else { |
9c8398f0 | 3178 | return 0; |
0f6927c2 | 3179 | } |
9c8398f0 SR |
3180 | |
3181 | return 1; | |
0f6927c2 SR |
3182 | } |
3183 | ||
68061e34 JK |
3184 | static void check_unsafe_feature(const char *feature, int from_stream) |
3185 | { | |
3186 | if (from_stream && !allow_unsafe_features) | |
3187 | die(_("feature '%s' forbidden in input without --allow-unsafe-features"), | |
3188 | feature); | |
3189 | } | |
3190 | ||
081751c8 | 3191 | static int parse_one_feature(const char *feature, int from_stream) |
f963bd5d | 3192 | { |
ae021d87 JK |
3193 | const char *arg; |
3194 | ||
3195 | if (skip_prefix(feature, "date-format=", &arg)) { | |
3196 | option_date_format(arg); | |
3197 | } else if (skip_prefix(feature, "import-marks=", &arg)) { | |
a52ed761 | 3198 | check_unsafe_feature("import-marks", from_stream); |
ae021d87 JK |
3199 | option_import_marks(arg, from_stream, 0); |
3200 | } else if (skip_prefix(feature, "import-marks-if-exists=", &arg)) { | |
a52ed761 | 3201 | check_unsafe_feature("import-marks-if-exists", from_stream); |
ae021d87 JK |
3202 | option_import_marks(arg, from_stream, 1); |
3203 | } else if (skip_prefix(feature, "export-marks=", &arg)) { | |
68061e34 | 3204 | check_unsafe_feature(feature, from_stream); |
ae021d87 | 3205 | option_export_marks(arg); |
28c7b1f7 MH |
3206 | } else if (!strcmp(feature, "get-mark")) { |
3207 | ; /* Don't die - this feature is supported */ | |
85c62395 DB |
3208 | } else if (!strcmp(feature, "cat-blob")) { |
3209 | ; /* Don't die - this feature is supported */ | |
4cce4ef2 | 3210 | } else if (!strcmp(feature, "relative-marks")) { |
bc3c79ae | 3211 | relative_marks_paths = 1; |
4cce4ef2 | 3212 | } else if (!strcmp(feature, "no-relative-marks")) { |
bc3c79ae | 3213 | relative_marks_paths = 0; |
be56862f SR |
3214 | } else if (!strcmp(feature, "done")) { |
3215 | require_explicit_termination = 1; | |
4cce4ef2 | 3216 | } else if (!strcmp(feature, "force")) { |
f963bd5d | 3217 | force_update = 1; |
8dc6a373 | 3218 | } else if (!strcmp(feature, "notes") || !strcmp(feature, "ls")) { |
547e8b92 | 3219 | ; /* do nothing; we have the feature */ |
f963bd5d SR |
3220 | } else { |
3221 | return 0; | |
3222 | } | |
3223 | ||
3224 | return 1; | |
3225 | } | |
3226 | ||
97313bef | 3227 | static void parse_feature(const char *feature) |
f963bd5d | 3228 | { |
f963bd5d SR |
3229 | if (seen_data_command) |
3230 | die("Got feature command '%s' after data command", feature); | |
3231 | ||
081751c8 | 3232 | if (parse_one_feature(feature, 1)) |
f963bd5d SR |
3233 | return; |
3234 | ||
3235 | die("This version of fast-import does not support feature %s.", feature); | |
3236 | } | |
3237 | ||
97313bef | 3238 | static void parse_option(const char *option) |
9c8398f0 | 3239 | { |
9c8398f0 SR |
3240 | if (seen_data_command) |
3241 | die("Got option command '%s' after data command", option); | |
3242 | ||
3243 | if (parse_one_option(option)) | |
3244 | return; | |
3245 | ||
3246 | die("This version of fast-import does not support option: %s", option); | |
e8438420 SP |
3247 | } |
3248 | ||
536900e5 | 3249 | static void git_pack_config(void) |
bb23fdfa | 3250 | { |
536900e5 | 3251 | int indexversion_value; |
d9545c7f | 3252 | int limit; |
536900e5 TA |
3253 | unsigned long packsizelimit_value; |
3254 | ||
3255 | if (!git_config_get_ulong("pack.depth", &max_depth)) { | |
bb23fdfa SP |
3256 | if (max_depth > MAX_DEPTH) |
3257 | max_depth = MAX_DEPTH; | |
bb23fdfa | 3258 | } |
536900e5 TA |
3259 | if (!git_config_get_int("pack.indexversion", &indexversion_value)) { |
3260 | pack_idx_opts.version = indexversion_value; | |
ebcfb379 | 3261 | if (pack_idx_opts.version > 2) |
536900e5 TA |
3262 | git_die_config("pack.indexversion", |
3263 | "bad pack.indexversion=%"PRIu32, pack_idx_opts.version); | |
8c2ca8dd | 3264 | } |
536900e5 TA |
3265 | if (!git_config_get_ulong("pack.packsizelimit", &packsizelimit_value)) |
3266 | max_packsize = packsizelimit_value; | |
3267 | ||
d9545c7f EW |
3268 | if (!git_config_get_int("fastimport.unpacklimit", &limit)) |
3269 | unpack_limit = limit; | |
3270 | else if (!git_config_get_int("transfer.unpacklimit", &limit)) | |
3271 | unpack_limit = limit; | |
3272 | ||
536900e5 | 3273 | git_config(git_default_config, NULL); |
bb23fdfa SP |
3274 | } |
3275 | ||
d5c57b28 | 3276 | static const char fast_import_usage[] = |
62b4698e | 3277 | "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]"; |
d5c57b28 | 3278 | |
9c8398f0 SR |
3279 | static void parse_argv(void) |
3280 | { | |
3281 | unsigned int i; | |
3282 | ||
3283 | for (i = 1; i < global_argc; i++) { | |
3284 | const char *a = global_argv[i]; | |
3285 | ||
3286 | if (*a != '-' || !strcmp(a, "--")) | |
3287 | break; | |
3288 | ||
ff45c0d4 JK |
3289 | if (!skip_prefix(a, "--", &a)) |
3290 | die("unknown option %s", a); | |
3291 | ||
3292 | if (parse_one_option(a)) | |
9c8398f0 SR |
3293 | continue; |
3294 | ||
ff45c0d4 | 3295 | if (parse_one_feature(a, 0)) |
9c8398f0 SR |
3296 | continue; |
3297 | ||
ff45c0d4 JK |
3298 | if (skip_prefix(a, "cat-blob-fd=", &a)) { |
3299 | option_cat_blob_fd(a); | |
85c62395 DB |
3300 | continue; |
3301 | } | |
3302 | ||
ff45c0d4 | 3303 | die("unknown option --%s", a); |
9c8398f0 SR |
3304 | } |
3305 | if (i != global_argc) | |
3306 | usage(fast_import_usage); | |
3307 | ||
3308 | seen_data_command = 1; | |
3309 | if (import_marks_file) | |
3310 | read_marks(); | |
3311 | } | |
3312 | ||
3f2e2297 | 3313 | int cmd_main(int argc, const char **argv) |
8bcce301 | 3314 | { |
0f6927c2 | 3315 | unsigned int i; |
8bcce301 | 3316 | |
71a04a8b JN |
3317 | if (argc == 2 && !strcmp(argv[1], "-h")) |
3318 | usage(fast_import_usage); | |
3319 | ||
733ee2b7 | 3320 | setup_git_directory(); |
ebcfb379 | 3321 | reset_pack_idx_option(&pack_idx_opts); |
536900e5 | 3322 | git_pack_config(); |
bb23fdfa | 3323 | |
93e72d8d | 3324 | alloc_objects(object_entry_alloc); |
f1696ee3 | 3325 | strbuf_init(&command_buf, 0); |
93e72d8d SP |
3326 | atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*)); |
3327 | branch_table = xcalloc(branch_table_sz, sizeof(struct branch*)); | |
3328 | avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*)); | |
96c47d14 | 3329 | marks = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set)); |
463acbe1 | 3330 | |
68061e34 JK |
3331 | /* |
3332 | * We don't parse most options until after we've seen the set of | |
3333 | * "feature" lines at the start of the stream (which allows the command | |
3334 | * line to override stream data). But we must do an early parse of any | |
3335 | * command-line options that impact how we interpret the feature lines. | |
3336 | */ | |
3337 | for (i = 1; i < argc; i++) { | |
3338 | const char *arg = argv[i]; | |
3339 | if (*arg != '-' || !strcmp(arg, "--")) | |
3340 | break; | |
3341 | if (!strcmp(arg, "--allow-unsafe-features")) | |
3342 | allow_unsafe_features = 1; | |
3343 | } | |
3344 | ||
9c8398f0 SR |
3345 | global_argc = argc; |
3346 | global_argv = argv; | |
d5c57b28 | 3347 | |
96c47d14 | 3348 | rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free)); |
904b1941 SP |
3349 | for (i = 0; i < (cmd_save - 1); i++) |
3350 | rc_free[i].next = &rc_free[i + 1]; | |
3351 | rc_free[cmd_save - 1].next = NULL; | |
3352 | ||
f70b6534 | 3353 | start_packfile(); |
8acb3297 | 3354 | set_die_routine(die_nicely); |
dc01f59d | 3355 | set_checkpoint_signal(); |
e6c019d0 | 3356 | while (read_next_command() != EOF) { |
97313bef | 3357 | const char *v; |
e6c019d0 | 3358 | if (!strcmp("blob", command_buf.buf)) |
b3031781 | 3359 | parse_new_blob(); |
97313bef JK |
3360 | else if (skip_prefix(command_buf.buf, "commit ", &v)) |
3361 | parse_new_commit(v); | |
3362 | else if (skip_prefix(command_buf.buf, "tag ", &v)) | |
3363 | parse_new_tag(v); | |
3364 | else if (skip_prefix(command_buf.buf, "reset ", &v)) | |
3365 | parse_reset_branch(v); | |
5056bb76 EN |
3366 | else if (skip_prefix(command_buf.buf, "ls ", &v)) |
3367 | parse_ls(v, NULL); | |
7ffde293 EN |
3368 | else if (skip_prefix(command_buf.buf, "cat-blob ", &v)) |
3369 | parse_cat_blob(v); | |
cf7b857a EN |
3370 | else if (skip_prefix(command_buf.buf, "get-mark ", &v)) |
3371 | parse_get_mark(v); | |
7bfe6e26 | 3372 | else if (!strcmp("checkpoint", command_buf.buf)) |
b3031781 | 3373 | parse_checkpoint(); |
be56862f SR |
3374 | else if (!strcmp("done", command_buf.buf)) |
3375 | break; | |
59556548 | 3376 | else if (starts_with(command_buf.buf, "progress ")) |
b3031781 | 3377 | parse_progress(); |
97313bef JK |
3378 | else if (skip_prefix(command_buf.buf, "feature ", &v)) |
3379 | parse_feature(v); | |
3380 | else if (skip_prefix(command_buf.buf, "option git ", &v)) | |
3381 | parse_option(v); | |
59556548 | 3382 | else if (starts_with(command_buf.buf, "option ")) |
9c8398f0 | 3383 | /* ignore non-git options*/; |
c44cdc7e SP |
3384 | else |
3385 | die("Unsupported command: %s", command_buf.buf); | |
dc01f59d JN |
3386 | |
3387 | if (checkpoint_requested) | |
3388 | checkpoint(); | |
db5e523f | 3389 | } |
9c8398f0 SR |
3390 | |
3391 | /* argv hasn't been parsed yet, do so */ | |
3392 | if (!seen_data_command) | |
3393 | parse_argv(); | |
3394 | ||
be56862f SR |
3395 | if (require_explicit_termination && feof(stdin)) |
3396 | die("stream ends early"); | |
3397 | ||
f70b6534 | 3398 | end_packfile(); |
c44cdc7e | 3399 | |
463acbe1 | 3400 | dump_branches(); |
72303d44 | 3401 | dump_tags(); |
8455e484 | 3402 | unkeep_all_packs(); |
a6a1a831 | 3403 | dump_marks(); |
8bcce301 | 3404 | |
bdf1c06d SP |
3405 | if (pack_edges) |
3406 | fclose(pack_edges); | |
3407 | ||
c499d768 SP |
3408 | if (show_stats) { |
3409 | uintmax_t total_count = 0, duplicate_count = 0; | |
3410 | for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++) | |
3411 | total_count += object_count_by_type[i]; | |
3412 | for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++) | |
3413 | duplicate_count += duplicate_count_by_type[i]; | |
3414 | ||
3415 | fprintf(stderr, "%s statistics:\n", argv[0]); | |
3416 | fprintf(stderr, "---------------------------------------------------------------------\n"); | |
3efb1f34 JR |
3417 | fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count); |
3418 | fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count); | |
94c3b482 DI |
3419 | fprintf(stderr, " blobs : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB], delta_count_attempts_by_type[OBJ_BLOB]); |
3420 | fprintf(stderr, " trees : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE], delta_count_attempts_by_type[OBJ_TREE]); | |
3421 | fprintf(stderr, " commits: %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT], delta_count_attempts_by_type[OBJ_COMMIT]); | |
3422 | fprintf(stderr, " tags : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG], delta_count_attempts_by_type[OBJ_TAG]); | |
c499d768 | 3423 | fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count); |
3efb1f34 | 3424 | fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count); |
c499d768 | 3425 | fprintf(stderr, " atoms: %10u\n", atom_cnt); |
96c47d14 JM |
3426 | fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (tree_entry_allocd + fi_mem_pool.pool_alloc + alloc_count*sizeof(struct object_entry))/1024); |
3427 | fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd + fi_mem_pool.pool_alloc) /1024)); | |
3efb1f34 | 3428 | fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024); |
c499d768 SP |
3429 | fprintf(stderr, "---------------------------------------------------------------------\n"); |
3430 | pack_report(); | |
3431 | fprintf(stderr, "---------------------------------------------------------------------\n"); | |
3432 | fprintf(stderr, "\n"); | |
3433 | } | |
db5e523f | 3434 | |
7073e69e | 3435 | return failure ? 1 : 0; |
db5e523f | 3436 | } |