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