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