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