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