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