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