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