]> git.ipfire.org Git - thirdparty/git.git/blame - upload-pack.c
shallow: add repository argument to set_alternate_shallow_file
[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"
cbd53a21 6#include "object-store.h"
f6b42a81
JH
7#include "tag.h"
8#include "object.h"
f0243f26 9#include "commit.h"
77cb17e9 10#include "exec_cmd.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"
9812f213 21#include "parse-options.h"
569e554b 22#include "argv-array.h"
5411b10c 23#include "prio-queue.h"
aa9bab29 24#include "protocol.h"
10ac85c7 25#include "quote.h"
def88e9a 26
9812f213
AQ
27static const char * const upload_pack_usage[] = {
28 N_("git upload-pack [<options>] <dir>"),
29 NULL
30};
def88e9a 31
208acbfb 32/* Remember to update object flag allocation in object.h */
937a515a
JH
33#define THEY_HAVE (1u << 11)
34#define OUR_REF (1u << 12)
35#define WANTED (1u << 13)
36#define COMMON_KNOWN (1u << 14)
37#define REACHABLE (1u << 15)
38
f53514bc
JS
39#define SHALLOW (1u << 16)
40#define NOT_SHALLOW (1u << 17)
41#define CLIENT_SHALLOW (1u << 18)
390eb36b 42#define HIDDEN_REF (1u << 19)
f53514bc 43
dddbad72 44static timestamp_t oldest_have;
937a515a 45
cccf74e2 46static int deepen_relative;
3f1da57f 47static int multi_ack;
4e10cf9a 48static int no_done;
348e390b 49static int use_thin_pack, use_ofs_delta, use_include_tag;
9462e3f5 50static int no_progress, daemon_mode;
7199c093
FM
51/* Allow specifying sha1 if it is a ref tip. */
52#define ALLOW_TIP_SHA1 01
68ee6289
FM
53/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54#define ALLOW_REACHABLE_SHA1 02
f8edeaa0
DT
55/* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
56#define ALLOW_ANY_SHA1 07
7199c093 57static unsigned int allow_unadvertised_object_request;
f0cea83f 58static int shallow_nr;
b1e9fff7
JH
59static struct object_array have_obj;
60static struct object_array want_obj;
6523078b 61static struct object_array extra_edge_obj;
96f1e58f 62static unsigned int timeout;
115dedd7 63static int keepalive = 5;
d47f3db7
JH
64/* 0 for no sideband,
65 * otherwise maximum packet size (up to 65520 bytes).
66 */
96f1e58f 67static int use_sideband;
42526b47
SP
68static int advertise_refs;
69static int stateless_rpc;
20b20a22 70static const char *pack_objects_hook;
960deccb 71
10ac85c7 72static int filter_capability_requested;
c7620bd0 73static int allow_filter;
10ac85c7
JH
74static struct list_objects_filter_options filter_options;
75
960deccb
PA
76static void reset_timeout(void)
77{
78 alarm(timeout);
79}
fb9040cc 80
fcf0fe9e 81static void send_client_data(int fd, const char *data, ssize_t sz)
583b7ea3 82{
4c4b7d1d
LF
83 if (use_sideband) {
84 send_sideband(1, fd, data, sz, use_sideband);
fcf0fe9e 85 return;
4c4b7d1d 86 }
958c24b1
JH
87 if (fd == 3)
88 /* emergency quit */
89 fd = 2;
90 if (fd == 2) {
93822c22 91 /* XXX: are we happy to lose stuff here? */
958c24b1 92 xwrite(fd, data, sz);
fcf0fe9e 93 return;
583b7ea3 94 }
cdf4fb8e 95 write_or_die(fd, data, sz);
583b7ea3
JH
96}
97
b790e0f6
NTND
98static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
99{
100 FILE *fp = cb_data;
101 if (graft->nr_parent == -1)
7683e2e6 102 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
b790e0f6
NTND
103 return 0;
104}
105
fb9040cc
LT
106static void create_pack_file(void)
107{
d3180279 108 struct child_process pack_objects = CHILD_PROCESS_INIT;
363b7817 109 char data[8193], progress[128];
583b7ea3
JH
110 char abort_msg[] = "aborting due to possible repository "
111 "corruption on the remote side.";
b1c71b72 112 int buffered = -1;
1456b043 113 ssize_t sz;
65a3629e 114 int i;
cdab4858 115 FILE *pipe_fd;
75bfc6c2 116
20b20a22
JK
117 if (!pack_objects_hook)
118 pack_objects.git_cmd = 1;
119 else {
120 argv_array_push(&pack_objects.args, pack_objects_hook);
121 argv_array_push(&pack_objects.args, "git");
122 pack_objects.use_shell = 1;
123 }
124
cdab4858 125 if (shallow_nr) {
65a3629e
MP
126 argv_array_push(&pack_objects.args, "--shallow-file");
127 argv_array_push(&pack_objects.args, "");
f0cea83f 128 }
65a3629e
MP
129 argv_array_push(&pack_objects.args, "pack-objects");
130 argv_array_push(&pack_objects.args, "--revs");
cdab4858 131 if (use_thin_pack)
65a3629e 132 argv_array_push(&pack_objects.args, "--thin");
75bfc6c2 133
65a3629e 134 argv_array_push(&pack_objects.args, "--stdout");
2dacf26d 135 if (shallow_nr)
65a3629e 136 argv_array_push(&pack_objects.args, "--shallow");
cc41fa8d 137 if (!no_progress)
65a3629e 138 argv_array_push(&pack_objects.args, "--progress");
cc41fa8d 139 if (use_ofs_delta)
65a3629e 140 argv_array_push(&pack_objects.args, "--delta-base-offset");
348e390b 141 if (use_include_tag)
65a3629e 142 argv_array_push(&pack_objects.args, "--include-tag");
10ac85c7 143 if (filter_options.filter_spec) {
0b6069fe
JT
144 if (pack_objects.use_shell) {
145 struct strbuf buf = STRBUF_INIT;
146 sq_quote_buf(&buf, filter_options.filter_spec);
147 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
148 strbuf_release(&buf);
149 } else {
150 argv_array_pushf(&pack_objects.args, "--filter=%s",
151 filter_options.filter_spec);
152 }
10ac85c7 153 }
cc41fa8d 154
b9612197 155 pack_objects.in = -1;
cc41fa8d
JS
156 pack_objects.out = -1;
157 pack_objects.err = -1;
21edd3f1 158
4c324c00 159 if (start_command(&pack_objects))
7e44c935 160 die("git upload-pack: unable to fork git-pack-objects");
b1c71b72 161
cdab4858
NTND
162 pipe_fd = xfdopen(pack_objects.in, "w");
163
b790e0f6
NTND
164 if (shallow_nr)
165 for_each_commit_graft(write_one_shallow, pipe_fd);
166
cdab4858
NTND
167 for (i = 0; i < want_obj.nr; i++)
168 fprintf(pipe_fd, "%s\n",
f2fd0760 169 oid_to_hex(&want_obj.objects[i].item->oid));
cdab4858
NTND
170 fprintf(pipe_fd, "--not\n");
171 for (i = 0; i < have_obj.nr; i++)
172 fprintf(pipe_fd, "%s\n",
f2fd0760 173 oid_to_hex(&have_obj.objects[i].item->oid));
cdab4858
NTND
174 for (i = 0; i < extra_edge_obj.nr; i++)
175 fprintf(pipe_fd, "%s\n",
f2fd0760 176 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
cdab4858
NTND
177 fprintf(pipe_fd, "\n");
178 fflush(pipe_fd);
179 fclose(pipe_fd);
f0cea83f 180
cc41fa8d
JS
181 /* We read from pack_objects.err to capture stderr output for
182 * progress bar, and pack_objects.out to capture the pack data.
b1c71b72 183 */
b1c71b72
JH
184
185 while (1) {
b1c71b72 186 struct pollfd pfd[2];
363b7817 187 int pe, pu, pollsize;
05e95155 188 int ret;
b1c71b72 189
0d516ada
ML
190 reset_timeout();
191
b1c71b72 192 pollsize = 0;
363b7817 193 pe = pu = -1;
b1c71b72 194
cc41fa8d
JS
195 if (0 <= pack_objects.out) {
196 pfd[pollsize].fd = pack_objects.out;
b1c71b72
JH
197 pfd[pollsize].events = POLLIN;
198 pu = pollsize;
199 pollsize++;
200 }
cc41fa8d
JS
201 if (0 <= pack_objects.err) {
202 pfd[pollsize].fd = pack_objects.err;
363b7817
JH
203 pfd[pollsize].events = POLLIN;
204 pe = pollsize;
205 pollsize++;
206 }
b1c71b72 207
4c324c00
JS
208 if (!pollsize)
209 break;
210
6c71f8b0
ET
211 ret = poll(pfd, pollsize,
212 keepalive < 0 ? -1 : 1000 * keepalive);
213
05e95155 214 if (ret < 0) {
4c324c00 215 if (errno != EINTR) {
d2b6afa2 216 error_errno("poll failed, resuming");
4c324c00 217 sleep(1);
b1c71b72 218 }
4c324c00
JS
219 continue;
220 }
6b59f51b
NP
221 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
222 /* Status ready; we ship that in the side-band
223 * or dump to the standard error.
224 */
225 sz = xread(pack_objects.err, progress,
226 sizeof(progress));
227 if (0 < sz)
228 send_client_data(2, progress, sz);
229 else if (sz == 0) {
230 close(pack_objects.err);
231 pack_objects.err = -1;
232 }
233 else
234 goto fail;
235 /* give priority to status messages */
236 continue;
237 }
4c324c00
JS
238 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
239 /* Data ready; we keep the last byte to ourselves
240 * in case we detect broken rev-list, so that we
241 * can leave the stream corrupted. This is
242 * unfortunate -- unpack-objects would happily
243 * accept a valid packdata with trailing garbage,
244 * so appending garbage after we pass all the
245 * pack data is not good enough to signal
246 * breakage to downstream.
247 */
248 char *cp = data;
249 ssize_t outsz = 0;
250 if (0 <= buffered) {
251 *cp++ = buffered;
252 outsz++;
b1c71b72 253 }
4c324c00
JS
254 sz = xread(pack_objects.out, cp,
255 sizeof(data) - outsz);
256 if (0 < sz)
1456b043 257 ;
4c324c00
JS
258 else if (sz == 0) {
259 close(pack_objects.out);
260 pack_objects.out = -1;
363b7817 261 }
4c324c00
JS
262 else
263 goto fail;
264 sz += outsz;
265 if (1 < sz) {
266 buffered = data[sz-1] & 0xFF;
267 sz--;
b1c71b72 268 }
4c324c00
JS
269 else
270 buffered = -1;
fcf0fe9e 271 send_client_data(1, data, sz);
4c324c00 272 }
05e95155
JK
273
274 /*
275 * We hit the keepalive timeout without saying anything; send
276 * an empty message on the data sideband just to let the other
277 * side know we're still working on it, but don't have any data
278 * yet.
279 *
280 * If we don't have a sideband channel, there's no room in the
281 * protocol to say anything, so those clients are just out of
282 * luck.
283 */
284 if (!ret && use_sideband) {
285 static const char buf[] = "0005\1";
286 write_or_die(1, buf, 5);
287 }
4c324c00 288 }
b1c71b72 289
4c324c00 290 if (finish_command(&pack_objects)) {
7e44c935 291 error("git upload-pack: git-pack-objects died with error.");
4c324c00
JS
292 goto fail;
293 }
b1c71b72 294
4c324c00
JS
295 /* flush the data */
296 if (0 <= buffered) {
297 data[0] = buffered;
fcf0fe9e 298 send_client_data(1, data, 1);
4c324c00 299 fprintf(stderr, "flushed.\n");
b1c71b72 300 }
4c324c00
JS
301 if (use_sideband)
302 packet_flush(1);
303 return;
304
b1c71b72 305 fail:
583b7ea3 306 send_client_data(3, abort_msg, sizeof(abort_msg));
7e44c935 307 die("git upload-pack: %s", abort_msg);
fb9040cc
LT
308}
309
cf93982f 310static int got_oid(const char *hex, struct object_id *oid)
def88e9a 311{
b1e9fff7 312 struct object *o;
937a515a 313 int we_knew_they_have = 0;
b1e9fff7 314
cf93982f 315 if (get_oid_hex(hex, oid))
7e44c935 316 die("git upload-pack: expected SHA1 object, got '%s'", hex);
cf93982f 317 if (!has_object_file(oid))
937a515a 318 return -1;
b1e9fff7 319
c251c83d 320 o = parse_object(oid);
b1e9fff7 321 if (!o)
cf93982f 322 die("oops (%s)", oid_to_hex(oid));
182a8dab 323 if (o->type == OBJ_COMMIT) {
b1e9fff7 324 struct commit_list *parents;
937a515a 325 struct commit *commit = (struct commit *)o;
b1e9fff7 326 if (o->flags & THEY_HAVE)
937a515a
JH
327 we_knew_they_have = 1;
328 else
329 o->flags |= THEY_HAVE;
330 if (!oldest_have || (commit->date < oldest_have))
331 oldest_have = commit->date;
332 for (parents = commit->parents;
b1e9fff7
JH
333 parents;
334 parents = parents->next)
335 parents->item->object.flags |= THEY_HAVE;
fb9040cc 336 }
937a515a
JH
337 if (!we_knew_they_have) {
338 add_object_array(o, NULL, &have_obj);
339 return 1;
340 }
341 return 0;
342}
343
344static int reachable(struct commit *want)
345{
5411b10c 346 struct prio_queue work = { compare_commits_by_commit_date };
937a515a 347
5411b10c
JK
348 prio_queue_put(&work, want);
349 while (work.nr) {
e510ab89 350 struct commit_list *list;
5411b10c 351 struct commit *commit = prio_queue_get(&work);
937a515a
JH
352
353 if (commit->object.flags & THEY_HAVE) {
354 want->object.flags |= COMMON_KNOWN;
355 break;
356 }
357 if (!commit->object.parsed)
c251c83d 358 parse_object(&commit->object.oid);
937a515a
JH
359 if (commit->object.flags & REACHABLE)
360 continue;
361 commit->object.flags |= REACHABLE;
362 if (commit->date < oldest_have)
363 continue;
364 for (list = commit->parents; list; list = list->next) {
365 struct commit *parent = list->item;
366 if (!(parent->object.flags & REACHABLE))
5411b10c 367 prio_queue_put(&work, parent);
937a515a
JH
368 }
369 }
370 want->object.flags |= REACHABLE;
371 clear_commit_marks(want, REACHABLE);
5411b10c 372 clear_prio_queue(&work);
937a515a
JH
373 return (want->object.flags & COMMON_KNOWN);
374}
375
376static int ok_to_give_up(void)
377{
378 int i;
379
380 if (!have_obj.nr)
381 return 0;
382
383 for (i = 0; i < want_obj.nr; i++) {
384 struct object *want = want_obj.objects[i].item;
385
386 if (want->flags & COMMON_KNOWN)
387 continue;
388 want = deref_tag(want, "a want line", 0);
389 if (!want || want->type != OBJ_COMMIT) {
390 /* no way to tell if this is reachable by
391 * looking at the ancestry chain alone, so
392 * leave a note to ourselves not to worry about
393 * this object anymore.
394 */
395 want_obj.objects[i].item->flags |= COMMON_KNOWN;
396 continue;
397 }
398 if (!reachable((struct commit *)want))
399 return 0;
400 }
fb9040cc 401 return 1;
def88e9a
LT
402}
403
404static int get_common_commits(void)
405{
cf93982f 406 struct object_id oid;
407 char last_hex[GIT_MAX_HEXSZ + 1];
49bee717
SP
408 int got_common = 0;
409 int got_other = 0;
4e10cf9a 410 int sent_ready = 0;
def88e9a 411
f0243f26
JS
412 save_commit_buffer = 0;
413
eeefa7c9 414 for (;;) {
74543a04 415 char *line = packet_read_line(0, NULL);
8bf3b758
NTND
416 const char *arg;
417
960deccb 418 reset_timeout();
def88e9a 419
74543a04 420 if (!line) {
49bee717 421 if (multi_ack == 2 && got_common
4e10cf9a
JH
422 && !got_other && ok_to_give_up()) {
423 sent_ready = 1;
81c634e9 424 packet_write_fmt(1, "ACK %s ready\n", last_hex);
4e10cf9a 425 }
b1e9fff7 426 if (have_obj.nr == 0 || multi_ack)
81c634e9 427 packet_write_fmt(1, "NAK\n");
4e10cf9a
JH
428
429 if (no_done && sent_ready) {
81c634e9 430 packet_write_fmt(1, "ACK %s\n", last_hex);
4e10cf9a
JH
431 return 0;
432 }
42526b47
SP
433 if (stateless_rpc)
434 exit(0);
49bee717
SP
435 got_common = 0;
436 got_other = 0;
def88e9a
LT
437 continue;
438 }
8bf3b758 439 if (skip_prefix(line, "have ", &arg)) {
cf93982f 440 switch (got_oid(arg, &oid)) {
937a515a 441 case -1: /* they have what we do not */
49bee717 442 got_other = 1;
78affc49 443 if (multi_ack && ok_to_give_up()) {
cf93982f 444 const char *hex = oid_to_hex(&oid);
4e10cf9a
JH
445 if (multi_ack == 2) {
446 sent_ready = 1;
81c634e9 447 packet_write_fmt(1, "ACK %s ready\n", hex);
4e10cf9a 448 } else
81c634e9 449 packet_write_fmt(1, "ACK %s continue\n", hex);
78affc49 450 }
937a515a
JH
451 break;
452 default:
49bee717 453 got_common = 1;
cf93982f 454 memcpy(last_hex, oid_to_hex(&oid), 41);
78affc49 455 if (multi_ack == 2)
81c634e9 456 packet_write_fmt(1, "ACK %s common\n", last_hex);
78affc49 457 else if (multi_ack)
81c634e9 458 packet_write_fmt(1, "ACK %s continue\n", last_hex);
c04c4e57 459 else if (have_obj.nr == 1)
81c634e9 460 packet_write_fmt(1, "ACK %s\n", last_hex);
937a515a 461 break;
af2d3aa4 462 }
def88e9a
LT
463 continue;
464 }
465 if (!strcmp(line, "done")) {
b1e9fff7 466 if (have_obj.nr > 0) {
1bd8c8f0 467 if (multi_ack)
81c634e9 468 packet_write_fmt(1, "ACK %s\n", last_hex);
1bd8c8f0
JS
469 return 0;
470 }
81c634e9 471 packet_write_fmt(1, "NAK\n");
def88e9a
LT
472 return -1;
473 }
7e44c935 474 die("git upload-pack: expected SHA1 list, got '%s'", line);
def88e9a 475 }
def88e9a
LT
476}
477
390eb36b
JH
478static int is_our_ref(struct object *o)
479{
68ee6289
FM
480 int allow_hidden_ref = (allow_unadvertised_object_request &
481 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
7199c093 482 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
390eb36b
JH
483}
484
2997178e
NTND
485/*
486 * on successful case, it's up to the caller to close cmd->out
487 */
488static int do_reachable_revlist(struct child_process *cmd,
079aa97e
NTND
489 struct object_array *src,
490 struct object_array *reachable)
051e4005
JH
491{
492 static const char *argv[] = {
493 "rev-list", "--stdin", NULL,
494 };
051e4005
JH
495 struct object *o;
496 char namebuf[42]; /* ^ + SHA-1 + LF */
497 int i;
498
2997178e
NTND
499 cmd->argv = argv;
500 cmd->git_cmd = 1;
501 cmd->no_stderr = 1;
502 cmd->in = -1;
503 cmd->out = -1;
051e4005
JH
504
505 /*
7fcbd37f
NTND
506 * If the next rev-list --stdin encounters an unknown commit,
507 * it terminates, which will cause SIGPIPE in the write loop
051e4005
JH
508 * below.
509 */
510 sigchain_push(SIGPIPE, SIG_IGN);
511
2997178e 512 if (start_command(cmd))
7fcbd37f
NTND
513 goto error;
514
051e4005 515 namebuf[0] = '^';
cf93982f 516 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
051e4005
JH
517 for (i = get_max_object_index(); 0 < i; ) {
518 o = get_indexed_object(--i);
2a745324
BH
519 if (!o)
520 continue;
079aa97e
NTND
521 if (reachable && o->type == OBJ_COMMIT)
522 o->flags &= ~TMP_MARK;
390eb36b 523 if (!is_our_ref(o))
051e4005 524 continue;
f2fd0760 525 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
cf93982f 526 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
051e4005
JH
527 goto error;
528 }
cf93982f 529 namebuf[GIT_SHA1_HEXSZ] = '\n';
3f0f6624
NTND
530 for (i = 0; i < src->nr; i++) {
531 o = src->objects[i].item;
079aa97e
NTND
532 if (is_our_ref(o)) {
533 if (reachable)
534 add_object_array(o, NULL, reachable);
051e4005 535 continue;
079aa97e
NTND
536 }
537 if (reachable && o->type == OBJ_COMMIT)
538 o->flags |= TMP_MARK;
f2fd0760 539 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
cf93982f 540 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
051e4005
JH
541 goto error;
542 }
2997178e
NTND
543 close(cmd->in);
544 cmd->in = -1;
545 sigchain_pop(SIGPIPE);
051e4005 546
2997178e
NTND
547 return 0;
548
549error:
051e4005
JH
550 sigchain_pop(SIGPIPE);
551
2997178e
NTND
552 if (cmd->in >= 0)
553 close(cmd->in);
554 if (cmd->out >= 0)
555 close(cmd->out);
556 return -1;
557}
558
079aa97e
NTND
559static int get_reachable_list(struct object_array *src,
560 struct object_array *reachable)
561{
562 struct child_process cmd = CHILD_PROCESS_INIT;
563 int i;
564 struct object *o;
565 char namebuf[42]; /* ^ + SHA-1 + LF */
566
567 if (do_reachable_revlist(&cmd, src, reachable) < 0)
568 return -1;
569
570 while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
571 struct object_id sha1;
572
573 if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
574 break;
575
576 o = lookup_object(sha1.hash);
577 if (o && o->type == OBJ_COMMIT) {
578 o->flags &= ~TMP_MARK;
579 }
580 }
581 for (i = get_max_object_index(); 0 < i; i--) {
582 o = get_indexed_object(i - 1);
583 if (o && o->type == OBJ_COMMIT &&
584 (o->flags & TMP_MARK)) {
585 add_object_array(o, NULL, reachable);
586 o->flags &= ~TMP_MARK;
587 }
588 }
589 close(cmd.out);
590
591 if (finish_command(&cmd))
592 return -1;
593
594 return 0;
595}
596
2997178e
NTND
597static int has_unreachable(struct object_array *src)
598{
599 struct child_process cmd = CHILD_PROCESS_INIT;
600 char buf[1];
601 int i;
602
079aa97e 603 if (do_reachable_revlist(&cmd, src, NULL) < 0)
2997178e 604 return 1;
051e4005
JH
605
606 /*
607 * The commits out of the rev-list are not ancestors of
608 * our ref.
609 */
2997178e 610 i = read_in_full(cmd.out, buf, 1);
051e4005
JH
611 if (i)
612 goto error;
613 close(cmd.out);
7fcbd37f 614 cmd.out = -1;
051e4005
JH
615
616 /*
617 * rev-list may have died by encountering a bad commit
618 * in the history, in which case we do want to bail out
619 * even when it showed no commit.
620 */
621 if (finish_command(&cmd))
622 goto error;
623
624 /* All the non-tip ones are ancestors of what we advertised */
3f0f6624 625 return 0;
051e4005
JH
626
627error:
7fcbd37f 628 sigchain_pop(SIGPIPE);
7fcbd37f
NTND
629 if (cmd.out >= 0)
630 close(cmd.out);
3f0f6624
NTND
631 return 1;
632}
7fcbd37f 633
3f0f6624
NTND
634static void check_non_tip(void)
635{
636 int i;
637
638 /*
639 * In the normal in-process case without
640 * uploadpack.allowReachableSHA1InWant,
641 * non-tip requests can never happen.
642 */
643 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
644 goto error;
645 if (!has_unreachable(&want_obj))
646 /* All the non-tip ones are ancestors of what we advertised */
647 return;
051e4005
JH
648
649error:
650 /* Pick one of them (we know there at least is one) */
651 for (i = 0; i < want_obj.nr; i++) {
3f0f6624 652 struct object *o = want_obj.objects[i].item;
390eb36b 653 if (!is_our_ref(o))
051e4005 654 die("git upload-pack: not our ref %s",
f2fd0760 655 oid_to_hex(&o->oid));
051e4005
JH
656 }
657}
658
5c24cdea
NTND
659static void send_shallow(struct commit_list *result)
660{
661 while (result) {
662 struct object *object = &result->item->object;
663 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
dbaa6bdc
JH
664 packet_write_fmt(1, "shallow %s",
665 oid_to_hex(&object->oid));
e92b848c 666 register_shallow(&object->oid);
5c24cdea
NTND
667 shallow_nr++;
668 }
669 result = result->next;
670 }
671}
672
873700c9 673static void send_unshallow(const struct object_array *shallows)
e8e44de7 674{
e8e44de7 675 int i;
873700c9 676
e8e44de7
NTND
677 for (i = 0; i < shallows->nr; i++) {
678 struct object *object = shallows->objects[i].item;
679 if (object->flags & NOT_SHALLOW) {
680 struct commit_list *parents;
dbaa6bdc
JH
681 packet_write_fmt(1, "unshallow %s",
682 oid_to_hex(&object->oid));
e8e44de7 683 object->flags &= ~CLIENT_SHALLOW;
873700c9
NTND
684 /*
685 * We want to _register_ "object" as shallow, but we
686 * also need to traverse object's parents to deepen a
687 * shallow clone. Unregister it for now so we can
688 * parse and add the parents to the want list, then
689 * re-register it.
690 */
e92b848c 691 unregister_shallow(&object->oid);
e8e44de7
NTND
692 object->parsed = 0;
693 parse_commit_or_die((struct commit *)object);
694 parents = ((struct commit *)object)->parents;
695 while (parents) {
696 add_object_array(&parents->item->object,
697 NULL, &want_obj);
698 parents = parents->next;
699 }
700 add_object_array(object, NULL, &extra_edge_obj);
701 }
702 /* make sure commit traversal conforms to client */
e92b848c 703 register_shallow(&object->oid);
e8e44de7 704 }
873700c9
NTND
705}
706
cccf74e2
NTND
707static void deepen(int depth, int deepen_relative,
708 struct object_array *shallows)
873700c9
NTND
709{
710 if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
711 int i;
712
713 for (i = 0; i < shallows->nr; i++) {
714 struct object *object = shallows->objects[i].item;
715 object->flags |= NOT_SHALLOW;
716 }
cccf74e2
NTND
717 } else if (deepen_relative) {
718 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
719 struct commit_list *result;
720
721 get_reachable_list(shallows, &reachable_shallows);
722 result = get_shallow_commits(&reachable_shallows,
723 depth + 1,
724 SHALLOW, NOT_SHALLOW);
725 send_shallow(result);
726 free_commit_list(result);
727 object_array_clear(&reachable_shallows);
873700c9
NTND
728 } else {
729 struct commit_list *result;
730
731 result = get_shallow_commits(&want_obj, depth,
732 SHALLOW, NOT_SHALLOW);
733 send_shallow(result);
734 free_commit_list(result);
735 }
736
737 send_unshallow(shallows);
e8e44de7
NTND
738 packet_flush(1);
739}
740
569e554b
NTND
741static void deepen_by_rev_list(int ac, const char **av,
742 struct object_array *shallows)
743{
744 struct commit_list *result;
745
746 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
747 send_shallow(result);
748 free_commit_list(result);
749 send_unshallow(shallows);
750 packet_flush(1);
751}
752
b1e9fff7 753static void receive_needs(void)
fb9040cc 754{
3cd47459 755 struct object_array shallows = OBJECT_ARRAY_INIT;
269a7a83 756 struct string_list deepen_not = STRING_LIST_INIT_DUP;
74543a04 757 int depth = 0;
051e4005 758 int has_non_tip = 0;
dddbad72 759 timestamp_t deepen_since = 0;
569e554b 760 int deepen_rev_list = 0;
fb9040cc 761
f0cea83f 762 shallow_nr = 0;
fb9040cc 763 for (;;) {
565ebbf7 764 struct object *o;
f47182c8 765 const char *features;
cf93982f 766 struct object_id oid_buf;
74543a04 767 char *line = packet_read_line(0, NULL);
8bf3b758
NTND
768 const char *arg;
769
960deccb 770 reset_timeout();
74543a04 771 if (!line)
ed09aef0 772 break;
e091eb93 773
8bf3b758 774 if (skip_prefix(line, "shallow ", &arg)) {
cf93982f 775 struct object_id oid;
ed09aef0 776 struct object *object;
cf93982f 777 if (get_oid_hex(arg, &oid))
ed09aef0 778 die("invalid shallow line: %s", line);
c251c83d 779 object = parse_object(&oid);
ed09aef0 780 if (!object)
af04fa2a 781 continue;
6293ded3 782 if (object->type != OBJ_COMMIT)
cf93982f 783 die("invalid shallow object %s", oid_to_hex(&oid));
e58e57e4
JK
784 if (!(object->flags & CLIENT_SHALLOW)) {
785 object->flags |= CLIENT_SHALLOW;
786 add_object_array(object, NULL, &shallows);
787 }
ed09aef0
JS
788 continue;
789 }
8bf3b758 790 if (skip_prefix(line, "deepen ", &arg)) {
6e414e30 791 char *end = NULL;
8bf3b758 792 depth = strtol(arg, &end, 0);
6e414e30 793 if (!end || *end || depth <= 0)
016e6ccb
JS
794 die("Invalid deepen: %s", line);
795 continue;
796 }
569e554b
NTND
797 if (skip_prefix(line, "deepen-since ", &arg)) {
798 char *end = NULL;
1aeb7e75 799 deepen_since = parse_timestamp(arg, &end, 0);
569e554b
NTND
800 if (!end || *end || !deepen_since ||
801 /* revisions.c's max_age -1 is special */
802 deepen_since == -1)
803 die("Invalid deepen-since: %s", line);
804 deepen_rev_list = 1;
805 continue;
806 }
269a7a83
NTND
807 if (skip_prefix(line, "deepen-not ", &arg)) {
808 char *ref = NULL;
cf93982f 809 struct object_id oid;
cca5fa64 810 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
269a7a83
NTND
811 die("git upload-pack: ambiguous deepen-not: %s", line);
812 string_list_append(&deepen_not, ref);
813 free(ref);
814 deepen_rev_list = 1;
815 continue;
816 }
10ac85c7
JH
817 if (skip_prefix(line, "filter ", &arg)) {
818 if (!filter_capability_requested)
819 die("git upload-pack: filtering capability not negotiated");
820 parse_list_objects_filter(&filter_options, arg);
821 continue;
822 }
8bf3b758 823 if (!skip_prefix(line, "want ", &arg) ||
cf93982f 824 get_oid_hex(arg, &oid_buf))
7e44c935 825 die("git upload-pack: protocol error, "
e091eb93 826 "expected to get sha, not '%s'", line);
f47182c8 827
8bf3b758 828 features = arg + 40;
f47182c8 829
cccf74e2
NTND
830 if (parse_feature_request(features, "deepen-relative"))
831 deepen_relative = 1;
f47182c8 832 if (parse_feature_request(features, "multi_ack_detailed"))
78affc49 833 multi_ack = 2;
f47182c8 834 else if (parse_feature_request(features, "multi_ack"))
1bd8c8f0 835 multi_ack = 1;
f47182c8 836 if (parse_feature_request(features, "no-done"))
4e10cf9a 837 no_done = 1;
f47182c8 838 if (parse_feature_request(features, "thin-pack"))
b19696c2 839 use_thin_pack = 1;
f47182c8 840 if (parse_feature_request(features, "ofs-delta"))
e4fe4b8e 841 use_ofs_delta = 1;
f47182c8 842 if (parse_feature_request(features, "side-band-64k"))
d47f3db7 843 use_sideband = LARGE_PACKET_MAX;
f47182c8 844 else if (parse_feature_request(features, "side-band"))
d47f3db7 845 use_sideband = DEFAULT_PACKET_MAX;
f47182c8 846 if (parse_feature_request(features, "no-progress"))
b0e90897 847 no_progress = 1;
f47182c8 848 if (parse_feature_request(features, "include-tag"))
348e390b 849 use_include_tag = 1;
c7620bd0 850 if (allow_filter && parse_feature_request(features, "filter"))
10ac85c7 851 filter_capability_requested = 1;
565ebbf7 852
c251c83d 853 o = parse_object(&oid_buf);
bdb31ead
JT
854 if (!o) {
855 packet_write_fmt(1,
856 "ERR upload-pack: not our ref %s",
cf93982f 857 oid_to_hex(&oid_buf));
9f9aa761 858 die("git upload-pack: not our ref %s",
cf93982f 859 oid_to_hex(&oid_buf));
bdb31ead 860 }
565ebbf7
JH
861 if (!(o->flags & WANTED)) {
862 o->flags |= WANTED;
f8edeaa0
DT
863 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
864 || is_our_ref(o)))
051e4005 865 has_non_tip = 1;
b1e9fff7 866 add_object_array(o, NULL, &want_obj);
565ebbf7 867 }
fb9040cc 868 }
9462e3f5 869
051e4005
JH
870 /*
871 * We have sent all our refs already, and the other end
872 * should have chosen out of them. When we are operating
873 * in the stateless RPC mode, however, their choice may
874 * have been based on the set of older refs advertised
875 * by another process that handled the initial request.
876 */
877 if (has_non_tip)
878 check_non_tip();
879
9462e3f5
JS
880 if (!use_sideband && daemon_mode)
881 no_progress = 1;
882
569e554b 883 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
f53514bc 884 return;
569e554b 885 if (depth > 0 && deepen_rev_list)
269a7a83 886 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
e8e44de7 887 if (depth > 0)
cccf74e2 888 deepen(depth, deepen_relative, &shallows);
569e554b
NTND
889 else if (deepen_rev_list) {
890 struct argv_array av = ARGV_ARRAY_INIT;
f53514bc 891 int i;
569e554b
NTND
892
893 argv_array_push(&av, "rev-list");
894 if (deepen_since)
cb71f8bd 895 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
269a7a83
NTND
896 if (deepen_not.nr) {
897 argv_array_push(&av, "--not");
898 for (i = 0; i < deepen_not.nr; i++) {
899 struct string_list_item *s = deepen_not.items + i;
900 argv_array_push(&av, s->string);
f53514bc 901 }
269a7a83 902 argv_array_push(&av, "--not");
016e6ccb 903 }
569e554b
NTND
904 for (i = 0; i < want_obj.nr; i++) {
905 struct object *o = want_obj.objects[i].item;
906 argv_array_push(&av, oid_to_hex(&o->oid));
f53514bc 907 }
569e554b
NTND
908 deepen_by_rev_list(av.argc, av.argv, &shallows);
909 argv_array_clear(&av);
910 }
e8e44de7 911 else
f53514bc
JS
912 if (shallows.nr > 0) {
913 int i;
914 for (i = 0; i < shallows.nr; i++)
e92b848c 915 register_shallow(&shallows.objects[i].item->oid);
f53514bc 916 }
f0cea83f
NE
917
918 shallow_nr += shallows.nr;
dcb572ab 919 object_array_clear(&shallows);
fb9040cc
LT
920}
921
daebaa78 922/* return non-zero if the ref is hidden, otherwise 0 */
78a766ab
LF
923static int mark_our_ref(const char *refname, const char *refname_full,
924 const struct object_id *oid)
cbbe50db 925{
363e98bf 926 struct object *o = lookup_unknown_object(oid->hash);
daebaa78 927
78a766ab 928 if (ref_is_hidden(refname, refname_full)) {
390eb36b 929 o->flags |= HIDDEN_REF;
daebaa78 930 return 1;
390eb36b 931 }
3f1da57f 932 o->flags |= OUR_REF;
cbbe50db
JH
933 return 0;
934}
935
78a766ab 936static int check_ref(const char *refname_full, const struct object_id *oid,
363e98bf 937 int flag, void *cb_data)
e172755b 938{
78a766ab
LF
939 const char *refname = strip_namespace(refname_full);
940
941 mark_our_ref(refname, refname_full, oid);
e172755b
JK
942 return 0;
943}
944
7171d8c1
JH
945static void format_symref_info(struct strbuf *buf, struct string_list *symref)
946{
947 struct string_list_item *item;
948
949 if (!symref->nr)
950 return;
951 for_each_string_list_item(item, symref)
952 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
953}
954
363e98bf
MH
955static int send_ref(const char *refname, const struct object_id *oid,
956 int flag, void *cb_data)
def88e9a 957{
ed09aef0 958 static const char *capabilities = "multi_ack thin-pack side-band"
cccf74e2
NTND
959 " side-band-64k ofs-delta shallow deepen-since deepen-not"
960 " deepen-relative no-progress include-tag multi_ack_detailed";
6b01ecfe 961 const char *refname_nons = strip_namespace(refname);
21758aff 962 struct object_id peeled;
b5b16990 963
78a766ab 964 if (mark_our_ref(refname_nons, refname, oid))
daebaa78 965 return 0;
cbbe50db 966
7171d8c1
JH
967 if (capabilities) {
968 struct strbuf symref_info = STRBUF_INIT;
969
970 format_symref_info(&symref_info, cb_data);
10ac85c7 971 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
363e98bf 972 oid_to_hex(oid), refname_nons,
cf2ad8e6 973 0, capabilities,
7199c093
FM
974 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
975 " allow-tip-sha1-in-want" : "",
68ee6289
FM
976 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
977 " allow-reachable-sha1-in-want" : "",
ff5effdf 978 stateless_rpc ? " no-done" : "",
7171d8c1 979 symref_info.buf,
c7620bd0 980 allow_filter ? " filter" : "",
ff5effdf 981 git_user_agent_sanitized());
7171d8c1
JH
982 strbuf_release(&symref_info);
983 } else {
81c634e9 984 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
7171d8c1 985 }
1f5881bb 986 capabilities = NULL;
b420d909 987 if (!peel_ref(refname, &peeled))
81c634e9 988 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
def88e9a
LT
989 return 0;
990}
991
7dabd056
MH
992static int find_symref(const char *refname, const struct object_id *oid,
993 int flag, void *cb_data)
7171d8c1
JH
994{
995 const char *symref_target;
996 struct string_list_item *item;
7171d8c1
JH
997
998 if ((flag & REF_ISSYMREF) == 0)
999 return 0;
744c040b 1000 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
7171d8c1
JH
1001 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1002 die("'%s' is a symref but it is not?", refname);
1003 item = string_list_append(cb_data, refname);
1004 item->util = xstrdup(symref_target);
1005 return 0;
1006}
1007
59076eba 1008static void upload_pack(void)
def88e9a 1009{
7171d8c1
JH
1010 struct string_list symref = STRING_LIST_INIT_DUP;
1011
1012 head_ref_namespaced(find_symref, &symref);
1013
42526b47
SP
1014 if (advertise_refs || !stateless_rpc) {
1015 reset_timeout();
7171d8c1
JH
1016 head_ref_namespaced(send_ref, &symref);
1017 for_each_namespaced_ref(send_ref, &symref);
ad491366 1018 advertise_shallow_grafts(1);
42526b47
SP
1019 packet_flush(1);
1020 } else {
e172755b
JK
1021 head_ref_namespaced(check_ref, NULL);
1022 for_each_namespaced_ref(check_ref, NULL);
42526b47 1023 }
7171d8c1 1024 string_list_clear(&symref, 1);
42526b47
SP
1025 if (advertise_refs)
1026 return;
1027
b1e9fff7 1028 receive_needs();
59076eba
DR
1029 if (want_obj.nr) {
1030 get_common_commits();
1031 create_pack_file();
1032 }
def88e9a
LT
1033}
1034
daebaa78
JH
1035static int upload_pack_config(const char *var, const char *value, void *unused)
1036{
7199c093
FM
1037 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1038 if (git_config_bool(var, value))
1039 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1040 else
1041 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
68ee6289
FM
1042 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1043 if (git_config_bool(var, value))
1044 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1045 else
1046 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
f8edeaa0
DT
1047 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1048 if (git_config_bool(var, value))
1049 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1050 else
1051 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
7199c093 1052 } else if (!strcmp("uploadpack.keepalive", var)) {
05e95155
JK
1053 keepalive = git_config_int(var, value);
1054 if (!keepalive)
1055 keepalive = -1;
20b20a22
JK
1056 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1057 if (!strcmp("uploadpack.packobjectshook", var))
1058 return git_config_string(&pack_objects_hook, var, value);
10ac85c7 1059 } else if (!strcmp("uploadpack.allowfilter", var)) {
c7620bd0 1060 allow_filter = git_config_bool(var, value);
05e95155 1061 }
daebaa78
JH
1062 return parse_hide_refs_config(var, value, "uploadpack");
1063}
1064
3f2e2297 1065int cmd_main(int argc, const char **argv)
def88e9a 1066{
9812f213 1067 const char *dir;
960deccb 1068 int strict = 0;
9812f213
AQ
1069 struct option options[] = {
1070 OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
1071 N_("quit after a single request/response exchange")),
1072 OPT_BOOL(0, "advertise-refs", &advertise_refs,
2e3a16b2 1073 N_("exit immediately after initial ref advertisement")),
9812f213
AQ
1074 OPT_BOOL(0, "strict", &strict,
1075 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1076 OPT_INTEGER(0, "timeout", &timeout,
1077 N_("interrupt transfer after <n> seconds of inactivity")),
1078 OPT_END()
1079 };
960deccb 1080
bbc30f99 1081 packet_trace_identity("upload-pack");
afc711b8 1082 check_replace_refs = 0;
2fb3f6db 1083
9812f213 1084 argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0);
960deccb 1085
9812f213
AQ
1086 if (argc != 1)
1087 usage_with_options(upload_pack_usage, options);
a6080a0a 1088
9812f213
AQ
1089 if (timeout)
1090 daemon_mode = 1;
04b33055 1091
e1464ca7 1092 setup_path();
04b33055 1093
9812f213 1094 dir = argv[0];
113b9475 1095
8d630132 1096 if (!enter_repo(dir, strict))
05ac6b34 1097 die("'%s' does not appear to be a git repository", dir);
ad491366 1098
daebaa78 1099 git_config(upload_pack_config, NULL);
aa9bab29
BW
1100
1101 switch (determine_protocol_version_server()) {
1102 case protocol_v1:
1103 /*
1104 * v1 is just the original protocol with a version string,
1105 * so just fall through after writing the version string.
1106 */
1107 if (advertise_refs || !stateless_rpc)
1108 packet_write_fmt(1, "version 1\n");
1109
1110 /* fallthrough */
1111 case protocol_v0:
1112 upload_pack();
1113 break;
1114 case protocol_unknown_version:
1115 BUG("unknown protocol version");
1116 }
1117
def88e9a
LT
1118 return 0;
1119}