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