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