]>
Commit | Line | Data |
---|---|---|
5579f44d | 1 | #include "git-compat-util.h" |
b2141fc1 | 2 | #include "config.h" |
32a8f510 | 3 | #include "environment.h" |
f394e093 | 4 | #include "gettext.h" |
41771fa4 | 5 | #include "hex.h" |
def88e9a LT |
6 | #include "refs.h" |
7 | #include "pkt-line.h" | |
958c24b1 | 8 | #include "sideband.h" |
109cd76d | 9 | #include "repository.h" |
a034e910 | 10 | #include "object-store-ll.h" |
6f2d7430 | 11 | #include "oid-array.h" |
f6b42a81 JH |
12 | #include "tag.h" |
13 | #include "object.h" | |
f0243f26 | 14 | #include "commit.h" |
9b8dc263 JS |
15 | #include "diff.h" |
16 | #include "revision.h" | |
17 | #include "list-objects.h" | |
10ac85c7 JH |
18 | #include "list-objects-filter.h" |
19 | #include "list-objects-filter-options.h" | |
cc41fa8d | 20 | #include "run-command.h" |
47a59185 | 21 | #include "connect.h" |
051e4005 | 22 | #include "sigchain.h" |
ff5effdf | 23 | #include "version.h" |
daebaa78 | 24 | #include "string-list.h" |
dbbcd44f | 25 | #include "strvec.h" |
74ea5c95 | 26 | #include "trace2.h" |
5411b10c | 27 | #include "prio-queue.h" |
aa9bab29 | 28 | #include "protocol.h" |
10ac85c7 | 29 | #include "quote.h" |
a3d6b53e | 30 | #include "upload-pack.h" |
3145ea95 | 31 | #include "serve.h" |
829a3215 | 32 | #include "commit-graph.h" |
ba3ca1ed | 33 | #include "commit-reach.h" |
120ad2b0 | 34 | #include "shallow.h" |
d48be35c | 35 | #include "write-or-die.h" |
b8f58c20 | 36 | #include "json-writer.h" |
def88e9a | 37 | |
208acbfb | 38 | /* Remember to update object flag allocation in object.h */ |
937a515a JH |
39 | #define THEY_HAVE (1u << 11) |
40 | #define OUR_REF (1u << 12) | |
41 | #define WANTED (1u << 13) | |
42 | #define COMMON_KNOWN (1u << 14) | |
937a515a | 43 | |
f53514bc JS |
44 | #define SHALLOW (1u << 16) |
45 | #define NOT_SHALLOW (1u << 17) | |
46 | #define CLIENT_SHALLOW (1u << 18) | |
390eb36b | 47 | #define HIDDEN_REF (1u << 19) |
f53514bc | 48 | |
d1035cac JT |
49 | #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \ |
50 | NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF) | |
51 | ||
629060d9 CC |
52 | /* Enum for allowed unadvertised object request (UOR) */ |
53 | enum allow_uor { | |
54 | /* Allow specifying sha1 if it is a ref tip. */ | |
55 | ALLOW_TIP_SHA1 = 0x01, | |
56 | /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */ | |
57 | ALLOW_REACHABLE_SHA1 = 0x02, | |
58 | /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */ | |
59 | ALLOW_ANY_SHA1 = 0x07 | |
60 | }; | |
960deccb | 61 | |
a8497288 CC |
62 | /* |
63 | * Please annotate, and if possible group together, fields used only | |
64 | * for protocol v0 or only for protocol v2. | |
65 | */ | |
e8498322 | 66 | struct upload_pack_data { |
a8497288 | 67 | struct string_list symref; /* v0 only */ |
e8498322 CC |
68 | struct object_array want_obj; |
69 | struct object_array have_obj; | |
a8497288 CC |
70 | struct oid_array haves; /* v2 only */ |
71 | struct string_list wanted_refs; /* v2 only */ | |
c45841ff | 72 | struct strvec hidden_refs; |
e8498322 CC |
73 | |
74 | struct object_array shallows; | |
75 | struct string_list deepen_not; | |
de0e9f74 | 76 | struct object_array extra_edge_obj; |
e8498322 CC |
77 | int depth; |
78 | timestamp_t deepen_since; | |
79 | int deepen_rev_list; | |
80 | int deepen_relative; | |
f203a88c | 81 | int keepalive; |
35b43a10 | 82 | int shallow_nr; |
f01c7916 | 83 | timestamp_t oldest_have; |
e8498322 | 84 | |
d40f04e0 | 85 | unsigned int timeout; /* v0 only */ |
e9d882b8 CC |
86 | enum { |
87 | NO_MULTI_ACK = 0, | |
88 | MULTI_ACK = 1, | |
89 | MULTI_ACK_DETAILED = 2 | |
90 | } multi_ack; /* v0 only */ | |
d40f04e0 | 91 | |
f8edd1ca CC |
92 | /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */ |
93 | int use_sideband; | |
94 | ||
dd4b732d | 95 | struct string_list uri_protocols; |
629060d9 | 96 | enum allow_uor allow_uor; |
f1514c6a | 97 | |
e8498322 | 98 | struct list_objects_filter_options filter_options; |
6dd3456a | 99 | struct string_list allowed_filters; |
e8498322 CC |
100 | |
101 | struct packet_writer writer; | |
102 | ||
339a9840 CC |
103 | const char *pack_objects_hook; |
104 | ||
a8497288 | 105 | unsigned stateless_rpc : 1; /* v0 only */ |
d40f04e0 CC |
106 | unsigned no_done : 1; /* v0 only */ |
107 | unsigned daemon_mode : 1; /* v0 only */ | |
59a90261 | 108 | unsigned filter_capability_requested : 1; /* v0 only */ |
e8498322 CC |
109 | |
110 | unsigned use_thin_pack : 1; | |
111 | unsigned use_ofs_delta : 1; | |
112 | unsigned no_progress : 1; | |
113 | unsigned use_include_tag : 1; | |
9c1e657a | 114 | unsigned wait_for_done : 1; |
59abe196 | 115 | unsigned allow_filter : 1; |
6dd3456a | 116 | unsigned allow_filter_fallback : 1; |
5b01a4e8 | 117 | unsigned long tree_filter_max_depth; |
a8497288 CC |
118 | |
119 | unsigned done : 1; /* v2 only */ | |
d1d7a945 | 120 | unsigned allow_ref_in_want : 1; /* v2 only */ |
e3835cd4 | 121 | unsigned allow_sideband_all : 1; /* v2 only */ |
791e1adf | 122 | unsigned advertise_sid : 1; |
933e3a4e | 123 | unsigned sent_capabilities : 1; |
e8498322 CC |
124 | }; |
125 | ||
126 | static void upload_pack_data_init(struct upload_pack_data *data) | |
127 | { | |
438528f6 | 128 | struct string_list symref = STRING_LIST_INIT_DUP; |
e8498322 | 129 | struct string_list wanted_refs = STRING_LIST_INIT_DUP; |
c45841ff | 130 | struct strvec hidden_refs = STRVEC_INIT; |
e8498322 CC |
131 | struct object_array want_obj = OBJECT_ARRAY_INIT; |
132 | struct object_array have_obj = OBJECT_ARRAY_INIT; | |
133 | struct oid_array haves = OID_ARRAY_INIT; | |
134 | struct object_array shallows = OBJECT_ARRAY_INIT; | |
135 | struct string_list deepen_not = STRING_LIST_INIT_DUP; | |
dd4b732d | 136 | struct string_list uri_protocols = STRING_LIST_INIT_DUP; |
de0e9f74 | 137 | struct object_array extra_edge_obj = OBJECT_ARRAY_INIT; |
6dd3456a | 138 | struct string_list allowed_filters = STRING_LIST_INIT_DUP; |
e8498322 CC |
139 | |
140 | memset(data, 0, sizeof(*data)); | |
438528f6 | 141 | data->symref = symref; |
e8498322 | 142 | data->wanted_refs = wanted_refs; |
9b67eb6f | 143 | data->hidden_refs = hidden_refs; |
e8498322 CC |
144 | data->want_obj = want_obj; |
145 | data->have_obj = have_obj; | |
146 | data->haves = haves; | |
147 | data->shallows = shallows; | |
148 | data->deepen_not = deepen_not; | |
dd4b732d | 149 | data->uri_protocols = uri_protocols; |
de0e9f74 | 150 | data->extra_edge_obj = extra_edge_obj; |
6dd3456a TB |
151 | data->allowed_filters = allowed_filters; |
152 | data->allow_filter_fallback = 1; | |
5b01a4e8 | 153 | data->tree_filter_max_depth = ULONG_MAX; |
e8498322 | 154 | packet_writer_init(&data->writer, 1); |
2a01bded | 155 | list_objects_filter_init(&data->filter_options); |
f203a88c CC |
156 | |
157 | data->keepalive = 5; | |
791e1adf | 158 | data->advertise_sid = 0; |
e8498322 CC |
159 | } |
160 | ||
161 | static void upload_pack_data_clear(struct upload_pack_data *data) | |
162 | { | |
438528f6 | 163 | string_list_clear(&data->symref, 1); |
e8498322 | 164 | string_list_clear(&data->wanted_refs, 1); |
c45841ff | 165 | strvec_clear(&data->hidden_refs); |
e8498322 CC |
166 | object_array_clear(&data->want_obj); |
167 | object_array_clear(&data->have_obj); | |
168 | oid_array_clear(&data->haves); | |
169 | object_array_clear(&data->shallows); | |
170 | string_list_clear(&data->deepen_not, 0); | |
de0e9f74 | 171 | object_array_clear(&data->extra_edge_obj); |
e8498322 | 172 | list_objects_filter_release(&data->filter_options); |
8d133f50 | 173 | string_list_clear(&data->allowed_filters, 0); |
339a9840 CC |
174 | |
175 | free((char *)data->pack_objects_hook); | |
e8498322 CC |
176 | } |
177 | ||
d40f04e0 | 178 | static void reset_timeout(unsigned int timeout) |
960deccb PA |
179 | { |
180 | alarm(timeout); | |
181 | } | |
fb9040cc | 182 | |
f8edd1ca CC |
183 | static void send_client_data(int fd, const char *data, ssize_t sz, |
184 | int use_sideband) | |
583b7ea3 | 185 | { |
4c4b7d1d LF |
186 | if (use_sideband) { |
187 | send_sideband(1, fd, data, sz, use_sideband); | |
fcf0fe9e | 188 | return; |
4c4b7d1d | 189 | } |
958c24b1 JH |
190 | if (fd == 3) |
191 | /* emergency quit */ | |
192 | fd = 2; | |
193 | if (fd == 2) { | |
93822c22 | 194 | /* XXX: are we happy to lose stuff here? */ |
958c24b1 | 195 | xwrite(fd, data, sz); |
fcf0fe9e | 196 | return; |
583b7ea3 | 197 | } |
cdf4fb8e | 198 | write_or_die(fd, data, sz); |
583b7ea3 JH |
199 | } |
200 | ||
b790e0f6 NTND |
201 | static int write_one_shallow(const struct commit_graft *graft, void *cb_data) |
202 | { | |
203 | FILE *fp = cb_data; | |
204 | if (graft->nr_parent == -1) | |
7683e2e6 | 205 | fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid)); |
b790e0f6 NTND |
206 | return 0; |
207 | } | |
208 | ||
acaaca7d | 209 | struct output_state { |
55a9651d JV |
210 | /* |
211 | * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with | |
212 | * sideband-64k the band designator takes up 1 byte of space. Because | |
213 | * relay_pack_data keeps the last byte to itself, we make the buffer 1 | |
214 | * byte bigger than the intended maximum write size. | |
215 | */ | |
216 | char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1]; | |
acaaca7d | 217 | int used; |
dd4b732d JT |
218 | unsigned packfile_uris_started : 1; |
219 | unsigned packfile_started : 1; | |
acaaca7d JT |
220 | }; |
221 | ||
222 | static int relay_pack_data(int pack_objects_out, struct output_state *os, | |
dd4b732d | 223 | int use_sideband, int write_packfile_line) |
acaaca7d JT |
224 | { |
225 | /* | |
226 | * We keep the last byte to ourselves | |
227 | * in case we detect broken rev-list, so that we | |
228 | * can leave the stream corrupted. This is | |
229 | * unfortunate -- unpack-objects would happily | |
230 | * accept a valid packdata with trailing garbage, | |
231 | * so appending garbage after we pass all the | |
232 | * pack data is not good enough to signal | |
233 | * breakage to downstream. | |
234 | */ | |
235 | ssize_t readsz; | |
236 | ||
237 | readsz = xread(pack_objects_out, os->buffer + os->used, | |
238 | sizeof(os->buffer) - os->used); | |
239 | if (readsz < 0) { | |
240 | return readsz; | |
241 | } | |
242 | os->used += readsz; | |
243 | ||
dd4b732d JT |
244 | while (!os->packfile_started) { |
245 | char *p; | |
246 | if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) { | |
247 | os->packfile_started = 1; | |
248 | if (write_packfile_line) { | |
249 | if (os->packfile_uris_started) | |
250 | packet_delim(1); | |
251 | packet_write_fmt(1, "\1packfile\n"); | |
252 | } | |
253 | break; | |
254 | } | |
255 | if ((p = memchr(os->buffer, '\n', os->used))) { | |
256 | if (!os->packfile_uris_started) { | |
257 | os->packfile_uris_started = 1; | |
258 | if (!write_packfile_line) | |
259 | BUG("packfile_uris requires sideband-all"); | |
260 | packet_write_fmt(1, "\1packfile-uris\n"); | |
261 | } | |
262 | *p = '\0'; | |
263 | packet_write_fmt(1, "\1%s\n", os->buffer); | |
264 | ||
265 | os->used -= p - os->buffer + 1; | |
266 | memmove(os->buffer, p + 1, os->used); | |
267 | } else { | |
268 | /* | |
269 | * Incomplete line. | |
270 | */ | |
271 | return readsz; | |
272 | } | |
273 | } | |
274 | ||
acaaca7d JT |
275 | if (os->used > 1) { |
276 | send_client_data(1, os->buffer, os->used - 1, use_sideband); | |
277 | os->buffer[0] = os->buffer[os->used - 1]; | |
278 | os->used = 1; | |
279 | } else { | |
280 | send_client_data(1, os->buffer, os->used, use_sideband); | |
281 | os->used = 0; | |
282 | } | |
283 | ||
284 | return readsz; | |
285 | } | |
286 | ||
dd4b732d JT |
287 | static void create_pack_file(struct upload_pack_data *pack_data, |
288 | const struct string_list *uri_protocols) | |
fb9040cc | 289 | { |
d3180279 | 290 | struct child_process pack_objects = CHILD_PROCESS_INIT; |
55a9651d | 291 | struct output_state *output_state = xcalloc(1, sizeof(struct output_state)); |
acaaca7d | 292 | char progress[128]; |
583b7ea3 JH |
293 | char abort_msg[] = "aborting due to possible repository " |
294 | "corruption on the remote side."; | |
1456b043 | 295 | ssize_t sz; |
65a3629e | 296 | int i; |
cdab4858 | 297 | FILE *pipe_fd; |
75bfc6c2 | 298 | |
339a9840 | 299 | if (!pack_data->pack_objects_hook) |
20b20a22 JK |
300 | pack_objects.git_cmd = 1; |
301 | else { | |
c972bf4c JK |
302 | strvec_push(&pack_objects.args, pack_data->pack_objects_hook); |
303 | strvec_push(&pack_objects.args, "git"); | |
20b20a22 JK |
304 | pack_objects.use_shell = 1; |
305 | } | |
306 | ||
35b43a10 | 307 | if (pack_data->shallow_nr) { |
c972bf4c JK |
308 | strvec_push(&pack_objects.args, "--shallow-file"); |
309 | strvec_push(&pack_objects.args, ""); | |
f0cea83f | 310 | } |
c972bf4c JK |
311 | strvec_push(&pack_objects.args, "pack-objects"); |
312 | strvec_push(&pack_objects.args, "--revs"); | |
b5a2068c | 313 | if (pack_data->use_thin_pack) |
c972bf4c | 314 | strvec_push(&pack_objects.args, "--thin"); |
75bfc6c2 | 315 | |
c972bf4c | 316 | strvec_push(&pack_objects.args, "--stdout"); |
35b43a10 | 317 | if (pack_data->shallow_nr) |
c972bf4c | 318 | strvec_push(&pack_objects.args, "--shallow"); |
b5a2068c | 319 | if (!pack_data->no_progress) |
c972bf4c | 320 | strvec_push(&pack_objects.args, "--progress"); |
b5a2068c | 321 | if (pack_data->use_ofs_delta) |
c972bf4c | 322 | strvec_push(&pack_objects.args, "--delta-base-offset"); |
b5a2068c | 323 | if (pack_data->use_include_tag) |
c972bf4c | 324 | strvec_push(&pack_objects.args, "--include-tag"); |
c9f03259 | 325 | if (pack_data->filter_options.choice) { |
cf9ceb5a | 326 | const char *spec = |
c9f03259 | 327 | expand_list_objects_filter_spec(&pack_data->filter_options); |
ad5df6b7 | 328 | strvec_pushf(&pack_objects.args, "--filter=%s", spec); |
10ac85c7 | 329 | } |
dd4b732d JT |
330 | if (uri_protocols) { |
331 | for (i = 0; i < uri_protocols->nr; i++) | |
c972bf4c | 332 | strvec_pushf(&pack_objects.args, "--uri-protocol=%s", |
dd4b732d JT |
333 | uri_protocols->items[i].string); |
334 | } | |
cc41fa8d | 335 | |
b9612197 | 336 | pack_objects.in = -1; |
cc41fa8d JS |
337 | pack_objects.out = -1; |
338 | pack_objects.err = -1; | |
309a4028 | 339 | pack_objects.clean_on_exit = 1; |
21edd3f1 | 340 | |
4c324c00 | 341 | if (start_command(&pack_objects)) |
7e44c935 | 342 | die("git upload-pack: unable to fork git-pack-objects"); |
b1c71b72 | 343 | |
cdab4858 NTND |
344 | pipe_fd = xfdopen(pack_objects.in, "w"); |
345 | ||
35b43a10 | 346 | if (pack_data->shallow_nr) |
b790e0f6 NTND |
347 | for_each_commit_graft(write_one_shallow, pipe_fd); |
348 | ||
c9f03259 | 349 | for (i = 0; i < pack_data->want_obj.nr; i++) |
cdab4858 | 350 | fprintf(pipe_fd, "%s\n", |
c9f03259 | 351 | oid_to_hex(&pack_data->want_obj.objects[i].item->oid)); |
cdab4858 | 352 | fprintf(pipe_fd, "--not\n"); |
c9f03259 | 353 | for (i = 0; i < pack_data->have_obj.nr; i++) |
cdab4858 | 354 | fprintf(pipe_fd, "%s\n", |
c9f03259 | 355 | oid_to_hex(&pack_data->have_obj.objects[i].item->oid)); |
de0e9f74 | 356 | for (i = 0; i < pack_data->extra_edge_obj.nr; i++) |
cdab4858 | 357 | fprintf(pipe_fd, "%s\n", |
de0e9f74 | 358 | oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid)); |
cdab4858 NTND |
359 | fprintf(pipe_fd, "\n"); |
360 | fflush(pipe_fd); | |
361 | fclose(pipe_fd); | |
f0cea83f | 362 | |
cc41fa8d JS |
363 | /* We read from pack_objects.err to capture stderr output for |
364 | * progress bar, and pack_objects.out to capture the pack data. | |
b1c71b72 | 365 | */ |
b1c71b72 JH |
366 | |
367 | while (1) { | |
b1c71b72 | 368 | struct pollfd pfd[2]; |
f203a88c | 369 | int pe, pu, pollsize, polltimeout; |
05e95155 | 370 | int ret; |
b1c71b72 | 371 | |
d40f04e0 | 372 | reset_timeout(pack_data->timeout); |
0d516ada | 373 | |
b1c71b72 | 374 | pollsize = 0; |
363b7817 | 375 | pe = pu = -1; |
b1c71b72 | 376 | |
cc41fa8d JS |
377 | if (0 <= pack_objects.out) { |
378 | pfd[pollsize].fd = pack_objects.out; | |
b1c71b72 JH |
379 | pfd[pollsize].events = POLLIN; |
380 | pu = pollsize; | |
381 | pollsize++; | |
382 | } | |
cc41fa8d JS |
383 | if (0 <= pack_objects.err) { |
384 | pfd[pollsize].fd = pack_objects.err; | |
363b7817 JH |
385 | pfd[pollsize].events = POLLIN; |
386 | pe = pollsize; | |
387 | pollsize++; | |
388 | } | |
b1c71b72 | 389 | |
4c324c00 JS |
390 | if (!pollsize) |
391 | break; | |
392 | ||
f203a88c CC |
393 | polltimeout = pack_data->keepalive < 0 |
394 | ? -1 | |
395 | : 1000 * pack_data->keepalive; | |
396 | ||
397 | ret = poll(pfd, pollsize, polltimeout); | |
6c71f8b0 | 398 | |
05e95155 | 399 | if (ret < 0) { |
4c324c00 | 400 | if (errno != EINTR) { |
d2b6afa2 | 401 | error_errno("poll failed, resuming"); |
4c324c00 | 402 | sleep(1); |
b1c71b72 | 403 | } |
4c324c00 JS |
404 | continue; |
405 | } | |
6b59f51b NP |
406 | if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) { |
407 | /* Status ready; we ship that in the side-band | |
408 | * or dump to the standard error. | |
409 | */ | |
410 | sz = xread(pack_objects.err, progress, | |
411 | sizeof(progress)); | |
412 | if (0 < sz) | |
f8edd1ca CC |
413 | send_client_data(2, progress, sz, |
414 | pack_data->use_sideband); | |
6b59f51b NP |
415 | else if (sz == 0) { |
416 | close(pack_objects.err); | |
417 | pack_objects.err = -1; | |
418 | } | |
419 | else | |
420 | goto fail; | |
421 | /* give priority to status messages */ | |
422 | continue; | |
423 | } | |
4c324c00 | 424 | if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) { |
acaaca7d | 425 | int result = relay_pack_data(pack_objects.out, |
55a9651d | 426 | output_state, |
dd4b732d JT |
427 | pack_data->use_sideband, |
428 | !!uri_protocols); | |
acaaca7d JT |
429 | |
430 | if (result == 0) { | |
4c324c00 JS |
431 | close(pack_objects.out); |
432 | pack_objects.out = -1; | |
acaaca7d | 433 | } else if (result < 0) { |
4c324c00 | 434 | goto fail; |
b1c71b72 | 435 | } |
4c324c00 | 436 | } |
05e95155 JK |
437 | |
438 | /* | |
439 | * We hit the keepalive timeout without saying anything; send | |
440 | * an empty message on the data sideband just to let the other | |
441 | * side know we're still working on it, but don't have any data | |
442 | * yet. | |
443 | * | |
444 | * If we don't have a sideband channel, there's no room in the | |
445 | * protocol to say anything, so those clients are just out of | |
446 | * luck. | |
447 | */ | |
f8edd1ca | 448 | if (!ret && pack_data->use_sideband) { |
05e95155 JK |
449 | static const char buf[] = "0005\1"; |
450 | write_or_die(1, buf, 5); | |
451 | } | |
4c324c00 | 452 | } |
b1c71b72 | 453 | |
4c324c00 | 454 | if (finish_command(&pack_objects)) { |
7e44c935 | 455 | error("git upload-pack: git-pack-objects died with error."); |
4c324c00 JS |
456 | goto fail; |
457 | } | |
b1c71b72 | 458 | |
4c324c00 | 459 | /* flush the data */ |
55a9651d JV |
460 | if (output_state->used > 0) { |
461 | send_client_data(1, output_state->buffer, output_state->used, | |
f8edd1ca | 462 | pack_data->use_sideband); |
4c324c00 | 463 | fprintf(stderr, "flushed.\n"); |
b1c71b72 | 464 | } |
55a9651d | 465 | free(output_state); |
f8edd1ca | 466 | if (pack_data->use_sideband) |
4c324c00 JS |
467 | packet_flush(1); |
468 | return; | |
469 | ||
b1c71b72 | 470 | fail: |
c68d5dbc | 471 | free(output_state); |
f8edd1ca CC |
472 | send_client_data(3, abort_msg, sizeof(abort_msg), |
473 | pack_data->use_sideband); | |
7e44c935 | 474 | die("git upload-pack: %s", abort_msg); |
fb9040cc LT |
475 | } |
476 | ||
ea2c6e60 | 477 | static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid) |
def88e9a | 478 | { |
937a515a | 479 | int we_knew_they_have = 0; |
ea2c6e60 | 480 | struct object *o = parse_object(the_repository, oid); |
b1e9fff7 | 481 | |
b1e9fff7 | 482 | if (!o) |
cf93982f | 483 | die("oops (%s)", oid_to_hex(oid)); |
182a8dab | 484 | if (o->type == OBJ_COMMIT) { |
b1e9fff7 | 485 | struct commit_list *parents; |
937a515a | 486 | struct commit *commit = (struct commit *)o; |
b1e9fff7 | 487 | if (o->flags & THEY_HAVE) |
937a515a JH |
488 | we_knew_they_have = 1; |
489 | else | |
490 | o->flags |= THEY_HAVE; | |
f01c7916 CC |
491 | if (!data->oldest_have || (commit->date < data->oldest_have)) |
492 | data->oldest_have = commit->date; | |
937a515a | 493 | for (parents = commit->parents; |
b1e9fff7 JH |
494 | parents; |
495 | parents = parents->next) | |
496 | parents->item->object.flags |= THEY_HAVE; | |
fb9040cc | 497 | } |
937a515a | 498 | if (!we_knew_they_have) { |
460ed0d4 | 499 | add_object_array(o, NULL, &data->have_obj); |
937a515a JH |
500 | return 1; |
501 | } | |
502 | return 0; | |
503 | } | |
504 | ||
ea2c6e60 CC |
505 | static int got_oid(struct upload_pack_data *data, |
506 | const char *hex, struct object_id *oid) | |
507 | { | |
508 | if (get_oid_hex(hex, oid)) | |
509 | die("git upload-pack: expected SHA1 object, got '%s'", hex); | |
bc726bd0 ÆAB |
510 | if (!repo_has_object_file_with_flags(the_repository, oid, |
511 | OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT)) | |
ea2c6e60 CC |
512 | return -1; |
513 | return do_got_oid(data, oid); | |
514 | } | |
515 | ||
08667348 | 516 | static int ok_to_give_up(struct upload_pack_data *data) |
937a515a | 517 | { |
d7f92784 | 518 | timestamp_t min_generation = GENERATION_NUMBER_ZERO; |
937a515a | 519 | |
08667348 | 520 | if (!data->have_obj.nr) |
937a515a JH |
521 | return 0; |
522 | ||
08667348 | 523 | return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE, |
f01c7916 | 524 | COMMON_KNOWN, data->oldest_have, |
4fbcca4e | 525 | min_generation); |
def88e9a LT |
526 | } |
527 | ||
07977695 CC |
528 | static int get_common_commits(struct upload_pack_data *data, |
529 | struct packet_reader *reader) | |
def88e9a | 530 | { |
cf93982f | 531 | struct object_id oid; |
532 | char last_hex[GIT_MAX_HEXSZ + 1]; | |
49bee717 SP |
533 | int got_common = 0; |
534 | int got_other = 0; | |
4e10cf9a | 535 | int sent_ready = 0; |
def88e9a | 536 | |
f0243f26 JS |
537 | save_commit_buffer = 0; |
538 | ||
eeefa7c9 | 539 | for (;;) { |
8bf3b758 NTND |
540 | const char *arg; |
541 | ||
d40f04e0 | 542 | reset_timeout(data->timeout); |
def88e9a | 543 | |
01f9ec64 | 544 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) { |
e9d882b8 | 545 | if (data->multi_ack == MULTI_ACK_DETAILED |
07977695 CC |
546 | && got_common |
547 | && !got_other | |
08667348 | 548 | && ok_to_give_up(data)) { |
4e10cf9a | 549 | sent_ready = 1; |
81c634e9 | 550 | packet_write_fmt(1, "ACK %s ready\n", last_hex); |
4e10cf9a | 551 | } |
53d69506 | 552 | if (data->have_obj.nr == 0 || data->multi_ack) |
81c634e9 | 553 | packet_write_fmt(1, "NAK\n"); |
4e10cf9a | 554 | |
d40f04e0 | 555 | if (data->no_done && sent_ready) { |
81c634e9 | 556 | packet_write_fmt(1, "ACK %s\n", last_hex); |
4e10cf9a JH |
557 | return 0; |
558 | } | |
df654abc | 559 | if (data->stateless_rpc) |
42526b47 | 560 | exit(0); |
49bee717 SP |
561 | got_common = 0; |
562 | got_other = 0; | |
def88e9a LT |
563 | continue; |
564 | } | |
01f9ec64 | 565 | if (skip_prefix(reader->line, "have ", &arg)) { |
460ed0d4 | 566 | switch (got_oid(data, arg, &oid)) { |
937a515a | 567 | case -1: /* they have what we do not */ |
49bee717 | 568 | got_other = 1; |
53d69506 | 569 | if (data->multi_ack |
08667348 | 570 | && ok_to_give_up(data)) { |
cf93982f | 571 | const char *hex = oid_to_hex(&oid); |
e9d882b8 | 572 | if (data->multi_ack == MULTI_ACK_DETAILED) { |
4e10cf9a | 573 | sent_ready = 1; |
81c634e9 | 574 | packet_write_fmt(1, "ACK %s ready\n", hex); |
4e10cf9a | 575 | } else |
81c634e9 | 576 | packet_write_fmt(1, "ACK %s continue\n", hex); |
78affc49 | 577 | } |
937a515a JH |
578 | break; |
579 | default: | |
49bee717 | 580 | got_common = 1; |
55dc227d | 581 | oid_to_hex_r(last_hex, &oid); |
e9d882b8 | 582 | if (data->multi_ack == MULTI_ACK_DETAILED) |
81c634e9 | 583 | packet_write_fmt(1, "ACK %s common\n", last_hex); |
53d69506 | 584 | else if (data->multi_ack) |
81c634e9 | 585 | packet_write_fmt(1, "ACK %s continue\n", last_hex); |
07977695 | 586 | else if (data->have_obj.nr == 1) |
81c634e9 | 587 | packet_write_fmt(1, "ACK %s\n", last_hex); |
937a515a | 588 | break; |
af2d3aa4 | 589 | } |
def88e9a LT |
590 | continue; |
591 | } | |
01f9ec64 | 592 | if (!strcmp(reader->line, "done")) { |
07977695 | 593 | if (data->have_obj.nr > 0) { |
53d69506 | 594 | if (data->multi_ack) |
81c634e9 | 595 | packet_write_fmt(1, "ACK %s\n", last_hex); |
1bd8c8f0 JS |
596 | return 0; |
597 | } | |
81c634e9 | 598 | packet_write_fmt(1, "NAK\n"); |
def88e9a LT |
599 | return -1; |
600 | } | |
01f9ec64 | 601 | die("git upload-pack: expected SHA1 list, got '%s'", reader->line); |
def88e9a | 602 | } |
def88e9a LT |
603 | } |
604 | ||
18b6b1b5 TB |
605 | static int allow_hidden_refs(enum allow_uor allow_uor) |
606 | { | |
607 | if ((allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1) | |
608 | return 1; | |
609 | return !(allow_uor & (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)); | |
610 | } | |
611 | ||
612 | static void for_each_namespaced_ref_1(each_ref_fn fn, | |
613 | struct upload_pack_data *data) | |
614 | { | |
615 | const char **excludes = NULL; | |
616 | /* | |
617 | * If `data->allow_uor` allows fetching hidden refs, we need to | |
618 | * mark all references (including hidden ones), to check in | |
619 | * `is_our_ref()` below. | |
620 | * | |
621 | * Otherwise, we only care about whether each reference's object | |
622 | * has the OUR_REF bit set or not, so do not need to visit | |
623 | * hidden references. | |
624 | */ | |
625 | if (allow_hidden_refs(data->allow_uor)) | |
626 | excludes = hidden_refs_to_excludes(&data->hidden_refs); | |
627 | ||
628 | for_each_namespaced_ref(excludes, fn, data); | |
629 | } | |
630 | ||
631 | ||
629060d9 | 632 | static int is_our_ref(struct object *o, enum allow_uor allow_uor) |
390eb36b | 633 | { |
18b6b1b5 | 634 | return o->flags & ((allow_hidden_refs(allow_uor) ? 0 : HIDDEN_REF) | OUR_REF); |
390eb36b JH |
635 | } |
636 | ||
2997178e NTND |
637 | /* |
638 | * on successful case, it's up to the caller to close cmd->out | |
639 | */ | |
640 | static int do_reachable_revlist(struct child_process *cmd, | |
079aa97e | 641 | struct object_array *src, |
f1514c6a | 642 | struct object_array *reachable, |
629060d9 | 643 | enum allow_uor allow_uor) |
051e4005 | 644 | { |
051e4005 | 645 | struct object *o; |
a698d67b | 646 | FILE *cmd_in = NULL; |
051e4005 JH |
647 | int i; |
648 | ||
2b709893 | 649 | strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL); |
2997178e NTND |
650 | cmd->git_cmd = 1; |
651 | cmd->no_stderr = 1; | |
652 | cmd->in = -1; | |
653 | cmd->out = -1; | |
051e4005 JH |
654 | |
655 | /* | |
7fcbd37f NTND |
656 | * If the next rev-list --stdin encounters an unknown commit, |
657 | * it terminates, which will cause SIGPIPE in the write loop | |
051e4005 JH |
658 | * below. |
659 | */ | |
660 | sigchain_push(SIGPIPE, SIG_IGN); | |
661 | ||
2997178e | 662 | if (start_command(cmd)) |
7fcbd37f NTND |
663 | goto error; |
664 | ||
a698d67b RS |
665 | cmd_in = xfdopen(cmd->in, "w"); |
666 | ||
051e4005 JH |
667 | for (i = get_max_object_index(); 0 < i; ) { |
668 | o = get_indexed_object(--i); | |
2a745324 BH |
669 | if (!o) |
670 | continue; | |
079aa97e NTND |
671 | if (reachable && o->type == OBJ_COMMIT) |
672 | o->flags &= ~TMP_MARK; | |
629060d9 | 673 | if (!is_our_ref(o, allow_uor)) |
051e4005 | 674 | continue; |
a698d67b | 675 | if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0) |
051e4005 JH |
676 | goto error; |
677 | } | |
3f0f6624 NTND |
678 | for (i = 0; i < src->nr; i++) { |
679 | o = src->objects[i].item; | |
629060d9 | 680 | if (is_our_ref(o, allow_uor)) { |
079aa97e NTND |
681 | if (reachable) |
682 | add_object_array(o, NULL, reachable); | |
051e4005 | 683 | continue; |
079aa97e NTND |
684 | } |
685 | if (reachable && o->type == OBJ_COMMIT) | |
686 | o->flags |= TMP_MARK; | |
a698d67b | 687 | if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0) |
051e4005 JH |
688 | goto error; |
689 | } | |
a698d67b RS |
690 | if (ferror(cmd_in) || fflush(cmd_in)) |
691 | goto error; | |
692 | fclose(cmd_in); | |
2997178e NTND |
693 | cmd->in = -1; |
694 | sigchain_pop(SIGPIPE); | |
051e4005 | 695 | |
2997178e NTND |
696 | return 0; |
697 | ||
698 | error: | |
051e4005 JH |
699 | sigchain_pop(SIGPIPE); |
700 | ||
a698d67b RS |
701 | if (cmd_in) |
702 | fclose(cmd_in); | |
2997178e NTND |
703 | if (cmd->out >= 0) |
704 | close(cmd->out); | |
705 | return -1; | |
706 | } | |
707 | ||
f1514c6a | 708 | static int get_reachable_list(struct upload_pack_data *data, |
079aa97e NTND |
709 | struct object_array *reachable) |
710 | { | |
711 | struct child_process cmd = CHILD_PROCESS_INIT; | |
712 | int i; | |
713 | struct object *o; | |
55dc227d | 714 | char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */ |
715 | const unsigned hexsz = the_hash_algo->hexsz; | |
079aa97e | 716 | |
f1514c6a | 717 | if (do_reachable_revlist(&cmd, &data->shallows, reachable, |
629060d9 | 718 | data->allow_uor) < 0) |
079aa97e NTND |
719 | return -1; |
720 | ||
55dc227d | 721 | while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) { |
62b89d43 | 722 | struct object_id oid; |
55dc227d | 723 | const char *p; |
079aa97e | 724 | |
62b89d43 | 725 | if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n') |
079aa97e NTND |
726 | break; |
727 | ||
d0229abd | 728 | o = lookup_object(the_repository, &oid); |
079aa97e NTND |
729 | if (o && o->type == OBJ_COMMIT) { |
730 | o->flags &= ~TMP_MARK; | |
731 | } | |
732 | } | |
733 | for (i = get_max_object_index(); 0 < i; i--) { | |
734 | o = get_indexed_object(i - 1); | |
735 | if (o && o->type == OBJ_COMMIT && | |
736 | (o->flags & TMP_MARK)) { | |
737 | add_object_array(o, NULL, reachable); | |
738 | o->flags &= ~TMP_MARK; | |
739 | } | |
740 | } | |
741 | close(cmd.out); | |
742 | ||
743 | if (finish_command(&cmd)) | |
744 | return -1; | |
745 | ||
746 | return 0; | |
747 | } | |
748 | ||
629060d9 | 749 | static int has_unreachable(struct object_array *src, enum allow_uor allow_uor) |
2997178e NTND |
750 | { |
751 | struct child_process cmd = CHILD_PROCESS_INIT; | |
752 | char buf[1]; | |
753 | int i; | |
754 | ||
629060d9 | 755 | if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0) |
2997178e | 756 | return 1; |
051e4005 JH |
757 | |
758 | /* | |
759 | * The commits out of the rev-list are not ancestors of | |
760 | * our ref. | |
761 | */ | |
2997178e | 762 | i = read_in_full(cmd.out, buf, 1); |
051e4005 JH |
763 | if (i) |
764 | goto error; | |
765 | close(cmd.out); | |
7fcbd37f | 766 | cmd.out = -1; |
051e4005 JH |
767 | |
768 | /* | |
769 | * rev-list may have died by encountering a bad commit | |
770 | * in the history, in which case we do want to bail out | |
771 | * even when it showed no commit. | |
772 | */ | |
773 | if (finish_command(&cmd)) | |
774 | goto error; | |
775 | ||
776 | /* All the non-tip ones are ancestors of what we advertised */ | |
3f0f6624 | 777 | return 0; |
051e4005 JH |
778 | |
779 | error: | |
7fcbd37f NTND |
780 | if (cmd.out >= 0) |
781 | close(cmd.out); | |
3f0f6624 NTND |
782 | return 1; |
783 | } | |
7fcbd37f | 784 | |
b08c9742 | 785 | static void check_non_tip(struct upload_pack_data *data) |
3f0f6624 NTND |
786 | { |
787 | int i; | |
788 | ||
789 | /* | |
790 | * In the normal in-process case without | |
791 | * uploadpack.allowReachableSHA1InWant, | |
792 | * non-tip requests can never happen. | |
793 | */ | |
629060d9 | 794 | if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1)) |
3f0f6624 | 795 | goto error; |
629060d9 | 796 | if (!has_unreachable(&data->want_obj, data->allow_uor)) |
3f0f6624 NTND |
797 | /* All the non-tip ones are ancestors of what we advertised */ |
798 | return; | |
051e4005 JH |
799 | |
800 | error: | |
801 | /* Pick one of them (we know there at least is one) */ | |
b08c9742 CC |
802 | for (i = 0; i < data->want_obj.nr; i++) { |
803 | struct object *o = data->want_obj.objects[i].item; | |
629060d9 | 804 | if (!is_our_ref(o, data->allow_uor)) { |
7ba7c52d DS |
805 | error("git upload-pack: not our ref %s", |
806 | oid_to_hex(&o->oid)); | |
b08c9742 | 807 | packet_writer_error(&data->writer, |
014ade74 JK |
808 | "upload-pack: not our ref %s", |
809 | oid_to_hex(&o->oid)); | |
5f33a843 | 810 | exit(128); |
014ade74 | 811 | } |
051e4005 JH |
812 | } |
813 | } | |
814 | ||
35b43a10 | 815 | static void send_shallow(struct upload_pack_data *data, |
bc2e795c | 816 | struct commit_list *result) |
5c24cdea NTND |
817 | { |
818 | while (result) { | |
819 | struct object *object = &result->item->object; | |
820 | if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) { | |
35b43a10 | 821 | packet_writer_write(&data->writer, "shallow %s", |
bc2e795c | 822 | oid_to_hex(&object->oid)); |
19143f13 | 823 | register_shallow(the_repository, &object->oid); |
35b43a10 | 824 | data->shallow_nr++; |
5c24cdea NTND |
825 | } |
826 | result = result->next; | |
827 | } | |
828 | } | |
829 | ||
329f9960 | 830 | static void send_unshallow(struct upload_pack_data *data) |
e8e44de7 | 831 | { |
e8e44de7 | 832 | int i; |
873700c9 | 833 | |
329f9960 CC |
834 | for (i = 0; i < data->shallows.nr; i++) { |
835 | struct object *object = data->shallows.objects[i].item; | |
e8e44de7 NTND |
836 | if (object->flags & NOT_SHALLOW) { |
837 | struct commit_list *parents; | |
329f9960 | 838 | packet_writer_write(&data->writer, "unshallow %s", |
bc2e795c | 839 | oid_to_hex(&object->oid)); |
e8e44de7 | 840 | object->flags &= ~CLIENT_SHALLOW; |
873700c9 NTND |
841 | /* |
842 | * We want to _register_ "object" as shallow, but we | |
843 | * also need to traverse object's parents to deepen a | |
844 | * shallow clone. Unregister it for now so we can | |
845 | * parse and add the parents to the want list, then | |
846 | * re-register it. | |
847 | */ | |
e92b848c | 848 | unregister_shallow(&object->oid); |
e8e44de7 NTND |
849 | object->parsed = 0; |
850 | parse_commit_or_die((struct commit *)object); | |
851 | parents = ((struct commit *)object)->parents; | |
852 | while (parents) { | |
853 | add_object_array(&parents->item->object, | |
329f9960 | 854 | NULL, &data->want_obj); |
e8e44de7 NTND |
855 | parents = parents->next; |
856 | } | |
de0e9f74 | 857 | add_object_array(object, NULL, &data->extra_edge_obj); |
e8e44de7 NTND |
858 | } |
859 | /* make sure commit traversal conforms to client */ | |
19143f13 | 860 | register_shallow(the_repository, &object->oid); |
e8e44de7 | 861 | } |
873700c9 NTND |
862 | } |
863 | ||
5056cf4a JT |
864 | static int check_ref(const char *refname_full, const struct object_id *oid, |
865 | int flag, void *cb_data); | |
b1492f22 | 866 | static void deepen(struct upload_pack_data *data, int depth) |
873700c9 | 867 | { |
c8813487 | 868 | if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) { |
873700c9 NTND |
869 | int i; |
870 | ||
b1492f22 CC |
871 | for (i = 0; i < data->shallows.nr; i++) { |
872 | struct object *object = data->shallows.objects[i].item; | |
873700c9 NTND |
873 | object->flags |= NOT_SHALLOW; |
874 | } | |
b1492f22 | 875 | } else if (data->deepen_relative) { |
cccf74e2 NTND |
876 | struct object_array reachable_shallows = OBJECT_ARRAY_INIT; |
877 | struct commit_list *result; | |
878 | ||
5056cf4a JT |
879 | /* |
880 | * Checking for reachable shallows requires that our refs be | |
881 | * marked with OUR_REF. | |
882 | */ | |
9b67eb6f | 883 | head_ref_namespaced(check_ref, data); |
18b6b1b5 | 884 | for_each_namespaced_ref_1(check_ref, data); |
5056cf4a | 885 | |
f1514c6a | 886 | get_reachable_list(data, &reachable_shallows); |
cccf74e2 NTND |
887 | result = get_shallow_commits(&reachable_shallows, |
888 | depth + 1, | |
889 | SHALLOW, NOT_SHALLOW); | |
35b43a10 | 890 | send_shallow(data, result); |
cccf74e2 NTND |
891 | free_commit_list(result); |
892 | object_array_clear(&reachable_shallows); | |
873700c9 NTND |
893 | } else { |
894 | struct commit_list *result; | |
895 | ||
b1492f22 | 896 | result = get_shallow_commits(&data->want_obj, depth, |
873700c9 | 897 | SHALLOW, NOT_SHALLOW); |
35b43a10 | 898 | send_shallow(data, result); |
873700c9 NTND |
899 | free_commit_list(result); |
900 | } | |
901 | ||
329f9960 | 902 | send_unshallow(data); |
e8e44de7 NTND |
903 | } |
904 | ||
446e42c5 CC |
905 | static void deepen_by_rev_list(struct upload_pack_data *data, |
906 | int ac, | |
907 | const char **av) | |
569e554b NTND |
908 | { |
909 | struct commit_list *result; | |
910 | ||
6abada18 | 911 | disable_commit_graph(the_repository); |
569e554b | 912 | result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW); |
35b43a10 | 913 | send_shallow(data, result); |
569e554b | 914 | free_commit_list(result); |
329f9960 | 915 | send_unshallow(data); |
685fbd32 BW |
916 | } |
917 | ||
918 | /* Returns 1 if a shallow list is sent or 0 otherwise */ | |
ee703c8a | 919 | static int send_shallow_list(struct upload_pack_data *data) |
685fbd32 BW |
920 | { |
921 | int ret = 0; | |
922 | ||
ee703c8a | 923 | if (data->depth > 0 && data->deepen_rev_list) |
685fbd32 | 924 | die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together"); |
ee703c8a | 925 | if (data->depth > 0) { |
b1492f22 | 926 | deepen(data, data->depth); |
685fbd32 | 927 | ret = 1; |
ee703c8a | 928 | } else if (data->deepen_rev_list) { |
c972bf4c | 929 | struct strvec av = STRVEC_INIT; |
685fbd32 BW |
930 | int i; |
931 | ||
c972bf4c | 932 | strvec_push(&av, "rev-list"); |
ee703c8a | 933 | if (data->deepen_since) |
c972bf4c | 934 | strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since); |
ee703c8a | 935 | if (data->deepen_not.nr) { |
c972bf4c | 936 | strvec_push(&av, "--not"); |
ee703c8a CC |
937 | for (i = 0; i < data->deepen_not.nr; i++) { |
938 | struct string_list_item *s = data->deepen_not.items + i; | |
c972bf4c | 939 | strvec_push(&av, s->string); |
685fbd32 | 940 | } |
c972bf4c | 941 | strvec_push(&av, "--not"); |
685fbd32 | 942 | } |
ee703c8a CC |
943 | for (i = 0; i < data->want_obj.nr; i++) { |
944 | struct object *o = data->want_obj.objects[i].item; | |
c972bf4c | 945 | strvec_push(&av, oid_to_hex(&o->oid)); |
685fbd32 | 946 | } |
d70a9eb6 | 947 | deepen_by_rev_list(data, av.nr, av.v); |
c972bf4c | 948 | strvec_clear(&av); |
685fbd32 BW |
949 | ret = 1; |
950 | } else { | |
ee703c8a | 951 | if (data->shallows.nr > 0) { |
685fbd32 | 952 | int i; |
ee703c8a | 953 | for (i = 0; i < data->shallows.nr; i++) |
00624d60 | 954 | register_shallow(the_repository, |
ee703c8a | 955 | &data->shallows.objects[i].item->oid); |
685fbd32 BW |
956 | } |
957 | } | |
958 | ||
35b43a10 | 959 | data->shallow_nr += data->shallows.nr; |
685fbd32 | 960 | return ret; |
569e554b NTND |
961 | } |
962 | ||
ae2948f3 BW |
963 | static int process_shallow(const char *line, struct object_array *shallows) |
964 | { | |
965 | const char *arg; | |
966 | if (skip_prefix(line, "shallow ", &arg)) { | |
967 | struct object_id oid; | |
968 | struct object *object; | |
969 | if (get_oid_hex(arg, &oid)) | |
970 | die("invalid shallow line: %s", line); | |
109cd76d | 971 | object = parse_object(the_repository, &oid); |
ae2948f3 BW |
972 | if (!object) |
973 | return 1; | |
974 | if (object->type != OBJ_COMMIT) | |
975 | die("invalid shallow object %s", oid_to_hex(&oid)); | |
976 | if (!(object->flags & CLIENT_SHALLOW)) { | |
977 | object->flags |= CLIENT_SHALLOW; | |
978 | add_object_array(object, NULL, shallows); | |
979 | } | |
980 | return 1; | |
981 | } | |
982 | ||
983 | return 0; | |
984 | } | |
985 | ||
986 | static int process_deepen(const char *line, int *depth) | |
987 | { | |
988 | const char *arg; | |
989 | if (skip_prefix(line, "deepen ", &arg)) { | |
990 | char *end = NULL; | |
991 | *depth = (int)strtol(arg, &end, 0); | |
992 | if (!end || *end || *depth <= 0) | |
993 | die("Invalid deepen: %s", line); | |
994 | return 1; | |
995 | } | |
996 | ||
997 | return 0; | |
998 | } | |
999 | ||
1000 | static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list) | |
1001 | { | |
1002 | const char *arg; | |
1003 | if (skip_prefix(line, "deepen-since ", &arg)) { | |
1004 | char *end = NULL; | |
1005 | *deepen_since = parse_timestamp(arg, &end, 0); | |
1006 | if (!end || *end || !deepen_since || | |
1007 | /* revisions.c's max_age -1 is special */ | |
1008 | *deepen_since == -1) | |
1009 | die("Invalid deepen-since: %s", line); | |
1010 | *deepen_rev_list = 1; | |
1011 | return 1; | |
1012 | } | |
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list) | |
1017 | { | |
1018 | const char *arg; | |
1019 | if (skip_prefix(line, "deepen-not ", &arg)) { | |
1020 | char *ref = NULL; | |
1021 | struct object_id oid; | |
0b1dbf53 | 1022 | if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1) |
ae2948f3 BW |
1023 | die("git upload-pack: ambiguous deepen-not: %s", line); |
1024 | string_list_append(deepen_not, ref); | |
1025 | free(ref); | |
1026 | *deepen_rev_list = 1; | |
1027 | return 1; | |
1028 | } | |
1029 | return 0; | |
569e554b NTND |
1030 | } |
1031 | ||
6dd3456a TB |
1032 | NORETURN __attribute__((format(printf,2,3))) |
1033 | static void send_err_and_die(struct upload_pack_data *data, | |
1034 | const char *fmt, ...) | |
1035 | { | |
1036 | struct strbuf buf = STRBUF_INIT; | |
1037 | va_list ap; | |
1038 | ||
1039 | va_start(ap, fmt); | |
1040 | strbuf_vaddf(&buf, fmt, ap); | |
1041 | va_end(ap); | |
1042 | ||
1043 | packet_writer_error(&data->writer, "%s", buf.buf); | |
1044 | die("%s", buf.buf); | |
1045 | } | |
1046 | ||
1047 | static void check_one_filter(struct upload_pack_data *data, | |
1048 | struct list_objects_filter_options *opts) | |
1049 | { | |
1050 | const char *key = list_object_filter_config_name(opts->choice); | |
1051 | struct string_list_item *item = string_list_lookup(&data->allowed_filters, | |
1052 | key); | |
1053 | int allowed; | |
1054 | ||
1055 | if (item) | |
1056 | allowed = (intptr_t)item->util; | |
1057 | else | |
1058 | allowed = data->allow_filter_fallback; | |
1059 | ||
1060 | if (!allowed) | |
1061 | send_err_and_die(data, "filter '%s' not supported", key); | |
5b01a4e8 TB |
1062 | |
1063 | if (opts->choice == LOFC_TREE_DEPTH && | |
1064 | opts->tree_exclude_depth > data->tree_filter_max_depth) | |
1065 | send_err_and_die(data, | |
1066 | "tree filter allows max depth %lu, but got %lu", | |
1067 | data->tree_filter_max_depth, | |
1068 | opts->tree_exclude_depth); | |
6dd3456a TB |
1069 | } |
1070 | ||
1071 | static void check_filter_recurse(struct upload_pack_data *data, | |
1072 | struct list_objects_filter_options *opts) | |
1073 | { | |
1074 | size_t i; | |
1075 | ||
1076 | check_one_filter(data, opts); | |
1077 | if (opts->choice != LOFC_COMBINE) | |
1078 | return; | |
1079 | ||
1080 | for (i = 0; i < opts->sub_nr; i++) | |
1081 | check_filter_recurse(data, &opts->sub[i]); | |
1082 | } | |
1083 | ||
1084 | static void die_if_using_banned_filter(struct upload_pack_data *data) | |
1085 | { | |
1086 | check_filter_recurse(data, &data->filter_options); | |
1087 | } | |
1088 | ||
d92ae2c0 CC |
1089 | static void receive_needs(struct upload_pack_data *data, |
1090 | struct packet_reader *reader) | |
fb9040cc | 1091 | { |
051e4005 | 1092 | int has_non_tip = 0; |
fb9040cc | 1093 | |
35b43a10 | 1094 | data->shallow_nr = 0; |
fb9040cc | 1095 | for (;;) { |
565ebbf7 | 1096 | struct object *o; |
f47182c8 | 1097 | const char *features; |
cf93982f | 1098 | struct object_id oid_buf; |
8bf3b758 | 1099 | const char *arg; |
7ce4c8f7 | 1100 | size_t feature_len; |
8bf3b758 | 1101 | |
d40f04e0 | 1102 | reset_timeout(data->timeout); |
01f9ec64 | 1103 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
ed09aef0 | 1104 | break; |
e091eb93 | 1105 | |
7a516764 | 1106 | if (process_shallow(reader->line, &data->shallows)) |
ed09aef0 | 1107 | continue; |
7a516764 | 1108 | if (process_deepen(reader->line, &data->depth)) |
016e6ccb | 1109 | continue; |
7a516764 | 1110 | if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list)) |
569e554b | 1111 | continue; |
7a516764 | 1112 | if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list)) |
269a7a83 | 1113 | continue; |
ae2948f3 | 1114 | |
01f9ec64 | 1115 | if (skip_prefix(reader->line, "filter ", &arg)) { |
59a90261 | 1116 | if (!data->filter_capability_requested) |
10ac85c7 | 1117 | die("git upload-pack: filtering capability not negotiated"); |
d92ae2c0 CC |
1118 | list_objects_filter_die_if_populated(&data->filter_options); |
1119 | parse_list_objects_filter(&data->filter_options, arg); | |
6dd3456a | 1120 | die_if_using_banned_filter(data); |
10ac85c7 JH |
1121 | continue; |
1122 | } | |
9bfa0f9b | 1123 | |
01f9ec64 | 1124 | if (!skip_prefix(reader->line, "want ", &arg) || |
55dc227d | 1125 | parse_oid_hex(arg, &oid_buf, &features)) |
7e44c935 | 1126 | die("git upload-pack: protocol error, " |
01f9ec64 | 1127 | "expected to get object ID, not '%s'", reader->line); |
f47182c8 | 1128 | |
cccf74e2 | 1129 | if (parse_feature_request(features, "deepen-relative")) |
7a516764 | 1130 | data->deepen_relative = 1; |
f47182c8 | 1131 | if (parse_feature_request(features, "multi_ack_detailed")) |
e9d882b8 | 1132 | data->multi_ack = MULTI_ACK_DETAILED; |
f47182c8 | 1133 | else if (parse_feature_request(features, "multi_ack")) |
e9d882b8 | 1134 | data->multi_ack = MULTI_ACK; |
f47182c8 | 1135 | if (parse_feature_request(features, "no-done")) |
d40f04e0 | 1136 | data->no_done = 1; |
f47182c8 | 1137 | if (parse_feature_request(features, "thin-pack")) |
b5a2068c | 1138 | data->use_thin_pack = 1; |
f47182c8 | 1139 | if (parse_feature_request(features, "ofs-delta")) |
b5a2068c | 1140 | data->use_ofs_delta = 1; |
f47182c8 | 1141 | if (parse_feature_request(features, "side-band-64k")) |
f8edd1ca | 1142 | data->use_sideband = LARGE_PACKET_MAX; |
f47182c8 | 1143 | else if (parse_feature_request(features, "side-band")) |
f8edd1ca | 1144 | data->use_sideband = DEFAULT_PACKET_MAX; |
f47182c8 | 1145 | if (parse_feature_request(features, "no-progress")) |
b5a2068c | 1146 | data->no_progress = 1; |
f47182c8 | 1147 | if (parse_feature_request(features, "include-tag")) |
b5a2068c | 1148 | data->use_include_tag = 1; |
59abe196 CC |
1149 | if (data->allow_filter && |
1150 | parse_feature_request(features, "filter")) | |
59a90261 | 1151 | data->filter_capability_requested = 1; |
565ebbf7 | 1152 | |
82959467 JS |
1153 | arg = parse_feature_value(features, "session-id", &feature_len, NULL); |
1154 | if (arg) { | |
1155 | char *client_sid = xstrndup(arg, feature_len); | |
1156 | trace2_data_string("transfer", NULL, "client-sid", client_sid); | |
1157 | free(client_sid); | |
1158 | } | |
1159 | ||
109cd76d | 1160 | o = parse_object(the_repository, &oid_buf); |
bdb31ead | 1161 | if (!o) { |
4ace0283 | 1162 | packet_writer_error(&data->writer, |
bc2e795c JT |
1163 | "upload-pack: not our ref %s", |
1164 | oid_to_hex(&oid_buf)); | |
9f9aa761 | 1165 | die("git upload-pack: not our ref %s", |
cf93982f | 1166 | oid_to_hex(&oid_buf)); |
bdb31ead | 1167 | } |
565ebbf7 JH |
1168 | if (!(o->flags & WANTED)) { |
1169 | o->flags |= WANTED; | |
629060d9 CC |
1170 | if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1 |
1171 | || is_our_ref(o, data->allow_uor))) | |
051e4005 | 1172 | has_non_tip = 1; |
d92ae2c0 | 1173 | add_object_array(o, NULL, &data->want_obj); |
565ebbf7 | 1174 | } |
fb9040cc | 1175 | } |
9462e3f5 | 1176 | |
051e4005 JH |
1177 | /* |
1178 | * We have sent all our refs already, and the other end | |
1179 | * should have chosen out of them. When we are operating | |
1180 | * in the stateless RPC mode, however, their choice may | |
1181 | * have been based on the set of older refs advertised | |
1182 | * by another process that handled the initial request. | |
1183 | */ | |
1184 | if (has_non_tip) | |
b08c9742 | 1185 | check_non_tip(data); |
051e4005 | 1186 | |
f8edd1ca | 1187 | if (!data->use_sideband && data->daemon_mode) |
b5a2068c | 1188 | data->no_progress = 1; |
9462e3f5 | 1189 | |
7a516764 | 1190 | if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0) |
f53514bc | 1191 | return; |
569e554b | 1192 | |
ee703c8a | 1193 | if (send_shallow_list(data)) |
685fbd32 | 1194 | packet_flush(1); |
fb9040cc LT |
1195 | } |
1196 | ||
daebaa78 | 1197 | /* return non-zero if the ref is hidden, otherwise 0 */ |
78a766ab | 1198 | static int mark_our_ref(const char *refname, const char *refname_full, |
c45841ff | 1199 | const struct object_id *oid, const struct strvec *hidden_refs) |
cbbe50db | 1200 | { |
45a187cc | 1201 | struct object *o = lookup_unknown_object(the_repository, oid); |
daebaa78 | 1202 | |
9b67eb6f | 1203 | if (ref_is_hidden(refname, refname_full, hidden_refs)) { |
390eb36b | 1204 | o->flags |= HIDDEN_REF; |
daebaa78 | 1205 | return 1; |
390eb36b | 1206 | } |
3f1da57f | 1207 | o->flags |= OUR_REF; |
cbbe50db JH |
1208 | return 0; |
1209 | } | |
1210 | ||
78a766ab | 1211 | static int check_ref(const char *refname_full, const struct object_id *oid, |
9b67eb6f | 1212 | int flag UNUSED, void *cb_data) |
e172755b | 1213 | { |
78a766ab | 1214 | const char *refname = strip_namespace(refname_full); |
9b67eb6f | 1215 | struct upload_pack_data *data = cb_data; |
78a766ab | 1216 | |
9b67eb6f | 1217 | mark_our_ref(refname, refname_full, oid, &data->hidden_refs); |
e172755b JK |
1218 | return 0; |
1219 | } | |
1220 | ||
7171d8c1 JH |
1221 | static void format_symref_info(struct strbuf *buf, struct string_list *symref) |
1222 | { | |
1223 | struct string_list_item *item; | |
1224 | ||
1225 | if (!symref->nr) | |
1226 | return; | |
1227 | for_each_string_list_item(item, symref) | |
1228 | strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util); | |
1229 | } | |
1230 | ||
791e1adf JS |
1231 | static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) { |
1232 | if (d->advertise_sid) | |
1233 | strbuf_addf(buf, " session-id=%s", trace2_session_id()); | |
1234 | } | |
1235 | ||
933e3a4e | 1236 | static void write_v0_ref(struct upload_pack_data *data, |
1237 | const char *refname, const char *refname_nons, | |
1238 | const struct object_id *oid) | |
def88e9a | 1239 | { |
ed09aef0 | 1240 | static const char *capabilities = "multi_ack thin-pack side-band" |
cccf74e2 NTND |
1241 | " side-band-64k ofs-delta shallow deepen-since deepen-not" |
1242 | " deepen-relative no-progress include-tag multi_ack_detailed"; | |
21758aff | 1243 | struct object_id peeled; |
b5b16990 | 1244 | |
9b67eb6f | 1245 | if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs)) |
933e3a4e | 1246 | return; |
cbbe50db | 1247 | |
7171d8c1 JH |
1248 | if (capabilities) { |
1249 | struct strbuf symref_info = STRBUF_INIT; | |
791e1adf | 1250 | struct strbuf session_id = STRBUF_INIT; |
7171d8c1 | 1251 | |
762f9276 | 1252 | format_symref_info(&symref_info, &data->symref); |
791e1adf | 1253 | format_session_id(&session_id, data); |
70afef5c | 1254 | packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n", |
363e98bf | 1255 | oid_to_hex(oid), refname_nons, |
cf2ad8e6 | 1256 | 0, capabilities, |
629060d9 | 1257 | (data->allow_uor & ALLOW_TIP_SHA1) ? |
7199c093 | 1258 | " allow-tip-sha1-in-want" : "", |
629060d9 | 1259 | (data->allow_uor & ALLOW_REACHABLE_SHA1) ? |
68ee6289 | 1260 | " allow-reachable-sha1-in-want" : "", |
f234da80 | 1261 | data->no_done ? " no-done" : "", |
7171d8c1 | 1262 | symref_info.buf, |
59abe196 | 1263 | data->allow_filter ? " filter" : "", |
791e1adf | 1264 | session_id.buf, |
bf30dbf8 | 1265 | the_hash_algo->name, |
ff5effdf | 1266 | git_user_agent_sanitized()); |
7171d8c1 | 1267 | strbuf_release(&symref_info); |
791e1adf | 1268 | strbuf_release(&session_id); |
933e3a4e | 1269 | data->sent_capabilities = 1; |
7171d8c1 | 1270 | } else { |
70afef5c | 1271 | packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons); |
7171d8c1 | 1272 | } |
1f5881bb | 1273 | capabilities = NULL; |
36a31792 | 1274 | if (!peel_iterated_oid(oid, &peeled)) |
70afef5c | 1275 | packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons); |
933e3a4e | 1276 | return; |
1277 | } | |
1278 | ||
1279 | static int send_ref(const char *refname, const struct object_id *oid, | |
1280 | int flag UNUSED, void *cb_data) | |
1281 | { | |
1282 | write_v0_ref(cb_data, refname, strip_namespace(refname), oid); | |
def88e9a LT |
1283 | return 0; |
1284 | } | |
1285 | ||
63e14ee2 | 1286 | static int find_symref(const char *refname, |
5cf88fd8 | 1287 | const struct object_id *oid UNUSED, |
7dabd056 | 1288 | int flag, void *cb_data) |
7171d8c1 JH |
1289 | { |
1290 | const char *symref_target; | |
1291 | struct string_list_item *item; | |
7171d8c1 JH |
1292 | |
1293 | if ((flag & REF_ISSYMREF) == 0) | |
1294 | return 0; | |
744c040b | 1295 | symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag); |
7171d8c1 JH |
1296 | if (!symref_target || (flag & REF_ISSYMREF) == 0) |
1297 | die("'%s' is a symref but it is not?", refname); | |
533e0882 JK |
1298 | item = string_list_append(cb_data, strip_namespace(refname)); |
1299 | item->util = xstrdup(strip_namespace(symref_target)); | |
7171d8c1 JH |
1300 | return 0; |
1301 | } | |
1302 | ||
6dd3456a | 1303 | static int parse_object_filter_config(const char *var, const char *value, |
8868b1eb GC |
1304 | const struct key_value_info *kvi, |
1305 | struct upload_pack_data *data) | |
6dd3456a TB |
1306 | { |
1307 | struct strbuf buf = STRBUF_INIT; | |
1308 | const char *sub, *key; | |
1309 | size_t sub_len; | |
1310 | ||
1311 | if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key)) | |
1312 | return 0; | |
1313 | ||
1314 | if (!sub) { | |
1315 | if (!strcmp(key, "allow")) | |
1316 | data->allow_filter_fallback = git_config_bool(var, value); | |
1317 | return 0; | |
1318 | } | |
1319 | ||
1320 | strbuf_add(&buf, sub, sub_len); | |
1321 | ||
1322 | if (!strcmp(key, "allow")) | |
1323 | string_list_insert(&data->allowed_filters, buf.buf)->util = | |
1324 | (void *)(intptr_t)git_config_bool(var, value); | |
5b01a4e8 TB |
1325 | else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) { |
1326 | if (!value) { | |
1327 | strbuf_release(&buf); | |
1328 | return config_error_nonbool(var); | |
1329 | } | |
1330 | string_list_insert(&data->allowed_filters, buf.buf)->util = | |
1331 | (void *)(intptr_t)1; | |
8868b1eb GC |
1332 | data->tree_filter_max_depth = git_config_ulong(var, value, |
1333 | kvi); | |
5b01a4e8 | 1334 | } |
6dd3456a TB |
1335 | |
1336 | strbuf_release(&buf); | |
1337 | return 0; | |
1338 | } | |
1339 | ||
a4e7e317 | 1340 | static int upload_pack_config(const char *var, const char *value, |
8868b1eb | 1341 | const struct config_context *ctx, |
a4e7e317 | 1342 | void *cb_data) |
daebaa78 | 1343 | { |
f203a88c CC |
1344 | struct upload_pack_data *data = cb_data; |
1345 | ||
7199c093 FM |
1346 | if (!strcmp("uploadpack.allowtipsha1inwant", var)) { |
1347 | if (git_config_bool(var, value)) | |
629060d9 | 1348 | data->allow_uor |= ALLOW_TIP_SHA1; |
7199c093 | 1349 | else |
629060d9 | 1350 | data->allow_uor &= ~ALLOW_TIP_SHA1; |
68ee6289 FM |
1351 | } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) { |
1352 | if (git_config_bool(var, value)) | |
629060d9 | 1353 | data->allow_uor |= ALLOW_REACHABLE_SHA1; |
68ee6289 | 1354 | else |
629060d9 | 1355 | data->allow_uor &= ~ALLOW_REACHABLE_SHA1; |
f8edeaa0 DT |
1356 | } else if (!strcmp("uploadpack.allowanysha1inwant", var)) { |
1357 | if (git_config_bool(var, value)) | |
629060d9 | 1358 | data->allow_uor |= ALLOW_ANY_SHA1; |
f8edeaa0 | 1359 | else |
629060d9 | 1360 | data->allow_uor &= ~ALLOW_ANY_SHA1; |
7199c093 | 1361 | } else if (!strcmp("uploadpack.keepalive", var)) { |
8868b1eb | 1362 | data->keepalive = git_config_int(var, value, ctx->kvi); |
f203a88c CC |
1363 | if (!data->keepalive) |
1364 | data->keepalive = -1; | |
10ac85c7 | 1365 | } else if (!strcmp("uploadpack.allowfilter", var)) { |
59abe196 | 1366 | data->allow_filter = git_config_bool(var, value); |
516e2b76 | 1367 | } else if (!strcmp("uploadpack.allowrefinwant", var)) { |
d1d7a945 | 1368 | data->allow_ref_in_want = git_config_bool(var, value); |
0bbc0bc5 | 1369 | } else if (!strcmp("uploadpack.allowsidebandall", var)) { |
e3835cd4 | 1370 | data->allow_sideband_all = git_config_bool(var, value); |
8e712ef6 EN |
1371 | } else if (!strcmp("core.precomposeunicode", var)) { |
1372 | precomposed_unicode = git_config_bool(var, value); | |
791e1adf JS |
1373 | } else if (!strcmp("transfer.advertisesid", var)) { |
1374 | data->advertise_sid = git_config_bool(var, value); | |
05e95155 | 1375 | } |
aaaa8818 | 1376 | |
8868b1eb | 1377 | if (parse_object_filter_config(var, value, ctx->kvi, data) < 0) |
d43a21bd | 1378 | return -1; |
6dd3456a | 1379 | |
9b67eb6f | 1380 | return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs); |
daebaa78 JH |
1381 | } |
1382 | ||
a4e7e317 GC |
1383 | static int upload_pack_protected_config(const char *var, const char *value, |
1384 | const struct config_context *ctx UNUSED, | |
1385 | void *cb_data) | |
5b3c6507 GC |
1386 | { |
1387 | struct upload_pack_data *data = cb_data; | |
1388 | ||
1389 | if (!strcmp("uploadpack.packobjectshook", var)) | |
1390 | return git_config_string(&data->pack_objects_hook, var, value); | |
1391 | return 0; | |
1392 | } | |
1393 | ||
1394 | static void get_upload_pack_config(struct upload_pack_data *data) | |
1395 | { | |
1396 | git_config(upload_pack_config, data); | |
1397 | git_protected_config(upload_pack_protected_config, data); | |
1398 | } | |
1399 | ||
f234da80 ÆAB |
1400 | void upload_pack(const int advertise_refs, const int stateless_rpc, |
1401 | const int timeout) | |
def88e9a | 1402 | { |
01f9ec64 | 1403 | struct packet_reader reader; |
ebf8ebcc | 1404 | struct upload_pack_data data; |
960deccb | 1405 | |
ebf8ebcc | 1406 | upload_pack_data_init(&data); |
5b3c6507 | 1407 | get_upload_pack_config(&data); |
8a0e6f16 | 1408 | |
f234da80 ÆAB |
1409 | data.stateless_rpc = stateless_rpc; |
1410 | data.timeout = timeout; | |
1411 | if (data.timeout) | |
1412 | data.daemon_mode = 1; | |
df654abc | 1413 | |
438528f6 | 1414 | head_ref_namespaced(find_symref, &data.symref); |
a6080a0a | 1415 | |
f234da80 | 1416 | if (advertise_refs || !data.stateless_rpc) { |
d40f04e0 | 1417 | reset_timeout(data.timeout); |
f234da80 ÆAB |
1418 | if (advertise_refs) |
1419 | data.no_done = 1; | |
762f9276 | 1420 | head_ref_namespaced(send_ref, &data); |
18b6b1b5 | 1421 | for_each_namespaced_ref_1(send_ref, &data); |
933e3a4e | 1422 | if (!data.sent_capabilities) { |
1423 | const char *refname = "capabilities^{}"; | |
1424 | write_v0_ref(&data, refname, refname, null_oid()); | |
1425 | } | |
70afef5c JV |
1426 | /* |
1427 | * fflush stdout before calling advertise_shallow_grafts because send_ref | |
1428 | * uses stdio. | |
1429 | */ | |
1430 | fflush_or_die(stdout); | |
a3d6b53e BW |
1431 | advertise_shallow_grafts(1); |
1432 | packet_flush(1); | |
1433 | } else { | |
9b67eb6f | 1434 | head_ref_namespaced(check_ref, &data); |
18b6b1b5 | 1435 | for_each_namespaced_ref_1(check_ref, &data); |
aa9bab29 | 1436 | } |
01f9ec64 | 1437 | |
f234da80 | 1438 | if (!advertise_refs) { |
ebf8ebcc CC |
1439 | packet_reader_init(&reader, 0, NULL, 0, |
1440 | PACKET_READ_CHOMP_NEWLINE | | |
1441 | PACKET_READ_DIE_ON_ERR_PACKET); | |
1442 | ||
d92ae2c0 | 1443 | receive_needs(&data, &reader); |
fb3d1a08 DD |
1444 | |
1445 | /* | |
1446 | * An EOF at this exact point in negotiation should be | |
1447 | * acceptable from stateless clients as they will consume the | |
1448 | * shallow list before doing subsequent rpc with haves/etc. | |
1449 | */ | |
1450 | if (data.stateless_rpc) | |
1451 | reader.options |= PACKET_READ_GENTLE_ON_EOF; | |
1452 | ||
1453 | if (data.want_obj.nr && | |
1454 | packet_reader_peek(&reader) != PACKET_READ_EOF) { | |
1455 | reader.options &= ~PACKET_READ_GENTLE_ON_EOF; | |
07977695 | 1456 | get_common_commits(&data, &reader); |
cae2ee10 | 1457 | create_pack_file(&data, NULL); |
ebf8ebcc | 1458 | } |
a3d6b53e | 1459 | } |
08450ef7 | 1460 | |
ebf8ebcc | 1461 | upload_pack_data_clear(&data); |
def88e9a | 1462 | } |
04b33055 | 1463 | |
bc2e795c JT |
1464 | static int parse_want(struct packet_writer *writer, const char *line, |
1465 | struct object_array *want_obj) | |
3145ea95 BW |
1466 | { |
1467 | const char *arg; | |
1468 | if (skip_prefix(line, "want ", &arg)) { | |
1469 | struct object_id oid; | |
1470 | struct object *o; | |
1471 | ||
1472 | if (get_oid_hex(arg, &oid)) | |
1473 | die("git upload-pack: protocol error, " | |
1474 | "expected to get oid, not '%s'", line); | |
1475 | ||
9a8c3c4a JK |
1476 | o = parse_object_with_flags(the_repository, &oid, |
1477 | PARSE_OBJECT_SKIP_HASH_CHECK); | |
4de65626 | 1478 | |
3145ea95 | 1479 | if (!o) { |
bc2e795c JT |
1480 | packet_writer_error(writer, |
1481 | "upload-pack: not our ref %s", | |
1482 | oid_to_hex(&oid)); | |
3145ea95 BW |
1483 | die("git upload-pack: not our ref %s", |
1484 | oid_to_hex(&oid)); | |
1485 | } | |
1486 | ||
1487 | if (!(o->flags & WANTED)) { | |
1488 | o->flags |= WANTED; | |
1d1243fe | 1489 | add_object_array(o, NULL, want_obj); |
3145ea95 BW |
1490 | } |
1491 | ||
1492 | return 1; | |
1493 | } | |
1494 | ||
1495 | return 0; | |
1496 | } | |
1497 | ||
bc2e795c JT |
1498 | static int parse_want_ref(struct packet_writer *writer, const char *line, |
1499 | struct string_list *wanted_refs, | |
c45841ff | 1500 | struct strvec *hidden_refs, |
1d1243fe | 1501 | struct object_array *want_obj) |
516e2b76 | 1502 | { |
39551406 KA |
1503 | const char *refname_nons; |
1504 | if (skip_prefix(line, "want-ref ", &refname_nons)) { | |
516e2b76 BW |
1505 | struct object_id oid; |
1506 | struct string_list_item *item; | |
4de65626 | 1507 | struct object *o = NULL; |
39551406 | 1508 | struct strbuf refname = STRBUF_INIT; |
516e2b76 | 1509 | |
39551406 | 1510 | strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons); |
9b67eb6f | 1511 | if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) || |
39551406 KA |
1512 | read_ref(refname.buf, &oid)) { |
1513 | packet_writer_error(writer, "unknown ref %s", refname_nons); | |
1514 | die("unknown ref %s", refname_nons); | |
516e2b76 | 1515 | } |
39551406 | 1516 | strbuf_release(&refname); |
516e2b76 | 1517 | |
39551406 | 1518 | item = string_list_append(wanted_refs, refname_nons); |
516e2b76 BW |
1519 | item->util = oiddup(&oid); |
1520 | ||
4de65626 PS |
1521 | if (!starts_with(refname_nons, "refs/tags/")) { |
1522 | struct commit *commit = lookup_commit_in_graph(the_repository, &oid); | |
1523 | if (commit) | |
1524 | o = &commit->object; | |
1525 | } | |
1526 | ||
1527 | if (!o) | |
1528 | o = parse_object_or_die(&oid, refname_nons); | |
1529 | ||
516e2b76 BW |
1530 | if (!(o->flags & WANTED)) { |
1531 | o->flags |= WANTED; | |
1d1243fe | 1532 | add_object_array(o, NULL, want_obj); |
516e2b76 BW |
1533 | } |
1534 | ||
1535 | return 1; | |
1536 | } | |
1537 | ||
1538 | return 0; | |
1539 | } | |
1540 | ||
3145ea95 BW |
1541 | static int parse_have(const char *line, struct oid_array *haves) |
1542 | { | |
1543 | const char *arg; | |
1544 | if (skip_prefix(line, "have ", &arg)) { | |
1545 | struct object_id oid; | |
1546 | ||
1547 | if (get_oid_hex(arg, &oid)) | |
1548 | die("git upload-pack: expected SHA1 object, got '%s'", arg); | |
1549 | oid_array_append(haves, &oid); | |
1550 | return 1; | |
aa9bab29 BW |
1551 | } |
1552 | ||
def88e9a LT |
1553 | return 0; |
1554 | } | |
3145ea95 | 1555 | |
b8f58c20 RC |
1556 | static void trace2_fetch_info(struct upload_pack_data *data) |
1557 | { | |
1558 | struct json_writer jw = JSON_WRITER_INIT; | |
1559 | ||
1560 | jw_object_begin(&jw, 0); | |
1561 | jw_object_intmax(&jw, "haves", data->haves.nr); | |
1562 | jw_object_intmax(&jw, "wants", data->want_obj.nr); | |
1563 | jw_object_intmax(&jw, "want-refs", data->wanted_refs.nr); | |
1564 | jw_object_intmax(&jw, "depth", data->depth); | |
1565 | jw_object_intmax(&jw, "shallows", data->shallows.nr); | |
1566 | jw_object_bool(&jw, "deepen-since", data->deepen_since); | |
1567 | jw_object_intmax(&jw, "deepen-not", data->deepen_not.nr); | |
1568 | jw_object_bool(&jw, "deepen-relative", data->deepen_relative); | |
1569 | if (data->filter_options.choice) | |
1570 | jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice)); | |
1571 | else | |
1572 | jw_object_null(&jw, "filter"); | |
1573 | jw_end(&jw); | |
1574 | ||
1575 | trace2_data_json("upload-pack", the_repository, "fetch-info", &jw); | |
1576 | ||
1577 | jw_release(&jw); | |
1578 | } | |
1579 | ||
3145ea95 | 1580 | static void process_args(struct packet_reader *request, |
389f161a | 1581 | struct upload_pack_data *data) |
3145ea95 | 1582 | { |
4845b772 | 1583 | while (packet_reader_read(request) == PACKET_READ_NORMAL) { |
3145ea95 | 1584 | const char *arg = request->line; |
ba95710a | 1585 | const char *p; |
3145ea95 BW |
1586 | |
1587 | /* process want */ | |
389f161a | 1588 | if (parse_want(&data->writer, arg, &data->want_obj)) |
3145ea95 | 1589 | continue; |
d1d7a945 | 1590 | if (data->allow_ref_in_want && |
bc2e795c | 1591 | parse_want_ref(&data->writer, arg, &data->wanted_refs, |
9b67eb6f | 1592 | &data->hidden_refs, &data->want_obj)) |
516e2b76 | 1593 | continue; |
3145ea95 BW |
1594 | /* process have line */ |
1595 | if (parse_have(arg, &data->haves)) | |
1596 | continue; | |
1597 | ||
1598 | /* process args like thin-pack */ | |
1599 | if (!strcmp(arg, "thin-pack")) { | |
b5a2068c | 1600 | data->use_thin_pack = 1; |
3145ea95 BW |
1601 | continue; |
1602 | } | |
1603 | if (!strcmp(arg, "ofs-delta")) { | |
b5a2068c | 1604 | data->use_ofs_delta = 1; |
3145ea95 BW |
1605 | continue; |
1606 | } | |
1607 | if (!strcmp(arg, "no-progress")) { | |
b5a2068c | 1608 | data->no_progress = 1; |
3145ea95 BW |
1609 | continue; |
1610 | } | |
1611 | if (!strcmp(arg, "include-tag")) { | |
b5a2068c | 1612 | data->use_include_tag = 1; |
3145ea95 BW |
1613 | continue; |
1614 | } | |
1615 | if (!strcmp(arg, "done")) { | |
1616 | data->done = 1; | |
1617 | continue; | |
1618 | } | |
9c1e657a JT |
1619 | if (!strcmp(arg, "wait-for-done")) { |
1620 | data->wait_for_done = 1; | |
1621 | continue; | |
1622 | } | |
3145ea95 | 1623 | |
685fbd32 BW |
1624 | /* Shallow related arguments */ |
1625 | if (process_shallow(arg, &data->shallows)) | |
1626 | continue; | |
1627 | if (process_deepen(arg, &data->depth)) | |
1628 | continue; | |
1629 | if (process_deepen_since(arg, &data->deepen_since, | |
1630 | &data->deepen_rev_list)) | |
1631 | continue; | |
1632 | if (process_deepen_not(arg, &data->deepen_not, | |
1633 | &data->deepen_rev_list)) | |
1634 | continue; | |
1635 | if (!strcmp(arg, "deepen-relative")) { | |
1636 | data->deepen_relative = 1; | |
1637 | continue; | |
1638 | } | |
1639 | ||
59abe196 | 1640 | if (data->allow_filter && skip_prefix(arg, "filter ", &p)) { |
08450ef7 CC |
1641 | list_objects_filter_die_if_populated(&data->filter_options); |
1642 | parse_list_objects_filter(&data->filter_options, p); | |
6dd3456a | 1643 | die_if_using_banned_filter(data); |
ba95710a JT |
1644 | continue; |
1645 | } | |
1646 | ||
07c3c2aa | 1647 | if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) || |
e3835cd4 | 1648 | data->allow_sideband_all) && |
07c3c2aa | 1649 | !strcmp(arg, "sideband-all")) { |
0bbc0bc5 JT |
1650 | data->writer.use_sideband = 1; |
1651 | continue; | |
1652 | } | |
1653 | ||
dd4b732d JT |
1654 | if (skip_prefix(arg, "packfile-uris ", &p)) { |
1655 | string_list_split(&data->uri_protocols, p, ',', -1); | |
1656 | continue; | |
1657 | } | |
1658 | ||
3145ea95 | 1659 | /* ignore unknown lines maybe? */ |
7cc6ed2d | 1660 | die("unexpected line: '%s'", arg); |
3145ea95 | 1661 | } |
4845b772 | 1662 | |
dd4b732d JT |
1663 | if (data->uri_protocols.nr && !data->writer.use_sideband) |
1664 | string_list_clear(&data->uri_protocols, 0); | |
1665 | ||
4845b772 JK |
1666 | if (request->status != PACKET_READ_FLUSH) |
1667 | die(_("expected flush after fetch arguments")); | |
b8f58c20 RC |
1668 | |
1669 | if (trace2_is_enabled()) | |
1670 | trace2_fetch_info(data); | |
3145ea95 BW |
1671 | } |
1672 | ||
8dcf2278 | 1673 | static int process_haves(struct upload_pack_data *data, struct oid_array *common) |
3145ea95 BW |
1674 | { |
1675 | int i; | |
1676 | ||
1677 | /* Process haves */ | |
8dcf2278 CC |
1678 | for (i = 0; i < data->haves.nr; i++) { |
1679 | const struct object_id *oid = &data->haves.oid[i]; | |
3145ea95 | 1680 | |
bc726bd0 ÆAB |
1681 | if (!repo_has_object_file_with_flags(the_repository, oid, |
1682 | OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT)) | |
3145ea95 BW |
1683 | continue; |
1684 | ||
1685 | oid_array_append(common, oid); | |
1686 | ||
ea2c6e60 | 1687 | do_got_oid(data, oid); |
3145ea95 BW |
1688 | } |
1689 | ||
1690 | return 0; | |
1691 | } | |
1692 | ||
6fbbc437 | 1693 | static int send_acks(struct upload_pack_data *data, struct oid_array *acks) |
3145ea95 BW |
1694 | { |
1695 | int i; | |
1696 | ||
6fbbc437 | 1697 | packet_writer_write(&data->writer, "acknowledgments\n"); |
3145ea95 BW |
1698 | |
1699 | /* Send Acks */ | |
1700 | if (!acks->nr) | |
6fbbc437 | 1701 | packet_writer_write(&data->writer, "NAK\n"); |
3145ea95 BW |
1702 | |
1703 | for (i = 0; i < acks->nr; i++) { | |
6fbbc437 | 1704 | packet_writer_write(&data->writer, "ACK %s\n", |
bc2e795c | 1705 | oid_to_hex(&acks->oid[i])); |
3145ea95 BW |
1706 | } |
1707 | ||
9c1e657a | 1708 | if (!data->wait_for_done && ok_to_give_up(data)) { |
3145ea95 | 1709 | /* Send Ready */ |
6fbbc437 | 1710 | packet_writer_write(&data->writer, "ready\n"); |
3145ea95 BW |
1711 | return 1; |
1712 | } | |
1713 | ||
1714 | return 0; | |
1715 | } | |
1716 | ||
389f161a | 1717 | static int process_haves_and_send_acks(struct upload_pack_data *data) |
3145ea95 BW |
1718 | { |
1719 | struct oid_array common = OID_ARRAY_INIT; | |
3145ea95 BW |
1720 | int ret = 0; |
1721 | ||
8dcf2278 | 1722 | process_haves(data, &common); |
3145ea95 BW |
1723 | if (data->done) { |
1724 | ret = 1; | |
6fbbc437 | 1725 | } else if (send_acks(data, &common)) { |
bc2e795c | 1726 | packet_writer_delim(&data->writer); |
3145ea95 BW |
1727 | ret = 1; |
1728 | } else { | |
1729 | /* Add Flush */ | |
bc2e795c | 1730 | packet_writer_flush(&data->writer); |
3145ea95 BW |
1731 | ret = 0; |
1732 | } | |
1733 | ||
3145ea95 BW |
1734 | oid_array_clear(&data->haves); |
1735 | oid_array_clear(&common); | |
1736 | return ret; | |
1737 | } | |
1738 | ||
516e2b76 BW |
1739 | static void send_wanted_ref_info(struct upload_pack_data *data) |
1740 | { | |
1741 | const struct string_list_item *item; | |
1742 | ||
1743 | if (!data->wanted_refs.nr) | |
1744 | return; | |
1745 | ||
bc2e795c | 1746 | packet_writer_write(&data->writer, "wanted-refs\n"); |
516e2b76 BW |
1747 | |
1748 | for_each_string_list_item(item, &data->wanted_refs) { | |
bc2e795c JT |
1749 | packet_writer_write(&data->writer, "%s %s\n", |
1750 | oid_to_hex(item->util), | |
1751 | item->string); | |
516e2b76 BW |
1752 | } |
1753 | ||
bc2e795c | 1754 | packet_writer_delim(&data->writer); |
516e2b76 BW |
1755 | } |
1756 | ||
389f161a | 1757 | static void send_shallow_info(struct upload_pack_data *data) |
685fbd32 BW |
1758 | { |
1759 | /* No shallow info needs to be sent */ | |
1760 | if (!data->depth && !data->deepen_rev_list && !data->shallows.nr && | |
00624d60 | 1761 | !is_repository_shallow(the_repository)) |
685fbd32 BW |
1762 | return; |
1763 | ||
bc2e795c | 1764 | packet_writer_write(&data->writer, "shallow-info\n"); |
685fbd32 | 1765 | |
ee703c8a | 1766 | if (!send_shallow_list(data) && |
00624d60 | 1767 | is_repository_shallow(the_repository)) |
b1492f22 | 1768 | deepen(data, INFINITE_DEPTH); |
685fbd32 BW |
1769 | |
1770 | packet_delim(1); | |
1771 | } | |
1772 | ||
3145ea95 BW |
1773 | enum fetch_state { |
1774 | FETCH_PROCESS_ARGS = 0, | |
1775 | FETCH_SEND_ACKS, | |
1776 | FETCH_SEND_PACK, | |
1777 | FETCH_DONE, | |
1778 | }; | |
1779 | ||
74595cca | 1780 | int upload_pack_v2(struct repository *r UNUSED, struct packet_reader *request) |
3145ea95 BW |
1781 | { |
1782 | enum fetch_state state = FETCH_PROCESS_ARGS; | |
1783 | struct upload_pack_data data; | |
d1035cac JT |
1784 | |
1785 | clear_object_flags(ALL_FLAGS); | |
3145ea95 BW |
1786 | |
1787 | upload_pack_data_init(&data); | |
f8edd1ca | 1788 | data.use_sideband = LARGE_PACKET_MAX; |
5b3c6507 | 1789 | get_upload_pack_config(&data); |
8a0e6f16 | 1790 | |
3145ea95 BW |
1791 | while (state != FETCH_DONE) { |
1792 | switch (state) { | |
1793 | case FETCH_PROCESS_ARGS: | |
389f161a | 1794 | process_args(request, &data); |
3145ea95 | 1795 | |
9c1e657a | 1796 | if (!data.want_obj.nr && !data.wait_for_done) { |
3145ea95 | 1797 | /* |
9c1e657a JT |
1798 | * Request didn't contain any 'want' lines (and |
1799 | * the request does not contain | |
1800 | * "wait-for-done", in which it is reasonable | |
1801 | * to just send 'have's without 'want's); guess | |
1802 | * they didn't want anything. | |
3145ea95 BW |
1803 | */ |
1804 | state = FETCH_DONE; | |
1805 | } else if (data.haves.nr) { | |
1806 | /* | |
1807 | * Request had 'have' lines, so lets ACK them. | |
1808 | */ | |
1809 | state = FETCH_SEND_ACKS; | |
1810 | } else { | |
1811 | /* | |
1812 | * Request had 'want's but no 'have's so we can | |
1813 | * immedietly go to construct and send a pack. | |
1814 | */ | |
1815 | state = FETCH_SEND_PACK; | |
1816 | } | |
1817 | break; | |
1818 | case FETCH_SEND_ACKS: | |
389f161a | 1819 | if (process_haves_and_send_acks(&data)) |
3145ea95 BW |
1820 | state = FETCH_SEND_PACK; |
1821 | else | |
1822 | state = FETCH_DONE; | |
1823 | break; | |
1824 | case FETCH_SEND_PACK: | |
516e2b76 | 1825 | send_wanted_ref_info(&data); |
389f161a | 1826 | send_shallow_info(&data); |
685fbd32 | 1827 | |
dd4b732d JT |
1828 | if (data.uri_protocols.nr) { |
1829 | create_pack_file(&data, &data.uri_protocols); | |
1830 | } else { | |
1831 | packet_writer_write(&data.writer, "packfile\n"); | |
1832 | create_pack_file(&data, NULL); | |
1833 | } | |
3145ea95 BW |
1834 | state = FETCH_DONE; |
1835 | break; | |
1836 | case FETCH_DONE: | |
1837 | continue; | |
1838 | } | |
1839 | } | |
1840 | ||
1841 | upload_pack_data_clear(&data); | |
1842 | return 0; | |
1843 | } | |
685fbd32 BW |
1844 | |
1845 | int upload_pack_advertise(struct repository *r, | |
1846 | struct strbuf *value) | |
1847 | { | |
ba95710a JT |
1848 | if (value) { |
1849 | int allow_filter_value; | |
516e2b76 | 1850 | int allow_ref_in_want; |
0bbc0bc5 | 1851 | int allow_sideband_all_value; |
dd4b732d | 1852 | char *str = NULL; |
516e2b76 | 1853 | |
9c1e657a | 1854 | strbuf_addstr(value, "shallow wait-for-done"); |
516e2b76 | 1855 | |
4b4e75dd | 1856 | if (!repo_config_get_bool(r, |
ba95710a JT |
1857 | "uploadpack.allowfilter", |
1858 | &allow_filter_value) && | |
1859 | allow_filter_value) | |
1860 | strbuf_addstr(value, " filter"); | |
516e2b76 | 1861 | |
4b4e75dd | 1862 | if (!repo_config_get_bool(r, |
516e2b76 BW |
1863 | "uploadpack.allowrefinwant", |
1864 | &allow_ref_in_want) && | |
1865 | allow_ref_in_want) | |
1866 | strbuf_addstr(value, " ref-in-want"); | |
0bbc0bc5 | 1867 | |
07c3c2aa | 1868 | if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) || |
4b4e75dd | 1869 | (!repo_config_get_bool(r, |
07c3c2aa JT |
1870 | "uploadpack.allowsidebandall", |
1871 | &allow_sideband_all_value) && | |
1872 | allow_sideband_all_value)) | |
0bbc0bc5 | 1873 | strbuf_addstr(value, " sideband-all"); |
dd4b732d | 1874 | |
4b4e75dd | 1875 | if (!repo_config_get_string(r, |
dd4b732d JT |
1876 | "uploadpack.blobpackfileuri", |
1877 | &str) && | |
1878 | str) { | |
1879 | strbuf_addstr(value, " packfile-uris"); | |
1880 | free(str); | |
1881 | } | |
ba95710a | 1882 | } |
516e2b76 | 1883 | |
685fbd32 BW |
1884 | return 1; |
1885 | } |