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