]> git.ipfire.org Git - thirdparty/git.git/blame - upload-pack.c
upload-pack: convert to a builtin
[thirdparty/git.git] / upload-pack.c
CommitLineData
def88e9a 1#include "cache.h"
b2141fc1 2#include "config.h"
def88e9a
LT
3#include "refs.h"
4#include "pkt-line.h"
958c24b1 5#include "sideband.h"
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
b1e9fff7 727static void receive_needs(void)
fb9040cc 728{
3cd47459 729 struct object_array shallows = OBJECT_ARRAY_INIT;
269a7a83 730 struct string_list deepen_not = STRING_LIST_INIT_DUP;
74543a04 731 int depth = 0;
051e4005 732 int has_non_tip = 0;
dddbad72 733 timestamp_t deepen_since = 0;
569e554b 734 int deepen_rev_list = 0;
fb9040cc 735
f0cea83f 736 shallow_nr = 0;
fb9040cc 737 for (;;) {
565ebbf7 738 struct object *o;
f47182c8 739 const char *features;
cf93982f 740 struct object_id oid_buf;
74543a04 741 char *line = packet_read_line(0, NULL);
8bf3b758
NTND
742 const char *arg;
743
960deccb 744 reset_timeout();
74543a04 745 if (!line)
ed09aef0 746 break;
e091eb93 747
8bf3b758 748 if (skip_prefix(line, "shallow ", &arg)) {
cf93982f 749 struct object_id oid;
ed09aef0 750 struct object *object;
cf93982f 751 if (get_oid_hex(arg, &oid))
ed09aef0 752 die("invalid shallow line: %s", line);
c251c83d 753 object = parse_object(&oid);
ed09aef0 754 if (!object)
af04fa2a 755 continue;
6293ded3 756 if (object->type != OBJ_COMMIT)
cf93982f 757 die("invalid shallow object %s", oid_to_hex(&oid));
e58e57e4
JK
758 if (!(object->flags & CLIENT_SHALLOW)) {
759 object->flags |= CLIENT_SHALLOW;
760 add_object_array(object, NULL, &shallows);
761 }
ed09aef0
JS
762 continue;
763 }
8bf3b758 764 if (skip_prefix(line, "deepen ", &arg)) {
6e414e30 765 char *end = NULL;
8bf3b758 766 depth = strtol(arg, &end, 0);
6e414e30 767 if (!end || *end || depth <= 0)
016e6ccb
JS
768 die("Invalid deepen: %s", line);
769 continue;
770 }
569e554b
NTND
771 if (skip_prefix(line, "deepen-since ", &arg)) {
772 char *end = NULL;
1aeb7e75 773 deepen_since = parse_timestamp(arg, &end, 0);
569e554b
NTND
774 if (!end || *end || !deepen_since ||
775 /* revisions.c's max_age -1 is special */
776 deepen_since == -1)
777 die("Invalid deepen-since: %s", line);
778 deepen_rev_list = 1;
779 continue;
780 }
269a7a83
NTND
781 if (skip_prefix(line, "deepen-not ", &arg)) {
782 char *ref = NULL;
cf93982f 783 struct object_id oid;
cca5fa64 784 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
269a7a83
NTND
785 die("git upload-pack: ambiguous deepen-not: %s", line);
786 string_list_append(&deepen_not, ref);
787 free(ref);
788 deepen_rev_list = 1;
789 continue;
790 }
8bf3b758 791 if (!skip_prefix(line, "want ", &arg) ||
cf93982f 792 get_oid_hex(arg, &oid_buf))
7e44c935 793 die("git upload-pack: protocol error, "
e091eb93 794 "expected to get sha, not '%s'", line);
f47182c8 795
8bf3b758 796 features = arg + 40;
f47182c8 797
cccf74e2
NTND
798 if (parse_feature_request(features, "deepen-relative"))
799 deepen_relative = 1;
f47182c8 800 if (parse_feature_request(features, "multi_ack_detailed"))
78affc49 801 multi_ack = 2;
f47182c8 802 else if (parse_feature_request(features, "multi_ack"))
1bd8c8f0 803 multi_ack = 1;
f47182c8 804 if (parse_feature_request(features, "no-done"))
4e10cf9a 805 no_done = 1;
f47182c8 806 if (parse_feature_request(features, "thin-pack"))
b19696c2 807 use_thin_pack = 1;
f47182c8 808 if (parse_feature_request(features, "ofs-delta"))
e4fe4b8e 809 use_ofs_delta = 1;
f47182c8 810 if (parse_feature_request(features, "side-band-64k"))
d47f3db7 811 use_sideband = LARGE_PACKET_MAX;
f47182c8 812 else if (parse_feature_request(features, "side-band"))
d47f3db7 813 use_sideband = DEFAULT_PACKET_MAX;
f47182c8 814 if (parse_feature_request(features, "no-progress"))
b0e90897 815 no_progress = 1;
f47182c8 816 if (parse_feature_request(features, "include-tag"))
348e390b 817 use_include_tag = 1;
565ebbf7 818
c251c83d 819 o = parse_object(&oid_buf);
bdb31ead
JT
820 if (!o) {
821 packet_write_fmt(1,
822 "ERR upload-pack: not our ref %s",
cf93982f 823 oid_to_hex(&oid_buf));
9f9aa761 824 die("git upload-pack: not our ref %s",
cf93982f 825 oid_to_hex(&oid_buf));
bdb31ead 826 }
565ebbf7
JH
827 if (!(o->flags & WANTED)) {
828 o->flags |= WANTED;
f8edeaa0
DT
829 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
830 || is_our_ref(o)))
051e4005 831 has_non_tip = 1;
b1e9fff7 832 add_object_array(o, NULL, &want_obj);
565ebbf7 833 }
fb9040cc 834 }
9462e3f5 835
051e4005
JH
836 /*
837 * We have sent all our refs already, and the other end
838 * should have chosen out of them. When we are operating
839 * in the stateless RPC mode, however, their choice may
840 * have been based on the set of older refs advertised
841 * by another process that handled the initial request.
842 */
843 if (has_non_tip)
844 check_non_tip();
845
9462e3f5
JS
846 if (!use_sideband && daemon_mode)
847 no_progress = 1;
848
569e554b 849 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
f53514bc 850 return;
569e554b 851 if (depth > 0 && deepen_rev_list)
269a7a83 852 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
e8e44de7 853 if (depth > 0)
cccf74e2 854 deepen(depth, deepen_relative, &shallows);
569e554b
NTND
855 else if (deepen_rev_list) {
856 struct argv_array av = ARGV_ARRAY_INIT;
f53514bc 857 int i;
569e554b
NTND
858
859 argv_array_push(&av, "rev-list");
860 if (deepen_since)
cb71f8bd 861 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
269a7a83
NTND
862 if (deepen_not.nr) {
863 argv_array_push(&av, "--not");
864 for (i = 0; i < deepen_not.nr; i++) {
865 struct string_list_item *s = deepen_not.items + i;
866 argv_array_push(&av, s->string);
f53514bc 867 }
269a7a83 868 argv_array_push(&av, "--not");
016e6ccb 869 }
569e554b
NTND
870 for (i = 0; i < want_obj.nr; i++) {
871 struct object *o = want_obj.objects[i].item;
872 argv_array_push(&av, oid_to_hex(&o->oid));
f53514bc 873 }
569e554b
NTND
874 deepen_by_rev_list(av.argc, av.argv, &shallows);
875 argv_array_clear(&av);
876 }
e8e44de7 877 else
f53514bc
JS
878 if (shallows.nr > 0) {
879 int i;
880 for (i = 0; i < shallows.nr; i++)
e92b848c 881 register_shallow(&shallows.objects[i].item->oid);
f53514bc 882 }
f0cea83f
NE
883
884 shallow_nr += shallows.nr;
dcb572ab 885 object_array_clear(&shallows);
fb9040cc
LT
886}
887
daebaa78 888/* return non-zero if the ref is hidden, otherwise 0 */
78a766ab
LF
889static int mark_our_ref(const char *refname, const char *refname_full,
890 const struct object_id *oid)
cbbe50db 891{
363e98bf 892 struct object *o = lookup_unknown_object(oid->hash);
daebaa78 893
78a766ab 894 if (ref_is_hidden(refname, refname_full)) {
390eb36b 895 o->flags |= HIDDEN_REF;
daebaa78 896 return 1;
390eb36b 897 }
3f1da57f 898 o->flags |= OUR_REF;
cbbe50db
JH
899 return 0;
900}
901
78a766ab 902static int check_ref(const char *refname_full, const struct object_id *oid,
363e98bf 903 int flag, void *cb_data)
e172755b 904{
78a766ab
LF
905 const char *refname = strip_namespace(refname_full);
906
907 mark_our_ref(refname, refname_full, oid);
e172755b
JK
908 return 0;
909}
910
7171d8c1
JH
911static void format_symref_info(struct strbuf *buf, struct string_list *symref)
912{
913 struct string_list_item *item;
914
915 if (!symref->nr)
916 return;
917 for_each_string_list_item(item, symref)
918 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
919}
920
363e98bf
MH
921static int send_ref(const char *refname, const struct object_id *oid,
922 int flag, void *cb_data)
def88e9a 923{
ed09aef0 924 static const char *capabilities = "multi_ack thin-pack side-band"
cccf74e2
NTND
925 " side-band-64k ofs-delta shallow deepen-since deepen-not"
926 " deepen-relative no-progress include-tag multi_ack_detailed";
6b01ecfe 927 const char *refname_nons = strip_namespace(refname);
21758aff 928 struct object_id peeled;
b5b16990 929
78a766ab 930 if (mark_our_ref(refname_nons, refname, oid))
daebaa78 931 return 0;
cbbe50db 932
7171d8c1
JH
933 if (capabilities) {
934 struct strbuf symref_info = STRBUF_INIT;
935
936 format_symref_info(&symref_info, cb_data);
81c634e9 937 packet_write_fmt(1, "%s %s%c%s%s%s%s%s agent=%s\n",
363e98bf 938 oid_to_hex(oid), refname_nons,
cf2ad8e6 939 0, capabilities,
7199c093
FM
940 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
941 " allow-tip-sha1-in-want" : "",
68ee6289
FM
942 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
943 " allow-reachable-sha1-in-want" : "",
ff5effdf 944 stateless_rpc ? " no-done" : "",
7171d8c1 945 symref_info.buf,
ff5effdf 946 git_user_agent_sanitized());
7171d8c1
JH
947 strbuf_release(&symref_info);
948 } else {
81c634e9 949 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
7171d8c1 950 }
1f5881bb 951 capabilities = NULL;
b420d909 952 if (!peel_ref(refname, &peeled))
81c634e9 953 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
def88e9a
LT
954 return 0;
955}
956
7dabd056
MH
957static int find_symref(const char *refname, const struct object_id *oid,
958 int flag, void *cb_data)
7171d8c1
JH
959{
960 const char *symref_target;
961 struct string_list_item *item;
7171d8c1
JH
962
963 if ((flag & REF_ISSYMREF) == 0)
964 return 0;
744c040b 965 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
7171d8c1
JH
966 if (!symref_target || (flag & REF_ISSYMREF) == 0)
967 die("'%s' is a symref but it is not?", refname);
968 item = string_list_append(cb_data, refname);
969 item->util = xstrdup(symref_target);
970 return 0;
971}
972
daebaa78
JH
973static int upload_pack_config(const char *var, const char *value, void *unused)
974{
7199c093
FM
975 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
976 if (git_config_bool(var, value))
977 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
978 else
979 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
68ee6289
FM
980 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
981 if (git_config_bool(var, value))
982 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
983 else
984 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
f8edeaa0
DT
985 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
986 if (git_config_bool(var, value))
987 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
988 else
989 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
7199c093 990 } else if (!strcmp("uploadpack.keepalive", var)) {
05e95155
JK
991 keepalive = git_config_int(var, value);
992 if (!keepalive)
993 keepalive = -1;
20b20a22
JK
994 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
995 if (!strcmp("uploadpack.packobjectshook", var))
996 return git_config_string(&pack_objects_hook, var, value);
05e95155 997 }
daebaa78
JH
998 return parse_hide_refs_config(var, value, "uploadpack");
999}
1000
a3d6b53e 1001void upload_pack(struct upload_pack_options *options)
def88e9a 1002{
a3d6b53e 1003 struct string_list symref = STRING_LIST_INIT_DUP;
113b9475 1004
a3d6b53e
BW
1005 stateless_rpc = options->stateless_rpc;
1006 timeout = options->timeout;
1007 daemon_mode = options->daemon_mode;
ad491366 1008
daebaa78 1009 git_config(upload_pack_config, NULL);
aa9bab29 1010
a3d6b53e
BW
1011 head_ref_namespaced(find_symref, &symref);
1012
1013 if (options->advertise_refs || !stateless_rpc) {
1014 reset_timeout();
1015 head_ref_namespaced(send_ref, &symref);
1016 for_each_namespaced_ref(send_ref, &symref);
1017 advertise_shallow_grafts(1);
1018 packet_flush(1);
1019 } else {
1020 head_ref_namespaced(check_ref, NULL);
1021 for_each_namespaced_ref(check_ref, NULL);
aa9bab29 1022 }
a3d6b53e
BW
1023 string_list_clear(&symref, 1);
1024 if (options->advertise_refs)
1025 return;
aa9bab29 1026
a3d6b53e
BW
1027 receive_needs();
1028 if (want_obj.nr) {
1029 get_common_commits();
1030 create_pack_file();
1031 }
def88e9a 1032}