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