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