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