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