| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 3 | |
| 4 | #include "builtin.h" |
| 5 | #include "config.h" |
| 6 | #include "entry.h" |
| 7 | #include "environment.h" |
| 8 | #include "gettext.h" |
| 9 | #include "parallel-checkout.h" |
| 10 | #include "parse-options.h" |
| 11 | #include "pkt-line.h" |
| 12 | #include "read-cache-ll.h" |
| 13 | |
| 14 | static void packet_to_pc_item(const char *buffer, int len, |
| 15 | struct parallel_checkout_item *pc_item) |
| 16 | { |
| 17 | const struct pc_item_fixed_portion *fixed_portion; |
| 18 | const char *variant; |
| 19 | char *encoding; |
| 20 | |
| 21 | if (len < sizeof(struct pc_item_fixed_portion)) |
| 22 | BUG("checkout worker received too short item (got %dB, exp %dB)", |
| 23 | len, (int)sizeof(struct pc_item_fixed_portion)); |
| 24 | |
| 25 | fixed_portion = (struct pc_item_fixed_portion *)buffer; |
| 26 | |
| 27 | if (len - sizeof(struct pc_item_fixed_portion) != |
| 28 | fixed_portion->name_len + fixed_portion->working_tree_encoding_len) |
| 29 | BUG("checkout worker received corrupted item"); |
| 30 | |
| 31 | variant = buffer + sizeof(struct pc_item_fixed_portion); |
| 32 | |
| 33 | /* |
| 34 | * Note: the main process uses zero length to communicate that the |
| 35 | * encoding is NULL. There is no use case that requires sending an |
| 36 | * actual empty string, since convert_attrs() never sets |
| 37 | * ca.working_tree_enconding to "". |
| 38 | */ |
| 39 | if (fixed_portion->working_tree_encoding_len) { |
| 40 | encoding = xmemdupz(variant, |
| 41 | fixed_portion->working_tree_encoding_len); |
| 42 | variant += fixed_portion->working_tree_encoding_len; |
| 43 | } else { |
| 44 | encoding = NULL; |
| 45 | } |
| 46 | |
| 47 | memset(pc_item, 0, sizeof(*pc_item)); |
| 48 | pc_item->ce = make_empty_transient_cache_entry(fixed_portion->name_len, NULL); |
| 49 | pc_item->ce->ce_namelen = fixed_portion->name_len; |
| 50 | pc_item->ce->ce_mode = fixed_portion->ce_mode; |
| 51 | memcpy(pc_item->ce->name, variant, pc_item->ce->ce_namelen); |
| 52 | oidcpy(&pc_item->ce->oid, &fixed_portion->oid); |
| 53 | |
| 54 | pc_item->id = fixed_portion->id; |
| 55 | pc_item->ca.crlf_action = fixed_portion->crlf_action; |
| 56 | pc_item->ca.ident = fixed_portion->ident; |
| 57 | pc_item->ca.working_tree_encoding = encoding; |
| 58 | } |
| 59 | |
| 60 | static void report_result(struct parallel_checkout_item *pc_item) |
| 61 | { |
| 62 | struct pc_item_result res = { 0 }; |
| 63 | size_t size; |
| 64 | |
| 65 | res.id = pc_item->id; |
| 66 | res.status = pc_item->status; |
| 67 | |
| 68 | if (pc_item->status == PC_ITEM_WRITTEN) { |
| 69 | res.st = pc_item->st; |
| 70 | size = sizeof(res); |
| 71 | } else { |
| 72 | size = PC_ITEM_RESULT_BASE_SIZE; |
| 73 | } |
| 74 | |
| 75 | packet_write(1, (const char *)&res, size); |
| 76 | } |
| 77 | |
| 78 | /* Free the worker-side malloced data, but not pc_item itself. */ |
| 79 | static void release_pc_item_data(struct parallel_checkout_item *pc_item) |
| 80 | { |
| 81 | free((char *)pc_item->ca.working_tree_encoding); |
| 82 | discard_cache_entry(pc_item->ce); |
| 83 | } |
| 84 | |
| 85 | static void worker_loop(struct checkout *state) |
| 86 | { |
| 87 | struct parallel_checkout_item *items = NULL; |
| 88 | size_t i, nr = 0, alloc = 0; |
| 89 | |
| 90 | while (1) { |
| 91 | int len = packet_read(0, packet_buffer, sizeof(packet_buffer), |
| 92 | 0); |
| 93 | |
| 94 | if (len < 0) |
| 95 | BUG("packet_read() returned negative value"); |
| 96 | else if (!len) |
| 97 | break; |
| 98 | |
| 99 | ALLOC_GROW(items, nr + 1, alloc); |
| 100 | packet_to_pc_item(packet_buffer, len, &items[nr++]); |
| 101 | } |
| 102 | |
| 103 | for (i = 0; i < nr; i++) { |
| 104 | struct parallel_checkout_item *pc_item = &items[i]; |
| 105 | write_pc_item(pc_item, state); |
| 106 | report_result(pc_item); |
| 107 | release_pc_item_data(pc_item); |
| 108 | } |
| 109 | |
| 110 | packet_flush(1); |
| 111 | |
| 112 | free(items); |
| 113 | } |
| 114 | |
| 115 | static const char * const checkout_worker_usage[] = { |
| 116 | N_("git checkout--worker [<options>]"), |
| 117 | NULL |
| 118 | }; |
| 119 | |
| 120 | int cmd_checkout__worker(int argc, |
| 121 | const char **argv, |
| 122 | const char *prefix, |
| 123 | struct repository *repo UNUSED) |
| 124 | { |
| 125 | struct checkout state = CHECKOUT_INIT; |
| 126 | struct option checkout_worker_options[] = { |
| 127 | OPT_STRING(0, "prefix", &state.base_dir, N_("string"), |
| 128 | N_("when creating files, prepend <string>")), |
| 129 | OPT_END() |
| 130 | }; |
| 131 | |
| 132 | show_usage_with_options_if_asked(argc, argv, |
| 133 | checkout_worker_usage, |
| 134 | checkout_worker_options); |
| 135 | |
| 136 | repo_config(the_repository, git_default_config, NULL); |
| 137 | argc = parse_options(argc, argv, prefix, checkout_worker_options, |
| 138 | checkout_worker_usage, 0); |
| 139 | if (argc > 0) |
| 140 | usage_with_options(checkout_worker_usage, checkout_worker_options); |
| 141 | |
| 142 | if (state.base_dir) |
| 143 | state.base_dir_len = strlen(state.base_dir); |
| 144 | |
| 145 | /* |
| 146 | * Setting this on a worker won't actually update the index. We just |
| 147 | * need to tell the checkout machinery to lstat() the written entries, |
| 148 | * so that we can send this data back to the main process. |
| 149 | */ |
| 150 | state.refresh_cache = 1; |
| 151 | |
| 152 | worker_loop(&state); |
| 153 | return 0; |
| 154 | } |