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