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