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