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