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