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