fetch/clone: detect dubious ownership of local repositories
[git.git] / builtin / checkout--worker.c
blobede7dc32a43c01a78a0074e1bddd5ba18b487568
1 #include "builtin.h"
2 #include "config.h"
3 #include "entry.h"
4 #include "parallel-checkout.h"
5 #include "parse-options.h"
6 #include "pkt-line.h"
8 static void packet_to_pc_item(const char *buffer, int len,
9 struct parallel_checkout_item *pc_item)
11 const struct pc_item_fixed_portion *fixed_portion;
12 const char *variant;
13 char *encoding;
15 if (len < sizeof(struct pc_item_fixed_portion))
16 BUG("checkout worker received too short item (got %dB, exp %dB)",
17 len, (int)sizeof(struct pc_item_fixed_portion));
19 fixed_portion = (struct pc_item_fixed_portion *)buffer;
21 if (len - sizeof(struct pc_item_fixed_portion) !=
22 fixed_portion->name_len + fixed_portion->working_tree_encoding_len)
23 BUG("checkout worker received corrupted item");
25 variant = buffer + sizeof(struct pc_item_fixed_portion);
28 * Note: the main process uses zero length to communicate that the
29 * encoding is NULL. There is no use case that requires sending an
30 * actual empty string, since convert_attrs() never sets
31 * ca.working_tree_enconding to "".
33 if (fixed_portion->working_tree_encoding_len) {
34 encoding = xmemdupz(variant,
35 fixed_portion->working_tree_encoding_len);
36 variant += fixed_portion->working_tree_encoding_len;
37 } else {
38 encoding = NULL;
41 memset(pc_item, 0, sizeof(*pc_item));
42 pc_item->ce = make_empty_transient_cache_entry(fixed_portion->name_len, NULL);
43 pc_item->ce->ce_namelen = fixed_portion->name_len;
44 pc_item->ce->ce_mode = fixed_portion->ce_mode;
45 memcpy(pc_item->ce->name, variant, pc_item->ce->ce_namelen);
46 oidcpy(&pc_item->ce->oid, &fixed_portion->oid);
48 pc_item->id = fixed_portion->id;
49 pc_item->ca.crlf_action = fixed_portion->crlf_action;
50 pc_item->ca.ident = fixed_portion->ident;
51 pc_item->ca.working_tree_encoding = encoding;
54 static void report_result(struct parallel_checkout_item *pc_item)
56 struct pc_item_result res = { 0 };
57 size_t size;
59 res.id = pc_item->id;
60 res.status = pc_item->status;
62 if (pc_item->status == PC_ITEM_WRITTEN) {
63 res.st = pc_item->st;
64 size = sizeof(res);
65 } else {
66 size = PC_ITEM_RESULT_BASE_SIZE;
69 packet_write(1, (const char *)&res, size);
72 /* Free the worker-side malloced data, but not pc_item itself. */
73 static void release_pc_item_data(struct parallel_checkout_item *pc_item)
75 free((char *)pc_item->ca.working_tree_encoding);
76 discard_cache_entry(pc_item->ce);
79 static void worker_loop(struct checkout *state)
81 struct parallel_checkout_item *items = NULL;
82 size_t i, nr = 0, alloc = 0;
84 while (1) {
85 int len = packet_read(0, packet_buffer, sizeof(packet_buffer),
86 0);
88 if (len < 0)
89 BUG("packet_read() returned negative value");
90 else if (!len)
91 break;
93 ALLOC_GROW(items, nr + 1, alloc);
94 packet_to_pc_item(packet_buffer, len, &items[nr++]);
97 for (i = 0; i < nr; i++) {
98 struct parallel_checkout_item *pc_item = &items[i];
99 write_pc_item(pc_item, state);
100 report_result(pc_item);
101 release_pc_item_data(pc_item);
104 packet_flush(1);
106 free(items);
109 static const char * const checkout_worker_usage[] = {
110 N_("git checkout--worker [<options>]"),
111 NULL
114 int cmd_checkout__worker(int argc, const char **argv, const char *prefix)
116 struct checkout state = CHECKOUT_INIT;
117 struct option checkout_worker_options[] = {
118 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
119 N_("when creating files, prepend <string>")),
120 OPT_END()
123 if (argc == 2 && !strcmp(argv[1], "-h"))
124 usage_with_options(checkout_worker_usage,
125 checkout_worker_options);
127 git_config(git_default_config, NULL);
128 argc = parse_options(argc, argv, prefix, checkout_worker_options,
129 checkout_worker_usage, 0);
130 if (argc > 0)
131 usage_with_options(checkout_worker_usage, checkout_worker_options);
133 if (state.base_dir)
134 state.base_dir_len = strlen(state.base_dir);
137 * Setting this on a worker won't actually update the index. We just
138 * need to tell the checkout machinery to lstat() the written entries,
139 * so that we can send this data back to the main process.
141 state.refresh_cache = 1;
143 worker_loop(&state);
144 return 0;