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