Change git_connect() to return a struct child_process instead of a pid_t.
[git/git.git] / builtin-fetch-pack.c
CommitLineData
def88e9a 1#include "cache.h"
fb9040cc 2#include "refs.h"
def88e9a 3#include "pkt-line.h"
49bb805e
JH
4#include "commit.h"
5#include "tag.h"
da093d37 6#include "exec_cmd.h"
9e10fd1a 7#include "pack.h"
da093d37 8#include "sideband.h"
2d4177c0 9#include "fetch-pack.h"
def88e9a 10
e28714c5
JH
11static int transfer_unpack_limit = -1;
12static int fetch_unpack_limit = -1;
af7cf268 13static int unpack_limit = 100;
fa740529
SP
14static struct fetch_pack_args args;
15
33b83034 16static const char fetch_pack_usage[] =
83a5ad61 17"git-fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--upload-pack=<git-upload-pack>] [--depth=<n>] [--no-progress] [-v] [<host>:]<directory> [<refs>...]";
27dca07f 18static const char *uploadpack = "git-upload-pack";
def88e9a 19
0a8944dd 20#define COMPLETE (1U << 0)
23d61f83
JS
21#define COMMON (1U << 1)
22#define COMMON_REF (1U << 2)
23#define SEEN (1U << 3)
24#define POPPED (1U << 4)
25
f061e5fd
JH
26/*
27 * After sending this many "have"s if we do not get any new ACK , we
28 * give up traversing our history.
29 */
30#define MAX_IN_VAIN 256
31
96f1e58f
DR
32static struct commit_list *rev_list;
33static int non_common_revs, multi_ack, use_thin_pack, use_sideband;
23d61f83
JS
34
35static void rev_list_push(struct commit *commit, int mark)
36{
37 if (!(commit->object.flags & mark)) {
38 commit->object.flags |= mark;
39
40 if (!(commit->object.parsed))
41 parse_commit(commit);
42
43 insert_by_date(commit, &rev_list);
44
45 if (!(commit->object.flags & COMMON))
46 non_common_revs++;
47 }
48}
49
8da19775 50static int rev_list_insert_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
23d61f83 51{
9534f40b 52 struct object *o = deref_tag(parse_object(sha1), path, 0);
23d61f83 53
1974632c 54 if (o && o->type == OBJ_COMMIT)
23d61f83
JS
55 rev_list_push((struct commit *)o, SEEN);
56
57 return 0;
58}
59
60/*
61 This function marks a rev and its ancestors as common.
62 In some cases, it is desirable to mark only the ancestors (for example
63 when only the server does not yet know that they are common).
64*/
65
66static void mark_common(struct commit *commit,
67 int ancestors_only, int dont_parse)
68{
69 if (commit != NULL && !(commit->object.flags & COMMON)) {
70 struct object *o = (struct object *)commit;
71
72 if (!ancestors_only)
73 o->flags |= COMMON;
74
75 if (!(o->flags & SEEN))
76 rev_list_push(commit, SEEN);
77 else {
78 struct commit_list *parents;
79
80 if (!ancestors_only && !(o->flags & POPPED))
81 non_common_revs--;
82 if (!o->parsed && !dont_parse)
83 parse_commit(commit);
84
85 for (parents = commit->parents;
86 parents;
87 parents = parents->next)
88 mark_common(parents->item, 0, dont_parse);
89 }
90 }
91}
92
93/*
94 Get the next rev to send, ignoring the common.
95*/
96
962554c6 97static const unsigned char* get_rev(void)
23d61f83
JS
98{
99 struct commit *commit = NULL;
100
101 while (commit == NULL) {
102 unsigned int mark;
103 struct commit_list* parents;
104
105 if (rev_list == NULL || non_common_revs == 0)
106 return NULL;
107
108 commit = rev_list->item;
109 if (!(commit->object.parsed))
110 parse_commit(commit);
111 commit->object.flags |= POPPED;
112 if (!(commit->object.flags & COMMON))
113 non_common_revs--;
a6080a0a 114
23d61f83
JS
115 parents = commit->parents;
116
117 if (commit->object.flags & COMMON) {
118 /* do not send "have", and ignore ancestors */
119 commit = NULL;
120 mark = COMMON | SEEN;
121 } else if (commit->object.flags & COMMON_REF)
122 /* send "have", and ignore ancestors */
123 mark = COMMON | SEEN;
124 else
125 /* send "have", also for its ancestors */
126 mark = SEEN;
127
128 while (parents) {
129 if (!(parents->item->object.flags & SEEN))
130 rev_list_push(parents->item, mark);
131 if (mark & COMMON)
132 mark_common(parents->item, 1, 0);
133 parents = parents->next;
134 }
135
136 rev_list = rev_list->next;
137 }
138
139 return commit->object.sha1;
140}
0a8944dd 141
33b83034
JH
142static int find_common(int fd[2], unsigned char *result_sha1,
143 struct ref *refs)
def88e9a 144{
2759cbc7 145 int fetching;
23d61f83
JS
146 int count = 0, flushes = 0, retval;
147 const unsigned char *sha1;
f061e5fd
JH
148 unsigned in_vain = 0;
149 int got_continue = 0;
23d61f83 150
cb5d709f 151 for_each_ref(rev_list_insert_ref, NULL);
def88e9a 152
2759cbc7
LT
153 fetching = 0;
154 for ( ; refs ; refs = refs->next) {
33b83034 155 unsigned char *remote = refs->old_sha1;
4dab94d5 156 struct object *o;
2759cbc7 157
0a8944dd 158 /*
4dab94d5
JH
159 * If that object is complete (i.e. it is an ancestor of a
160 * local ref), we tell them we have it but do not have to
161 * tell them about its ancestors, which they already know
162 * about.
f1f0a2be
JH
163 *
164 * We use lookup_object here because we are only
165 * interested in the case we *know* the object is
166 * reachable and we have already scanned it.
4dab94d5 167 */
f1f0a2be 168 if (((o = lookup_object(remote)) != NULL) &&
1baaae5e 169 (o->flags & COMPLETE)) {
2759cbc7 170 continue;
0a8944dd 171 }
23d61f83 172
583b7ea3 173 if (!fetching)
b0e90897 174 packet_write(fd[1], "want %s%s%s%s%s%s%s\n",
583b7ea3
JH
175 sha1_to_hex(remote),
176 (multi_ack ? " multi_ack" : ""),
d47f3db7
JH
177 (use_sideband == 2 ? " side-band-64k" : ""),
178 (use_sideband == 1 ? " side-band" : ""),
e4fe4b8e 179 (use_thin_pack ? " thin-pack" : ""),
fa740529 180 (args.no_progress ? " no-progress" : ""),
e4fe4b8e 181 " ofs-delta");
583b7ea3
JH
182 else
183 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
2759cbc7 184 fetching++;
33b83034 185 }
ed09aef0
JS
186 if (is_repository_shallow())
187 write_shallow_commits(fd[1], 1);
fa740529
SP
188 if (args.depth > 0)
189 packet_write(fd[1], "deepen %d", args.depth);
fb9040cc 190 packet_flush(fd[1]);
2759cbc7
LT
191 if (!fetching)
192 return 1;
0a8944dd 193
fa740529 194 if (args.depth > 0) {
016e6ccb
JS
195 char line[1024];
196 unsigned char sha1[20];
197 int len;
198
199 while ((len = packet_read_line(fd[0], line, sizeof(line)))) {
599065a3 200 if (!prefixcmp(line, "shallow ")) {
016e6ccb
JS
201 if (get_sha1_hex(line + 8, sha1))
202 die("invalid shallow line: %s", line);
016e6ccb 203 register_shallow(sha1);
cf01bd52
JH
204 continue;
205 }
599065a3 206 if (!prefixcmp(line, "unshallow ")) {
f53514bc
JS
207 if (get_sha1_hex(line + 10, sha1))
208 die("invalid unshallow line: %s", line);
209 if (!lookup_object(sha1))
210 die("object not found: %s", line);
211 /* make sure that it is parsed as shallow */
212 parse_object(sha1);
213 if (unregister_shallow(sha1))
214 die("no shallow found: %s", line);
cf01bd52
JH
215 continue;
216 }
217 die("expected shallow/unshallow, got %s", line);
016e6ccb
JS
218 }
219 }
220
23d61f83 221 flushes = 0;
75bfc6c2 222 retval = -1;
23d61f83 223 while ((sha1 = get_rev())) {
def88e9a 224 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
fa740529 225 if (args.verbose)
33b83034 226 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
f061e5fd 227 in_vain++;
def88e9a 228 if (!(31 & ++count)) {
c4c86f07
JS
229 int ack;
230
def88e9a
LT
231 packet_flush(fd[1]);
232 flushes++;
233
234 /*
235 * We keep one window "ahead" of the other side, and
236 * will wait for an ACK only on the next one
237 */
238 if (count == 32)
239 continue;
c4c86f07
JS
240
241 do {
242 ack = get_ack(fd[0], result_sha1);
fa740529 243 if (args.verbose && ack)
c4c86f07
JS
244 fprintf(stderr, "got ack %d %s\n", ack,
245 sha1_to_hex(result_sha1));
246 if (ack == 1) {
247 flushes = 0;
248 multi_ack = 0;
249 retval = 0;
250 goto done;
251 } else if (ack == 2) {
252 struct commit *commit =
253 lookup_commit(result_sha1);
254 mark_common(commit, 0, 1);
255 retval = 0;
f061e5fd
JH
256 in_vain = 0;
257 got_continue = 1;
c4c86f07
JS
258 }
259 } while (ack);
def88e9a 260 flushes--;
f061e5fd 261 if (got_continue && MAX_IN_VAIN < in_vain) {
fa740529 262 if (args.verbose)
f061e5fd
JH
263 fprintf(stderr, "giving up\n");
264 break; /* give up */
265 }
def88e9a
LT
266 }
267 }
c4c86f07 268done:
75bfc6c2 269 packet_write(fd[1], "done\n");
fa740529 270 if (args.verbose)
33b83034 271 fprintf(stderr, "done\n");
c4c86f07
JS
272 if (retval != 0) {
273 multi_ack = 0;
23d61f83 274 flushes++;
c4c86f07
JS
275 }
276 while (flushes || multi_ack) {
277 int ack = get_ack(fd[0], result_sha1);
278 if (ack) {
fa740529 279 if (args.verbose)
c4c86f07
JS
280 fprintf(stderr, "got ack (%d) %s\n", ack,
281 sha1_to_hex(result_sha1));
282 if (ack == 1)
283 return 0;
284 multi_ack = 1;
285 continue;
33b83034 286 }
c4c86f07 287 flushes--;
def88e9a 288 }
75bfc6c2 289 return retval;
def88e9a
LT
290}
291
96f1e58f 292static struct commit_list *complete;
49bb805e 293
8da19775 294static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
49bb805e
JH
295{
296 struct object *o = parse_object(sha1);
297
1974632c 298 while (o && o->type == OBJ_TAG) {
f1f0a2be
JH
299 struct tag *t = (struct tag *) o;
300 if (!t->tagged)
301 break; /* broken repository */
49bb805e 302 o->flags |= COMPLETE;
f1f0a2be 303 o = parse_object(t->tagged->sha1);
49bb805e 304 }
1974632c 305 if (o && o->type == OBJ_COMMIT) {
49bb805e
JH
306 struct commit *commit = (struct commit *)o;
307 commit->object.flags |= COMPLETE;
308 insert_by_date(commit, &complete);
309 }
310 return 0;
311}
312
313static void mark_recent_complete_commits(unsigned long cutoff)
314{
315 while (complete && cutoff <= complete->item->date) {
fa740529 316 if (args.verbose)
49bb805e
JH
317 fprintf(stderr, "Marking %s as complete\n",
318 sha1_to_hex(complete->item->object.sha1));
319 pop_most_recent_commit(&complete, COMPLETE);
320 }
321}
322
1baaae5e
JS
323static void filter_refs(struct ref **refs, int nr_match, char **match)
324{
9546010b
JH
325 struct ref **return_refs;
326 struct ref *newlist = NULL;
327 struct ref **newtail = &newlist;
328 struct ref *ref, *next;
329 struct ref *fastarray[32];
330
fa740529 331 if (nr_match && !args.fetch_all) {
9546010b
JH
332 if (ARRAY_SIZE(fastarray) < nr_match)
333 return_refs = xcalloc(nr_match, sizeof(struct ref *));
334 else {
335 return_refs = fastarray;
336 memset(return_refs, 0, sizeof(struct ref *) * nr_match);
337 }
338 }
339 else
340 return_refs = NULL;
341
342 for (ref = *refs; ref; ref = next) {
343 next = ref->next;
344 if (!memcmp(ref->name, "refs/", 5) &&
345 check_ref_format(ref->name + 5))
346 ; /* trash */
fa740529
SP
347 else if (args.fetch_all &&
348 (!args.depth || prefixcmp(ref->name, "refs/tags/") )) {
9546010b
JH
349 *newtail = ref;
350 ref->next = NULL;
351 newtail = &ref->next;
352 continue;
353 }
354 else {
355 int order = path_match(ref->name, nr_match, match);
356 if (order) {
357 return_refs[order-1] = ref;
358 continue; /* we will link it later */
359 }
360 }
361 free(ref);
362 }
363
fa740529 364 if (!args.fetch_all) {
9546010b
JH
365 int i;
366 for (i = 0; i < nr_match; i++) {
367 ref = return_refs[i];
368 if (ref) {
369 *newtail = ref;
370 ref->next = NULL;
371 newtail = &ref->next;
372 }
373 }
374 if (return_refs != fastarray)
375 free(return_refs);
1baaae5e 376 }
9546010b 377 *refs = newlist;
1baaae5e
JS
378}
379
380static int everything_local(struct ref **refs, int nr_match, char **match)
2759cbc7 381{
49bb805e 382 struct ref *ref;
2759cbc7 383 int retval;
49bb805e
JH
384 unsigned long cutoff = 0;
385
386 track_object_refs = 0;
387 save_commit_buffer = 0;
388
1baaae5e 389 for (ref = *refs; ref; ref = ref->next) {
49bb805e
JH
390 struct object *o;
391
392 o = parse_object(ref->old_sha1);
393 if (!o)
394 continue;
395
396 /* We already have it -- which may mean that we were
397 * in sync with the other side at some time after
398 * that (it is OK if we guess wrong here).
399 */
1974632c 400 if (o->type == OBJ_COMMIT) {
49bb805e
JH
401 struct commit *commit = (struct commit *)o;
402 if (!cutoff || cutoff < commit->date)
403 cutoff = commit->date;
404 }
405 }
406
fa740529 407 if (!args.depth) {
f53514bc
JS
408 for_each_ref(mark_complete, NULL);
409 if (cutoff)
410 mark_recent_complete_commits(cutoff);
411 }
2759cbc7 412
1baaae5e
JS
413 /*
414 * Mark all complete remote refs as common refs.
415 * Don't mark them common yet; the server has to be told so first.
416 */
417 for (ref = *refs; ref; ref = ref->next) {
9534f40b
JH
418 struct object *o = deref_tag(lookup_object(ref->old_sha1),
419 NULL, 0);
1baaae5e 420
1974632c 421 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
1baaae5e
JS
422 continue;
423
424 if (!(o->flags & SEEN)) {
425 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
426
427 mark_common((struct commit *)o, 1, 1);
428 }
429 }
430
431 filter_refs(refs, nr_match, match);
432
433 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
434 const unsigned char *remote = ref->old_sha1;
2759cbc7 435 unsigned char local[20];
49bb805e 436 struct object *o;
2759cbc7 437
1baaae5e 438 o = lookup_object(remote);
49bb805e 439 if (!o || !(o->flags & COMPLETE)) {
2759cbc7 440 retval = 0;
fa740529 441 if (!args.verbose)
2759cbc7
LT
442 continue;
443 fprintf(stderr,
444 "want %s (%s)\n", sha1_to_hex(remote),
1baaae5e 445 ref->name);
2759cbc7
LT
446 continue;
447 }
448
e702496e 449 hashcpy(ref->new_sha1, local);
fa740529 450 if (!args.verbose)
2759cbc7
LT
451 continue;
452 fprintf(stderr,
453 "already have %s (%s)\n", sha1_to_hex(remote),
1baaae5e 454 ref->name);
2759cbc7
LT
455 }
456 return retval;
457}
458
da093d37
NP
459static pid_t setup_sideband(int fd[2], int xd[2])
460{
461 pid_t side_pid;
462
463 if (!use_sideband) {
464 fd[0] = xd[0];
465 fd[1] = xd[1];
466 return 0;
467 }
468 /* xd[] is talking with upload-pack; subprocess reads from
469 * xd[0], spits out band#2 to stderr, and feeds us band#1
470 * through our fd[0].
471 */
472 if (pipe(fd) < 0)
473 die("fetch-pack: unable to set up pipe");
474 side_pid = fork();
475 if (side_pid < 0)
476 die("fetch-pack: unable to fork off sideband demultiplexer");
477 if (!side_pid) {
478 /* subprocess */
479 close(fd[0]);
480 if (xd[0] != xd[1])
481 close(xd[1]);
482 if (recv_sideband("fetch-pack", xd[0], fd[1], 2))
483 exit(1);
484 exit(0);
485 }
486 close(xd[0]);
487 close(fd[1]);
488 fd[1] = xd[1];
489 return side_pid;
490}
491
1788c39c 492static int get_pack(int xd[2], char **pack_lockfile)
da093d37
NP
493{
494 int status;
495 pid_t pid, side_pid;
496 int fd[2];
9e10fd1a
JH
497 const char *argv[20];
498 char keep_arg[256];
499 char hdr_arg[256];
500 const char **av;
fa740529 501 int do_keep = args.keep_pack;
1788c39c 502 int keep_pipe[2];
da093d37
NP
503
504 side_pid = setup_sideband(fd, xd);
9e10fd1a
JH
505
506 av = argv;
507 *hdr_arg = 0;
fa740529 508 if (!args.keep_pack && unpack_limit) {
9e10fd1a
JH
509 struct pack_header header;
510
511 if (read_pack_header(fd[0], &header))
512 die("protocol error: bad pack header");
513 snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u",
514 ntohl(header.hdr_version), ntohl(header.hdr_entries));
af7cf268 515 if (ntohl(header.hdr_entries) < unpack_limit)
9e10fd1a
JH
516 do_keep = 0;
517 else
518 do_keep = 1;
519 }
520
521 if (do_keep) {
1788c39c
SP
522 if (pack_lockfile && pipe(keep_pipe))
523 die("fetch-pack: pipe setup failure: %s", strerror(errno));
9e10fd1a
JH
524 *av++ = "index-pack";
525 *av++ = "--stdin";
fa740529 526 if (!args.quiet && !args.no_progress)
9e10fd1a 527 *av++ = "-v";
fa740529 528 if (args.use_thin_pack)
9e10fd1a 529 *av++ = "--fix-thin";
fa740529 530 if (args.lock_pack || unpack_limit) {
9e10fd1a
JH
531 int s = sprintf(keep_arg,
532 "--keep=fetch-pack %d on ", getpid());
533 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
534 strcpy(keep_arg + s, "localhost");
535 *av++ = keep_arg;
536 }
537 }
538 else {
539 *av++ = "unpack-objects";
fa740529 540 if (args.quiet)
9e10fd1a
JH
541 *av++ = "-q";
542 }
543 if (*hdr_arg)
544 *av++ = hdr_arg;
545 *av++ = NULL;
546
da093d37
NP
547 pid = fork();
548 if (pid < 0)
549 die("fetch-pack: unable to fork off %s", argv[0]);
550 if (!pid) {
551 dup2(fd[0], 0);
1788c39c
SP
552 if (do_keep && pack_lockfile) {
553 dup2(keep_pipe[1], 1);
554 close(keep_pipe[0]);
555 close(keep_pipe[1]);
556 }
da093d37
NP
557 close(fd[0]);
558 close(fd[1]);
559 execv_git_cmd(argv);
560 die("%s exec failed", argv[0]);
561 }
562 close(fd[0]);
563 close(fd[1]);
1788c39c
SP
564 if (do_keep && pack_lockfile) {
565 close(keep_pipe[1]);
566 *pack_lockfile = index_pack_lockfile(keep_pipe[0]);
567 close(keep_pipe[0]);
568 }
da093d37
NP
569 while (waitpid(pid, &status, 0) < 0) {
570 if (errno != EINTR)
571 die("waiting for %s: %s", argv[0], strerror(errno));
572 }
573 if (WIFEXITED(status)) {
574 int code = WEXITSTATUS(status);
575 if (code)
576 die("%s died with error code %d", argv[0], code);
577 return 0;
578 }
579 if (WIFSIGNALED(status)) {
580 int sig = WTERMSIG(status);
581 die("%s died of signal %d", argv[0], sig);
582 }
583 die("%s died of unnatural causes %d", argv[0], status);
584}
585
1788c39c
SP
586static struct ref *do_fetch_pack(int fd[2],
587 int nr_match,
588 char **match,
589 char **pack_lockfile)
def88e9a 590{
d1c133f5
LT
591 struct ref *ref;
592 unsigned char sha1[20];
def88e9a 593
1baaae5e 594 get_remote_heads(fd[0], &ref, 0, NULL, 0);
ed09aef0
JS
595 if (is_repository_shallow() && !server_supports("shallow"))
596 die("Server does not support shallow clients");
c4c86f07 597 if (server_supports("multi_ack")) {
fa740529 598 if (args.verbose)
c4c86f07
JS
599 fprintf(stderr, "Server supports multi_ack\n");
600 multi_ack = 1;
601 }
d47f3db7 602 if (server_supports("side-band-64k")) {
fa740529 603 if (args.verbose)
d47f3db7
JH
604 fprintf(stderr, "Server supports side-band-64k\n");
605 use_sideband = 2;
606 }
607 else if (server_supports("side-band")) {
fa740529 608 if (args.verbose)
583b7ea3
JH
609 fprintf(stderr, "Server supports side-band\n");
610 use_sideband = 1;
611 }
d1c133f5
LT
612 if (!ref) {
613 packet_flush(fd[1]);
614 die("no matching remote head");
615 }
1baaae5e 616 if (everything_local(&ref, nr_match, match)) {
2759cbc7
LT
617 packet_flush(fd[1]);
618 goto all_done;
619 }
33b83034 620 if (find_common(fd, sha1, ref) < 0)
fa740529 621 if (!args.keep_pack)
dfeff66e
JH
622 /* When cloning, it is not unusual to have
623 * no common commit.
624 */
625 fprintf(stderr, "warning: no common commits\n");
ad897215 626
1788c39c 627 if (get_pack(fd, pack_lockfile))
ad897215
JH
628 die("git-fetch-pack: fetch failed.");
629
630 all_done:
2d4177c0 631 return ref;
def88e9a
LT
632}
633
310b86d4
JH
634static int remove_duplicates(int nr_heads, char **heads)
635{
636 int src, dst;
637
638 for (src = dst = 0; src < nr_heads; src++) {
639 /* If heads[src] is different from any of
640 * heads[0..dst], push it in.
641 */
642 int i;
643 for (i = 0; i < dst; i++) {
644 if (!strcmp(heads[i], heads[src]))
645 break;
646 }
647 if (i < dst)
648 continue;
649 if (src != dst)
650 heads[dst] = heads[src];
651 dst++;
652 }
310b86d4
JH
653 return dst;
654}
655
af7cf268
JH
656static int fetch_pack_config(const char *var, const char *value)
657{
658 if (strcmp(var, "fetch.unpacklimit") == 0) {
e28714c5
JH
659 fetch_unpack_limit = git_config_int(var, value);
660 return 0;
661 }
662
663 if (strcmp(var, "transfer.unpacklimit") == 0) {
664 transfer_unpack_limit = git_config_int(var, value);
af7cf268
JH
665 return 0;
666 }
667
668 return git_default_config(var, value);
669}
670
54b9e022
JH
671static struct lock_file lock;
672
50ab5fd3 673static void fetch_pack_setup(void)
def88e9a 674{
50ab5fd3
SP
675 static int did_setup;
676 if (did_setup)
677 return;
af7cf268 678 git_config(fetch_pack_config);
e28714c5
JH
679 if (0 <= transfer_unpack_limit)
680 unpack_limit = transfer_unpack_limit;
681 else if (0 <= fetch_unpack_limit)
682 unpack_limit = fetch_unpack_limit;
50ab5fd3
SP
683 did_setup = 1;
684}
685
686int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
687{
688 int i, ret, nr_heads;
689 struct ref *ref;
690 char *dest = NULL, **heads;
e28714c5 691
def88e9a
LT
692 nr_heads = 0;
693 heads = NULL;
694 for (i = 1; i < argc; i++) {
2d4177c0 695 const char *arg = argv[i];
def88e9a
LT
696
697 if (*arg == '-') {
599065a3 698 if (!prefixcmp(arg, "--upload-pack=")) {
fa740529 699 args.uploadpack = arg + 14;
27dca07f
UKK
700 continue;
701 }
599065a3 702 if (!prefixcmp(arg, "--exec=")) {
fa740529 703 args.uploadpack = arg + 7;
8b3d9dc0
JH
704 continue;
705 }
2247efb4 706 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
fa740529 707 args.quiet = 1;
33b83034
JH
708 continue;
709 }
2247efb4 710 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
fa740529
SP
711 args.lock_pack = args.keep_pack;
712 args.keep_pack = 1;
9e10fd1a
JH
713 continue;
714 }
b19696c2 715 if (!strcmp("--thin", arg)) {
fa740529 716 args.use_thin_pack = 1;
b19696c2
JH
717 continue;
718 }
dfeff66e 719 if (!strcmp("--all", arg)) {
fa740529 720 args.fetch_all = 1;
dfeff66e
JH
721 continue;
722 }
33b83034 723 if (!strcmp("-v", arg)) {
fa740529 724 args.verbose = 1;
33b83034
JH
725 continue;
726 }
599065a3 727 if (!prefixcmp(arg, "--depth=")) {
fa740529 728 args.depth = strtol(arg + 8, NULL, 0);
016e6ccb
JS
729 continue;
730 }
83a5ad61 731 if (!strcmp("--no-progress", arg)) {
fa740529 732 args.no_progress = 1;
83a5ad61
JS
733 continue;
734 }
def88e9a
LT
735 usage(fetch_pack_usage);
736 }
2d4177c0
DB
737 dest = (char *)arg;
738 heads = (char **)(argv + i + 1);
def88e9a
LT
739 nr_heads = argc - i - 1;
740 break;
741 }
742 if (!dest)
743 usage(fetch_pack_usage);
2d4177c0 744
fa740529 745 ref = fetch_pack(&args, dest, nr_heads, heads, NULL);
2d4177c0
DB
746 ret = !ref;
747
748 while (ref) {
749 printf("%s %s\n",
750 sha1_to_hex(ref->old_sha1), ref->name);
751 ref = ref->next;
752 }
753
754 return ret;
755}
756
fa740529
SP
757struct ref *fetch_pack(struct fetch_pack_args *my_args,
758 const char *dest,
1788c39c
SP
759 int nr_heads,
760 char **heads,
761 char **pack_lockfile)
2d4177c0
DB
762{
763 int i, ret;
764 int fd[2];
98158e9c 765 struct child_process *conn;
2d4177c0
DB
766 struct ref *ref;
767 struct stat st;
768
50ab5fd3 769 fetch_pack_setup();
fa740529
SP
770 memcpy(&args, my_args, sizeof(args));
771 if (args.depth > 0) {
2d4177c0
DB
772 if (stat(git_path("shallow"), &st))
773 st.st_mtime = 0;
774 }
775
98158e9c 776 conn = git_connect(fd, (char *)dest, uploadpack,
fa740529 777 args.verbose ? CONNECT_VERBOSE : 0);
310b86d4
JH
778 if (heads && nr_heads)
779 nr_heads = remove_duplicates(nr_heads, heads);
1788c39c 780 ref = do_fetch_pack(fd, nr_heads, heads, pack_lockfile);
def88e9a
LT
781 close(fd[0]);
782 close(fd[1]);
98158e9c 783 ret = finish_connect(conn);
9e5d2b40
JH
784
785 if (!ret && nr_heads) {
786 /* If the heads to pull were given, we should have
787 * consumed all of them by matching the remote.
788 * Otherwise, 'git-fetch remote no-such-ref' would
789 * silently succeed without issuing an error.
790 */
791 for (i = 0; i < nr_heads; i++)
792 if (heads[i] && heads[i][0]) {
793 error("no such remote ref %s", heads[i]);
794 ret = 1;
795 }
796 }
797
fa740529 798 if (!ret && args.depth > 0) {
016e6ccb
JS
799 struct cache_time mtime;
800 char *shallow = git_path("shallow");
801 int fd;
802
803 mtime.sec = st.st_mtime;
804#ifdef USE_NSEC
805 mtime.usec = st.st_mtim.usec;
806#endif
807 if (stat(shallow, &st)) {
808 if (mtime.sec)
809 die("shallow file was removed during fetch");
810 } else if (st.st_mtime != mtime.sec
811#ifdef USE_NSEC
812 || st.st_mtim.usec != mtime.usec
813#endif
814 )
815 die("shallow file was changed during fetch");
816
817 fd = hold_lock_file_for_update(&lock, shallow, 1);
818 if (!write_shallow_commits(fd, 0)) {
d6491e3a 819 unlink(shallow);
016e6ccb
JS
820 rollback_lock_file(&lock);
821 } else {
822 close(fd);
823 commit_lock_file(&lock);
824 }
825 }
826
2d4177c0
DB
827 if (ret)
828 ref = NULL;
829
830 return ref;
def88e9a 831}