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