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