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