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