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