2 #include "repository.h"
12 #include "fetch-pack.h"
14 #include "run-command.h"
16 #include "transport.h"
18 #include "prio-queue.h"
19 #include "sha1-array.h"
23 static int transfer_unpack_limit
= -1;
24 static int fetch_unpack_limit
= -1;
25 static int unpack_limit
= 100;
26 static int prefer_ofs_delta
= 1;
28 static int deepen_since_ok
;
29 static int deepen_not_ok
;
30 static int fetch_fsck_objects
= -1;
31 static int transfer_fsck_objects
= -1;
32 static int agent_supported
;
33 static int server_supports_filtering
;
34 static struct lock_file shallow_lock
;
35 static const char *alternate_shallow_file
;
37 /* Remember to update object flag allocation in object.h */
38 #define COMPLETE (1U << 0)
39 #define COMMON (1U << 1)
40 #define COMMON_REF (1U << 2)
41 #define SEEN (1U << 3)
42 #define POPPED (1U << 4)
43 #define ALTERNATE (1U << 5)
48 * After sending this many "have"s if we do not get any new ACK , we
49 * give up traversing our history.
51 #define MAX_IN_VAIN 256
53 static struct prio_queue rev_list
= { compare_commits_by_commit_date
};
54 static int non_common_revs
, multi_ack
, use_sideband
;
55 /* Allow specifying sha1 if it is a ref tip. */
56 #define ALLOW_TIP_SHA1 01
57 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
58 #define ALLOW_REACHABLE_SHA1 02
59 static unsigned int allow_unadvertised_object_request
;
61 __attribute__((format (printf
, 2, 3)))
62 static inline void print_verbose(const struct fetch_pack_args
*args
,
70 va_start(params
, fmt
);
71 vfprintf(stderr
, fmt
, params
);
76 struct alternate_object_cache
{
77 struct object
**items
;
81 static void cache_one_alternate(const char *refname
,
82 const struct object_id
*oid
,
85 struct alternate_object_cache
*cache
= vcache
;
86 struct object
*obj
= parse_object(oid
);
88 if (!obj
|| (obj
->flags
& ALTERNATE
))
91 obj
->flags
|= ALTERNATE
;
92 ALLOC_GROW(cache
->items
, cache
->nr
+ 1, cache
->alloc
);
93 cache
->items
[cache
->nr
++] = obj
;
96 static void for_each_cached_alternate(void (*cb
)(struct object
*))
98 static int initialized
;
99 static struct alternate_object_cache cache
;
103 for_each_alternate_ref(cache_one_alternate
, &cache
);
107 for (i
= 0; i
< cache
.nr
; i
++)
111 static void rev_list_push(struct commit
*commit
, int mark
)
113 if (!(commit
->object
.flags
& mark
)) {
114 commit
->object
.flags
|= mark
;
116 if (parse_commit(commit
))
119 prio_queue_put(&rev_list
, commit
);
121 if (!(commit
->object
.flags
& COMMON
))
126 static int rev_list_insert_ref(const char *refname
, const struct object_id
*oid
)
128 struct object
*o
= deref_tag(parse_object(oid
), refname
, 0);
130 if (o
&& o
->type
== OBJ_COMMIT
)
131 rev_list_push((struct commit
*)o
, SEEN
);
136 static int rev_list_insert_ref_oid(const char *refname
, const struct object_id
*oid
,
137 int flag
, void *cb_data
)
139 return rev_list_insert_ref(refname
, oid
);
142 static int clear_marks(const char *refname
, const struct object_id
*oid
,
143 int flag
, void *cb_data
)
145 struct object
*o
= deref_tag(parse_object(oid
), refname
, 0);
147 if (o
&& o
->type
== OBJ_COMMIT
)
148 clear_commit_marks((struct commit
*)o
,
149 COMMON
| COMMON_REF
| SEEN
| POPPED
);
154 This function marks a rev and its ancestors as common.
155 In some cases, it is desirable to mark only the ancestors (for example
156 when only the server does not yet know that they are common).
159 static void mark_common(struct commit
*commit
,
160 int ancestors_only
, int dont_parse
)
162 if (commit
!= NULL
&& !(commit
->object
.flags
& COMMON
)) {
163 struct object
*o
= (struct object
*)commit
;
168 if (!(o
->flags
& SEEN
))
169 rev_list_push(commit
, SEEN
);
171 struct commit_list
*parents
;
173 if (!ancestors_only
&& !(o
->flags
& POPPED
))
175 if (!o
->parsed
&& !dont_parse
)
176 if (parse_commit(commit
))
179 for (parents
= commit
->parents
;
181 parents
= parents
->next
)
182 mark_common(parents
->item
, 0, dont_parse
);
188 Get the next rev to send, ignoring the common.
191 static const struct object_id
*get_rev(void)
193 struct commit
*commit
= NULL
;
195 while (commit
== NULL
) {
197 struct commit_list
*parents
;
199 if (rev_list
.nr
== 0 || non_common_revs
== 0)
202 commit
= prio_queue_get(&rev_list
);
203 parse_commit(commit
);
204 parents
= commit
->parents
;
206 commit
->object
.flags
|= POPPED
;
207 if (!(commit
->object
.flags
& COMMON
))
210 if (commit
->object
.flags
& COMMON
) {
211 /* do not send "have", and ignore ancestors */
213 mark
= COMMON
| SEEN
;
214 } else if (commit
->object
.flags
& COMMON_REF
)
215 /* send "have", and ignore ancestors */
216 mark
= COMMON
| SEEN
;
218 /* send "have", also for its ancestors */
222 if (!(parents
->item
->object
.flags
& SEEN
))
223 rev_list_push(parents
->item
, mark
);
225 mark_common(parents
->item
, 1, 0);
226 parents
= parents
->next
;
230 return &commit
->object
.oid
;
241 static void consume_shallow_list(struct fetch_pack_args
*args
, int fd
)
243 if (args
->stateless_rpc
&& args
->deepen
) {
244 /* If we sent a depth we will get back "duplicate"
245 * shallow and unshallow commands every time there
246 * is a block of have lines exchanged.
249 while ((line
= packet_read_line(fd
, NULL
))) {
250 if (starts_with(line
, "shallow "))
252 if (starts_with(line
, "unshallow "))
254 die(_("git fetch-pack: expected shallow list"));
259 static enum ack_type
get_ack(int fd
, struct object_id
*result_oid
)
262 char *line
= packet_read_line(fd
, &len
);
266 die(_("git fetch-pack: expected ACK/NAK, got EOF"));
267 if (!strcmp(line
, "NAK"))
269 if (skip_prefix(line
, "ACK ", &arg
)) {
270 if (!get_oid_hex(arg
, result_oid
)) {
275 if (strstr(arg
, "continue"))
277 if (strstr(arg
, "common"))
279 if (strstr(arg
, "ready"))
284 if (skip_prefix(line
, "ERR ", &arg
))
285 die(_("remote error: %s"), arg
);
286 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line
);
289 static void send_request(struct fetch_pack_args
*args
,
290 int fd
, struct strbuf
*buf
)
292 if (args
->stateless_rpc
) {
293 send_sideband(fd
, -1, buf
->buf
, buf
->len
, LARGE_PACKET_MAX
);
296 write_or_die(fd
, buf
->buf
, buf
->len
);
299 static void insert_one_alternate_object(struct object
*obj
)
301 rev_list_insert_ref(NULL
, &obj
->oid
);
304 #define INITIAL_FLUSH 16
305 #define PIPESAFE_FLUSH 32
306 #define LARGE_FLUSH 16384
308 static int next_flush(struct fetch_pack_args
*args
, int count
)
310 if (args
->stateless_rpc
) {
311 if (count
< LARGE_FLUSH
)
314 count
= count
* 11 / 10;
316 if (count
< PIPESAFE_FLUSH
)
319 count
+= PIPESAFE_FLUSH
;
324 static int find_common(struct fetch_pack_args
*args
,
325 int fd
[2], struct object_id
*result_oid
,
329 int count
= 0, flushes
= 0, flush_at
= INITIAL_FLUSH
, retval
;
330 const struct object_id
*oid
;
331 unsigned in_vain
= 0;
332 int got_continue
= 0;
334 struct strbuf req_buf
= STRBUF_INIT
;
335 size_t state_len
= 0;
337 if (args
->stateless_rpc
&& multi_ack
== 1)
338 die(_("--stateless-rpc requires multi_ack_detailed"));
340 for_each_ref(clear_marks
, NULL
);
343 for_each_ref(rev_list_insert_ref_oid
, NULL
);
344 for_each_cached_alternate(insert_one_alternate_object
);
347 for ( ; refs
; refs
= refs
->next
) {
348 struct object_id
*remote
= &refs
->old_oid
;
349 const char *remote_hex
;
353 * If that object is complete (i.e. it is an ancestor of a
354 * local ref), we tell them we have it but do not have to
355 * tell them about its ancestors, which they already know
358 * We use lookup_object here because we are only
359 * interested in the case we *know* the object is
360 * reachable and we have already scanned it.
362 if (((o
= lookup_object(remote
->hash
)) != NULL
) &&
363 (o
->flags
& COMPLETE
)) {
367 remote_hex
= oid_to_hex(remote
);
369 struct strbuf c
= STRBUF_INIT
;
370 if (multi_ack
== 2) strbuf_addstr(&c
, " multi_ack_detailed");
371 if (multi_ack
== 1) strbuf_addstr(&c
, " multi_ack");
372 if (no_done
) strbuf_addstr(&c
, " no-done");
373 if (use_sideband
== 2) strbuf_addstr(&c
, " side-band-64k");
374 if (use_sideband
== 1) strbuf_addstr(&c
, " side-band");
375 if (args
->deepen_relative
) strbuf_addstr(&c
, " deepen-relative");
376 if (args
->use_thin_pack
) strbuf_addstr(&c
, " thin-pack");
377 if (args
->no_progress
) strbuf_addstr(&c
, " no-progress");
378 if (args
->include_tag
) strbuf_addstr(&c
, " include-tag");
379 if (prefer_ofs_delta
) strbuf_addstr(&c
, " ofs-delta");
380 if (deepen_since_ok
) strbuf_addstr(&c
, " deepen-since");
381 if (deepen_not_ok
) strbuf_addstr(&c
, " deepen-not");
382 if (agent_supported
) strbuf_addf(&c
, " agent=%s",
383 git_user_agent_sanitized());
384 if (args
->filter_options
.choice
)
385 strbuf_addstr(&c
, " filter");
386 packet_buf_write(&req_buf
, "want %s%s\n", remote_hex
, c
.buf
);
389 packet_buf_write(&req_buf
, "want %s\n", remote_hex
);
394 strbuf_release(&req_buf
);
399 if (is_repository_shallow())
400 write_shallow_commits(&req_buf
, 1, NULL
);
402 packet_buf_write(&req_buf
, "deepen %d", args
->depth
);
403 if (args
->deepen_since
) {
404 timestamp_t max_age
= approxidate(args
->deepen_since
);
405 packet_buf_write(&req_buf
, "deepen-since %"PRItime
, max_age
);
407 if (args
->deepen_not
) {
409 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
410 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
411 packet_buf_write(&req_buf
, "deepen-not %s", s
->string
);
414 if (server_supports_filtering
&& args
->filter_options
.choice
)
415 packet_buf_write(&req_buf
, "filter %s",
416 args
->filter_options
.filter_spec
);
417 packet_buf_flush(&req_buf
);
418 state_len
= req_buf
.len
;
423 struct object_id oid
;
425 send_request(args
, fd
[1], &req_buf
);
426 while ((line
= packet_read_line(fd
[0], NULL
))) {
427 if (skip_prefix(line
, "shallow ", &arg
)) {
428 if (get_oid_hex(arg
, &oid
))
429 die(_("invalid shallow line: %s"), line
);
430 register_shallow(&oid
);
433 if (skip_prefix(line
, "unshallow ", &arg
)) {
434 if (get_oid_hex(arg
, &oid
))
435 die(_("invalid unshallow line: %s"), line
);
436 if (!lookup_object(oid
.hash
))
437 die(_("object not found: %s"), line
);
438 /* make sure that it is parsed as shallow */
439 if (!parse_object(&oid
))
440 die(_("error in object: %s"), line
);
441 if (unregister_shallow(&oid
))
442 die(_("no shallow found: %s"), line
);
445 die(_("expected shallow/unshallow, got %s"), line
);
447 } else if (!args
->stateless_rpc
)
448 send_request(args
, fd
[1], &req_buf
);
450 if (!args
->stateless_rpc
) {
451 /* If we aren't using the stateless-rpc interface
452 * we don't need to retain the headers.
454 strbuf_setlen(&req_buf
, 0);
460 if (args
->no_dependents
)
462 while ((oid
= get_rev())) {
463 packet_buf_write(&req_buf
, "have %s\n", oid_to_hex(oid
));
464 print_verbose(args
, "have %s", oid_to_hex(oid
));
466 if (flush_at
<= ++count
) {
469 packet_buf_flush(&req_buf
);
470 send_request(args
, fd
[1], &req_buf
);
471 strbuf_setlen(&req_buf
, state_len
);
473 flush_at
= next_flush(args
, count
);
476 * We keep one window "ahead" of the other side, and
477 * will wait for an ACK only on the next one
479 if (!args
->stateless_rpc
&& count
== INITIAL_FLUSH
)
482 consume_shallow_list(args
, fd
[0]);
484 ack
= get_ack(fd
[0], result_oid
);
486 print_verbose(args
, _("got %s %d %s"), "ack",
487 ack
, oid_to_hex(result_oid
));
497 struct commit
*commit
=
498 lookup_commit(result_oid
);
500 die(_("invalid commit %s"), oid_to_hex(result_oid
));
501 if (args
->stateless_rpc
503 && !(commit
->object
.flags
& COMMON
)) {
504 /* We need to replay the have for this object
505 * on the next RPC request so the peer knows
506 * it is in common with us.
508 const char *hex
= oid_to_hex(result_oid
);
509 packet_buf_write(&req_buf
, "have %s\n", hex
);
510 state_len
= req_buf
.len
;
512 * Reset in_vain because an ack
513 * for this commit has not been
517 } else if (!args
->stateless_rpc
518 || ack
!= ACK_common
)
520 mark_common(commit
, 0, 1);
523 if (ack
== ACK_ready
) {
524 clear_prio_queue(&rev_list
);
532 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
533 print_verbose(args
, _("giving up"));
539 if (!got_ready
|| !no_done
) {
540 packet_buf_write(&req_buf
, "done\n");
541 send_request(args
, fd
[1], &req_buf
);
543 print_verbose(args
, _("done"));
548 strbuf_release(&req_buf
);
550 if (!got_ready
|| !no_done
)
551 consume_shallow_list(args
, fd
[0]);
552 while (flushes
|| multi_ack
) {
553 int ack
= get_ack(fd
[0], result_oid
);
555 print_verbose(args
, _("got %s (%d) %s"), "ack",
556 ack
, oid_to_hex(result_oid
));
564 /* it is no error to fetch into a completely empty repo */
565 return count
? retval
: 0;
568 static struct commit_list
*complete
;
570 static int mark_complete(const struct object_id
*oid
)
572 struct object
*o
= parse_object(oid
);
574 while (o
&& o
->type
== OBJ_TAG
) {
575 struct tag
*t
= (struct tag
*) o
;
577 break; /* broken repository */
578 o
->flags
|= COMPLETE
;
579 o
= parse_object(&t
->tagged
->oid
);
581 if (o
&& o
->type
== OBJ_COMMIT
) {
582 struct commit
*commit
= (struct commit
*)o
;
583 if (!(commit
->object
.flags
& COMPLETE
)) {
584 commit
->object
.flags
|= COMPLETE
;
585 commit_list_insert(commit
, &complete
);
591 static int mark_complete_oid(const char *refname
, const struct object_id
*oid
,
592 int flag
, void *cb_data
)
594 return mark_complete(oid
);
597 static void mark_recent_complete_commits(struct fetch_pack_args
*args
,
600 while (complete
&& cutoff
<= complete
->item
->date
) {
601 print_verbose(args
, _("Marking %s as complete"),
602 oid_to_hex(&complete
->item
->object
.oid
));
603 pop_most_recent_commit(&complete
, COMPLETE
);
607 static void add_refs_to_oidset(struct oidset
*oids
, struct ref
*refs
)
609 for (; refs
; refs
= refs
->next
)
610 oidset_insert(oids
, &refs
->old_oid
);
613 static int tip_oids_contain(struct oidset
*tip_oids
,
614 struct ref
*unmatched
, struct ref
*newlist
,
615 const struct object_id
*id
)
618 * Note that this only looks at the ref lists the first time it's
619 * called. This works out in filter_refs() because even though it may
620 * add to "newlist" between calls, the additions will always be for
621 * oids that are already in the set.
623 if (!tip_oids
->map
.map
.tablesize
) {
624 add_refs_to_oidset(tip_oids
, unmatched
);
625 add_refs_to_oidset(tip_oids
, newlist
);
627 return oidset_contains(tip_oids
, id
);
630 static void filter_refs(struct fetch_pack_args
*args
,
632 struct ref
**sought
, int nr_sought
)
634 struct ref
*newlist
= NULL
;
635 struct ref
**newtail
= &newlist
;
636 struct ref
*unmatched
= NULL
;
637 struct ref
*ref
, *next
;
638 struct oidset tip_oids
= OIDSET_INIT
;
642 for (ref
= *refs
; ref
; ref
= next
) {
646 if (starts_with(ref
->name
, "refs/") &&
647 check_refname_format(ref
->name
, 0))
650 while (i
< nr_sought
) {
651 int cmp
= strcmp(ref
->name
, sought
[i
]->name
);
653 break; /* definitely do not have it */
655 keep
= 1; /* definitely have it */
656 sought
[i
]->match_status
= REF_MATCHED
;
662 if (!keep
&& args
->fetch_all
&&
663 (!args
->deepen
|| !starts_with(ref
->name
, "refs/tags/")))
669 newtail
= &ref
->next
;
671 ref
->next
= unmatched
;
676 /* Append unmatched requests to the list */
677 for (i
= 0; i
< nr_sought
; i
++) {
678 struct object_id oid
;
682 if (ref
->match_status
!= REF_NOT_MATCHED
)
684 if (parse_oid_hex(ref
->name
, &oid
, &p
) ||
686 oidcmp(&oid
, &ref
->old_oid
))
689 if ((allow_unadvertised_object_request
&
690 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
)) ||
691 tip_oids_contain(&tip_oids
, unmatched
, newlist
,
693 ref
->match_status
= REF_MATCHED
;
694 *newtail
= copy_ref(ref
);
695 newtail
= &(*newtail
)->next
;
697 ref
->match_status
= REF_UNADVERTISED_NOT_ALLOWED
;
701 oidset_clear(&tip_oids
);
702 for (ref
= unmatched
; ref
; ref
= next
) {
710 static void mark_alternate_complete(struct object
*obj
)
712 mark_complete(&obj
->oid
);
715 static int everything_local(struct fetch_pack_args
*args
,
717 struct ref
**sought
, int nr_sought
)
721 int old_save_commit_buffer
= save_commit_buffer
;
722 timestamp_t cutoff
= 0;
724 save_commit_buffer
= 0;
726 for (ref
= *refs
; ref
; ref
= ref
->next
) {
729 if (!has_object_file_with_flags(&ref
->old_oid
,
733 o
= parse_object(&ref
->old_oid
);
737 /* We already have it -- which may mean that we were
738 * in sync with the other side at some time after
739 * that (it is OK if we guess wrong here).
741 if (o
->type
== OBJ_COMMIT
) {
742 struct commit
*commit
= (struct commit
*)o
;
743 if (!cutoff
|| cutoff
< commit
->date
)
744 cutoff
= commit
->date
;
748 if (!args
->no_dependents
) {
750 for_each_ref(mark_complete_oid
, NULL
);
751 for_each_cached_alternate(mark_alternate_complete
);
752 commit_list_sort_by_date(&complete
);
754 mark_recent_complete_commits(args
, cutoff
);
758 * Mark all complete remote refs as common refs.
759 * Don't mark them common yet; the server has to be told so first.
761 for (ref
= *refs
; ref
; ref
= ref
->next
) {
762 struct object
*o
= deref_tag(lookup_object(ref
->old_oid
.hash
),
765 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
768 if (!(o
->flags
& SEEN
)) {
769 rev_list_push((struct commit
*)o
, COMMON_REF
| SEEN
);
771 mark_common((struct commit
*)o
, 1, 1);
776 filter_refs(args
, refs
, sought
, nr_sought
);
778 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
779 const struct object_id
*remote
= &ref
->old_oid
;
782 o
= lookup_object(remote
->hash
);
783 if (!o
|| !(o
->flags
& COMPLETE
)) {
785 print_verbose(args
, "want %s (%s)", oid_to_hex(remote
),
789 print_verbose(args
, _("already have %s (%s)"), oid_to_hex(remote
),
793 save_commit_buffer
= old_save_commit_buffer
;
798 static int sideband_demux(int in
, int out
, void *data
)
803 ret
= recv_sideband("fetch-pack", xd
[0], out
);
808 static int get_pack(struct fetch_pack_args
*args
,
809 int xd
[2], char **pack_lockfile
)
812 int do_keep
= args
->keep_pack
;
813 const char *cmd_name
;
814 struct pack_header header
;
816 struct child_process cmd
= CHILD_PROCESS_INIT
;
819 memset(&demux
, 0, sizeof(demux
));
821 /* xd[] is talking with upload-pack; subprocess reads from
822 * xd[0], spits out band#2 to stderr, and feeds us band#1
823 * through demux->out.
825 demux
.proc
= sideband_demux
;
828 demux
.isolate_sigpipe
= 1;
829 if (start_async(&demux
))
830 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
835 if (!args
->keep_pack
&& unpack_limit
) {
837 if (read_pack_header(demux
.out
, &header
))
838 die(_("protocol error: bad pack header"));
840 if (ntohl(header
.hdr_entries
) < unpack_limit
)
846 if (alternate_shallow_file
) {
847 argv_array_push(&cmd
.args
, "--shallow-file");
848 argv_array_push(&cmd
.args
, alternate_shallow_file
);
851 if (do_keep
|| args
->from_promisor
) {
854 cmd_name
= "index-pack";
855 argv_array_push(&cmd
.args
, cmd_name
);
856 argv_array_push(&cmd
.args
, "--stdin");
857 if (!args
->quiet
&& !args
->no_progress
)
858 argv_array_push(&cmd
.args
, "-v");
859 if (args
->use_thin_pack
)
860 argv_array_push(&cmd
.args
, "--fix-thin");
861 if (do_keep
&& (args
->lock_pack
|| unpack_limit
)) {
862 char hostname
[HOST_NAME_MAX
+ 1];
863 if (xgethostname(hostname
, sizeof(hostname
)))
864 xsnprintf(hostname
, sizeof(hostname
), "localhost");
865 argv_array_pushf(&cmd
.args
,
866 "--keep=fetch-pack %"PRIuMAX
" on %s",
867 (uintmax_t)getpid(), hostname
);
869 if (args
->check_self_contained_and_connected
)
870 argv_array_push(&cmd
.args
, "--check-self-contained-and-connected");
871 if (args
->from_promisor
)
872 argv_array_push(&cmd
.args
, "--promisor");
875 cmd_name
= "unpack-objects";
876 argv_array_push(&cmd
.args
, cmd_name
);
877 if (args
->quiet
|| args
->no_progress
)
878 argv_array_push(&cmd
.args
, "-q");
879 args
->check_self_contained_and_connected
= 0;
883 argv_array_pushf(&cmd
.args
, "--pack_header=%"PRIu32
",%"PRIu32
,
884 ntohl(header
.hdr_version
),
885 ntohl(header
.hdr_entries
));
886 if (fetch_fsck_objects
>= 0
888 : transfer_fsck_objects
>= 0
889 ? transfer_fsck_objects
891 argv_array_push(&cmd
.args
, "--strict");
895 if (start_command(&cmd
))
896 die(_("fetch-pack: unable to fork off %s"), cmd_name
);
897 if (do_keep
&& pack_lockfile
) {
898 *pack_lockfile
= index_pack_lockfile(cmd
.out
);
903 /* Closed by start_command() */
906 ret
= finish_command(&cmd
);
907 if (!ret
|| (args
->check_self_contained_and_connected
&& ret
== 1))
908 args
->self_contained_and_connected
=
909 args
->check_self_contained_and_connected
&&
912 die(_("%s failed"), cmd_name
);
913 if (use_sideband
&& finish_async(&demux
))
914 die(_("error in sideband demultiplexer"));
918 static int cmp_ref_by_name(const void *a_
, const void *b_
)
920 const struct ref
*a
= *((const struct ref
**)a_
);
921 const struct ref
*b
= *((const struct ref
**)b_
);
922 return strcmp(a
->name
, b
->name
);
925 static struct ref
*do_fetch_pack(struct fetch_pack_args
*args
,
927 const struct ref
*orig_ref
,
928 struct ref
**sought
, int nr_sought
,
929 struct shallow_info
*si
,
930 char **pack_lockfile
)
932 struct ref
*ref
= copy_ref_list(orig_ref
);
933 struct object_id oid
;
934 const char *agent_feature
;
937 sort_ref_list(&ref
, ref_compare_name
);
938 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
940 if ((args
->depth
> 0 || is_repository_shallow()) && !server_supports("shallow"))
941 die(_("Server does not support shallow clients"));
942 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
944 if (server_supports("multi_ack_detailed")) {
945 print_verbose(args
, _("Server supports multi_ack_detailed"));
947 if (server_supports("no-done")) {
948 print_verbose(args
, _("Server supports no-done"));
949 if (args
->stateless_rpc
)
953 else if (server_supports("multi_ack")) {
954 print_verbose(args
, _("Server supports multi_ack"));
957 if (server_supports("side-band-64k")) {
958 print_verbose(args
, _("Server supports side-band-64k"));
961 else if (server_supports("side-band")) {
962 print_verbose(args
, _("Server supports side-band"));
965 if (server_supports("allow-tip-sha1-in-want")) {
966 print_verbose(args
, _("Server supports allow-tip-sha1-in-want"));
967 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
969 if (server_supports("allow-reachable-sha1-in-want")) {
970 print_verbose(args
, _("Server supports allow-reachable-sha1-in-want"));
971 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
973 if (!server_supports("thin-pack"))
974 args
->use_thin_pack
= 0;
975 if (!server_supports("no-progress"))
976 args
->no_progress
= 0;
977 if (!server_supports("include-tag"))
978 args
->include_tag
= 0;
979 if (server_supports("ofs-delta"))
980 print_verbose(args
, _("Server supports ofs-delta"));
982 prefer_ofs_delta
= 0;
984 if (server_supports("filter")) {
985 server_supports_filtering
= 1;
986 print_verbose(args
, _("Server supports filter"));
987 } else if (args
->filter_options
.choice
) {
988 warning("filtering not recognized by server, ignoring");
991 if ((agent_feature
= server_feature_value("agent", &agent_len
))) {
994 print_verbose(args
, _("Server version is %.*s"),
995 agent_len
, agent_feature
);
997 if (server_supports("deepen-since"))
999 else if (args
->deepen_since
)
1000 die(_("Server does not support --shallow-since"));
1001 if (server_supports("deepen-not"))
1003 else if (args
->deepen_not
)
1004 die(_("Server does not support --shallow-exclude"));
1005 if (!server_supports("deepen-relative") && args
->deepen_relative
)
1006 die(_("Server does not support --deepen"));
1008 if (everything_local(args
, &ref
, sought
, nr_sought
)) {
1009 packet_flush(fd
[1]);
1012 if (find_common(args
, fd
, &oid
, ref
) < 0)
1013 if (!args
->keep_pack
)
1014 /* When cloning, it is not unusual to have
1017 warning(_("no common commits"));
1019 if (args
->stateless_rpc
)
1020 packet_flush(fd
[1]);
1022 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
1024 else if (si
->nr_ours
|| si
->nr_theirs
)
1025 alternate_shallow_file
= setup_temporary_shallow(si
->shallow
);
1027 alternate_shallow_file
= NULL
;
1028 if (get_pack(args
, fd
, pack_lockfile
))
1029 die(_("git fetch-pack: fetch failed."));
1035 static void fetch_pack_config(void)
1037 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit
);
1038 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit
);
1039 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta
);
1040 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects
);
1041 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects
);
1043 git_config(git_default_config
, NULL
);
1046 static void fetch_pack_setup(void)
1048 static int did_setup
;
1051 fetch_pack_config();
1052 if (0 <= transfer_unpack_limit
)
1053 unpack_limit
= transfer_unpack_limit
;
1054 else if (0 <= fetch_unpack_limit
)
1055 unpack_limit
= fetch_unpack_limit
;
1059 static int remove_duplicates_in_refs(struct ref
**ref
, int nr
)
1061 struct string_list names
= STRING_LIST_INIT_NODUP
;
1064 for (src
= dst
= 0; src
< nr
; src
++) {
1065 struct string_list_item
*item
;
1066 item
= string_list_insert(&names
, ref
[src
]->name
);
1068 continue; /* already have it */
1069 item
->util
= ref
[src
];
1071 ref
[dst
] = ref
[src
];
1074 for (src
= dst
; src
< nr
; src
++)
1076 string_list_clear(&names
, 0);
1080 static void update_shallow(struct fetch_pack_args
*args
,
1081 struct ref
**sought
, int nr_sought
,
1082 struct shallow_info
*si
)
1084 struct oid_array ref
= OID_ARRAY_INIT
;
1088 if (args
->deepen
&& alternate_shallow_file
) {
1089 if (*alternate_shallow_file
== '\0') { /* --unshallow */
1090 unlink_or_warn(git_path_shallow());
1091 rollback_lock_file(&shallow_lock
);
1093 commit_lock_file(&shallow_lock
);
1097 if (!si
->shallow
|| !si
->shallow
->nr
)
1100 if (args
->cloning
) {
1102 * remote is shallow, but this is a clone, there are
1103 * no objects in repo to worry about. Accept any
1104 * shallow points that exist in the pack (iow in repo
1105 * after get_pack() and reprepare_packed_git())
1107 struct oid_array extra
= OID_ARRAY_INIT
;
1108 struct object_id
*oid
= si
->shallow
->oid
;
1109 for (i
= 0; i
< si
->shallow
->nr
; i
++)
1110 if (has_object_file(&oid
[i
]))
1111 oid_array_append(&extra
, &oid
[i
]);
1113 setup_alternate_shallow(&shallow_lock
,
1114 &alternate_shallow_file
,
1116 commit_lock_file(&shallow_lock
);
1118 oid_array_clear(&extra
);
1122 if (!si
->nr_ours
&& !si
->nr_theirs
)
1125 remove_nonexistent_theirs_shallow(si
);
1126 if (!si
->nr_ours
&& !si
->nr_theirs
)
1128 for (i
= 0; i
< nr_sought
; i
++)
1129 oid_array_append(&ref
, &sought
[i
]->old_oid
);
1132 if (args
->update_shallow
) {
1134 * remote is also shallow, .git/shallow may be updated
1135 * so all refs can be accepted. Make sure we only add
1136 * shallow roots that are actually reachable from new
1139 struct oid_array extra
= OID_ARRAY_INIT
;
1140 struct object_id
*oid
= si
->shallow
->oid
;
1141 assign_shallow_commits_to_refs(si
, NULL
, NULL
);
1142 if (!si
->nr_ours
&& !si
->nr_theirs
) {
1143 oid_array_clear(&ref
);
1146 for (i
= 0; i
< si
->nr_ours
; i
++)
1147 oid_array_append(&extra
, &oid
[si
->ours
[i
]]);
1148 for (i
= 0; i
< si
->nr_theirs
; i
++)
1149 oid_array_append(&extra
, &oid
[si
->theirs
[i
]]);
1150 setup_alternate_shallow(&shallow_lock
,
1151 &alternate_shallow_file
,
1153 commit_lock_file(&shallow_lock
);
1154 oid_array_clear(&extra
);
1155 oid_array_clear(&ref
);
1160 * remote is also shallow, check what ref is safe to update
1161 * without updating .git/shallow
1163 status
= xcalloc(nr_sought
, sizeof(*status
));
1164 assign_shallow_commits_to_refs(si
, NULL
, status
);
1165 if (si
->nr_ours
|| si
->nr_theirs
) {
1166 for (i
= 0; i
< nr_sought
; i
++)
1168 sought
[i
]->status
= REF_STATUS_REJECT_SHALLOW
;
1171 oid_array_clear(&ref
);
1174 struct ref
*fetch_pack(struct fetch_pack_args
*args
,
1175 int fd
[], struct child_process
*conn
,
1176 const struct ref
*ref
,
1178 struct ref
**sought
, int nr_sought
,
1179 struct oid_array
*shallow
,
1180 char **pack_lockfile
)
1182 struct ref
*ref_cpy
;
1183 struct shallow_info si
;
1187 nr_sought
= remove_duplicates_in_refs(sought
, nr_sought
);
1190 packet_flush(fd
[1]);
1191 die(_("no matching remote head"));
1193 prepare_shallow_info(&si
, shallow
);
1194 ref_cpy
= do_fetch_pack(args
, fd
, ref
, sought
, nr_sought
,
1195 &si
, pack_lockfile
);
1196 reprepare_packed_git(the_repository
);
1197 update_shallow(args
, sought
, nr_sought
, &si
);
1198 clear_shallow_info(&si
);
1202 int report_unmatched_refs(struct ref
**sought
, int nr_sought
)
1206 for (i
= 0; i
< nr_sought
; i
++) {
1209 switch (sought
[i
]->match_status
) {
1212 case REF_NOT_MATCHED
:
1213 error(_("no such remote ref %s"), sought
[i
]->name
);
1215 case REF_UNADVERTISED_NOT_ALLOWED
:
1216 error(_("Server does not allow request for unadvertised object %s"),