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