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