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