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