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