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