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