1#include "cache.h"
2#include "config.h"
3#include "refs.h"
4#include "pkt-line.h"
5#include "sideband.h"
6#include "repository.h"
7#include "object-store.h"
8#include "tag.h"
9#include "object.h"
10#include "commit.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 "argv-array.h"
22#include "prio-queue.h"
23#include "protocol.h"
24#include "quote.h"
25#include "upload-pack.h"
26#include "serve.h"
27#include "commit-graph.h"
28#include "commit-reach.h"
29
30/* Remember to update object flag allocation in object.h */
31#define THEY_HAVE (1u << 11)
32#define OUR_REF (1u << 12)
33#define WANTED (1u << 13)
34#define COMMON_KNOWN (1u << 14)
35
36#define SHALLOW (1u << 16)
37#define NOT_SHALLOW (1u << 17)
38#define CLIENT_SHALLOW (1u << 18)
39#define HIDDEN_REF (1u << 19)
40
41static timestamp_t oldest_have;
42
43static int deepen_relative;
44static int multi_ack;
45static int no_done;
46static int use_thin_pack, use_ofs_delta, use_include_tag;
47static int no_progress, daemon_mode;
48/* Allow specifying sha1 if it is a ref tip. */
49#define ALLOW_TIP_SHA1 01
50/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
51#define ALLOW_REACHABLE_SHA1 02
52/* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
53#define ALLOW_ANY_SHA1 07
54static unsigned int allow_unadvertised_object_request;
55static int shallow_nr;
56static struct object_array have_obj;
57static struct object_array want_obj;
58static struct object_array extra_edge_obj;
59static unsigned int timeout;
60static int keepalive = 5;
61/* 0 for no sideband,
62 * otherwise maximum packet size (up to 65520 bytes).
63 */
64static int use_sideband;
65static int stateless_rpc;
66static const char *pack_objects_hook;
67
68static int filter_capability_requested;
69static int allow_filter;
70static int allow_ref_in_want;
71static struct list_objects_filter_options filter_options;
72
73static void reset_timeout(void)
74{
75 alarm(timeout);
76}
77
78static void send_client_data(int fd, const char *data, ssize_t sz)
79{
80 if (use_sideband) {
81 send_sideband(1, fd, data, sz, use_sideband);
82 return;
83 }
84 if (fd == 3)
85 /* emergency quit */
86 fd = 2;
87 if (fd == 2) {
88 /* XXX: are we happy to lose stuff here? */
89 xwrite(fd, data, sz);
90 return;
91 }
92 write_or_die(fd, data, sz);
93}
94
95static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
96{
97 FILE *fp = cb_data;
98 if (graft->nr_parent == -1)
99 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
100 return 0;
101}
102
103static void create_pack_file(void)
104{
105 struct child_process pack_objects = CHILD_PROCESS_INIT;
106 char data[8193], progress[128];
107 char abort_msg[] = "aborting due to possible repository "
108 "corruption on the remote side.";
109 int buffered = -1;
110 ssize_t sz;
111 int i;
112 FILE *pipe_fd;
113
114 if (!pack_objects_hook)
115 pack_objects.git_cmd = 1;
116 else {
117 argv_array_push(&pack_objects.args, pack_objects_hook);
118 argv_array_push(&pack_objects.args, "git");
119 pack_objects.use_shell = 1;
120 }
121
122 if (shallow_nr) {
123 argv_array_push(&pack_objects.args, "--shallow-file");
124 argv_array_push(&pack_objects.args, "");
125 }
126 argv_array_push(&pack_objects.args, "pack-objects");
127 argv_array_push(&pack_objects.args, "--revs");
128 if (use_thin_pack)
129 argv_array_push(&pack_objects.args, "--thin");
130
131 argv_array_push(&pack_objects.args, "--stdout");
132 if (shallow_nr)
133 argv_array_push(&pack_objects.args, "--shallow");
134 if (!no_progress)
135 argv_array_push(&pack_objects.args, "--progress");
136 if (use_ofs_delta)
137 argv_array_push(&pack_objects.args, "--delta-base-offset");
138 if (use_include_tag)
139 argv_array_push(&pack_objects.args, "--include-tag");
140 if (filter_options.filter_spec) {
141 if (pack_objects.use_shell) {
142 struct strbuf buf = STRBUF_INIT;
143 sq_quote_buf(&buf, filter_options.filter_spec);
144 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
145 strbuf_release(&buf);
146 } else {
147 argv_array_pushf(&pack_objects.args, "--filter=%s",
148 filter_options.filter_spec);
149 }
150 }
151
152 pack_objects.in = -1;
153 pack_objects.out = -1;
154 pack_objects.err = -1;
155
156 if (start_command(&pack_objects))
157 die("git upload-pack: unable to fork git-pack-objects");
158
159 pipe_fd = xfdopen(pack_objects.in, "w");
160
161 if (shallow_nr)
162 for_each_commit_graft(write_one_shallow, pipe_fd);
163
164 for (i = 0; i < want_obj.nr; i++)
165 fprintf(pipe_fd, "%s\n",
166 oid_to_hex(&want_obj.objects[i].item->oid));
167 fprintf(pipe_fd, "--not\n");
168 for (i = 0; i < have_obj.nr; i++)
169 fprintf(pipe_fd, "%s\n",
170 oid_to_hex(&have_obj.objects[i].item->oid));
171 for (i = 0; i < extra_edge_obj.nr; i++)
172 fprintf(pipe_fd, "%s\n",
173 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
174 fprintf(pipe_fd, "\n");
175 fflush(pipe_fd);
176 fclose(pipe_fd);
177
178 /* We read from pack_objects.err to capture stderr output for
179 * progress bar, and pack_objects.out to capture the pack data.
180 */
181
182 while (1) {
183 struct pollfd pfd[2];
184 int pe, pu, pollsize;
185 int ret;
186
187 reset_timeout();
188
189 pollsize = 0;
190 pe = pu = -1;
191
192 if (0 <= pack_objects.out) {
193 pfd[pollsize].fd = pack_objects.out;
194 pfd[pollsize].events = POLLIN;
195 pu = pollsize;
196 pollsize++;
197 }
198 if (0 <= pack_objects.err) {
199 pfd[pollsize].fd = pack_objects.err;
200 pfd[pollsize].events = POLLIN;
201 pe = pollsize;
202 pollsize++;
203 }
204
205 if (!pollsize)
206 break;
207
208 ret = poll(pfd, pollsize,
209 keepalive < 0 ? -1 : 1000 * keepalive);
210
211 if (ret < 0) {
212 if (errno != EINTR) {
213 error_errno("poll failed, resuming");
214 sleep(1);
215 }
216 continue;
217 }
218 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
219 /* Status ready; we ship that in the side-band
220 * or dump to the standard error.
221 */
222 sz = xread(pack_objects.err, progress,
223 sizeof(progress));
224 if (0 < sz)
225 send_client_data(2, progress, sz);
226 else if (sz == 0) {
227 close(pack_objects.err);
228 pack_objects.err = -1;
229 }
230 else
231 goto fail;
232 /* give priority to status messages */
233 continue;
234 }
235 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
236 /* Data ready; we keep the last byte to ourselves
237 * in case we detect broken rev-list, so that we
238 * can leave the stream corrupted. This is
239 * unfortunate -- unpack-objects would happily
240 * accept a valid packdata with trailing garbage,
241 * so appending garbage after we pass all the
242 * pack data is not good enough to signal
243 * breakage to downstream.
244 */
245 char *cp = data;
246 ssize_t outsz = 0;
247 if (0 <= buffered) {
248 *cp++ = buffered;
249 outsz++;
250 }
251 sz = xread(pack_objects.out, cp,
252 sizeof(data) - outsz);
253 if (0 < sz)
254 ;
255 else if (sz == 0) {
256 close(pack_objects.out);
257 pack_objects.out = -1;
258 }
259 else
260 goto fail;
261 sz += outsz;
262 if (1 < sz) {
263 buffered = data[sz-1] & 0xFF;
264 sz--;
265 }
266 else
267 buffered = -1;
268 send_client_data(1, data, sz);
269 }
270
271 /*
272 * We hit the keepalive timeout without saying anything; send
273 * an empty message on the data sideband just to let the other
274 * side know we're still working on it, but don't have any data
275 * yet.
276 *
277 * If we don't have a sideband channel, there's no room in the
278 * protocol to say anything, so those clients are just out of
279 * luck.
280 */
281 if (!ret && use_sideband) {
282 static const char buf[] = "0005\1";
283 write_or_die(1, buf, 5);
284 }
285 }
286
287 if (finish_command(&pack_objects)) {
288 error("git upload-pack: git-pack-objects died with error.");
289 goto fail;
290 }
291
292 /* flush the data */
293 if (0 <= buffered) {
294 data[0] = buffered;
295 send_client_data(1, data, 1);
296 fprintf(stderr, "flushed.\n");
297 }
298 if (use_sideband)
299 packet_flush(1);
300 return;
301
302 fail:
303 send_client_data(3, abort_msg, sizeof(abort_msg));
304 die("git upload-pack: %s", abort_msg);
305}
306
307static int got_oid(const char *hex, struct object_id *oid)
308{
309 struct object *o;
310 int we_knew_they_have = 0;
311
312 if (get_oid_hex(hex, oid))
313 die("git upload-pack: expected SHA1 object, got '%s'", hex);
314 if (!has_object_file(oid))
315 return -1;
316
317 o = parse_object(the_repository, oid);
318 if (!o)
319 die("oops (%s)", oid_to_hex(oid));
320 if (o->type == OBJ_COMMIT) {
321 struct commit_list *parents;
322 struct commit *commit = (struct commit *)o;
323 if (o->flags & THEY_HAVE)
324 we_knew_they_have = 1;
325 else
326 o->flags |= THEY_HAVE;
327 if (!oldest_have || (commit->date < oldest_have))
328 oldest_have = commit->date;
329 for (parents = commit->parents;
330 parents;
331 parents = parents->next)
332 parents->item->object.flags |= THEY_HAVE;
333 }
334 if (!we_knew_they_have) {
335 add_object_array(o, NULL, &have_obj);
336 return 1;
337 }
338 return 0;
339}
340
341static int ok_to_give_up(void)
342{
343 uint32_t min_generation = GENERATION_NUMBER_ZERO;
344
345 if (!have_obj.nr)
346 return 0;
347
348 return can_all_from_reach_with_flag(&want_obj, THEY_HAVE,
349 COMMON_KNOWN, oldest_have,
350 min_generation);
351}
352
353static int get_common_commits(void)
354{
355 struct object_id oid;
356 char last_hex[GIT_MAX_HEXSZ + 1];
357 int got_common = 0;
358 int got_other = 0;
359 int sent_ready = 0;
360
361 save_commit_buffer = 0;
362
363 for (;;) {
364 char *line = packet_read_line(0, NULL);
365 const char *arg;
366
367 reset_timeout();
368
369 if (!line) {
370 if (multi_ack == 2 && got_common
371 && !got_other && ok_to_give_up()) {
372 sent_ready = 1;
373 packet_write_fmt(1, "ACK %s ready\n", last_hex);
374 }
375 if (have_obj.nr == 0 || multi_ack)
376 packet_write_fmt(1, "NAK\n");
377
378 if (no_done && sent_ready) {
379 packet_write_fmt(1, "ACK %s\n", last_hex);
380 return 0;
381 }
382 if (stateless_rpc)
383 exit(0);
384 got_common = 0;
385 got_other = 0;
386 continue;
387 }
388 if (skip_prefix(line, "have ", &arg)) {
389 switch (got_oid(arg, &oid)) {
390 case -1: /* they have what we do not */
391 got_other = 1;
392 if (multi_ack && ok_to_give_up()) {
393 const char *hex = oid_to_hex(&oid);
394 if (multi_ack == 2) {
395 sent_ready = 1;
396 packet_write_fmt(1, "ACK %s ready\n", hex);
397 } else
398 packet_write_fmt(1, "ACK %s continue\n", hex);
399 }
400 break;
401 default:
402 got_common = 1;
403 oid_to_hex_r(last_hex, &oid);
404 if (multi_ack == 2)
405 packet_write_fmt(1, "ACK %s common\n", last_hex);
406 else if (multi_ack)
407 packet_write_fmt(1, "ACK %s continue\n", last_hex);
408 else if (have_obj.nr == 1)
409 packet_write_fmt(1, "ACK %s\n", last_hex);
410 break;
411 }
412 continue;
413 }
414 if (!strcmp(line, "done")) {
415 if (have_obj.nr > 0) {
416 if (multi_ack)
417 packet_write_fmt(1, "ACK %s\n", last_hex);
418 return 0;
419 }
420 packet_write_fmt(1, "NAK\n");
421 return -1;
422 }
423 die("git upload-pack: expected SHA1 list, got '%s'", line);
424 }
425}
426
427static int is_our_ref(struct object *o)
428{
429 int allow_hidden_ref = (allow_unadvertised_object_request &
430 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
431 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
432}
433
434/*
435 * on successful case, it's up to the caller to close cmd->out
436 */
437static int do_reachable_revlist(struct child_process *cmd,
438 struct object_array *src,
439 struct object_array *reachable)
440{
441 static const char *argv[] = {
442 "rev-list", "--stdin", NULL,
443 };
444 struct object *o;
445 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
446 int i;
447 const unsigned hexsz = the_hash_algo->hexsz;
448
449 cmd->argv = argv;
450 cmd->git_cmd = 1;
451 cmd->no_stderr = 1;
452 cmd->in = -1;
453 cmd->out = -1;
454
455 /*
456 * If the next rev-list --stdin encounters an unknown commit,
457 * it terminates, which will cause SIGPIPE in the write loop
458 * below.
459 */
460 sigchain_push(SIGPIPE, SIG_IGN);
461
462 if (start_command(cmd))
463 goto error;
464
465 namebuf[0] = '^';
466 namebuf[hexsz + 1] = '\n';
467 for (i = get_max_object_index(); 0 < i; ) {
468 o = get_indexed_object(--i);
469 if (!o)
470 continue;
471 if (reachable && o->type == OBJ_COMMIT)
472 o->flags &= ~TMP_MARK;
473 if (!is_our_ref(o))
474 continue;
475 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
476 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
477 goto error;
478 }
479 namebuf[hexsz] = '\n';
480 for (i = 0; i < src->nr; i++) {
481 o = src->objects[i].item;
482 if (is_our_ref(o)) {
483 if (reachable)
484 add_object_array(o, NULL, reachable);
485 continue;
486 }
487 if (reachable && o->type == OBJ_COMMIT)
488 o->flags |= TMP_MARK;
489 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
490 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
491 goto error;
492 }
493 close(cmd->in);
494 cmd->in = -1;
495 sigchain_pop(SIGPIPE);
496
497 return 0;
498
499error:
500 sigchain_pop(SIGPIPE);
501
502 if (cmd->in >= 0)
503 close(cmd->in);
504 if (cmd->out >= 0)
505 close(cmd->out);
506 return -1;
507}
508
509static int get_reachable_list(struct object_array *src,
510 struct object_array *reachable)
511{
512 struct child_process cmd = CHILD_PROCESS_INIT;
513 int i;
514 struct object *o;
515 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
516 const unsigned hexsz = the_hash_algo->hexsz;
517
518 if (do_reachable_revlist(&cmd, src, reachable) < 0)
519 return -1;
520
521 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
522 struct object_id sha1;
523 const char *p;
524
525 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
526 break;
527
528 o = lookup_object(the_repository, sha1.hash);
529 if (o && o->type == OBJ_COMMIT) {
530 o->flags &= ~TMP_MARK;
531 }
532 }
533 for (i = get_max_object_index(); 0 < i; i--) {
534 o = get_indexed_object(i - 1);
535 if (o && o->type == OBJ_COMMIT &&
536 (o->flags & TMP_MARK)) {
537 add_object_array(o, NULL, reachable);
538 o->flags &= ~TMP_MARK;
539 }
540 }
541 close(cmd.out);
542
543 if (finish_command(&cmd))
544 return -1;
545
546 return 0;
547}
548
549static int has_unreachable(struct object_array *src)
550{
551 struct child_process cmd = CHILD_PROCESS_INIT;
552 char buf[1];
553 int i;
554
555 if (do_reachable_revlist(&cmd, src, NULL) < 0)
556 return 1;
557
558 /*
559 * The commits out of the rev-list are not ancestors of
560 * our ref.
561 */
562 i = read_in_full(cmd.out, buf, 1);
563 if (i)
564 goto error;
565 close(cmd.out);
566 cmd.out = -1;
567
568 /*
569 * rev-list may have died by encountering a bad commit
570 * in the history, in which case we do want to bail out
571 * even when it showed no commit.
572 */
573 if (finish_command(&cmd))
574 goto error;
575
576 /* All the non-tip ones are ancestors of what we advertised */
577 return 0;
578
579error:
580 sigchain_pop(SIGPIPE);
581 if (cmd.out >= 0)
582 close(cmd.out);
583 return 1;
584}
585
586static void check_non_tip(void)
587{
588 int i;
589
590 /*
591 * In the normal in-process case without
592 * uploadpack.allowReachableSHA1InWant,
593 * non-tip requests can never happen.
594 */
595 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
596 goto error;
597 if (!has_unreachable(&want_obj))
598 /* All the non-tip ones are ancestors of what we advertised */
599 return;
600
601error:
602 /* Pick one of them (we know there at least is one) */
603 for (i = 0; i < want_obj.nr; i++) {
604 struct object *o = want_obj.objects[i].item;
605 if (!is_our_ref(o))
606 die("git upload-pack: not our ref %s",
607 oid_to_hex(&o->oid));
608 }
609}
610
611static void send_shallow(struct commit_list *result)
612{
613 while (result) {
614 struct object *object = &result->item->object;
615 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
616 packet_write_fmt(1, "shallow %s",
617 oid_to_hex(&object->oid));
618 register_shallow(the_repository, &object->oid);
619 shallow_nr++;
620 }
621 result = result->next;
622 }
623}
624
625static void send_unshallow(const struct object_array *shallows)
626{
627 int i;
628
629 for (i = 0; i < shallows->nr; i++) {
630 struct object *object = shallows->objects[i].item;
631 if (object->flags & NOT_SHALLOW) {
632 struct commit_list *parents;
633 packet_write_fmt(1, "unshallow %s",
634 oid_to_hex(&object->oid));
635 object->flags &= ~CLIENT_SHALLOW;
636 /*
637 * We want to _register_ "object" as shallow, but we
638 * also need to traverse object's parents to deepen a
639 * shallow clone. Unregister it for now so we can
640 * parse and add the parents to the want list, then
641 * re-register it.
642 */
643 unregister_shallow(&object->oid);
644 object->parsed = 0;
645 parse_commit_or_die((struct commit *)object);
646 parents = ((struct commit *)object)->parents;
647 while (parents) {
648 add_object_array(&parents->item->object,
649 NULL, &want_obj);
650 parents = parents->next;
651 }
652 add_object_array(object, NULL, &extra_edge_obj);
653 }
654 /* make sure commit traversal conforms to client */
655 register_shallow(the_repository, &object->oid);
656 }
657}
658
659static void deepen(int depth, int deepen_relative,
660 struct object_array *shallows)
661{
662 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
663 int i;
664
665 for (i = 0; i < shallows->nr; i++) {
666 struct object *object = shallows->objects[i].item;
667 object->flags |= NOT_SHALLOW;
668 }
669 } else if (deepen_relative) {
670 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
671 struct commit_list *result;
672
673 get_reachable_list(shallows, &reachable_shallows);
674 result = get_shallow_commits(&reachable_shallows,
675 depth + 1,
676 SHALLOW, NOT_SHALLOW);
677 send_shallow(result);
678 free_commit_list(result);
679 object_array_clear(&reachable_shallows);
680 } else {
681 struct commit_list *result;
682
683 result = get_shallow_commits(&want_obj, depth,
684 SHALLOW, NOT_SHALLOW);
685 send_shallow(result);
686 free_commit_list(result);
687 }
688
689 send_unshallow(shallows);
690}
691
692static void deepen_by_rev_list(int ac, const char **av,
693 struct object_array *shallows)
694{
695 struct commit_list *result;
696
697 close_commit_graph(the_repository);
698 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
699 send_shallow(result);
700 free_commit_list(result);
701 send_unshallow(shallows);
702}
703
704/* Returns 1 if a shallow list is sent or 0 otherwise */
705static int send_shallow_list(int depth, int deepen_rev_list,
706 timestamp_t deepen_since,
707 struct string_list *deepen_not,
708 struct object_array *shallows)
709{
710 int ret = 0;
711
712 if (depth > 0 && deepen_rev_list)
713 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
714 if (depth > 0) {
715 deepen(depth, deepen_relative, shallows);
716 ret = 1;
717 } else if (deepen_rev_list) {
718 struct argv_array av = ARGV_ARRAY_INIT;
719 int i;
720
721 argv_array_push(&av, "rev-list");
722 if (deepen_since)
723 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
724 if (deepen_not->nr) {
725 argv_array_push(&av, "--not");
726 for (i = 0; i < deepen_not->nr; i++) {
727 struct string_list_item *s = deepen_not->items + i;
728 argv_array_push(&av, s->string);
729 }
730 argv_array_push(&av, "--not");
731 }
732 for (i = 0; i < want_obj.nr; i++) {
733 struct object *o = want_obj.objects[i].item;
734 argv_array_push(&av, oid_to_hex(&o->oid));
735 }
736 deepen_by_rev_list(av.argc, av.argv, shallows);
737 argv_array_clear(&av);
738 ret = 1;
739 } else {
740 if (shallows->nr > 0) {
741 int i;
742 for (i = 0; i < shallows->nr; i++)
743 register_shallow(the_repository,
744 &shallows->objects[i].item->oid);
745 }
746 }
747
748 shallow_nr += shallows->nr;
749 return ret;
750}
751
752static int process_shallow(const char *line, struct object_array *shallows)
753{
754 const char *arg;
755 if (skip_prefix(line, "shallow ", &arg)) {
756 struct object_id oid;
757 struct object *object;
758 if (get_oid_hex(arg, &oid))
759 die("invalid shallow line: %s", line);
760 object = parse_object(the_repository, &oid);
761 if (!object)
762 return 1;
763 if (object->type != OBJ_COMMIT)
764 die("invalid shallow object %s", oid_to_hex(&oid));
765 if (!(object->flags & CLIENT_SHALLOW)) {
766 object->flags |= CLIENT_SHALLOW;
767 add_object_array(object, NULL, shallows);
768 }
769 return 1;
770 }
771
772 return 0;
773}
774
775static int process_deepen(const char *line, int *depth)
776{
777 const char *arg;
778 if (skip_prefix(line, "deepen ", &arg)) {
779 char *end = NULL;
780 *depth = (int)strtol(arg, &end, 0);
781 if (!end || *end || *depth <= 0)
782 die("Invalid deepen: %s", line);
783 return 1;
784 }
785
786 return 0;
787}
788
789static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
790{
791 const char *arg;
792 if (skip_prefix(line, "deepen-since ", &arg)) {
793 char *end = NULL;
794 *deepen_since = parse_timestamp(arg, &end, 0);
795 if (!end || *end || !deepen_since ||
796 /* revisions.c's max_age -1 is special */
797 *deepen_since == -1)
798 die("Invalid deepen-since: %s", line);
799 *deepen_rev_list = 1;
800 return 1;
801 }
802 return 0;
803}
804
805static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
806{
807 const char *arg;
808 if (skip_prefix(line, "deepen-not ", &arg)) {
809 char *ref = NULL;
810 struct object_id oid;
811 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
812 die("git upload-pack: ambiguous deepen-not: %s", line);
813 string_list_append(deepen_not, ref);
814 free(ref);
815 *deepen_rev_list = 1;
816 return 1;
817 }
818 return 0;
819}
820
821static void receive_needs(void)
822{
823 struct object_array shallows = OBJECT_ARRAY_INIT;
824 struct string_list deepen_not = STRING_LIST_INIT_DUP;
825 int depth = 0;
826 int has_non_tip = 0;
827 timestamp_t deepen_since = 0;
828 int deepen_rev_list = 0;
829
830 shallow_nr = 0;
831 for (;;) {
832 struct object *o;
833 const char *features;
834 struct object_id oid_buf;
835 char *line = packet_read_line(0, NULL);
836 const char *arg;
837
838 reset_timeout();
839 if (!line)
840 break;
841
842 if (process_shallow(line, &shallows))
843 continue;
844 if (process_deepen(line, &depth))
845 continue;
846 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
847 continue;
848 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
849 continue;
850
851 if (skip_prefix(line, "filter ", &arg)) {
852 if (!filter_capability_requested)
853 die("git upload-pack: filtering capability not negotiated");
854 parse_list_objects_filter(&filter_options, arg);
855 continue;
856 }
857
858 if (!skip_prefix(line, "want ", &arg) ||
859 parse_oid_hex(arg, &oid_buf, &features))
860 die("git upload-pack: protocol error, "
861 "expected to get object ID, not '%s'", line);
862
863 if (parse_feature_request(features, "deepen-relative"))
864 deepen_relative = 1;
865 if (parse_feature_request(features, "multi_ack_detailed"))
866 multi_ack = 2;
867 else if (parse_feature_request(features, "multi_ack"))
868 multi_ack = 1;
869 if (parse_feature_request(features, "no-done"))
870 no_done = 1;
871 if (parse_feature_request(features, "thin-pack"))
872 use_thin_pack = 1;
873 if (parse_feature_request(features, "ofs-delta"))
874 use_ofs_delta = 1;
875 if (parse_feature_request(features, "side-band-64k"))
876 use_sideband = LARGE_PACKET_MAX;
877 else if (parse_feature_request(features, "side-band"))
878 use_sideband = DEFAULT_PACKET_MAX;
879 if (parse_feature_request(features, "no-progress"))
880 no_progress = 1;
881 if (parse_feature_request(features, "include-tag"))
882 use_include_tag = 1;
883 if (allow_filter && parse_feature_request(features, "filter"))
884 filter_capability_requested = 1;
885
886 o = parse_object(the_repository, &oid_buf);
887 if (!o) {
888 packet_write_fmt(1,
889 "ERR upload-pack: not our ref %s",
890 oid_to_hex(&oid_buf));
891 die("git upload-pack: not our ref %s",
892 oid_to_hex(&oid_buf));
893 }
894 if (!(o->flags & WANTED)) {
895 o->flags |= WANTED;
896 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
897 || is_our_ref(o)))
898 has_non_tip = 1;
899 add_object_array(o, NULL, &want_obj);
900 }
901 }
902
903 /*
904 * We have sent all our refs already, and the other end
905 * should have chosen out of them. When we are operating
906 * in the stateless RPC mode, however, their choice may
907 * have been based on the set of older refs advertised
908 * by another process that handled the initial request.
909 */
910 if (has_non_tip)
911 check_non_tip();
912
913 if (!use_sideband && daemon_mode)
914 no_progress = 1;
915
916 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
917 return;
918
919 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
920 &deepen_not, &shallows))
921 packet_flush(1);
922 object_array_clear(&shallows);
923}
924
925/* return non-zero if the ref is hidden, otherwise 0 */
926static int mark_our_ref(const char *refname, const char *refname_full,
927 const struct object_id *oid)
928{
929 struct object *o = lookup_unknown_object(oid->hash);
930
931 if (ref_is_hidden(refname, refname_full)) {
932 o->flags |= HIDDEN_REF;
933 return 1;
934 }
935 o->flags |= OUR_REF;
936 return 0;
937}
938
939static int check_ref(const char *refname_full, const struct object_id *oid,
940 int flag, void *cb_data)
941{
942 const char *refname = strip_namespace(refname_full);
943
944 mark_our_ref(refname, refname_full, oid);
945 return 0;
946}
947
948static void format_symref_info(struct strbuf *buf, struct string_list *symref)
949{
950 struct string_list_item *item;
951
952 if (!symref->nr)
953 return;
954 for_each_string_list_item(item, symref)
955 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
956}
957
958static int send_ref(const char *refname, const struct object_id *oid,
959 int flag, void *cb_data)
960{
961 static const char *capabilities = "multi_ack thin-pack side-band"
962 " side-band-64k ofs-delta shallow deepen-since deepen-not"
963 " deepen-relative no-progress include-tag multi_ack_detailed";
964 const char *refname_nons = strip_namespace(refname);
965 struct object_id peeled;
966
967 if (mark_our_ref(refname_nons, refname, oid))
968 return 0;
969
970 if (capabilities) {
971 struct strbuf symref_info = STRBUF_INIT;
972
973 format_symref_info(&symref_info, cb_data);
974 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
975 oid_to_hex(oid), refname_nons,
976 0, capabilities,
977 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
978 " allow-tip-sha1-in-want" : "",
979 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
980 " allow-reachable-sha1-in-want" : "",
981 stateless_rpc ? " no-done" : "",
982 symref_info.buf,
983 allow_filter ? " filter" : "",
984 git_user_agent_sanitized());
985 strbuf_release(&symref_info);
986 } else {
987 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
988 }
989 capabilities = NULL;
990 if (!peel_ref(refname, &peeled))
991 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
992 return 0;
993}
994
995static int find_symref(const char *refname, const struct object_id *oid,
996 int flag, void *cb_data)
997{
998 const char *symref_target;
999 struct string_list_item *item;
1000
1001 if ((flag & REF_ISSYMREF) == 0)
1002 return 0;
1003 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1004 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1005 die("'%s' is a symref but it is not?", refname);
1006 item = string_list_append(cb_data, refname);
1007 item->util = xstrdup(symref_target);
1008 return 0;
1009}
1010
1011static int upload_pack_config(const char *var, const char *value, void *unused)
1012{
1013 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1014 if (git_config_bool(var, value))
1015 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1016 else
1017 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1018 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1019 if (git_config_bool(var, value))
1020 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1021 else
1022 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1023 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1024 if (git_config_bool(var, value))
1025 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1026 else
1027 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1028 } else if (!strcmp("uploadpack.keepalive", var)) {
1029 keepalive = git_config_int(var, value);
1030 if (!keepalive)
1031 keepalive = -1;
1032 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1033 if (!strcmp("uploadpack.packobjectshook", var))
1034 return git_config_string(&pack_objects_hook, var, value);
1035 } else if (!strcmp("uploadpack.allowfilter", var)) {
1036 allow_filter = git_config_bool(var, value);
1037 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1038 allow_ref_in_want = git_config_bool(var, value);
1039 }
1040 return parse_hide_refs_config(var, value, "uploadpack");
1041}
1042
1043void upload_pack(struct upload_pack_options *options)
1044{
1045 struct string_list symref = STRING_LIST_INIT_DUP;
1046
1047 stateless_rpc = options->stateless_rpc;
1048 timeout = options->timeout;
1049 daemon_mode = options->daemon_mode;
1050
1051 git_config(upload_pack_config, NULL);
1052
1053 head_ref_namespaced(find_symref, &symref);
1054
1055 if (options->advertise_refs || !stateless_rpc) {
1056 reset_timeout();
1057 head_ref_namespaced(send_ref, &symref);
1058 for_each_namespaced_ref(send_ref, &symref);
1059 advertise_shallow_grafts(1);
1060 packet_flush(1);
1061 } else {
1062 head_ref_namespaced(check_ref, NULL);
1063 for_each_namespaced_ref(check_ref, NULL);
1064 }
1065 string_list_clear(&symref, 1);
1066 if (options->advertise_refs)
1067 return;
1068
1069 receive_needs();
1070 if (want_obj.nr) {
1071 get_common_commits();
1072 create_pack_file();
1073 }
1074}
1075
1076struct upload_pack_data {
1077 struct object_array wants;
1078 struct string_list wanted_refs;
1079 struct oid_array haves;
1080
1081 struct object_array shallows;
1082 struct string_list deepen_not;
1083 int depth;
1084 timestamp_t deepen_since;
1085 int deepen_rev_list;
1086 int deepen_relative;
1087
1088 unsigned stateless_rpc : 1;
1089
1090 unsigned use_thin_pack : 1;
1091 unsigned use_ofs_delta : 1;
1092 unsigned no_progress : 1;
1093 unsigned use_include_tag : 1;
1094 unsigned done : 1;
1095};
1096
1097static void upload_pack_data_init(struct upload_pack_data *data)
1098{
1099 struct object_array wants = OBJECT_ARRAY_INIT;
1100 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1101 struct oid_array haves = OID_ARRAY_INIT;
1102 struct object_array shallows = OBJECT_ARRAY_INIT;
1103 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1104
1105 memset(data, 0, sizeof(*data));
1106 data->wants = wants;
1107 data->wanted_refs = wanted_refs;
1108 data->haves = haves;
1109 data->shallows = shallows;
1110 data->deepen_not = deepen_not;
1111}
1112
1113static void upload_pack_data_clear(struct upload_pack_data *data)
1114{
1115 object_array_clear(&data->wants);
1116 string_list_clear(&data->wanted_refs, 1);
1117 oid_array_clear(&data->haves);
1118 object_array_clear(&data->shallows);
1119 string_list_clear(&data->deepen_not, 0);
1120}
1121
1122static int parse_want(const char *line)
1123{
1124 const char *arg;
1125 if (skip_prefix(line, "want ", &arg)) {
1126 struct object_id oid;
1127 struct object *o;
1128
1129 if (get_oid_hex(arg, &oid))
1130 die("git upload-pack: protocol error, "
1131 "expected to get oid, not '%s'", line);
1132
1133 o = parse_object(the_repository, &oid);
1134 if (!o) {
1135 packet_write_fmt(1,
1136 "ERR upload-pack: not our ref %s",
1137 oid_to_hex(&oid));
1138 die("git upload-pack: not our ref %s",
1139 oid_to_hex(&oid));
1140 }
1141
1142 if (!(o->flags & WANTED)) {
1143 o->flags |= WANTED;
1144 add_object_array(o, NULL, &want_obj);
1145 }
1146
1147 return 1;
1148 }
1149
1150 return 0;
1151}
1152
1153static int parse_want_ref(const char *line, struct string_list *wanted_refs)
1154{
1155 const char *arg;
1156 if (skip_prefix(line, "want-ref ", &arg)) {
1157 struct object_id oid;
1158 struct string_list_item *item;
1159 struct object *o;
1160
1161 if (read_ref(arg, &oid)) {
1162 packet_write_fmt(1, "ERR unknown ref %s", arg);
1163 die("unknown ref %s", arg);
1164 }
1165
1166 item = string_list_append(wanted_refs, arg);
1167 item->util = oiddup(&oid);
1168
1169 o = parse_object_or_die(&oid, arg);
1170 if (!(o->flags & WANTED)) {
1171 o->flags |= WANTED;
1172 add_object_array(o, NULL, &want_obj);
1173 }
1174
1175 return 1;
1176 }
1177
1178 return 0;
1179}
1180
1181static int parse_have(const char *line, struct oid_array *haves)
1182{
1183 const char *arg;
1184 if (skip_prefix(line, "have ", &arg)) {
1185 struct object_id oid;
1186
1187 if (get_oid_hex(arg, &oid))
1188 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1189 oid_array_append(haves, &oid);
1190 return 1;
1191 }
1192
1193 return 0;
1194}
1195
1196static void process_args(struct packet_reader *request,
1197 struct upload_pack_data *data)
1198{
1199 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1200 const char *arg = request->line;
1201 const char *p;
1202
1203 /* process want */
1204 if (parse_want(arg))
1205 continue;
1206 if (allow_ref_in_want && parse_want_ref(arg, &data->wanted_refs))
1207 continue;
1208 /* process have line */
1209 if (parse_have(arg, &data->haves))
1210 continue;
1211
1212 /* process args like thin-pack */
1213 if (!strcmp(arg, "thin-pack")) {
1214 use_thin_pack = 1;
1215 continue;
1216 }
1217 if (!strcmp(arg, "ofs-delta")) {
1218 use_ofs_delta = 1;
1219 continue;
1220 }
1221 if (!strcmp(arg, "no-progress")) {
1222 no_progress = 1;
1223 continue;
1224 }
1225 if (!strcmp(arg, "include-tag")) {
1226 use_include_tag = 1;
1227 continue;
1228 }
1229 if (!strcmp(arg, "done")) {
1230 data->done = 1;
1231 continue;
1232 }
1233
1234 /* Shallow related arguments */
1235 if (process_shallow(arg, &data->shallows))
1236 continue;
1237 if (process_deepen(arg, &data->depth))
1238 continue;
1239 if (process_deepen_since(arg, &data->deepen_since,
1240 &data->deepen_rev_list))
1241 continue;
1242 if (process_deepen_not(arg, &data->deepen_not,
1243 &data->deepen_rev_list))
1244 continue;
1245 if (!strcmp(arg, "deepen-relative")) {
1246 data->deepen_relative = 1;
1247 continue;
1248 }
1249
1250 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1251 parse_list_objects_filter(&filter_options, p);
1252 continue;
1253 }
1254
1255 /* ignore unknown lines maybe? */
1256 die("unexpected line: '%s'", arg);
1257 }
1258}
1259
1260static int process_haves(struct oid_array *haves, struct oid_array *common)
1261{
1262 int i;
1263
1264 /* Process haves */
1265 for (i = 0; i < haves->nr; i++) {
1266 const struct object_id *oid = &haves->oid[i];
1267 struct object *o;
1268 int we_knew_they_have = 0;
1269
1270 if (!has_object_file(oid))
1271 continue;
1272
1273 oid_array_append(common, oid);
1274
1275 o = parse_object(the_repository, oid);
1276 if (!o)
1277 die("oops (%s)", oid_to_hex(oid));
1278 if (o->type == OBJ_COMMIT) {
1279 struct commit_list *parents;
1280 struct commit *commit = (struct commit *)o;
1281 if (o->flags & THEY_HAVE)
1282 we_knew_they_have = 1;
1283 else
1284 o->flags |= THEY_HAVE;
1285 if (!oldest_have || (commit->date < oldest_have))
1286 oldest_have = commit->date;
1287 for (parents = commit->parents;
1288 parents;
1289 parents = parents->next)
1290 parents->item->object.flags |= THEY_HAVE;
1291 }
1292 if (!we_knew_they_have)
1293 add_object_array(o, NULL, &have_obj);
1294 }
1295
1296 return 0;
1297}
1298
1299static int send_acks(struct oid_array *acks, struct strbuf *response)
1300{
1301 int i;
1302
1303 packet_buf_write(response, "acknowledgments\n");
1304
1305 /* Send Acks */
1306 if (!acks->nr)
1307 packet_buf_write(response, "NAK\n");
1308
1309 for (i = 0; i < acks->nr; i++) {
1310 packet_buf_write(response, "ACK %s\n",
1311 oid_to_hex(&acks->oid[i]));
1312 }
1313
1314 if (ok_to_give_up()) {
1315 /* Send Ready */
1316 packet_buf_write(response, "ready\n");
1317 return 1;
1318 }
1319
1320 return 0;
1321}
1322
1323static int process_haves_and_send_acks(struct upload_pack_data *data)
1324{
1325 struct oid_array common = OID_ARRAY_INIT;
1326 struct strbuf response = STRBUF_INIT;
1327 int ret = 0;
1328
1329 process_haves(&data->haves, &common);
1330 if (data->done) {
1331 ret = 1;
1332 } else if (send_acks(&common, &response)) {
1333 packet_buf_delim(&response);
1334 ret = 1;
1335 } else {
1336 /* Add Flush */
1337 packet_buf_flush(&response);
1338 ret = 0;
1339 }
1340
1341 /* Send response */
1342 write_or_die(1, response.buf, response.len);
1343 strbuf_release(&response);
1344
1345 oid_array_clear(&data->haves);
1346 oid_array_clear(&common);
1347 return ret;
1348}
1349
1350static void send_wanted_ref_info(struct upload_pack_data *data)
1351{
1352 const struct string_list_item *item;
1353
1354 if (!data->wanted_refs.nr)
1355 return;
1356
1357 packet_write_fmt(1, "wanted-refs\n");
1358
1359 for_each_string_list_item(item, &data->wanted_refs) {
1360 packet_write_fmt(1, "%s %s\n",
1361 oid_to_hex(item->util),
1362 item->string);
1363 }
1364
1365 packet_delim(1);
1366}
1367
1368static void send_shallow_info(struct upload_pack_data *data)
1369{
1370 /* No shallow info needs to be sent */
1371 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1372 !is_repository_shallow(the_repository))
1373 return;
1374
1375 packet_write_fmt(1, "shallow-info\n");
1376
1377 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1378 data->deepen_since, &data->deepen_not,
1379 &data->shallows) &&
1380 is_repository_shallow(the_repository))
1381 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1382
1383 packet_delim(1);
1384}
1385
1386enum fetch_state {
1387 FETCH_PROCESS_ARGS = 0,
1388 FETCH_SEND_ACKS,
1389 FETCH_SEND_PACK,
1390 FETCH_DONE,
1391};
1392
1393int upload_pack_v2(struct repository *r, struct argv_array *keys,
1394 struct packet_reader *request)
1395{
1396 enum fetch_state state = FETCH_PROCESS_ARGS;
1397 struct upload_pack_data data;
1398
1399 git_config(upload_pack_config, NULL);
1400
1401 upload_pack_data_init(&data);
1402 use_sideband = LARGE_PACKET_MAX;
1403
1404 while (state != FETCH_DONE) {
1405 switch (state) {
1406 case FETCH_PROCESS_ARGS:
1407 process_args(request, &data);
1408
1409 if (!want_obj.nr) {
1410 /*
1411 * Request didn't contain any 'want' lines,
1412 * guess they didn't want anything.
1413 */
1414 state = FETCH_DONE;
1415 } else if (data.haves.nr) {
1416 /*
1417 * Request had 'have' lines, so lets ACK them.
1418 */
1419 state = FETCH_SEND_ACKS;
1420 } else {
1421 /*
1422 * Request had 'want's but no 'have's so we can
1423 * immedietly go to construct and send a pack.
1424 */
1425 state = FETCH_SEND_PACK;
1426 }
1427 break;
1428 case FETCH_SEND_ACKS:
1429 if (process_haves_and_send_acks(&data))
1430 state = FETCH_SEND_PACK;
1431 else
1432 state = FETCH_DONE;
1433 break;
1434 case FETCH_SEND_PACK:
1435 send_wanted_ref_info(&data);
1436 send_shallow_info(&data);
1437
1438 packet_write_fmt(1, "packfile\n");
1439 create_pack_file();
1440 state = FETCH_DONE;
1441 break;
1442 case FETCH_DONE:
1443 continue;
1444 }
1445 }
1446
1447 upload_pack_data_clear(&data);
1448 return 0;
1449}
1450
1451int upload_pack_advertise(struct repository *r,
1452 struct strbuf *value)
1453{
1454 if (value) {
1455 int allow_filter_value;
1456 int allow_ref_in_want;
1457
1458 strbuf_addstr(value, "shallow");
1459
1460 if (!repo_config_get_bool(the_repository,
1461 "uploadpack.allowfilter",
1462 &allow_filter_value) &&
1463 allow_filter_value)
1464 strbuf_addstr(value, " filter");
1465
1466 if (!repo_config_get_bool(the_repository,
1467 "uploadpack.allowrefinwant",
1468 &allow_ref_in_want) &&
1469 allow_ref_in_want)
1470 strbuf_addstr(value, " ref-in-want");
1471 }
1472
1473 return 1;
1474}