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