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