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