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