submodule status: remove unused orig_* variables
[git/mjg.git] / builtin / fetch-pack.c
blobfdda36f1497eaa971e81cc1df0fb21ada6d21120
1 #include "builtin.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "commit.h"
5 #include "tag.h"
6 #include "exec_cmd.h"
7 #include "pack.h"
8 #include "sideband.h"
9 #include "fetch-pack.h"
10 #include "remote.h"
11 #include "run-command.h"
12 #include "transport.h"
13 #include "version.h"
15 static int transfer_unpack_limit = -1;
16 static int fetch_unpack_limit = -1;
17 static int unpack_limit = 100;
18 static int prefer_ofs_delta = 1;
19 static int no_done;
20 static int fetch_fsck_objects = -1;
21 static int transfer_fsck_objects = -1;
22 static int agent_supported;
23 static struct fetch_pack_args args = {
24 /* .uploadpack = */ "git-upload-pack",
27 static const char fetch_pack_usage[] =
28 "git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
29 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
30 "[--no-progress] [-v] [<host>:]<directory> [<refs>...]";
32 #define COMPLETE (1U << 0)
33 #define COMMON (1U << 1)
34 #define COMMON_REF (1U << 2)
35 #define SEEN (1U << 3)
36 #define POPPED (1U << 4)
38 static int marked;
41 * After sending this many "have"s if we do not get any new ACK , we
42 * give up traversing our history.
44 #define MAX_IN_VAIN 256
46 static struct commit_list *rev_list;
47 static int non_common_revs, multi_ack, use_sideband;
49 static void rev_list_push(struct commit *commit, int mark)
51 if (!(commit->object.flags & mark)) {
52 commit->object.flags |= mark;
54 if (!(commit->object.parsed))
55 if (parse_commit(commit))
56 return;
58 commit_list_insert_by_date(commit, &rev_list);
60 if (!(commit->object.flags & COMMON))
61 non_common_revs++;
65 static int rev_list_insert_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
67 struct object *o = deref_tag(parse_object(sha1), refname, 0);
69 if (o && o->type == OBJ_COMMIT)
70 rev_list_push((struct commit *)o, SEEN);
72 return 0;
75 static int clear_marks(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
77 struct object *o = deref_tag(parse_object(sha1), refname, 0);
79 if (o && o->type == OBJ_COMMIT)
80 clear_commit_marks((struct commit *)o,
81 COMMON | COMMON_REF | SEEN | POPPED);
82 return 0;
86 This function marks a rev and its ancestors as common.
87 In some cases, it is desirable to mark only the ancestors (for example
88 when only the server does not yet know that they are common).
91 static void mark_common(struct commit *commit,
92 int ancestors_only, int dont_parse)
94 if (commit != NULL && !(commit->object.flags & COMMON)) {
95 struct object *o = (struct object *)commit;
97 if (!ancestors_only)
98 o->flags |= COMMON;
100 if (!(o->flags & SEEN))
101 rev_list_push(commit, SEEN);
102 else {
103 struct commit_list *parents;
105 if (!ancestors_only && !(o->flags & POPPED))
106 non_common_revs--;
107 if (!o->parsed && !dont_parse)
108 if (parse_commit(commit))
109 return;
111 for (parents = commit->parents;
112 parents;
113 parents = parents->next)
114 mark_common(parents->item, 0, dont_parse);
120 Get the next rev to send, ignoring the common.
123 static const unsigned char *get_rev(void)
125 struct commit *commit = NULL;
127 while (commit == NULL) {
128 unsigned int mark;
129 struct commit_list *parents;
131 if (rev_list == NULL || non_common_revs == 0)
132 return NULL;
134 commit = rev_list->item;
135 if (!commit->object.parsed)
136 parse_commit(commit);
137 parents = commit->parents;
139 commit->object.flags |= POPPED;
140 if (!(commit->object.flags & COMMON))
141 non_common_revs--;
143 if (commit->object.flags & COMMON) {
144 /* do not send "have", and ignore ancestors */
145 commit = NULL;
146 mark = COMMON | SEEN;
147 } else if (commit->object.flags & COMMON_REF)
148 /* send "have", and ignore ancestors */
149 mark = COMMON | SEEN;
150 else
151 /* send "have", also for its ancestors */
152 mark = SEEN;
154 while (parents) {
155 if (!(parents->item->object.flags & SEEN))
156 rev_list_push(parents->item, mark);
157 if (mark & COMMON)
158 mark_common(parents->item, 1, 0);
159 parents = parents->next;
162 rev_list = rev_list->next;
165 return commit->object.sha1;
168 enum ack_type {
169 NAK = 0,
170 ACK,
171 ACK_continue,
172 ACK_common,
173 ACK_ready
176 static void consume_shallow_list(int fd)
178 if (args.stateless_rpc && args.depth > 0) {
179 /* If we sent a depth we will get back "duplicate"
180 * shallow and unshallow commands every time there
181 * is a block of have lines exchanged.
183 char line[1000];
184 while (packet_read_line(fd, line, sizeof(line))) {
185 if (!prefixcmp(line, "shallow "))
186 continue;
187 if (!prefixcmp(line, "unshallow "))
188 continue;
189 die("git fetch-pack: expected shallow list");
194 struct write_shallow_data {
195 struct strbuf *out;
196 int use_pack_protocol;
197 int count;
200 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
202 struct write_shallow_data *data = cb_data;
203 const char *hex = sha1_to_hex(graft->sha1);
204 data->count++;
205 if (data->use_pack_protocol)
206 packet_buf_write(data->out, "shallow %s", hex);
207 else {
208 strbuf_addstr(data->out, hex);
209 strbuf_addch(data->out, '\n');
211 return 0;
214 static int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
216 struct write_shallow_data data;
217 data.out = out;
218 data.use_pack_protocol = use_pack_protocol;
219 data.count = 0;
220 for_each_commit_graft(write_one_shallow, &data);
221 return data.count;
224 static enum ack_type get_ack(int fd, unsigned char *result_sha1)
226 static char line[1000];
227 int len = packet_read_line(fd, line, sizeof(line));
229 if (!len)
230 die("git fetch-pack: expected ACK/NAK, got EOF");
231 if (line[len-1] == '\n')
232 line[--len] = 0;
233 if (!strcmp(line, "NAK"))
234 return NAK;
235 if (!prefixcmp(line, "ACK ")) {
236 if (!get_sha1_hex(line+4, result_sha1)) {
237 if (strstr(line+45, "continue"))
238 return ACK_continue;
239 if (strstr(line+45, "common"))
240 return ACK_common;
241 if (strstr(line+45, "ready"))
242 return ACK_ready;
243 return ACK;
246 die("git fetch_pack: expected ACK/NAK, got '%s'", line);
249 static void send_request(int fd, struct strbuf *buf)
251 if (args.stateless_rpc) {
252 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
253 packet_flush(fd);
254 } else
255 safe_write(fd, buf->buf, buf->len);
258 static void insert_one_alternate_ref(const struct ref *ref, void *unused)
260 rev_list_insert_ref(NULL, ref->old_sha1, 0, NULL);
263 #define INITIAL_FLUSH 16
264 #define PIPESAFE_FLUSH 32
265 #define LARGE_FLUSH 1024
267 static int next_flush(int count)
269 int flush_limit = args.stateless_rpc ? LARGE_FLUSH : PIPESAFE_FLUSH;
271 if (count < flush_limit)
272 count <<= 1;
273 else
274 count += flush_limit;
275 return count;
278 static int find_common(int fd[2], unsigned char *result_sha1,
279 struct ref *refs)
281 int fetching;
282 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
283 const unsigned char *sha1;
284 unsigned in_vain = 0;
285 int got_continue = 0;
286 int got_ready = 0;
287 struct strbuf req_buf = STRBUF_INIT;
288 size_t state_len = 0;
290 if (args.stateless_rpc && multi_ack == 1)
291 die("--stateless-rpc requires multi_ack_detailed");
292 if (marked)
293 for_each_ref(clear_marks, NULL);
294 marked = 1;
296 for_each_ref(rev_list_insert_ref, NULL);
297 for_each_alternate_ref(insert_one_alternate_ref, NULL);
299 fetching = 0;
300 for ( ; refs ; refs = refs->next) {
301 unsigned char *remote = refs->old_sha1;
302 const char *remote_hex;
303 struct object *o;
306 * If that object is complete (i.e. it is an ancestor of a
307 * local ref), we tell them we have it but do not have to
308 * tell them about its ancestors, which they already know
309 * about.
311 * We use lookup_object here because we are only
312 * interested in the case we *know* the object is
313 * reachable and we have already scanned it.
315 if (((o = lookup_object(remote)) != NULL) &&
316 (o->flags & COMPLETE)) {
317 continue;
320 remote_hex = sha1_to_hex(remote);
321 if (!fetching) {
322 struct strbuf c = STRBUF_INIT;
323 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
324 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
325 if (no_done) strbuf_addstr(&c, " no-done");
326 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
327 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
328 if (args.use_thin_pack) strbuf_addstr(&c, " thin-pack");
329 if (args.no_progress) strbuf_addstr(&c, " no-progress");
330 if (args.include_tag) strbuf_addstr(&c, " include-tag");
331 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
332 if (agent_supported) strbuf_addf(&c, " agent=%s",
333 git_user_agent_sanitized());
334 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
335 strbuf_release(&c);
336 } else
337 packet_buf_write(&req_buf, "want %s\n", remote_hex);
338 fetching++;
341 if (!fetching) {
342 strbuf_release(&req_buf);
343 packet_flush(fd[1]);
344 return 1;
347 if (is_repository_shallow())
348 write_shallow_commits(&req_buf, 1);
349 if (args.depth > 0)
350 packet_buf_write(&req_buf, "deepen %d", args.depth);
351 packet_buf_flush(&req_buf);
352 state_len = req_buf.len;
354 if (args.depth > 0) {
355 char line[1024];
356 unsigned char sha1[20];
358 send_request(fd[1], &req_buf);
359 while (packet_read_line(fd[0], line, sizeof(line))) {
360 if (!prefixcmp(line, "shallow ")) {
361 if (get_sha1_hex(line + 8, sha1))
362 die("invalid shallow line: %s", line);
363 register_shallow(sha1);
364 continue;
366 if (!prefixcmp(line, "unshallow ")) {
367 if (get_sha1_hex(line + 10, sha1))
368 die("invalid unshallow line: %s", line);
369 if (!lookup_object(sha1))
370 die("object not found: %s", line);
371 /* make sure that it is parsed as shallow */
372 if (!parse_object(sha1))
373 die("error in object: %s", line);
374 if (unregister_shallow(sha1))
375 die("no shallow found: %s", line);
376 continue;
378 die("expected shallow/unshallow, got %s", line);
380 } else if (!args.stateless_rpc)
381 send_request(fd[1], &req_buf);
383 if (!args.stateless_rpc) {
384 /* If we aren't using the stateless-rpc interface
385 * we don't need to retain the headers.
387 strbuf_setlen(&req_buf, 0);
388 state_len = 0;
391 flushes = 0;
392 retval = -1;
393 while ((sha1 = get_rev())) {
394 packet_buf_write(&req_buf, "have %s\n", sha1_to_hex(sha1));
395 if (args.verbose)
396 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
397 in_vain++;
398 if (flush_at <= ++count) {
399 int ack;
401 packet_buf_flush(&req_buf);
402 send_request(fd[1], &req_buf);
403 strbuf_setlen(&req_buf, state_len);
404 flushes++;
405 flush_at = next_flush(count);
408 * We keep one window "ahead" of the other side, and
409 * will wait for an ACK only on the next one
411 if (!args.stateless_rpc && count == INITIAL_FLUSH)
412 continue;
414 consume_shallow_list(fd[0]);
415 do {
416 ack = get_ack(fd[0], result_sha1);
417 if (args.verbose && ack)
418 fprintf(stderr, "got ack %d %s\n", ack,
419 sha1_to_hex(result_sha1));
420 switch (ack) {
421 case ACK:
422 flushes = 0;
423 multi_ack = 0;
424 retval = 0;
425 goto done;
426 case ACK_common:
427 case ACK_ready:
428 case ACK_continue: {
429 struct commit *commit =
430 lookup_commit(result_sha1);
431 if (!commit)
432 die("invalid commit %s", sha1_to_hex(result_sha1));
433 if (args.stateless_rpc
434 && ack == ACK_common
435 && !(commit->object.flags & COMMON)) {
436 /* We need to replay the have for this object
437 * on the next RPC request so the peer knows
438 * it is in common with us.
440 const char *hex = sha1_to_hex(result_sha1);
441 packet_buf_write(&req_buf, "have %s\n", hex);
442 state_len = req_buf.len;
444 mark_common(commit, 0, 1);
445 retval = 0;
446 in_vain = 0;
447 got_continue = 1;
448 if (ack == ACK_ready) {
449 rev_list = NULL;
450 got_ready = 1;
452 break;
455 } while (ack);
456 flushes--;
457 if (got_continue && MAX_IN_VAIN < in_vain) {
458 if (args.verbose)
459 fprintf(stderr, "giving up\n");
460 break; /* give up */
464 done:
465 if (!got_ready || !no_done) {
466 packet_buf_write(&req_buf, "done\n");
467 send_request(fd[1], &req_buf);
469 if (args.verbose)
470 fprintf(stderr, "done\n");
471 if (retval != 0) {
472 multi_ack = 0;
473 flushes++;
475 strbuf_release(&req_buf);
477 consume_shallow_list(fd[0]);
478 while (flushes || multi_ack) {
479 int ack = get_ack(fd[0], result_sha1);
480 if (ack) {
481 if (args.verbose)
482 fprintf(stderr, "got ack (%d) %s\n", ack,
483 sha1_to_hex(result_sha1));
484 if (ack == ACK)
485 return 0;
486 multi_ack = 1;
487 continue;
489 flushes--;
491 /* it is no error to fetch into a completely empty repo */
492 return count ? retval : 0;
495 static struct commit_list *complete;
497 static int mark_complete(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
499 struct object *o = parse_object(sha1);
501 while (o && o->type == OBJ_TAG) {
502 struct tag *t = (struct tag *) o;
503 if (!t->tagged)
504 break; /* broken repository */
505 o->flags |= COMPLETE;
506 o = parse_object(t->tagged->sha1);
508 if (o && o->type == OBJ_COMMIT) {
509 struct commit *commit = (struct commit *)o;
510 if (!(commit->object.flags & COMPLETE)) {
511 commit->object.flags |= COMPLETE;
512 commit_list_insert_by_date(commit, &complete);
515 return 0;
518 static void mark_recent_complete_commits(unsigned long cutoff)
520 while (complete && cutoff <= complete->item->date) {
521 if (args.verbose)
522 fprintf(stderr, "Marking %s as complete\n",
523 sha1_to_hex(complete->item->object.sha1));
524 pop_most_recent_commit(&complete, COMPLETE);
528 static void filter_refs(struct ref **refs, int nr_match, char **match)
530 struct ref **return_refs;
531 struct ref *newlist = NULL;
532 struct ref **newtail = &newlist;
533 struct ref *ref, *next;
534 struct ref *fastarray[32];
535 int match_pos;
537 if (nr_match && !args.fetch_all) {
538 if (ARRAY_SIZE(fastarray) < nr_match)
539 return_refs = xcalloc(nr_match, sizeof(struct ref *));
540 else {
541 return_refs = fastarray;
542 memset(return_refs, 0, sizeof(struct ref *) * nr_match);
545 else
546 return_refs = NULL;
548 match_pos = 0;
549 for (ref = *refs; ref; ref = next) {
550 next = ref->next;
551 if (!memcmp(ref->name, "refs/", 5) &&
552 check_refname_format(ref->name + 5, 0))
553 ; /* trash */
554 else if (args.fetch_all &&
555 (!args.depth || prefixcmp(ref->name, "refs/tags/") )) {
556 *newtail = ref;
557 ref->next = NULL;
558 newtail = &ref->next;
559 continue;
561 else {
562 int cmp = -1;
563 while (match_pos < nr_match) {
564 cmp = strcmp(ref->name, match[match_pos]);
565 if (cmp < 0) /* definitely do not have it */
566 break;
567 else if (cmp == 0) { /* definitely have it */
568 match[match_pos][0] = '\0';
569 return_refs[match_pos] = ref;
570 break;
572 else /* might have it; keep looking */
573 match_pos++;
575 if (!cmp)
576 continue; /* we will link it later */
578 free(ref);
581 if (!args.fetch_all) {
582 int i;
583 for (i = 0; i < nr_match; i++) {
584 ref = return_refs[i];
585 if (ref) {
586 *newtail = ref;
587 ref->next = NULL;
588 newtail = &ref->next;
591 if (return_refs != fastarray)
592 free(return_refs);
594 *refs = newlist;
597 static void mark_alternate_complete(const struct ref *ref, void *unused)
599 mark_complete(NULL, ref->old_sha1, 0, NULL);
602 static int everything_local(struct ref **refs, int nr_match, char **match)
604 struct ref *ref;
605 int retval;
606 unsigned long cutoff = 0;
608 save_commit_buffer = 0;
610 for (ref = *refs; ref; ref = ref->next) {
611 struct object *o;
613 o = parse_object(ref->old_sha1);
614 if (!o)
615 continue;
617 /* We already have it -- which may mean that we were
618 * in sync with the other side at some time after
619 * that (it is OK if we guess wrong here).
621 if (o->type == OBJ_COMMIT) {
622 struct commit *commit = (struct commit *)o;
623 if (!cutoff || cutoff < commit->date)
624 cutoff = commit->date;
628 if (!args.depth) {
629 for_each_ref(mark_complete, NULL);
630 for_each_alternate_ref(mark_alternate_complete, NULL);
631 if (cutoff)
632 mark_recent_complete_commits(cutoff);
636 * Mark all complete remote refs as common refs.
637 * Don't mark them common yet; the server has to be told so first.
639 for (ref = *refs; ref; ref = ref->next) {
640 struct object *o = deref_tag(lookup_object(ref->old_sha1),
641 NULL, 0);
643 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
644 continue;
646 if (!(o->flags & SEEN)) {
647 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
649 mark_common((struct commit *)o, 1, 1);
653 filter_refs(refs, nr_match, match);
655 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
656 const unsigned char *remote = ref->old_sha1;
657 unsigned char local[20];
658 struct object *o;
660 o = lookup_object(remote);
661 if (!o || !(o->flags & COMPLETE)) {
662 retval = 0;
663 if (!args.verbose)
664 continue;
665 fprintf(stderr,
666 "want %s (%s)\n", sha1_to_hex(remote),
667 ref->name);
668 continue;
671 hashcpy(ref->new_sha1, local);
672 if (!args.verbose)
673 continue;
674 fprintf(stderr,
675 "already have %s (%s)\n", sha1_to_hex(remote),
676 ref->name);
678 return retval;
681 static int sideband_demux(int in, int out, void *data)
683 int *xd = data;
685 int ret = recv_sideband("fetch-pack", xd[0], out);
686 close(out);
687 return ret;
690 static int get_pack(int xd[2], char **pack_lockfile)
692 struct async demux;
693 const char *argv[20];
694 char keep_arg[256];
695 char hdr_arg[256];
696 const char **av;
697 int do_keep = args.keep_pack;
698 struct child_process cmd;
700 memset(&demux, 0, sizeof(demux));
701 if (use_sideband) {
702 /* xd[] is talking with upload-pack; subprocess reads from
703 * xd[0], spits out band#2 to stderr, and feeds us band#1
704 * through demux->out.
706 demux.proc = sideband_demux;
707 demux.data = xd;
708 demux.out = -1;
709 if (start_async(&demux))
710 die("fetch-pack: unable to fork off sideband"
711 " demultiplexer");
713 else
714 demux.out = xd[0];
716 memset(&cmd, 0, sizeof(cmd));
717 cmd.argv = argv;
718 av = argv;
719 *hdr_arg = 0;
720 if (!args.keep_pack && unpack_limit) {
721 struct pack_header header;
723 if (read_pack_header(demux.out, &header))
724 die("protocol error: bad pack header");
725 snprintf(hdr_arg, sizeof(hdr_arg),
726 "--pack_header=%"PRIu32",%"PRIu32,
727 ntohl(header.hdr_version), ntohl(header.hdr_entries));
728 if (ntohl(header.hdr_entries) < unpack_limit)
729 do_keep = 0;
730 else
731 do_keep = 1;
734 if (do_keep) {
735 if (pack_lockfile)
736 cmd.out = -1;
737 *av++ = "index-pack";
738 *av++ = "--stdin";
739 if (!args.quiet && !args.no_progress)
740 *av++ = "-v";
741 if (args.use_thin_pack)
742 *av++ = "--fix-thin";
743 if (args.lock_pack || unpack_limit) {
744 int s = sprintf(keep_arg,
745 "--keep=fetch-pack %"PRIuMAX " on ", (uintmax_t) getpid());
746 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
747 strcpy(keep_arg + s, "localhost");
748 *av++ = keep_arg;
751 else {
752 *av++ = "unpack-objects";
753 if (args.quiet || args.no_progress)
754 *av++ = "-q";
756 if (*hdr_arg)
757 *av++ = hdr_arg;
758 if (fetch_fsck_objects >= 0
759 ? fetch_fsck_objects
760 : transfer_fsck_objects >= 0
761 ? transfer_fsck_objects
762 : 0)
763 *av++ = "--strict";
764 *av++ = NULL;
766 cmd.in = demux.out;
767 cmd.git_cmd = 1;
768 if (start_command(&cmd))
769 die("fetch-pack: unable to fork off %s", argv[0]);
770 if (do_keep && pack_lockfile) {
771 *pack_lockfile = index_pack_lockfile(cmd.out);
772 close(cmd.out);
775 if (finish_command(&cmd))
776 die("%s failed", argv[0]);
777 if (use_sideband && finish_async(&demux))
778 die("error in sideband demultiplexer");
779 return 0;
782 static struct ref *do_fetch_pack(int fd[2],
783 const struct ref *orig_ref,
784 int nr_match,
785 char **match,
786 char **pack_lockfile)
788 struct ref *ref = copy_ref_list(orig_ref);
789 unsigned char sha1[20];
790 const char *agent_feature;
791 int agent_len;
793 sort_ref_list(&ref, ref_compare_name);
795 if (is_repository_shallow() && !server_supports("shallow"))
796 die("Server does not support shallow clients");
797 if (server_supports("multi_ack_detailed")) {
798 if (args.verbose)
799 fprintf(stderr, "Server supports multi_ack_detailed\n");
800 multi_ack = 2;
801 if (server_supports("no-done")) {
802 if (args.verbose)
803 fprintf(stderr, "Server supports no-done\n");
804 if (args.stateless_rpc)
805 no_done = 1;
808 else if (server_supports("multi_ack")) {
809 if (args.verbose)
810 fprintf(stderr, "Server supports multi_ack\n");
811 multi_ack = 1;
813 if (server_supports("side-band-64k")) {
814 if (args.verbose)
815 fprintf(stderr, "Server supports side-band-64k\n");
816 use_sideband = 2;
818 else if (server_supports("side-band")) {
819 if (args.verbose)
820 fprintf(stderr, "Server supports side-band\n");
821 use_sideband = 1;
823 if (!server_supports("thin-pack"))
824 args.use_thin_pack = 0;
825 if (!server_supports("no-progress"))
826 args.no_progress = 0;
827 if (!server_supports("include-tag"))
828 args.include_tag = 0;
829 if (server_supports("ofs-delta")) {
830 if (args.verbose)
831 fprintf(stderr, "Server supports ofs-delta\n");
832 } else
833 prefer_ofs_delta = 0;
835 if ((agent_feature = server_feature_value("agent", &agent_len))) {
836 agent_supported = 1;
837 if (args.verbose && agent_len)
838 fprintf(stderr, "Server version is %.*s\n",
839 agent_len, agent_feature);
842 if (everything_local(&ref, nr_match, match)) {
843 packet_flush(fd[1]);
844 goto all_done;
846 if (find_common(fd, sha1, ref) < 0)
847 if (!args.keep_pack)
848 /* When cloning, it is not unusual to have
849 * no common commit.
851 warning("no common commits");
853 if (args.stateless_rpc)
854 packet_flush(fd[1]);
855 if (get_pack(fd, pack_lockfile))
856 die("git fetch-pack: fetch failed.");
858 all_done:
859 return ref;
862 static int remove_duplicates(int nr_heads, char **heads)
864 int src, dst;
866 if (!nr_heads)
867 return 0;
869 for (src = dst = 1; src < nr_heads; src++)
870 if (strcmp(heads[src], heads[dst-1]))
871 heads[dst++] = heads[src];
872 return dst;
875 static int fetch_pack_config(const char *var, const char *value, void *cb)
877 if (strcmp(var, "fetch.unpacklimit") == 0) {
878 fetch_unpack_limit = git_config_int(var, value);
879 return 0;
882 if (strcmp(var, "transfer.unpacklimit") == 0) {
883 transfer_unpack_limit = git_config_int(var, value);
884 return 0;
887 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
888 prefer_ofs_delta = git_config_bool(var, value);
889 return 0;
892 if (!strcmp(var, "fetch.fsckobjects")) {
893 fetch_fsck_objects = git_config_bool(var, value);
894 return 0;
897 if (!strcmp(var, "transfer.fsckobjects")) {
898 transfer_fsck_objects = git_config_bool(var, value);
899 return 0;
902 return git_default_config(var, value, cb);
905 static struct lock_file lock;
907 static void fetch_pack_setup(void)
909 static int did_setup;
910 if (did_setup)
911 return;
912 git_config(fetch_pack_config, NULL);
913 if (0 <= transfer_unpack_limit)
914 unpack_limit = transfer_unpack_limit;
915 else if (0 <= fetch_unpack_limit)
916 unpack_limit = fetch_unpack_limit;
917 did_setup = 1;
920 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
922 int i, ret;
923 struct ref *ref = NULL;
924 const char *dest = NULL;
925 int alloc_heads = 0, nr_heads = 0;
926 char **heads = NULL;
927 int fd[2];
928 char *pack_lockfile = NULL;
929 char **pack_lockfile_ptr = NULL;
930 struct child_process *conn;
932 packet_trace_identity("fetch-pack");
934 for (i = 1; i < argc && *argv[i] == '-'; i++) {
935 const char *arg = argv[i];
937 if (!prefixcmp(arg, "--upload-pack=")) {
938 args.uploadpack = arg + 14;
939 continue;
941 if (!prefixcmp(arg, "--exec=")) {
942 args.uploadpack = arg + 7;
943 continue;
945 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
946 args.quiet = 1;
947 continue;
949 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
950 args.lock_pack = args.keep_pack;
951 args.keep_pack = 1;
952 continue;
954 if (!strcmp("--thin", arg)) {
955 args.use_thin_pack = 1;
956 continue;
958 if (!strcmp("--include-tag", arg)) {
959 args.include_tag = 1;
960 continue;
962 if (!strcmp("--all", arg)) {
963 args.fetch_all = 1;
964 continue;
966 if (!strcmp("--stdin", arg)) {
967 args.stdin_refs = 1;
968 continue;
970 if (!strcmp("-v", arg)) {
971 args.verbose = 1;
972 continue;
974 if (!prefixcmp(arg, "--depth=")) {
975 args.depth = strtol(arg + 8, NULL, 0);
976 continue;
978 if (!strcmp("--no-progress", arg)) {
979 args.no_progress = 1;
980 continue;
982 if (!strcmp("--stateless-rpc", arg)) {
983 args.stateless_rpc = 1;
984 continue;
986 if (!strcmp("--lock-pack", arg)) {
987 args.lock_pack = 1;
988 pack_lockfile_ptr = &pack_lockfile;
989 continue;
991 usage(fetch_pack_usage);
994 if (i < argc)
995 dest = argv[i++];
996 else
997 usage(fetch_pack_usage);
1000 * Copy refs from cmdline to growable list, then append any
1001 * refs from the standard input:
1003 ALLOC_GROW(heads, argc - i, alloc_heads);
1004 for (; i < argc; i++)
1005 heads[nr_heads++] = xstrdup(argv[i]);
1006 if (args.stdin_refs) {
1007 if (args.stateless_rpc) {
1008 /* in stateless RPC mode we use pkt-line to read
1009 * from stdin, until we get a flush packet
1011 static char line[1000];
1012 for (;;) {
1013 int n = packet_read_line(0, line, sizeof(line));
1014 if (!n)
1015 break;
1016 if (line[n-1] == '\n')
1017 n--;
1018 ALLOC_GROW(heads, nr_heads + 1, alloc_heads);
1019 heads[nr_heads++] = xmemdupz(line, n);
1022 else {
1023 /* read from stdin one ref per line, until EOF */
1024 struct strbuf line = STRBUF_INIT;
1025 while (strbuf_getline(&line, stdin, '\n') != EOF) {
1026 ALLOC_GROW(heads, nr_heads + 1, alloc_heads);
1027 heads[nr_heads++] = strbuf_detach(&line, NULL);
1029 strbuf_release(&line);
1033 if (args.stateless_rpc) {
1034 conn = NULL;
1035 fd[0] = 0;
1036 fd[1] = 1;
1037 } else {
1038 conn = git_connect(fd, dest, args.uploadpack,
1039 args.verbose ? CONNECT_VERBOSE : 0);
1042 get_remote_heads(fd[0], &ref, 0, NULL);
1044 ref = fetch_pack(&args, fd, conn, ref, dest,
1045 nr_heads, heads, pack_lockfile_ptr);
1046 if (pack_lockfile) {
1047 printf("lock %s\n", pack_lockfile);
1048 fflush(stdout);
1050 close(fd[0]);
1051 close(fd[1]);
1052 if (finish_connect(conn))
1053 ref = NULL;
1054 ret = !ref;
1056 if (!ret && nr_heads) {
1057 /* If the heads to pull were given, we should have
1058 * consumed all of them by matching the remote.
1059 * Otherwise, 'git fetch remote no-such-ref' would
1060 * silently succeed without issuing an error.
1062 for (i = 0; i < nr_heads; i++)
1063 if (heads[i] && heads[i][0]) {
1064 error("no such remote ref %s", heads[i]);
1065 ret = 1;
1068 while (ref) {
1069 printf("%s %s\n",
1070 sha1_to_hex(ref->old_sha1), ref->name);
1071 ref = ref->next;
1074 return ret;
1077 static int compare_heads(const void *a, const void *b)
1079 return strcmp(*(const char **)a, *(const char **)b);
1082 struct ref *fetch_pack(struct fetch_pack_args *my_args,
1083 int fd[], struct child_process *conn,
1084 const struct ref *ref,
1085 const char *dest,
1086 int nr_heads,
1087 char **heads,
1088 char **pack_lockfile)
1090 struct stat st;
1091 struct ref *ref_cpy;
1093 fetch_pack_setup();
1094 if (&args != my_args)
1095 memcpy(&args, my_args, sizeof(args));
1096 if (args.depth > 0) {
1097 if (stat(git_path("shallow"), &st))
1098 st.st_mtime = 0;
1101 if (heads && nr_heads) {
1102 qsort(heads, nr_heads, sizeof(*heads), compare_heads);
1103 nr_heads = remove_duplicates(nr_heads, heads);
1106 if (!ref) {
1107 packet_flush(fd[1]);
1108 die("no matching remote head");
1110 ref_cpy = do_fetch_pack(fd, ref, nr_heads, heads, pack_lockfile);
1112 if (args.depth > 0) {
1113 struct cache_time mtime;
1114 struct strbuf sb = STRBUF_INIT;
1115 char *shallow = git_path("shallow");
1116 int fd;
1118 mtime.sec = st.st_mtime;
1119 mtime.nsec = ST_MTIME_NSEC(st);
1120 if (stat(shallow, &st)) {
1121 if (mtime.sec)
1122 die("shallow file was removed during fetch");
1123 } else if (st.st_mtime != mtime.sec
1124 #ifdef USE_NSEC
1125 || ST_MTIME_NSEC(st) != mtime.nsec
1126 #endif
1128 die("shallow file was changed during fetch");
1130 fd = hold_lock_file_for_update(&lock, shallow,
1131 LOCK_DIE_ON_ERROR);
1132 if (!write_shallow_commits(&sb, 0)
1133 || write_in_full(fd, sb.buf, sb.len) != sb.len) {
1134 unlink_or_warn(shallow);
1135 rollback_lock_file(&lock);
1136 } else {
1137 commit_lock_file(&lock);
1139 strbuf_release(&sb);
1142 reprepare_packed_git();
1143 return ref_cpy;