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