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