]> git.ipfire.org Git - thirdparty/git.git/blame - upload-pack.c
t6038: remove problematic test
[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"
569e554b 21#include "argv-array.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
CC
90 struct list_objects_filter_options filter_options;
91
92 struct packet_writer writer;
93
339a9840
CC
94 const char *pack_objects_hook;
95
a8497288 96 unsigned stateless_rpc : 1; /* v0 only */
d40f04e0
CC
97 unsigned no_done : 1; /* v0 only */
98 unsigned daemon_mode : 1; /* v0 only */
59a90261 99 unsigned filter_capability_requested : 1; /* v0 only */
e8498322
CC
100
101 unsigned use_thin_pack : 1;
102 unsigned use_ofs_delta : 1;
103 unsigned no_progress : 1;
104 unsigned use_include_tag : 1;
59abe196 105 unsigned allow_filter : 1;
a8497288
CC
106
107 unsigned done : 1; /* v2 only */
d1d7a945 108 unsigned allow_ref_in_want : 1; /* v2 only */
e3835cd4 109 unsigned allow_sideband_all : 1; /* v2 only */
e8498322
CC
110};
111
112static void upload_pack_data_init(struct upload_pack_data *data)
113{
438528f6 114 struct string_list symref = STRING_LIST_INIT_DUP;
e8498322
CC
115 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
116 struct object_array want_obj = OBJECT_ARRAY_INIT;
117 struct object_array have_obj = OBJECT_ARRAY_INIT;
118 struct oid_array haves = OID_ARRAY_INIT;
119 struct object_array shallows = OBJECT_ARRAY_INIT;
120 struct string_list deepen_not = STRING_LIST_INIT_DUP;
dd4b732d 121 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
de0e9f74 122 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
e8498322
CC
123
124 memset(data, 0, sizeof(*data));
438528f6 125 data->symref = symref;
e8498322
CC
126 data->wanted_refs = wanted_refs;
127 data->want_obj = want_obj;
128 data->have_obj = have_obj;
129 data->haves = haves;
130 data->shallows = shallows;
131 data->deepen_not = deepen_not;
dd4b732d 132 data->uri_protocols = uri_protocols;
de0e9f74 133 data->extra_edge_obj = extra_edge_obj;
e8498322 134 packet_writer_init(&data->writer, 1);
f203a88c
CC
135
136 data->keepalive = 5;
e8498322
CC
137}
138
139static void upload_pack_data_clear(struct upload_pack_data *data)
140{
438528f6 141 string_list_clear(&data->symref, 1);
e8498322
CC
142 string_list_clear(&data->wanted_refs, 1);
143 object_array_clear(&data->want_obj);
144 object_array_clear(&data->have_obj);
145 oid_array_clear(&data->haves);
146 object_array_clear(&data->shallows);
147 string_list_clear(&data->deepen_not, 0);
de0e9f74 148 object_array_clear(&data->extra_edge_obj);
e8498322 149 list_objects_filter_release(&data->filter_options);
339a9840
CC
150
151 free((char *)data->pack_objects_hook);
e8498322
CC
152}
153
d40f04e0 154static void reset_timeout(unsigned int timeout)
960deccb
PA
155{
156 alarm(timeout);
157}
fb9040cc 158
f8edd1ca
CC
159static void send_client_data(int fd, const char *data, ssize_t sz,
160 int use_sideband)
583b7ea3 161{
4c4b7d1d
LF
162 if (use_sideband) {
163 send_sideband(1, fd, data, sz, use_sideband);
fcf0fe9e 164 return;
4c4b7d1d 165 }
958c24b1
JH
166 if (fd == 3)
167 /* emergency quit */
168 fd = 2;
169 if (fd == 2) {
93822c22 170 /* XXX: are we happy to lose stuff here? */
958c24b1 171 xwrite(fd, data, sz);
fcf0fe9e 172 return;
583b7ea3 173 }
cdf4fb8e 174 write_or_die(fd, data, sz);
583b7ea3
JH
175}
176
b790e0f6
NTND
177static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
178{
179 FILE *fp = cb_data;
180 if (graft->nr_parent == -1)
7683e2e6 181 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
b790e0f6
NTND
182 return 0;
183}
184
acaaca7d
JT
185struct output_state {
186 char buffer[8193];
187 int used;
dd4b732d
JT
188 unsigned packfile_uris_started : 1;
189 unsigned packfile_started : 1;
acaaca7d
JT
190};
191
192static int relay_pack_data(int pack_objects_out, struct output_state *os,
dd4b732d 193 int use_sideband, int write_packfile_line)
acaaca7d
JT
194{
195 /*
196 * We keep the last byte to ourselves
197 * in case we detect broken rev-list, so that we
198 * can leave the stream corrupted. This is
199 * unfortunate -- unpack-objects would happily
200 * accept a valid packdata with trailing garbage,
201 * so appending garbage after we pass all the
202 * pack data is not good enough to signal
203 * breakage to downstream.
204 */
205 ssize_t readsz;
206
207 readsz = xread(pack_objects_out, os->buffer + os->used,
208 sizeof(os->buffer) - os->used);
209 if (readsz < 0) {
210 return readsz;
211 }
212 os->used += readsz;
213
dd4b732d
JT
214 while (!os->packfile_started) {
215 char *p;
216 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
217 os->packfile_started = 1;
218 if (write_packfile_line) {
219 if (os->packfile_uris_started)
220 packet_delim(1);
221 packet_write_fmt(1, "\1packfile\n");
222 }
223 break;
224 }
225 if ((p = memchr(os->buffer, '\n', os->used))) {
226 if (!os->packfile_uris_started) {
227 os->packfile_uris_started = 1;
228 if (!write_packfile_line)
229 BUG("packfile_uris requires sideband-all");
230 packet_write_fmt(1, "\1packfile-uris\n");
231 }
232 *p = '\0';
233 packet_write_fmt(1, "\1%s\n", os->buffer);
234
235 os->used -= p - os->buffer + 1;
236 memmove(os->buffer, p + 1, os->used);
237 } else {
238 /*
239 * Incomplete line.
240 */
241 return readsz;
242 }
243 }
244
acaaca7d
JT
245 if (os->used > 1) {
246 send_client_data(1, os->buffer, os->used - 1, use_sideband);
247 os->buffer[0] = os->buffer[os->used - 1];
248 os->used = 1;
249 } else {
250 send_client_data(1, os->buffer, os->used, use_sideband);
251 os->used = 0;
252 }
253
254 return readsz;
255}
256
dd4b732d
JT
257static void create_pack_file(struct upload_pack_data *pack_data,
258 const struct string_list *uri_protocols)
fb9040cc 259{
d3180279 260 struct child_process pack_objects = CHILD_PROCESS_INIT;
acaaca7d
JT
261 struct output_state output_state = { { 0 } };
262 char progress[128];
583b7ea3
JH
263 char abort_msg[] = "aborting due to possible repository "
264 "corruption on the remote side.";
1456b043 265 ssize_t sz;
65a3629e 266 int i;
cdab4858 267 FILE *pipe_fd;
75bfc6c2 268
339a9840 269 if (!pack_data->pack_objects_hook)
20b20a22
JK
270 pack_objects.git_cmd = 1;
271 else {
339a9840 272 argv_array_push(&pack_objects.args, pack_data->pack_objects_hook);
20b20a22
JK
273 argv_array_push(&pack_objects.args, "git");
274 pack_objects.use_shell = 1;
275 }
276
35b43a10 277 if (pack_data->shallow_nr) {
65a3629e
MP
278 argv_array_push(&pack_objects.args, "--shallow-file");
279 argv_array_push(&pack_objects.args, "");
f0cea83f 280 }
65a3629e
MP
281 argv_array_push(&pack_objects.args, "pack-objects");
282 argv_array_push(&pack_objects.args, "--revs");
b5a2068c 283 if (pack_data->use_thin_pack)
65a3629e 284 argv_array_push(&pack_objects.args, "--thin");
75bfc6c2 285
65a3629e 286 argv_array_push(&pack_objects.args, "--stdout");
35b43a10 287 if (pack_data->shallow_nr)
65a3629e 288 argv_array_push(&pack_objects.args, "--shallow");
b5a2068c 289 if (!pack_data->no_progress)
65a3629e 290 argv_array_push(&pack_objects.args, "--progress");
b5a2068c 291 if (pack_data->use_ofs_delta)
65a3629e 292 argv_array_push(&pack_objects.args, "--delta-base-offset");
b5a2068c 293 if (pack_data->use_include_tag)
65a3629e 294 argv_array_push(&pack_objects.args, "--include-tag");
c9f03259 295 if (pack_data->filter_options.choice) {
cf9ceb5a 296 const char *spec =
c9f03259 297 expand_list_objects_filter_spec(&pack_data->filter_options);
0b6069fe
JT
298 if (pack_objects.use_shell) {
299 struct strbuf buf = STRBUF_INIT;
cf9ceb5a 300 sq_quote_buf(&buf, spec);
0b6069fe
JT
301 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
302 strbuf_release(&buf);
303 } else {
304 argv_array_pushf(&pack_objects.args, "--filter=%s",
cf9ceb5a 305 spec);
0b6069fe 306 }
10ac85c7 307 }
dd4b732d
JT
308 if (uri_protocols) {
309 for (i = 0; i < uri_protocols->nr; i++)
310 argv_array_pushf(&pack_objects.args, "--uri-protocol=%s",
311 uri_protocols->items[i].string);
312 }
cc41fa8d 313
b9612197 314 pack_objects.in = -1;
cc41fa8d
JS
315 pack_objects.out = -1;
316 pack_objects.err = -1;
21edd3f1 317
4c324c00 318 if (start_command(&pack_objects))
7e44c935 319 die("git upload-pack: unable to fork git-pack-objects");
b1c71b72 320
cdab4858
NTND
321 pipe_fd = xfdopen(pack_objects.in, "w");
322
35b43a10 323 if (pack_data->shallow_nr)
b790e0f6
NTND
324 for_each_commit_graft(write_one_shallow, pipe_fd);
325
c9f03259 326 for (i = 0; i < pack_data->want_obj.nr; i++)
cdab4858 327 fprintf(pipe_fd, "%s\n",
c9f03259 328 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
cdab4858 329 fprintf(pipe_fd, "--not\n");
c9f03259 330 for (i = 0; i < pack_data->have_obj.nr; i++)
cdab4858 331 fprintf(pipe_fd, "%s\n",
c9f03259 332 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
de0e9f74 333 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
cdab4858 334 fprintf(pipe_fd, "%s\n",
de0e9f74 335 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
cdab4858
NTND
336 fprintf(pipe_fd, "\n");
337 fflush(pipe_fd);
338 fclose(pipe_fd);
f0cea83f 339
cc41fa8d
JS
340 /* We read from pack_objects.err to capture stderr output for
341 * progress bar, and pack_objects.out to capture the pack data.
b1c71b72 342 */
b1c71b72
JH
343
344 while (1) {
b1c71b72 345 struct pollfd pfd[2];
f203a88c 346 int pe, pu, pollsize, polltimeout;
05e95155 347 int ret;
b1c71b72 348
d40f04e0 349 reset_timeout(pack_data->timeout);
0d516ada 350
b1c71b72 351 pollsize = 0;
363b7817 352 pe = pu = -1;
b1c71b72 353
cc41fa8d
JS
354 if (0 <= pack_objects.out) {
355 pfd[pollsize].fd = pack_objects.out;
b1c71b72
JH
356 pfd[pollsize].events = POLLIN;
357 pu = pollsize;
358 pollsize++;
359 }
cc41fa8d
JS
360 if (0 <= pack_objects.err) {
361 pfd[pollsize].fd = pack_objects.err;
363b7817
JH
362 pfd[pollsize].events = POLLIN;
363 pe = pollsize;
364 pollsize++;
365 }
b1c71b72 366
4c324c00
JS
367 if (!pollsize)
368 break;
369
f203a88c
CC
370 polltimeout = pack_data->keepalive < 0
371 ? -1
372 : 1000 * pack_data->keepalive;
373
374 ret = poll(pfd, pollsize, polltimeout);
6c71f8b0 375
05e95155 376 if (ret < 0) {
4c324c00 377 if (errno != EINTR) {
d2b6afa2 378 error_errno("poll failed, resuming");
4c324c00 379 sleep(1);
b1c71b72 380 }
4c324c00
JS
381 continue;
382 }
6b59f51b
NP
383 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
384 /* Status ready; we ship that in the side-band
385 * or dump to the standard error.
386 */
387 sz = xread(pack_objects.err, progress,
388 sizeof(progress));
389 if (0 < sz)
f8edd1ca
CC
390 send_client_data(2, progress, sz,
391 pack_data->use_sideband);
6b59f51b
NP
392 else if (sz == 0) {
393 close(pack_objects.err);
394 pack_objects.err = -1;
395 }
396 else
397 goto fail;
398 /* give priority to status messages */
399 continue;
400 }
4c324c00 401 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
acaaca7d
JT
402 int result = relay_pack_data(pack_objects.out,
403 &output_state,
dd4b732d
JT
404 pack_data->use_sideband,
405 !!uri_protocols);
acaaca7d
JT
406
407 if (result == 0) {
4c324c00
JS
408 close(pack_objects.out);
409 pack_objects.out = -1;
acaaca7d 410 } else if (result < 0) {
4c324c00 411 goto fail;
b1c71b72 412 }
4c324c00 413 }
05e95155
JK
414
415 /*
416 * We hit the keepalive timeout without saying anything; send
417 * an empty message on the data sideband just to let the other
418 * side know we're still working on it, but don't have any data
419 * yet.
420 *
421 * If we don't have a sideband channel, there's no room in the
422 * protocol to say anything, so those clients are just out of
423 * luck.
424 */
f8edd1ca 425 if (!ret && pack_data->use_sideband) {
05e95155
JK
426 static const char buf[] = "0005\1";
427 write_or_die(1, buf, 5);
428 }
4c324c00 429 }
b1c71b72 430
4c324c00 431 if (finish_command(&pack_objects)) {
7e44c935 432 error("git upload-pack: git-pack-objects died with error.");
4c324c00
JS
433 goto fail;
434 }
b1c71b72 435
4c324c00 436 /* flush the data */
acaaca7d
JT
437 if (output_state.used > 0) {
438 send_client_data(1, output_state.buffer, output_state.used,
f8edd1ca 439 pack_data->use_sideband);
4c324c00 440 fprintf(stderr, "flushed.\n");
b1c71b72 441 }
f8edd1ca 442 if (pack_data->use_sideband)
4c324c00
JS
443 packet_flush(1);
444 return;
445
b1c71b72 446 fail:
f8edd1ca
CC
447 send_client_data(3, abort_msg, sizeof(abort_msg),
448 pack_data->use_sideband);
7e44c935 449 die("git upload-pack: %s", abort_msg);
fb9040cc
LT
450}
451
ea2c6e60 452static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
def88e9a 453{
937a515a 454 int we_knew_they_have = 0;
ea2c6e60 455 struct object *o = parse_object(the_repository, oid);
b1e9fff7 456
b1e9fff7 457 if (!o)
cf93982f 458 die("oops (%s)", oid_to_hex(oid));
182a8dab 459 if (o->type == OBJ_COMMIT) {
b1e9fff7 460 struct commit_list *parents;
937a515a 461 struct commit *commit = (struct commit *)o;
b1e9fff7 462 if (o->flags & THEY_HAVE)
937a515a
JH
463 we_knew_they_have = 1;
464 else
465 o->flags |= THEY_HAVE;
f01c7916
CC
466 if (!data->oldest_have || (commit->date < data->oldest_have))
467 data->oldest_have = commit->date;
937a515a 468 for (parents = commit->parents;
b1e9fff7
JH
469 parents;
470 parents = parents->next)
471 parents->item->object.flags |= THEY_HAVE;
fb9040cc 472 }
937a515a 473 if (!we_knew_they_have) {
460ed0d4 474 add_object_array(o, NULL, &data->have_obj);
937a515a
JH
475 return 1;
476 }
477 return 0;
478}
479
ea2c6e60
CC
480static int got_oid(struct upload_pack_data *data,
481 const char *hex, struct object_id *oid)
482{
483 if (get_oid_hex(hex, oid))
484 die("git upload-pack: expected SHA1 object, got '%s'", hex);
77aa0941
JT
485 if (!has_object_file_with_flags(oid,
486 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
ea2c6e60
CC
487 return -1;
488 return do_got_oid(data, oid);
489}
490
08667348 491static int ok_to_give_up(struct upload_pack_data *data)
937a515a 492{
4fbcca4e 493 uint32_t min_generation = GENERATION_NUMBER_ZERO;
937a515a 494
08667348 495 if (!data->have_obj.nr)
937a515a
JH
496 return 0;
497
08667348 498 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
f01c7916 499 COMMON_KNOWN, data->oldest_have,
4fbcca4e 500 min_generation);
def88e9a
LT
501}
502
07977695
CC
503static int get_common_commits(struct upload_pack_data *data,
504 struct packet_reader *reader)
def88e9a 505{
cf93982f 506 struct object_id oid;
507 char last_hex[GIT_MAX_HEXSZ + 1];
49bee717
SP
508 int got_common = 0;
509 int got_other = 0;
4e10cf9a 510 int sent_ready = 0;
def88e9a 511
f0243f26
JS
512 save_commit_buffer = 0;
513
eeefa7c9 514 for (;;) {
8bf3b758
NTND
515 const char *arg;
516
d40f04e0 517 reset_timeout(data->timeout);
def88e9a 518
01f9ec64 519 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
e9d882b8 520 if (data->multi_ack == MULTI_ACK_DETAILED
07977695
CC
521 && got_common
522 && !got_other
08667348 523 && ok_to_give_up(data)) {
4e10cf9a 524 sent_ready = 1;
81c634e9 525 packet_write_fmt(1, "ACK %s ready\n", last_hex);
4e10cf9a 526 }
53d69506 527 if (data->have_obj.nr == 0 || data->multi_ack)
81c634e9 528 packet_write_fmt(1, "NAK\n");
4e10cf9a 529
d40f04e0 530 if (data->no_done && sent_ready) {
81c634e9 531 packet_write_fmt(1, "ACK %s\n", last_hex);
4e10cf9a
JH
532 return 0;
533 }
df654abc 534 if (data->stateless_rpc)
42526b47 535 exit(0);
49bee717
SP
536 got_common = 0;
537 got_other = 0;
def88e9a
LT
538 continue;
539 }
01f9ec64 540 if (skip_prefix(reader->line, "have ", &arg)) {
460ed0d4 541 switch (got_oid(data, arg, &oid)) {
937a515a 542 case -1: /* they have what we do not */
49bee717 543 got_other = 1;
53d69506 544 if (data->multi_ack
08667348 545 && ok_to_give_up(data)) {
cf93982f 546 const char *hex = oid_to_hex(&oid);
e9d882b8 547 if (data->multi_ack == MULTI_ACK_DETAILED) {
4e10cf9a 548 sent_ready = 1;
81c634e9 549 packet_write_fmt(1, "ACK %s ready\n", hex);
4e10cf9a 550 } else
81c634e9 551 packet_write_fmt(1, "ACK %s continue\n", hex);
78affc49 552 }
937a515a
JH
553 break;
554 default:
49bee717 555 got_common = 1;
55dc227d 556 oid_to_hex_r(last_hex, &oid);
e9d882b8 557 if (data->multi_ack == MULTI_ACK_DETAILED)
81c634e9 558 packet_write_fmt(1, "ACK %s common\n", last_hex);
53d69506 559 else if (data->multi_ack)
81c634e9 560 packet_write_fmt(1, "ACK %s continue\n", last_hex);
07977695 561 else if (data->have_obj.nr == 1)
81c634e9 562 packet_write_fmt(1, "ACK %s\n", last_hex);
937a515a 563 break;
af2d3aa4 564 }
def88e9a
LT
565 continue;
566 }
01f9ec64 567 if (!strcmp(reader->line, "done")) {
07977695 568 if (data->have_obj.nr > 0) {
53d69506 569 if (data->multi_ack)
81c634e9 570 packet_write_fmt(1, "ACK %s\n", last_hex);
1bd8c8f0
JS
571 return 0;
572 }
81c634e9 573 packet_write_fmt(1, "NAK\n");
def88e9a
LT
574 return -1;
575 }
01f9ec64 576 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
def88e9a 577 }
def88e9a
LT
578}
579
629060d9 580static int is_our_ref(struct object *o, enum allow_uor allow_uor)
390eb36b 581{
629060d9
CC
582 int allow_hidden_ref = (allow_uor &
583 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
7199c093 584 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
390eb36b
JH
585}
586
2997178e
NTND
587/*
588 * on successful case, it's up to the caller to close cmd->out
589 */
590static int do_reachable_revlist(struct child_process *cmd,
079aa97e 591 struct object_array *src,
f1514c6a 592 struct object_array *reachable,
629060d9 593 enum allow_uor allow_uor)
051e4005
JH
594{
595 static const char *argv[] = {
596 "rev-list", "--stdin", NULL,
597 };
051e4005 598 struct object *o;
55dc227d 599 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
051e4005 600 int i;
f690b6b0 601 const unsigned hexsz = the_hash_algo->hexsz;
051e4005 602
2997178e
NTND
603 cmd->argv = argv;
604 cmd->git_cmd = 1;
605 cmd->no_stderr = 1;
606 cmd->in = -1;
607 cmd->out = -1;
051e4005
JH
608
609 /*
7fcbd37f
NTND
610 * If the next rev-list --stdin encounters an unknown commit,
611 * it terminates, which will cause SIGPIPE in the write loop
051e4005
JH
612 * below.
613 */
614 sigchain_push(SIGPIPE, SIG_IGN);
615
2997178e 616 if (start_command(cmd))
7fcbd37f
NTND
617 goto error;
618
051e4005 619 namebuf[0] = '^';
f690b6b0 620 namebuf[hexsz + 1] = '\n';
051e4005
JH
621 for (i = get_max_object_index(); 0 < i; ) {
622 o = get_indexed_object(--i);
2a745324
BH
623 if (!o)
624 continue;
079aa97e
NTND
625 if (reachable && o->type == OBJ_COMMIT)
626 o->flags &= ~TMP_MARK;
629060d9 627 if (!is_our_ref(o, allow_uor))
051e4005 628 continue;
f690b6b0 629 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
630 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
051e4005
JH
631 goto error;
632 }
f690b6b0 633 namebuf[hexsz] = '\n';
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;
f690b6b0 643 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
644 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
051e4005
JH
645 goto error;
646 }
2997178e
NTND
647 close(cmd->in);
648 cmd->in = -1;
649 sigchain_pop(SIGPIPE);
051e4005 650
2997178e
NTND
651 return 0;
652
653error:
051e4005
JH
654 sigchain_pop(SIGPIPE);
655
2997178e
NTND
656 if (cmd->in >= 0)
657 close(cmd->in);
658 if (cmd->out >= 0)
659 close(cmd->out);
660 return -1;
661}
662
f1514c6a 663static int get_reachable_list(struct upload_pack_data *data,
079aa97e
NTND
664 struct object_array *reachable)
665{
666 struct child_process cmd = CHILD_PROCESS_INIT;
667 int i;
668 struct object *o;
55dc227d 669 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
670 const unsigned hexsz = the_hash_algo->hexsz;
079aa97e 671
f1514c6a 672 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
629060d9 673 data->allow_uor) < 0)
079aa97e
NTND
674 return -1;
675
55dc227d 676 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
62b89d43 677 struct object_id oid;
55dc227d 678 const char *p;
079aa97e 679
62b89d43 680 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
079aa97e
NTND
681 break;
682
d0229abd 683 o = lookup_object(the_repository, &oid);
079aa97e
NTND
684 if (o && o->type == OBJ_COMMIT) {
685 o->flags &= ~TMP_MARK;
686 }
687 }
688 for (i = get_max_object_index(); 0 < i; i--) {
689 o = get_indexed_object(i - 1);
690 if (o && o->type == OBJ_COMMIT &&
691 (o->flags & TMP_MARK)) {
692 add_object_array(o, NULL, reachable);
693 o->flags &= ~TMP_MARK;
694 }
695 }
696 close(cmd.out);
697
698 if (finish_command(&cmd))
699 return -1;
700
701 return 0;
702}
703
629060d9 704static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
2997178e
NTND
705{
706 struct child_process cmd = CHILD_PROCESS_INIT;
707 char buf[1];
708 int i;
709
629060d9 710 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
2997178e 711 return 1;
051e4005
JH
712
713 /*
714 * The commits out of the rev-list are not ancestors of
715 * our ref.
716 */
2997178e 717 i = read_in_full(cmd.out, buf, 1);
051e4005
JH
718 if (i)
719 goto error;
720 close(cmd.out);
7fcbd37f 721 cmd.out = -1;
051e4005
JH
722
723 /*
724 * rev-list may have died by encountering a bad commit
725 * in the history, in which case we do want to bail out
726 * even when it showed no commit.
727 */
728 if (finish_command(&cmd))
729 goto error;
730
731 /* All the non-tip ones are ancestors of what we advertised */
3f0f6624 732 return 0;
051e4005
JH
733
734error:
7fcbd37f 735 sigchain_pop(SIGPIPE);
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) {
685fbd32
BW
884 struct argv_array av = ARGV_ARRAY_INIT;
885 int i;
886
887 argv_array_push(&av, "rev-list");
ee703c8a
CC
888 if (data->deepen_since)
889 argv_array_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
890 if (data->deepen_not.nr) {
685fbd32 891 argv_array_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;
685fbd32
BW
894 argv_array_push(&av, s->string);
895 }
896 argv_array_push(&av, "--not");
897 }
ee703c8a
CC
898 for (i = 0; i < data->want_obj.nr; i++) {
899 struct object *o = data->want_obj.objects[i].item;
685fbd32
BW
900 argv_array_push(&av, oid_to_hex(&o->oid));
901 }
446e42c5 902 deepen_by_rev_list(data, av.argc, av.argv);
685fbd32
BW
903 argv_array_clear(&av);
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
d92ae2c0
CC
987static void receive_needs(struct upload_pack_data *data,
988 struct packet_reader *reader)
fb9040cc 989{
051e4005 990 int has_non_tip = 0;
fb9040cc 991
35b43a10 992 data->shallow_nr = 0;
fb9040cc 993 for (;;) {
565ebbf7 994 struct object *o;
f47182c8 995 const char *features;
cf93982f 996 struct object_id oid_buf;
8bf3b758
NTND
997 const char *arg;
998
d40f04e0 999 reset_timeout(data->timeout);
01f9ec64 1000 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
ed09aef0 1001 break;
e091eb93 1002
7a516764 1003 if (process_shallow(reader->line, &data->shallows))
ed09aef0 1004 continue;
7a516764 1005 if (process_deepen(reader->line, &data->depth))
016e6ccb 1006 continue;
7a516764 1007 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
569e554b 1008 continue;
7a516764 1009 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
269a7a83 1010 continue;
ae2948f3 1011
01f9ec64 1012 if (skip_prefix(reader->line, "filter ", &arg)) {
59a90261 1013 if (!data->filter_capability_requested)
10ac85c7 1014 die("git upload-pack: filtering capability not negotiated");
d92ae2c0
CC
1015 list_objects_filter_die_if_populated(&data->filter_options);
1016 parse_list_objects_filter(&data->filter_options, arg);
10ac85c7
JH
1017 continue;
1018 }
9bfa0f9b 1019
01f9ec64 1020 if (!skip_prefix(reader->line, "want ", &arg) ||
55dc227d 1021 parse_oid_hex(arg, &oid_buf, &features))
7e44c935 1022 die("git upload-pack: protocol error, "
01f9ec64 1023 "expected to get object ID, not '%s'", reader->line);
f47182c8 1024
cccf74e2 1025 if (parse_feature_request(features, "deepen-relative"))
7a516764 1026 data->deepen_relative = 1;
f47182c8 1027 if (parse_feature_request(features, "multi_ack_detailed"))
e9d882b8 1028 data->multi_ack = MULTI_ACK_DETAILED;
f47182c8 1029 else if (parse_feature_request(features, "multi_ack"))
e9d882b8 1030 data->multi_ack = MULTI_ACK;
f47182c8 1031 if (parse_feature_request(features, "no-done"))
d40f04e0 1032 data->no_done = 1;
f47182c8 1033 if (parse_feature_request(features, "thin-pack"))
b5a2068c 1034 data->use_thin_pack = 1;
f47182c8 1035 if (parse_feature_request(features, "ofs-delta"))
b5a2068c 1036 data->use_ofs_delta = 1;
f47182c8 1037 if (parse_feature_request(features, "side-band-64k"))
f8edd1ca 1038 data->use_sideband = LARGE_PACKET_MAX;
f47182c8 1039 else if (parse_feature_request(features, "side-band"))
f8edd1ca 1040 data->use_sideband = DEFAULT_PACKET_MAX;
f47182c8 1041 if (parse_feature_request(features, "no-progress"))
b5a2068c 1042 data->no_progress = 1;
f47182c8 1043 if (parse_feature_request(features, "include-tag"))
b5a2068c 1044 data->use_include_tag = 1;
59abe196
CC
1045 if (data->allow_filter &&
1046 parse_feature_request(features, "filter"))
59a90261 1047 data->filter_capability_requested = 1;
565ebbf7 1048
109cd76d 1049 o = parse_object(the_repository, &oid_buf);
bdb31ead 1050 if (!o) {
4ace0283 1051 packet_writer_error(&data->writer,
bc2e795c
JT
1052 "upload-pack: not our ref %s",
1053 oid_to_hex(&oid_buf));
9f9aa761 1054 die("git upload-pack: not our ref %s",
cf93982f 1055 oid_to_hex(&oid_buf));
bdb31ead 1056 }
565ebbf7
JH
1057 if (!(o->flags & WANTED)) {
1058 o->flags |= WANTED;
629060d9
CC
1059 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1060 || is_our_ref(o, data->allow_uor)))
051e4005 1061 has_non_tip = 1;
d92ae2c0 1062 add_object_array(o, NULL, &data->want_obj);
565ebbf7 1063 }
fb9040cc 1064 }
9462e3f5 1065
051e4005
JH
1066 /*
1067 * We have sent all our refs already, and the other end
1068 * should have chosen out of them. When we are operating
1069 * in the stateless RPC mode, however, their choice may
1070 * have been based on the set of older refs advertised
1071 * by another process that handled the initial request.
1072 */
1073 if (has_non_tip)
b08c9742 1074 check_non_tip(data);
051e4005 1075
f8edd1ca 1076 if (!data->use_sideband && data->daemon_mode)
b5a2068c 1077 data->no_progress = 1;
9462e3f5 1078
7a516764 1079 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
f53514bc 1080 return;
569e554b 1081
ee703c8a 1082 if (send_shallow_list(data))
685fbd32 1083 packet_flush(1);
fb9040cc
LT
1084}
1085
daebaa78 1086/* return non-zero if the ref is hidden, otherwise 0 */
78a766ab
LF
1087static int mark_our_ref(const char *refname, const char *refname_full,
1088 const struct object_id *oid)
cbbe50db 1089{
0ebbcf70 1090 struct object *o = lookup_unknown_object(oid);
daebaa78 1091
78a766ab 1092 if (ref_is_hidden(refname, refname_full)) {
390eb36b 1093 o->flags |= HIDDEN_REF;
daebaa78 1094 return 1;
390eb36b 1095 }
3f1da57f 1096 o->flags |= OUR_REF;
cbbe50db
JH
1097 return 0;
1098}
1099
78a766ab 1100static int check_ref(const char *refname_full, const struct object_id *oid,
363e98bf 1101 int flag, void *cb_data)
e172755b 1102{
78a766ab
LF
1103 const char *refname = strip_namespace(refname_full);
1104
1105 mark_our_ref(refname, refname_full, oid);
e172755b
JK
1106 return 0;
1107}
1108
7171d8c1
JH
1109static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1110{
1111 struct string_list_item *item;
1112
1113 if (!symref->nr)
1114 return;
1115 for_each_string_list_item(item, symref)
1116 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1117}
1118
363e98bf
MH
1119static int send_ref(const char *refname, const struct object_id *oid,
1120 int flag, void *cb_data)
def88e9a 1121{
ed09aef0 1122 static const char *capabilities = "multi_ack thin-pack side-band"
cccf74e2
NTND
1123 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1124 " deepen-relative no-progress include-tag multi_ack_detailed";
6b01ecfe 1125 const char *refname_nons = strip_namespace(refname);
21758aff 1126 struct object_id peeled;
762f9276 1127 struct upload_pack_data *data = cb_data;
b5b16990 1128
78a766ab 1129 if (mark_our_ref(refname_nons, refname, oid))
daebaa78 1130 return 0;
cbbe50db 1131
7171d8c1
JH
1132 if (capabilities) {
1133 struct strbuf symref_info = STRBUF_INIT;
1134
762f9276 1135 format_symref_info(&symref_info, &data->symref);
bf30dbf8 1136 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s object-format=%s agent=%s\n",
363e98bf 1137 oid_to_hex(oid), refname_nons,
cf2ad8e6 1138 0, capabilities,
629060d9 1139 (data->allow_uor & ALLOW_TIP_SHA1) ?
7199c093 1140 " allow-tip-sha1-in-want" : "",
629060d9 1141 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
68ee6289 1142 " allow-reachable-sha1-in-want" : "",
df654abc 1143 data->stateless_rpc ? " no-done" : "",
7171d8c1 1144 symref_info.buf,
59abe196 1145 data->allow_filter ? " filter" : "",
bf30dbf8 1146 the_hash_algo->name,
ff5effdf 1147 git_user_agent_sanitized());
7171d8c1
JH
1148 strbuf_release(&symref_info);
1149 } else {
81c634e9 1150 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
7171d8c1 1151 }
1f5881bb 1152 capabilities = NULL;
b420d909 1153 if (!peel_ref(refname, &peeled))
81c634e9 1154 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
def88e9a
LT
1155 return 0;
1156}
1157
7dabd056
MH
1158static int find_symref(const char *refname, const struct object_id *oid,
1159 int flag, void *cb_data)
7171d8c1
JH
1160{
1161 const char *symref_target;
1162 struct string_list_item *item;
7171d8c1
JH
1163
1164 if ((flag & REF_ISSYMREF) == 0)
1165 return 0;
744c040b 1166 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
7171d8c1
JH
1167 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1168 die("'%s' is a symref but it is not?", refname);
533e0882
JK
1169 item = string_list_append(cb_data, strip_namespace(refname));
1170 item->util = xstrdup(strip_namespace(symref_target));
7171d8c1
JH
1171 return 0;
1172}
1173
8a0e6f16 1174static int upload_pack_config(const char *var, const char *value, void *cb_data)
daebaa78 1175{
f203a88c
CC
1176 struct upload_pack_data *data = cb_data;
1177
7199c093
FM
1178 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1179 if (git_config_bool(var, value))
629060d9 1180 data->allow_uor |= ALLOW_TIP_SHA1;
7199c093 1181 else
629060d9 1182 data->allow_uor &= ~ALLOW_TIP_SHA1;
68ee6289
FM
1183 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1184 if (git_config_bool(var, value))
629060d9 1185 data->allow_uor |= ALLOW_REACHABLE_SHA1;
68ee6289 1186 else
629060d9 1187 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
f8edeaa0
DT
1188 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1189 if (git_config_bool(var, value))
629060d9 1190 data->allow_uor |= ALLOW_ANY_SHA1;
f8edeaa0 1191 else
629060d9 1192 data->allow_uor &= ~ALLOW_ANY_SHA1;
7199c093 1193 } else if (!strcmp("uploadpack.keepalive", var)) {
f203a88c
CC
1194 data->keepalive = git_config_int(var, value);
1195 if (!data->keepalive)
1196 data->keepalive = -1;
10ac85c7 1197 } else if (!strcmp("uploadpack.allowfilter", var)) {
59abe196 1198 data->allow_filter = git_config_bool(var, value);
516e2b76 1199 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
d1d7a945 1200 data->allow_ref_in_want = git_config_bool(var, value);
0bbc0bc5 1201 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
e3835cd4 1202 data->allow_sideband_all = git_config_bool(var, value);
8e712ef6
EN
1203 } else if (!strcmp("core.precomposeunicode", var)) {
1204 precomposed_unicode = git_config_bool(var, value);
05e95155 1205 }
aaaa8818 1206
6dc905d9
MR
1207 if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
1208 current_config_scope() != CONFIG_SCOPE_WORKTREE) {
aaaa8818 1209 if (!strcmp("uploadpack.packobjectshook", var))
339a9840 1210 return git_config_string(&data->pack_objects_hook, var, value);
aaaa8818
JK
1211 }
1212
daebaa78
JH
1213 return parse_hide_refs_config(var, value, "uploadpack");
1214}
1215
a3d6b53e 1216void upload_pack(struct upload_pack_options *options)
def88e9a 1217{
01f9ec64 1218 struct packet_reader reader;
ebf8ebcc 1219 struct upload_pack_data data;
960deccb 1220
ebf8ebcc
CC
1221 upload_pack_data_init(&data);
1222
8a0e6f16
CC
1223 git_config(upload_pack_config, &data);
1224
df654abc 1225 data.stateless_rpc = options->stateless_rpc;
d40f04e0
CC
1226 data.daemon_mode = options->daemon_mode;
1227 data.timeout = options->timeout;
df654abc 1228
438528f6 1229 head_ref_namespaced(find_symref, &data.symref);
a6080a0a 1230
df654abc 1231 if (options->advertise_refs || !data.stateless_rpc) {
d40f04e0 1232 reset_timeout(data.timeout);
762f9276
CC
1233 head_ref_namespaced(send_ref, &data);
1234 for_each_namespaced_ref(send_ref, &data);
a3d6b53e
BW
1235 advertise_shallow_grafts(1);
1236 packet_flush(1);
1237 } else {
1238 head_ref_namespaced(check_ref, NULL);
1239 for_each_namespaced_ref(check_ref, NULL);
aa9bab29 1240 }
01f9ec64 1241
ebf8ebcc
CC
1242 if (!options->advertise_refs) {
1243 packet_reader_init(&reader, 0, NULL, 0,
1244 PACKET_READ_CHOMP_NEWLINE |
1245 PACKET_READ_DIE_ON_ERR_PACKET);
1246
d92ae2c0 1247 receive_needs(&data, &reader);
ebf8ebcc 1248 if (data.want_obj.nr) {
07977695 1249 get_common_commits(&data, &reader);
cae2ee10 1250 create_pack_file(&data, NULL);
ebf8ebcc 1251 }
a3d6b53e 1252 }
08450ef7 1253
ebf8ebcc 1254 upload_pack_data_clear(&data);
def88e9a 1255}
04b33055 1256
bc2e795c
JT
1257static int parse_want(struct packet_writer *writer, const char *line,
1258 struct object_array *want_obj)
3145ea95
BW
1259{
1260 const char *arg;
1261 if (skip_prefix(line, "want ", &arg)) {
1262 struct object_id oid;
1263 struct object *o;
1264
1265 if (get_oid_hex(arg, &oid))
1266 die("git upload-pack: protocol error, "
1267 "expected to get oid, not '%s'", line);
1268
109cd76d 1269 o = parse_object(the_repository, &oid);
3145ea95 1270 if (!o) {
bc2e795c
JT
1271 packet_writer_error(writer,
1272 "upload-pack: not our ref %s",
1273 oid_to_hex(&oid));
3145ea95
BW
1274 die("git upload-pack: not our ref %s",
1275 oid_to_hex(&oid));
1276 }
1277
1278 if (!(o->flags & WANTED)) {
1279 o->flags |= WANTED;
1d1243fe 1280 add_object_array(o, NULL, want_obj);
3145ea95
BW
1281 }
1282
1283 return 1;
1284 }
1285
1286 return 0;
1287}
1288
bc2e795c
JT
1289static int parse_want_ref(struct packet_writer *writer, const char *line,
1290 struct string_list *wanted_refs,
1d1243fe 1291 struct object_array *want_obj)
516e2b76
BW
1292{
1293 const char *arg;
1294 if (skip_prefix(line, "want-ref ", &arg)) {
1295 struct object_id oid;
1296 struct string_list_item *item;
1297 struct object *o;
1298
1299 if (read_ref(arg, &oid)) {
bc2e795c 1300 packet_writer_error(writer, "unknown ref %s", arg);
516e2b76
BW
1301 die("unknown ref %s", arg);
1302 }
1303
1304 item = string_list_append(wanted_refs, arg);
1305 item->util = oiddup(&oid);
1306
1307 o = parse_object_or_die(&oid, arg);
1308 if (!(o->flags & WANTED)) {
1309 o->flags |= WANTED;
1d1243fe 1310 add_object_array(o, NULL, want_obj);
516e2b76
BW
1311 }
1312
1313 return 1;
1314 }
1315
1316 return 0;
1317}
1318
3145ea95
BW
1319static int parse_have(const char *line, struct oid_array *haves)
1320{
1321 const char *arg;
1322 if (skip_prefix(line, "have ", &arg)) {
1323 struct object_id oid;
1324
1325 if (get_oid_hex(arg, &oid))
1326 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1327 oid_array_append(haves, &oid);
1328 return 1;
aa9bab29
BW
1329 }
1330
def88e9a
LT
1331 return 0;
1332}
3145ea95
BW
1333
1334static void process_args(struct packet_reader *request,
389f161a 1335 struct upload_pack_data *data)
3145ea95 1336{
4845b772 1337 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
3145ea95 1338 const char *arg = request->line;
ba95710a 1339 const char *p;
3145ea95
BW
1340
1341 /* process want */
389f161a 1342 if (parse_want(&data->writer, arg, &data->want_obj))
3145ea95 1343 continue;
d1d7a945 1344 if (data->allow_ref_in_want &&
bc2e795c 1345 parse_want_ref(&data->writer, arg, &data->wanted_refs,
389f161a 1346 &data->want_obj))
516e2b76 1347 continue;
3145ea95
BW
1348 /* process have line */
1349 if (parse_have(arg, &data->haves))
1350 continue;
1351
1352 /* process args like thin-pack */
1353 if (!strcmp(arg, "thin-pack")) {
b5a2068c 1354 data->use_thin_pack = 1;
3145ea95
BW
1355 continue;
1356 }
1357 if (!strcmp(arg, "ofs-delta")) {
b5a2068c 1358 data->use_ofs_delta = 1;
3145ea95
BW
1359 continue;
1360 }
1361 if (!strcmp(arg, "no-progress")) {
b5a2068c 1362 data->no_progress = 1;
3145ea95
BW
1363 continue;
1364 }
1365 if (!strcmp(arg, "include-tag")) {
b5a2068c 1366 data->use_include_tag = 1;
3145ea95
BW
1367 continue;
1368 }
1369 if (!strcmp(arg, "done")) {
1370 data->done = 1;
1371 continue;
1372 }
1373
685fbd32
BW
1374 /* Shallow related arguments */
1375 if (process_shallow(arg, &data->shallows))
1376 continue;
1377 if (process_deepen(arg, &data->depth))
1378 continue;
1379 if (process_deepen_since(arg, &data->deepen_since,
1380 &data->deepen_rev_list))
1381 continue;
1382 if (process_deepen_not(arg, &data->deepen_not,
1383 &data->deepen_rev_list))
1384 continue;
1385 if (!strcmp(arg, "deepen-relative")) {
1386 data->deepen_relative = 1;
1387 continue;
1388 }
1389
59abe196 1390 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
08450ef7
CC
1391 list_objects_filter_die_if_populated(&data->filter_options);
1392 parse_list_objects_filter(&data->filter_options, p);
ba95710a
JT
1393 continue;
1394 }
1395
07c3c2aa 1396 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
e3835cd4 1397 data->allow_sideband_all) &&
07c3c2aa 1398 !strcmp(arg, "sideband-all")) {
0bbc0bc5
JT
1399 data->writer.use_sideband = 1;
1400 continue;
1401 }
1402
dd4b732d
JT
1403 if (skip_prefix(arg, "packfile-uris ", &p)) {
1404 string_list_split(&data->uri_protocols, p, ',', -1);
1405 continue;
1406 }
1407
3145ea95 1408 /* ignore unknown lines maybe? */
7cc6ed2d 1409 die("unexpected line: '%s'", arg);
3145ea95 1410 }
4845b772 1411
dd4b732d
JT
1412 if (data->uri_protocols.nr && !data->writer.use_sideband)
1413 string_list_clear(&data->uri_protocols, 0);
1414
4845b772
JK
1415 if (request->status != PACKET_READ_FLUSH)
1416 die(_("expected flush after fetch arguments"));
3145ea95
BW
1417}
1418
8dcf2278 1419static int process_haves(struct upload_pack_data *data, struct oid_array *common)
3145ea95
BW
1420{
1421 int i;
1422
1423 /* Process haves */
8dcf2278
CC
1424 for (i = 0; i < data->haves.nr; i++) {
1425 const struct object_id *oid = &data->haves.oid[i];
3145ea95 1426
77aa0941
JT
1427 if (!has_object_file_with_flags(oid,
1428 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
3145ea95
BW
1429 continue;
1430
1431 oid_array_append(common, oid);
1432
ea2c6e60 1433 do_got_oid(data, oid);
3145ea95
BW
1434 }
1435
1436 return 0;
1437}
1438
6fbbc437 1439static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
3145ea95
BW
1440{
1441 int i;
1442
6fbbc437 1443 packet_writer_write(&data->writer, "acknowledgments\n");
3145ea95
BW
1444
1445 /* Send Acks */
1446 if (!acks->nr)
6fbbc437 1447 packet_writer_write(&data->writer, "NAK\n");
3145ea95
BW
1448
1449 for (i = 0; i < acks->nr; i++) {
6fbbc437 1450 packet_writer_write(&data->writer, "ACK %s\n",
bc2e795c 1451 oid_to_hex(&acks->oid[i]));
3145ea95
BW
1452 }
1453
08667348 1454 if (ok_to_give_up(data)) {
3145ea95 1455 /* Send Ready */
6fbbc437 1456 packet_writer_write(&data->writer, "ready\n");
3145ea95
BW
1457 return 1;
1458 }
1459
1460 return 0;
1461}
1462
389f161a 1463static int process_haves_and_send_acks(struct upload_pack_data *data)
3145ea95
BW
1464{
1465 struct oid_array common = OID_ARRAY_INIT;
3145ea95
BW
1466 int ret = 0;
1467
8dcf2278 1468 process_haves(data, &common);
3145ea95
BW
1469 if (data->done) {
1470 ret = 1;
6fbbc437 1471 } else if (send_acks(data, &common)) {
bc2e795c 1472 packet_writer_delim(&data->writer);
3145ea95
BW
1473 ret = 1;
1474 } else {
1475 /* Add Flush */
bc2e795c 1476 packet_writer_flush(&data->writer);
3145ea95
BW
1477 ret = 0;
1478 }
1479
3145ea95
BW
1480 oid_array_clear(&data->haves);
1481 oid_array_clear(&common);
1482 return ret;
1483}
1484
516e2b76
BW
1485static void send_wanted_ref_info(struct upload_pack_data *data)
1486{
1487 const struct string_list_item *item;
1488
1489 if (!data->wanted_refs.nr)
1490 return;
1491
bc2e795c 1492 packet_writer_write(&data->writer, "wanted-refs\n");
516e2b76
BW
1493
1494 for_each_string_list_item(item, &data->wanted_refs) {
bc2e795c
JT
1495 packet_writer_write(&data->writer, "%s %s\n",
1496 oid_to_hex(item->util),
1497 item->string);
516e2b76
BW
1498 }
1499
bc2e795c 1500 packet_writer_delim(&data->writer);
516e2b76
BW
1501}
1502
389f161a 1503static void send_shallow_info(struct upload_pack_data *data)
685fbd32
BW
1504{
1505 /* No shallow info needs to be sent */
1506 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
00624d60 1507 !is_repository_shallow(the_repository))
685fbd32
BW
1508 return;
1509
bc2e795c 1510 packet_writer_write(&data->writer, "shallow-info\n");
685fbd32 1511
ee703c8a 1512 if (!send_shallow_list(data) &&
00624d60 1513 is_repository_shallow(the_repository))
b1492f22 1514 deepen(data, INFINITE_DEPTH);
685fbd32
BW
1515
1516 packet_delim(1);
1517}
1518
3145ea95
BW
1519enum fetch_state {
1520 FETCH_PROCESS_ARGS = 0,
1521 FETCH_SEND_ACKS,
1522 FETCH_SEND_PACK,
1523 FETCH_DONE,
1524};
1525
1526int upload_pack_v2(struct repository *r, struct argv_array *keys,
1527 struct packet_reader *request)
1528{
1529 enum fetch_state state = FETCH_PROCESS_ARGS;
1530 struct upload_pack_data data;
d1035cac
JT
1531
1532 clear_object_flags(ALL_FLAGS);
3145ea95
BW
1533
1534 upload_pack_data_init(&data);
f8edd1ca 1535 data.use_sideband = LARGE_PACKET_MAX;
3145ea95 1536
8a0e6f16
CC
1537 git_config(upload_pack_config, &data);
1538
3145ea95
BW
1539 while (state != FETCH_DONE) {
1540 switch (state) {
1541 case FETCH_PROCESS_ARGS:
389f161a 1542 process_args(request, &data);
3145ea95 1543
389f161a 1544 if (!data.want_obj.nr) {
3145ea95
BW
1545 /*
1546 * Request didn't contain any 'want' lines,
1547 * guess they didn't want anything.
1548 */
1549 state = FETCH_DONE;
1550 } else if (data.haves.nr) {
1551 /*
1552 * Request had 'have' lines, so lets ACK them.
1553 */
1554 state = FETCH_SEND_ACKS;
1555 } else {
1556 /*
1557 * Request had 'want's but no 'have's so we can
1558 * immedietly go to construct and send a pack.
1559 */
1560 state = FETCH_SEND_PACK;
1561 }
1562 break;
1563 case FETCH_SEND_ACKS:
389f161a 1564 if (process_haves_and_send_acks(&data))
3145ea95
BW
1565 state = FETCH_SEND_PACK;
1566 else
1567 state = FETCH_DONE;
1568 break;
1569 case FETCH_SEND_PACK:
516e2b76 1570 send_wanted_ref_info(&data);
389f161a 1571 send_shallow_info(&data);
685fbd32 1572
dd4b732d
JT
1573 if (data.uri_protocols.nr) {
1574 create_pack_file(&data, &data.uri_protocols);
1575 } else {
1576 packet_writer_write(&data.writer, "packfile\n");
1577 create_pack_file(&data, NULL);
1578 }
3145ea95
BW
1579 state = FETCH_DONE;
1580 break;
1581 case FETCH_DONE:
1582 continue;
1583 }
1584 }
1585
1586 upload_pack_data_clear(&data);
1587 return 0;
1588}
685fbd32
BW
1589
1590int upload_pack_advertise(struct repository *r,
1591 struct strbuf *value)
1592{
ba95710a
JT
1593 if (value) {
1594 int allow_filter_value;
516e2b76 1595 int allow_ref_in_want;
0bbc0bc5 1596 int allow_sideband_all_value;
dd4b732d 1597 char *str = NULL;
516e2b76 1598
685fbd32 1599 strbuf_addstr(value, "shallow");
516e2b76 1600
ba95710a
JT
1601 if (!repo_config_get_bool(the_repository,
1602 "uploadpack.allowfilter",
1603 &allow_filter_value) &&
1604 allow_filter_value)
1605 strbuf_addstr(value, " filter");
516e2b76
BW
1606
1607 if (!repo_config_get_bool(the_repository,
1608 "uploadpack.allowrefinwant",
1609 &allow_ref_in_want) &&
1610 allow_ref_in_want)
1611 strbuf_addstr(value, " ref-in-want");
0bbc0bc5 1612
07c3c2aa
JT
1613 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1614 (!repo_config_get_bool(the_repository,
1615 "uploadpack.allowsidebandall",
1616 &allow_sideband_all_value) &&
1617 allow_sideband_all_value))
0bbc0bc5 1618 strbuf_addstr(value, " sideband-all");
dd4b732d
JT
1619
1620 if (!repo_config_get_string(the_repository,
1621 "uploadpack.blobpackfileuri",
1622 &str) &&
1623 str) {
1624 strbuf_addstr(value, " packfile-uris");
1625 free(str);
1626 }
ba95710a 1627 }
516e2b76 1628
685fbd32
BW
1629 return 1;
1630}