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