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