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