Fix potential local deadlock during fetch-pack
[git/gitweb.git] / builtin / fetch-pack.c
blob147d67dca447aa4afa1c829865af636753cacf31
1 #include "cache.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"
13 static int transfer_unpack_limit = -1;
14 static int fetch_unpack_limit = -1;
15 static int unpack_limit = 100;
16 static int prefer_ofs_delta = 1;
17 static int no_done = 0;
18 static struct fetch_pack_args args = {
19 /* .uploadpack = */ "git-upload-pack",
22 static const char fetch_pack_usage[] =
23 "git fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] [--no-progress] [-v] [<host>:]<directory> [<refs>...]";
25 #define COMPLETE (1U << 0)
26 #define COMMON (1U << 1)
27 #define COMMON_REF (1U << 2)
28 #define SEEN (1U << 3)
29 #define POPPED (1U << 4)
31 static int marked;
34 * After sending this many "have"s if we do not get any new ACK , we
35 * give up traversing our history.
37 #define MAX_IN_VAIN 256
39 static struct commit_list *rev_list;
40 static int non_common_revs, multi_ack, use_sideband;
42 static void rev_list_push(struct commit *commit, int mark)
44 if (!(commit->object.flags & mark)) {
45 commit->object.flags |= mark;
47 if (!(commit->object.parsed))
48 if (parse_commit(commit))
49 return;
51 commit_list_insert_by_date(commit, &rev_list);
53 if (!(commit->object.flags & COMMON))
54 non_common_revs++;
58 static int rev_list_insert_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
60 struct object *o = deref_tag(parse_object(sha1), path, 0);
62 if (o && o->type == OBJ_COMMIT)
63 rev_list_push((struct commit *)o, SEEN);
65 return 0;
68 static int clear_marks(const char *path, const unsigned char *sha1, int flag, void *cb_data)
70 struct object *o = deref_tag(parse_object(sha1), path, 0);
72 if (o && o->type == OBJ_COMMIT)
73 clear_commit_marks((struct commit *)o,
74 COMMON | COMMON_REF | SEEN | POPPED);
75 return 0;
79 This function marks a rev and its ancestors as common.
80 In some cases, it is desirable to mark only the ancestors (for example
81 when only the server does not yet know that they are common).
84 static void mark_common(struct commit *commit,
85 int ancestors_only, int dont_parse)
87 if (commit != NULL && !(commit->object.flags & COMMON)) {
88 struct object *o = (struct object *)commit;
90 if (!ancestors_only)
91 o->flags |= COMMON;
93 if (!(o->flags & SEEN))
94 rev_list_push(commit, SEEN);
95 else {
96 struct commit_list *parents;
98 if (!ancestors_only && !(o->flags & POPPED))
99 non_common_revs--;
100 if (!o->parsed && !dont_parse)
101 if (parse_commit(commit))
102 return;
104 for (parents = commit->parents;
105 parents;
106 parents = parents->next)
107 mark_common(parents->item, 0, dont_parse);
113 Get the next rev to send, ignoring the common.
116 static const unsigned char *get_rev(void)
118 struct commit *commit = NULL;
120 while (commit == NULL) {
121 unsigned int mark;
122 struct commit_list *parents;
124 if (rev_list == NULL || non_common_revs == 0)
125 return NULL;
127 commit = rev_list->item;
128 if (!commit->object.parsed)
129 parse_commit(commit);
130 parents = commit->parents;
132 commit->object.flags |= POPPED;
133 if (!(commit->object.flags & COMMON))
134 non_common_revs--;
136 if (commit->object.flags & COMMON) {
137 /* do not send "have", and ignore ancestors */
138 commit = NULL;
139 mark = COMMON | SEEN;
140 } else if (commit->object.flags & COMMON_REF)
141 /* send "have", and ignore ancestors */
142 mark = COMMON | SEEN;
143 else
144 /* send "have", also for its ancestors */
145 mark = SEEN;
147 while (parents) {
148 if (!(parents->item->object.flags & SEEN))
149 rev_list_push(parents->item, mark);
150 if (mark & COMMON)
151 mark_common(parents->item, 1, 0);
152 parents = parents->next;
155 rev_list = rev_list->next;
158 return commit->object.sha1;
161 enum ack_type {
162 NAK = 0,
163 ACK,
164 ACK_continue,
165 ACK_common,
166 ACK_ready
169 static void consume_shallow_list(int fd)
171 if (args.stateless_rpc && args.depth > 0) {
172 /* If we sent a depth we will get back "duplicate"
173 * shallow and unshallow commands every time there
174 * is a block of have lines exchanged.
176 char line[1000];
177 while (packet_read_line(fd, line, sizeof(line))) {
178 if (!prefixcmp(line, "shallow "))
179 continue;
180 if (!prefixcmp(line, "unshallow "))
181 continue;
182 die("git fetch-pack: expected shallow list");
187 static enum ack_type get_ack(int fd, unsigned char *result_sha1)
189 static char line[1000];
190 int len = packet_read_line(fd, line, sizeof(line));
192 if (!len)
193 die("git fetch-pack: expected ACK/NAK, got EOF");
194 if (line[len-1] == '\n')
195 line[--len] = 0;
196 if (!strcmp(line, "NAK"))
197 return NAK;
198 if (!prefixcmp(line, "ACK ")) {
199 if (!get_sha1_hex(line+4, result_sha1)) {
200 if (strstr(line+45, "continue"))
201 return ACK_continue;
202 if (strstr(line+45, "common"))
203 return ACK_common;
204 if (strstr(line+45, "ready"))
205 return ACK_ready;
206 return ACK;
209 die("git fetch_pack: expected ACK/NAK, got '%s'", line);
212 static void send_request(int fd, struct strbuf *buf)
214 if (args.stateless_rpc) {
215 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
216 packet_flush(fd);
217 } else
218 safe_write(fd, buf->buf, buf->len);
221 #define INITIAL_FLUSH 16
222 #define PIPESAFE_FLUSH 32
223 #define LARGE_FLUSH 1024
225 static int next_flush(int count)
227 int flush_limit = args.stateless_rpc ? LARGE_FLUSH : PIPESAFE_FLUSH;
229 if (count < flush_limit)
230 count <<= 1;
231 else
232 count += flush_limit;
233 return count;
236 static int find_common(int fd[2], unsigned char *result_sha1,
237 struct ref *refs)
239 int fetching;
240 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
241 const unsigned char *sha1;
242 unsigned in_vain = 0;
243 int got_continue = 0;
244 int got_ready = 0;
245 struct strbuf req_buf = STRBUF_INIT;
246 size_t state_len = 0;
248 if (args.stateless_rpc && multi_ack == 1)
249 die("--stateless-rpc requires multi_ack_detailed");
250 if (marked)
251 for_each_ref(clear_marks, NULL);
252 marked = 1;
254 for_each_ref(rev_list_insert_ref, NULL);
256 fetching = 0;
257 for ( ; refs ; refs = refs->next) {
258 unsigned char *remote = refs->old_sha1;
259 const char *remote_hex;
260 struct object *o;
263 * If that object is complete (i.e. it is an ancestor of a
264 * local ref), we tell them we have it but do not have to
265 * tell them about its ancestors, which they already know
266 * about.
268 * We use lookup_object here because we are only
269 * interested in the case we *know* the object is
270 * reachable and we have already scanned it.
272 if (((o = lookup_object(remote)) != NULL) &&
273 (o->flags & COMPLETE)) {
274 continue;
277 remote_hex = sha1_to_hex(remote);
278 if (!fetching) {
279 struct strbuf c = STRBUF_INIT;
280 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
281 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
282 if (no_done) strbuf_addstr(&c, " no-done");
283 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
284 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
285 if (args.use_thin_pack) strbuf_addstr(&c, " thin-pack");
286 if (args.no_progress) strbuf_addstr(&c, " no-progress");
287 if (args.include_tag) strbuf_addstr(&c, " include-tag");
288 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
289 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
290 strbuf_release(&c);
291 } else
292 packet_buf_write(&req_buf, "want %s\n", remote_hex);
293 fetching++;
296 if (!fetching) {
297 strbuf_release(&req_buf);
298 packet_flush(fd[1]);
299 return 1;
302 if (is_repository_shallow())
303 write_shallow_commits(&req_buf, 1);
304 if (args.depth > 0)
305 packet_buf_write(&req_buf, "deepen %d", args.depth);
306 packet_buf_flush(&req_buf);
307 state_len = req_buf.len;
309 if (args.depth > 0) {
310 char line[1024];
311 unsigned char sha1[20];
313 send_request(fd[1], &req_buf);
314 while (packet_read_line(fd[0], line, sizeof(line))) {
315 if (!prefixcmp(line, "shallow ")) {
316 if (get_sha1_hex(line + 8, sha1))
317 die("invalid shallow line: %s", line);
318 register_shallow(sha1);
319 continue;
321 if (!prefixcmp(line, "unshallow ")) {
322 if (get_sha1_hex(line + 10, sha1))
323 die("invalid unshallow line: %s", line);
324 if (!lookup_object(sha1))
325 die("object not found: %s", line);
326 /* make sure that it is parsed as shallow */
327 if (!parse_object(sha1))
328 die("error in object: %s", line);
329 if (unregister_shallow(sha1))
330 die("no shallow found: %s", line);
331 continue;
333 die("expected shallow/unshallow, got %s", line);
335 } else if (!args.stateless_rpc)
336 send_request(fd[1], &req_buf);
338 if (!args.stateless_rpc) {
339 /* If we aren't using the stateless-rpc interface
340 * we don't need to retain the headers.
342 strbuf_setlen(&req_buf, 0);
343 state_len = 0;
346 flushes = 0;
347 retval = -1;
348 while ((sha1 = get_rev())) {
349 packet_buf_write(&req_buf, "have %s\n", sha1_to_hex(sha1));
350 if (args.verbose)
351 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
352 in_vain++;
353 if (flush_at <= ++count) {
354 int ack;
356 packet_buf_flush(&req_buf);
357 send_request(fd[1], &req_buf);
358 strbuf_setlen(&req_buf, state_len);
359 flushes++;
360 flush_at = next_flush(count);
363 * We keep one window "ahead" of the other side, and
364 * will wait for an ACK only on the next one
366 if (!args.stateless_rpc && count == INITIAL_FLUSH)
367 continue;
369 consume_shallow_list(fd[0]);
370 do {
371 ack = get_ack(fd[0], result_sha1);
372 if (args.verbose && ack)
373 fprintf(stderr, "got ack %d %s\n", ack,
374 sha1_to_hex(result_sha1));
375 switch (ack) {
376 case ACK:
377 flushes = 0;
378 multi_ack = 0;
379 retval = 0;
380 goto done;
381 case ACK_common:
382 case ACK_ready:
383 case ACK_continue: {
384 struct commit *commit =
385 lookup_commit(result_sha1);
386 if (args.stateless_rpc
387 && ack == ACK_common
388 && !(commit->object.flags & COMMON)) {
389 /* We need to replay the have for this object
390 * on the next RPC request so the peer knows
391 * it is in common with us.
393 const char *hex = sha1_to_hex(result_sha1);
394 packet_buf_write(&req_buf, "have %s\n", hex);
395 state_len = req_buf.len;
397 mark_common(commit, 0, 1);
398 retval = 0;
399 in_vain = 0;
400 got_continue = 1;
401 if (ack == ACK_ready) {
402 rev_list = NULL;
403 got_ready = 1;
405 break;
408 } while (ack);
409 flushes--;
410 if (got_continue && MAX_IN_VAIN < in_vain) {
411 if (args.verbose)
412 fprintf(stderr, "giving up\n");
413 break; /* give up */
417 done:
418 if (!got_ready || !no_done) {
419 packet_buf_write(&req_buf, "done\n");
420 send_request(fd[1], &req_buf);
422 if (args.verbose)
423 fprintf(stderr, "done\n");
424 if (retval != 0) {
425 multi_ack = 0;
426 flushes++;
428 strbuf_release(&req_buf);
430 consume_shallow_list(fd[0]);
431 while (flushes || multi_ack) {
432 int ack = get_ack(fd[0], result_sha1);
433 if (ack) {
434 if (args.verbose)
435 fprintf(stderr, "got ack (%d) %s\n", ack,
436 sha1_to_hex(result_sha1));
437 if (ack == ACK)
438 return 0;
439 multi_ack = 1;
440 continue;
442 flushes--;
444 /* it is no error to fetch into a completely empty repo */
445 return count ? retval : 0;
448 static struct commit_list *complete;
450 static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
452 struct object *o = parse_object(sha1);
454 while (o && o->type == OBJ_TAG) {
455 struct tag *t = (struct tag *) o;
456 if (!t->tagged)
457 break; /* broken repository */
458 o->flags |= COMPLETE;
459 o = parse_object(t->tagged->sha1);
461 if (o && o->type == OBJ_COMMIT) {
462 struct commit *commit = (struct commit *)o;
463 commit->object.flags |= COMPLETE;
464 commit_list_insert_by_date(commit, &complete);
466 return 0;
469 static void mark_recent_complete_commits(unsigned long cutoff)
471 while (complete && cutoff <= complete->item->date) {
472 if (args.verbose)
473 fprintf(stderr, "Marking %s as complete\n",
474 sha1_to_hex(complete->item->object.sha1));
475 pop_most_recent_commit(&complete, COMPLETE);
479 static void filter_refs(struct ref **refs, int nr_match, char **match)
481 struct ref **return_refs;
482 struct ref *newlist = NULL;
483 struct ref **newtail = &newlist;
484 struct ref *ref, *next;
485 struct ref *fastarray[32];
487 if (nr_match && !args.fetch_all) {
488 if (ARRAY_SIZE(fastarray) < nr_match)
489 return_refs = xcalloc(nr_match, sizeof(struct ref *));
490 else {
491 return_refs = fastarray;
492 memset(return_refs, 0, sizeof(struct ref *) * nr_match);
495 else
496 return_refs = NULL;
498 for (ref = *refs; ref; ref = next) {
499 next = ref->next;
500 if (!memcmp(ref->name, "refs/", 5) &&
501 check_ref_format(ref->name + 5))
502 ; /* trash */
503 else if (args.fetch_all &&
504 (!args.depth || prefixcmp(ref->name, "refs/tags/") )) {
505 *newtail = ref;
506 ref->next = NULL;
507 newtail = &ref->next;
508 continue;
510 else {
511 int order = path_match(ref->name, nr_match, match);
512 if (order) {
513 return_refs[order-1] = ref;
514 continue; /* we will link it later */
517 free(ref);
520 if (!args.fetch_all) {
521 int i;
522 for (i = 0; i < nr_match; i++) {
523 ref = return_refs[i];
524 if (ref) {
525 *newtail = ref;
526 ref->next = NULL;
527 newtail = &ref->next;
530 if (return_refs != fastarray)
531 free(return_refs);
533 *refs = newlist;
536 static int everything_local(struct ref **refs, int nr_match, char **match)
538 struct ref *ref;
539 int retval;
540 unsigned long cutoff = 0;
542 save_commit_buffer = 0;
544 for (ref = *refs; ref; ref = ref->next) {
545 struct object *o;
547 o = parse_object(ref->old_sha1);
548 if (!o)
549 continue;
551 /* We already have it -- which may mean that we were
552 * in sync with the other side at some time after
553 * that (it is OK if we guess wrong here).
555 if (o->type == OBJ_COMMIT) {
556 struct commit *commit = (struct commit *)o;
557 if (!cutoff || cutoff < commit->date)
558 cutoff = commit->date;
562 if (!args.depth) {
563 for_each_ref(mark_complete, NULL);
564 if (cutoff)
565 mark_recent_complete_commits(cutoff);
569 * Mark all complete remote refs as common refs.
570 * Don't mark them common yet; the server has to be told so first.
572 for (ref = *refs; ref; ref = ref->next) {
573 struct object *o = deref_tag(lookup_object(ref->old_sha1),
574 NULL, 0);
576 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
577 continue;
579 if (!(o->flags & SEEN)) {
580 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
582 mark_common((struct commit *)o, 1, 1);
586 filter_refs(refs, nr_match, match);
588 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
589 const unsigned char *remote = ref->old_sha1;
590 unsigned char local[20];
591 struct object *o;
593 o = lookup_object(remote);
594 if (!o || !(o->flags & COMPLETE)) {
595 retval = 0;
596 if (!args.verbose)
597 continue;
598 fprintf(stderr,
599 "want %s (%s)\n", sha1_to_hex(remote),
600 ref->name);
601 continue;
604 hashcpy(ref->new_sha1, local);
605 if (!args.verbose)
606 continue;
607 fprintf(stderr,
608 "already have %s (%s)\n", sha1_to_hex(remote),
609 ref->name);
611 return retval;
614 static int sideband_demux(int in, int out, void *data)
616 int *xd = data;
618 int ret = recv_sideband("fetch-pack", xd[0], out);
619 close(out);
620 return ret;
623 static int get_pack(int xd[2], char **pack_lockfile)
625 struct async demux;
626 const char *argv[20];
627 char keep_arg[256];
628 char hdr_arg[256];
629 const char **av;
630 int do_keep = args.keep_pack;
631 struct child_process cmd;
633 memset(&demux, 0, sizeof(demux));
634 if (use_sideband) {
635 /* xd[] is talking with upload-pack; subprocess reads from
636 * xd[0], spits out band#2 to stderr, and feeds us band#1
637 * through demux->out.
639 demux.proc = sideband_demux;
640 demux.data = xd;
641 demux.out = -1;
642 if (start_async(&demux))
643 die("fetch-pack: unable to fork off sideband"
644 " demultiplexer");
646 else
647 demux.out = xd[0];
649 memset(&cmd, 0, sizeof(cmd));
650 cmd.argv = argv;
651 av = argv;
652 *hdr_arg = 0;
653 if (!args.keep_pack && unpack_limit) {
654 struct pack_header header;
656 if (read_pack_header(demux.out, &header))
657 die("protocol error: bad pack header");
658 snprintf(hdr_arg, sizeof(hdr_arg),
659 "--pack_header=%"PRIu32",%"PRIu32,
660 ntohl(header.hdr_version), ntohl(header.hdr_entries));
661 if (ntohl(header.hdr_entries) < unpack_limit)
662 do_keep = 0;
663 else
664 do_keep = 1;
667 if (do_keep) {
668 if (pack_lockfile)
669 cmd.out = -1;
670 *av++ = "index-pack";
671 *av++ = "--stdin";
672 if (!args.quiet && !args.no_progress)
673 *av++ = "-v";
674 if (args.use_thin_pack)
675 *av++ = "--fix-thin";
676 if (args.lock_pack || unpack_limit) {
677 int s = sprintf(keep_arg,
678 "--keep=fetch-pack %"PRIuMAX " on ", (uintmax_t) getpid());
679 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
680 strcpy(keep_arg + s, "localhost");
681 *av++ = keep_arg;
684 else {
685 *av++ = "unpack-objects";
686 if (args.quiet)
687 *av++ = "-q";
689 if (*hdr_arg)
690 *av++ = hdr_arg;
691 *av++ = NULL;
693 cmd.in = demux.out;
694 cmd.git_cmd = 1;
695 if (start_command(&cmd))
696 die("fetch-pack: unable to fork off %s", argv[0]);
697 if (do_keep && pack_lockfile) {
698 *pack_lockfile = index_pack_lockfile(cmd.out);
699 close(cmd.out);
702 if (finish_command(&cmd))
703 die("%s failed", argv[0]);
704 if (use_sideband && finish_async(&demux))
705 die("error in sideband demultiplexer");
706 return 0;
709 static struct ref *do_fetch_pack(int fd[2],
710 const struct ref *orig_ref,
711 int nr_match,
712 char **match,
713 char **pack_lockfile)
715 struct ref *ref = copy_ref_list(orig_ref);
716 unsigned char sha1[20];
718 if (is_repository_shallow() && !server_supports("shallow"))
719 die("Server does not support shallow clients");
720 if (server_supports("multi_ack_detailed")) {
721 if (args.verbose)
722 fprintf(stderr, "Server supports multi_ack_detailed\n");
723 multi_ack = 2;
724 if (server_supports("no-done")) {
725 if (args.verbose)
726 fprintf(stderr, "Server supports no-done\n");
727 no_done = 1;
730 else if (server_supports("multi_ack")) {
731 if (args.verbose)
732 fprintf(stderr, "Server supports multi_ack\n");
733 multi_ack = 1;
735 if (server_supports("side-band-64k")) {
736 if (args.verbose)
737 fprintf(stderr, "Server supports side-band-64k\n");
738 use_sideband = 2;
740 else if (server_supports("side-band")) {
741 if (args.verbose)
742 fprintf(stderr, "Server supports side-band\n");
743 use_sideband = 1;
745 if (server_supports("ofs-delta")) {
746 if (args.verbose)
747 fprintf(stderr, "Server supports ofs-delta\n");
748 } else
749 prefer_ofs_delta = 0;
750 if (everything_local(&ref, nr_match, match)) {
751 packet_flush(fd[1]);
752 goto all_done;
754 if (find_common(fd, sha1, ref) < 0)
755 if (!args.keep_pack)
756 /* When cloning, it is not unusual to have
757 * no common commit.
759 warning("no common commits");
761 if (args.stateless_rpc)
762 packet_flush(fd[1]);
763 if (get_pack(fd, pack_lockfile))
764 die("git fetch-pack: fetch failed.");
766 all_done:
767 return ref;
770 static int remove_duplicates(int nr_heads, char **heads)
772 int src, dst;
774 for (src = dst = 0; src < nr_heads; src++) {
775 /* If heads[src] is different from any of
776 * heads[0..dst], push it in.
778 int i;
779 for (i = 0; i < dst; i++) {
780 if (!strcmp(heads[i], heads[src]))
781 break;
783 if (i < dst)
784 continue;
785 if (src != dst)
786 heads[dst] = heads[src];
787 dst++;
789 return dst;
792 static int fetch_pack_config(const char *var, const char *value, void *cb)
794 if (strcmp(var, "fetch.unpacklimit") == 0) {
795 fetch_unpack_limit = git_config_int(var, value);
796 return 0;
799 if (strcmp(var, "transfer.unpacklimit") == 0) {
800 transfer_unpack_limit = git_config_int(var, value);
801 return 0;
804 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
805 prefer_ofs_delta = git_config_bool(var, value);
806 return 0;
809 return git_default_config(var, value, cb);
812 static struct lock_file lock;
814 static void fetch_pack_setup(void)
816 static int did_setup;
817 if (did_setup)
818 return;
819 git_config(fetch_pack_config, NULL);
820 if (0 <= transfer_unpack_limit)
821 unpack_limit = transfer_unpack_limit;
822 else if (0 <= fetch_unpack_limit)
823 unpack_limit = fetch_unpack_limit;
824 did_setup = 1;
827 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
829 int i, ret, nr_heads;
830 struct ref *ref = NULL;
831 char *dest = NULL, **heads;
832 int fd[2];
833 char *pack_lockfile = NULL;
834 char **pack_lockfile_ptr = NULL;
835 struct child_process *conn;
837 nr_heads = 0;
838 heads = NULL;
839 for (i = 1; i < argc; i++) {
840 const char *arg = argv[i];
842 if (*arg == '-') {
843 if (!prefixcmp(arg, "--upload-pack=")) {
844 args.uploadpack = arg + 14;
845 continue;
847 if (!prefixcmp(arg, "--exec=")) {
848 args.uploadpack = arg + 7;
849 continue;
851 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
852 args.quiet = 1;
853 continue;
855 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
856 args.lock_pack = args.keep_pack;
857 args.keep_pack = 1;
858 continue;
860 if (!strcmp("--thin", arg)) {
861 args.use_thin_pack = 1;
862 continue;
864 if (!strcmp("--include-tag", arg)) {
865 args.include_tag = 1;
866 continue;
868 if (!strcmp("--all", arg)) {
869 args.fetch_all = 1;
870 continue;
872 if (!strcmp("-v", arg)) {
873 args.verbose = 1;
874 continue;
876 if (!prefixcmp(arg, "--depth=")) {
877 args.depth = strtol(arg + 8, NULL, 0);
878 continue;
880 if (!strcmp("--no-progress", arg)) {
881 args.no_progress = 1;
882 continue;
884 if (!strcmp("--stateless-rpc", arg)) {
885 args.stateless_rpc = 1;
886 continue;
888 if (!strcmp("--lock-pack", arg)) {
889 args.lock_pack = 1;
890 pack_lockfile_ptr = &pack_lockfile;
891 continue;
893 usage(fetch_pack_usage);
895 dest = (char *)arg;
896 heads = (char **)(argv + i + 1);
897 nr_heads = argc - i - 1;
898 break;
900 if (!dest)
901 usage(fetch_pack_usage);
903 if (args.stateless_rpc) {
904 conn = NULL;
905 fd[0] = 0;
906 fd[1] = 1;
907 } else {
908 conn = git_connect(fd, (char *)dest, args.uploadpack,
909 args.verbose ? CONNECT_VERBOSE : 0);
912 get_remote_heads(fd[0], &ref, 0, NULL, 0, NULL);
914 ref = fetch_pack(&args, fd, conn, ref, dest,
915 nr_heads, heads, pack_lockfile_ptr);
916 if (pack_lockfile) {
917 printf("lock %s\n", pack_lockfile);
918 fflush(stdout);
920 close(fd[0]);
921 close(fd[1]);
922 if (finish_connect(conn))
923 ref = NULL;
924 ret = !ref;
926 if (!ret && nr_heads) {
927 /* If the heads to pull were given, we should have
928 * consumed all of them by matching the remote.
929 * Otherwise, 'git fetch remote no-such-ref' would
930 * silently succeed without issuing an error.
932 for (i = 0; i < nr_heads; i++)
933 if (heads[i] && heads[i][0]) {
934 error("no such remote ref %s", heads[i]);
935 ret = 1;
938 while (ref) {
939 printf("%s %s\n",
940 sha1_to_hex(ref->old_sha1), ref->name);
941 ref = ref->next;
944 return ret;
947 struct ref *fetch_pack(struct fetch_pack_args *my_args,
948 int fd[], struct child_process *conn,
949 const struct ref *ref,
950 const char *dest,
951 int nr_heads,
952 char **heads,
953 char **pack_lockfile)
955 struct stat st;
956 struct ref *ref_cpy;
958 fetch_pack_setup();
959 if (&args != my_args)
960 memcpy(&args, my_args, sizeof(args));
961 if (args.depth > 0) {
962 if (stat(git_path("shallow"), &st))
963 st.st_mtime = 0;
966 if (heads && nr_heads)
967 nr_heads = remove_duplicates(nr_heads, heads);
968 if (!ref) {
969 packet_flush(fd[1]);
970 die("no matching remote head");
972 ref_cpy = do_fetch_pack(fd, ref, nr_heads, heads, pack_lockfile);
974 if (args.depth > 0) {
975 struct cache_time mtime;
976 struct strbuf sb = STRBUF_INIT;
977 char *shallow = git_path("shallow");
978 int fd;
980 mtime.sec = st.st_mtime;
981 mtime.nsec = ST_MTIME_NSEC(st);
982 if (stat(shallow, &st)) {
983 if (mtime.sec)
984 die("shallow file was removed during fetch");
985 } else if (st.st_mtime != mtime.sec
986 #ifdef USE_NSEC
987 || ST_MTIME_NSEC(st) != mtime.nsec
988 #endif
990 die("shallow file was changed during fetch");
992 fd = hold_lock_file_for_update(&lock, shallow,
993 LOCK_DIE_ON_ERROR);
994 if (!write_shallow_commits(&sb, 0)
995 || write_in_full(fd, sb.buf, sb.len) != sb.len) {
996 unlink_or_warn(shallow);
997 rollback_lock_file(&lock);
998 } else {
999 commit_lock_file(&lock);
1001 strbuf_release(&sb);
1004 reprepare_packed_git();
1005 return ref_cpy;