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