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