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