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