1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "repository.h"
7 #include "environment.h"
17 #include "fetch-pack.h"
19 #include "run-command.h"
23 #include "oid-array.h"
26 #include "object-store-ll.h"
28 #include "connected.h"
29 #include "fetch-negotiator.h"
32 #include "commit-reach.h"
33 #include "commit-graph.h"
35 #include "mergesort.h"
37 static int transfer_unpack_limit
= -1;
38 static int fetch_unpack_limit
= -1;
39 static int unpack_limit
= 100;
40 static int prefer_ofs_delta
= 1;
42 static int deepen_since_ok
;
43 static int deepen_not_ok
;
44 static int fetch_fsck_objects
= -1;
45 static int transfer_fsck_objects
= -1;
46 static int agent_supported
;
47 static int server_supports_filtering
;
48 static int advertise_sid
;
49 static struct shallow_lock shallow_lock
;
50 static const char *alternate_shallow_file
;
51 static struct fsck_options fsck_options
= FSCK_OPTIONS_MISSING_GITMODULES
;
52 static struct strbuf fsck_msg_types
= STRBUF_INIT
;
53 static struct string_list uri_protocols
= STRING_LIST_INIT_DUP
;
55 /* Remember to update object flag allocation in object.h */
56 #define COMPLETE (1U << 0)
57 #define ALTERNATE (1U << 1)
58 #define COMMON (1U << 6)
59 #define REACH_SCRATCH (1U << 7)
62 * After sending this many "have"s if we do not get any new ACK , we
63 * give up traversing our history.
65 #define MAX_IN_VAIN 256
67 static int multi_ack
, use_sideband
;
68 /* Allow specifying sha1 if it is a ref tip. */
69 #define ALLOW_TIP_SHA1 01
70 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
71 #define ALLOW_REACHABLE_SHA1 02
72 static unsigned int allow_unadvertised_object_request
;
74 __attribute__((format (printf
, 2, 3)))
75 static inline void print_verbose(const struct fetch_pack_args
*args
,
83 va_start(params
, fmt
);
84 vfprintf(stderr
, fmt
, params
);
89 struct alternate_object_cache
{
90 struct object
**items
;
94 static void cache_one_alternate(const struct object_id
*oid
,
97 struct alternate_object_cache
*cache
= vcache
;
98 struct object
*obj
= parse_object(the_repository
, oid
);
100 if (!obj
|| (obj
->flags
& ALTERNATE
))
103 obj
->flags
|= ALTERNATE
;
104 ALLOC_GROW(cache
->items
, cache
->nr
+ 1, cache
->alloc
);
105 cache
->items
[cache
->nr
++] = obj
;
108 static void for_each_cached_alternate(struct fetch_negotiator
*negotiator
,
109 void (*cb
)(struct fetch_negotiator
*,
112 static int initialized
;
113 static struct alternate_object_cache cache
;
117 for_each_alternate_ref(cache_one_alternate
, &cache
);
121 for (i
= 0; i
< cache
.nr
; i
++)
122 cb(negotiator
, cache
.items
[i
]);
125 static void die_in_commit_graph_only(const struct object_id
*oid
)
127 die(_("You are attempting to fetch %s, which is in the commit graph file but not in the object database.\n"
128 "This is probably due to repo corruption.\n"
129 "If you are attempting to repair this repo corruption by refetching the missing object, use 'git fetch --refetch' with the missing object."),
133 static struct commit
*deref_without_lazy_fetch(const struct object_id
*oid
,
134 int mark_tags_complete_and_check_obj_db
)
136 enum object_type type
;
137 struct object_info info
= { .typep
= &type
};
138 struct commit
*commit
;
140 commit
= lookup_commit_in_graph(the_repository
, oid
);
142 if (mark_tags_complete_and_check_obj_db
) {
143 if (!has_object(the_repository
, oid
, 0))
144 die_in_commit_graph_only(oid
);
150 if (oid_object_info_extended(the_repository
, oid
, &info
,
151 OBJECT_INFO_SKIP_FETCH_OBJECT
| OBJECT_INFO_QUICK
))
153 if (type
== OBJ_TAG
) {
154 struct tag
*tag
= (struct tag
*)
155 parse_object(the_repository
, oid
);
159 if (mark_tags_complete_and_check_obj_db
)
160 tag
->object
.flags
|= COMPLETE
;
161 oid
= &tag
->tagged
->oid
;
167 if (type
== OBJ_COMMIT
) {
168 struct commit
*commit
= lookup_commit(the_repository
, oid
);
169 if (!commit
|| repo_parse_commit(the_repository
, commit
))
177 static int rev_list_insert_ref(struct fetch_negotiator
*negotiator
,
178 const struct object_id
*oid
)
180 struct commit
*c
= deref_without_lazy_fetch(oid
, 0);
183 negotiator
->add_tip(negotiator
, c
);
187 static int rev_list_insert_ref_oid(const char *refname UNUSED
,
188 const char *referent UNUSED
,
189 const struct object_id
*oid
,
193 return rev_list_insert_ref(cb_data
, oid
);
204 static void consume_shallow_list(struct fetch_pack_args
*args
,
205 struct packet_reader
*reader
)
207 if (args
->stateless_rpc
&& args
->deepen
) {
208 /* If we sent a depth we will get back "duplicate"
209 * shallow and unshallow commands every time there
210 * is a block of have lines exchanged.
212 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
213 if (starts_with(reader
->line
, "shallow "))
215 if (starts_with(reader
->line
, "unshallow "))
217 die(_("git fetch-pack: expected shallow list"));
219 if (reader
->status
!= PACKET_READ_FLUSH
)
220 die(_("git fetch-pack: expected a flush packet after shallow list"));
224 static enum ack_type
get_ack(struct packet_reader
*reader
,
225 struct object_id
*result_oid
)
230 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
231 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
232 len
= reader
->pktlen
;
234 if (!strcmp(reader
->line
, "NAK"))
236 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
238 if (!parse_oid_hex(arg
, result_oid
, &p
)) {
239 len
-= p
- reader
->line
;
242 if (strstr(p
, "continue"))
244 if (strstr(p
, "common"))
246 if (strstr(p
, "ready"))
251 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader
->line
);
254 static void send_request(struct fetch_pack_args
*args
,
255 int fd
, struct strbuf
*buf
)
257 if (args
->stateless_rpc
) {
258 send_sideband(fd
, -1, buf
->buf
, buf
->len
, LARGE_PACKET_MAX
);
261 if (write_in_full(fd
, buf
->buf
, buf
->len
) < 0)
262 die_errno(_("unable to write to remote"));
266 static void insert_one_alternate_object(struct fetch_negotiator
*negotiator
,
269 rev_list_insert_ref(negotiator
, &obj
->oid
);
272 #define INITIAL_FLUSH 16
273 #define PIPESAFE_FLUSH 32
274 #define LARGE_FLUSH 16384
276 static int next_flush(int stateless_rpc
, int count
)
279 if (count
< LARGE_FLUSH
)
282 count
= count
* 11 / 10;
284 if (count
< PIPESAFE_FLUSH
)
287 count
+= PIPESAFE_FLUSH
;
292 static void mark_tips(struct fetch_negotiator
*negotiator
,
293 const struct oid_array
*negotiation_tips
)
297 if (!negotiation_tips
) {
298 refs_for_each_rawref(get_main_ref_store(the_repository
),
299 rev_list_insert_ref_oid
, negotiator
);
303 for (i
= 0; i
< negotiation_tips
->nr
; i
++)
304 rev_list_insert_ref(negotiator
, &negotiation_tips
->oid
[i
]);
308 static void send_filter(struct fetch_pack_args
*args
,
309 struct strbuf
*req_buf
,
310 int server_supports_filter
)
312 if (args
->filter_options
.choice
) {
314 expand_list_objects_filter_spec(&args
->filter_options
);
315 if (server_supports_filter
) {
316 print_verbose(args
, _("Server supports filter"));
317 packet_buf_write(req_buf
, "filter %s", spec
);
318 trace2_data_string("fetch", the_repository
,
319 "filter/effective", spec
);
321 warning("filtering not recognized by server, ignoring");
322 trace2_data_string("fetch", the_repository
,
323 "filter/unsupported", spec
);
326 trace2_data_string("fetch", the_repository
,
331 static int find_common(struct fetch_negotiator
*negotiator
,
332 struct fetch_pack_args
*args
,
333 int fd
[2], struct object_id
*result_oid
,
337 int count
= 0, flushes
= 0, flush_at
= INITIAL_FLUSH
, retval
;
338 int negotiation_round
= 0, haves
= 0;
339 const struct object_id
*oid
;
340 unsigned in_vain
= 0;
341 int got_continue
= 0;
343 struct strbuf req_buf
= STRBUF_INIT
;
344 size_t state_len
= 0;
345 struct packet_reader reader
;
347 if (args
->stateless_rpc
&& multi_ack
== 1)
348 die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed");
350 packet_reader_init(&reader
, fd
[0], NULL
, 0,
351 PACKET_READ_CHOMP_NEWLINE
|
352 PACKET_READ_DIE_ON_ERR_PACKET
);
354 mark_tips(negotiator
, args
->negotiation_tips
);
355 for_each_cached_alternate(negotiator
, insert_one_alternate_object
);
358 for ( ; refs
; refs
= refs
->next
) {
359 struct object_id
*remote
= &refs
->old_oid
;
360 const char *remote_hex
;
363 if (!args
->refetch
) {
365 * If that object is complete (i.e. it is an ancestor of a
366 * local ref), we tell them we have it but do not have to
367 * tell them about its ancestors, which they already know
370 * We use lookup_object here because we are only
371 * interested in the case we *know* the object is
372 * reachable and we have already scanned it.
374 if (((o
= lookup_object(the_repository
, remote
)) != NULL
) &&
375 (o
->flags
& COMPLETE
)) {
380 remote_hex
= oid_to_hex(remote
);
382 struct strbuf c
= STRBUF_INIT
;
383 if (multi_ack
== 2) strbuf_addstr(&c
, " multi_ack_detailed");
384 if (multi_ack
== 1) strbuf_addstr(&c
, " multi_ack");
385 if (no_done
) strbuf_addstr(&c
, " no-done");
386 if (use_sideband
== 2) strbuf_addstr(&c
, " side-band-64k");
387 if (use_sideband
== 1) strbuf_addstr(&c
, " side-band");
388 if (args
->deepen_relative
) strbuf_addstr(&c
, " deepen-relative");
389 if (args
->use_thin_pack
) strbuf_addstr(&c
, " thin-pack");
390 if (args
->no_progress
) strbuf_addstr(&c
, " no-progress");
391 if (args
->include_tag
) strbuf_addstr(&c
, " include-tag");
392 if (prefer_ofs_delta
) strbuf_addstr(&c
, " ofs-delta");
393 if (deepen_since_ok
) strbuf_addstr(&c
, " deepen-since");
394 if (deepen_not_ok
) strbuf_addstr(&c
, " deepen-not");
395 if (agent_supported
) strbuf_addf(&c
, " agent=%s",
396 git_user_agent_sanitized());
398 strbuf_addf(&c
, " session-id=%s", trace2_session_id());
399 if (args
->filter_options
.choice
)
400 strbuf_addstr(&c
, " filter");
401 packet_buf_write(&req_buf
, "want %s%s\n", remote_hex
, c
.buf
);
404 packet_buf_write(&req_buf
, "want %s\n", remote_hex
);
409 strbuf_release(&req_buf
);
414 if (is_repository_shallow(the_repository
))
415 write_shallow_commits(&req_buf
, 1, NULL
);
417 packet_buf_write(&req_buf
, "deepen %d", args
->depth
);
418 if (args
->deepen_since
) {
419 timestamp_t max_age
= approxidate(args
->deepen_since
);
420 packet_buf_write(&req_buf
, "deepen-since %"PRItime
, max_age
);
422 if (args
->deepen_not
) {
424 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
425 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
426 packet_buf_write(&req_buf
, "deepen-not %s", s
->string
);
429 send_filter(args
, &req_buf
, server_supports_filtering
);
430 packet_buf_flush(&req_buf
);
431 state_len
= req_buf
.len
;
435 struct object_id oid
;
437 send_request(args
, fd
[1], &req_buf
);
438 while (packet_reader_read(&reader
) == PACKET_READ_NORMAL
) {
439 if (skip_prefix(reader
.line
, "shallow ", &arg
)) {
440 if (get_oid_hex(arg
, &oid
))
441 die(_("invalid shallow line: %s"), reader
.line
);
442 register_shallow(the_repository
, &oid
);
445 if (skip_prefix(reader
.line
, "unshallow ", &arg
)) {
446 if (get_oid_hex(arg
, &oid
))
447 die(_("invalid unshallow line: %s"), reader
.line
);
448 if (!lookup_object(the_repository
, &oid
))
449 die(_("object not found: %s"), reader
.line
);
450 /* make sure that it is parsed as shallow */
451 if (!parse_object(the_repository
, &oid
))
452 die(_("error in object: %s"), reader
.line
);
453 if (unregister_shallow(&oid
))
454 die(_("no shallow found: %s"), reader
.line
);
457 die(_("expected shallow/unshallow, got %s"), reader
.line
);
459 } else if (!args
->stateless_rpc
)
460 send_request(args
, fd
[1], &req_buf
);
462 if (!args
->stateless_rpc
) {
463 /* If we aren't using the stateless-rpc interface
464 * we don't need to retain the headers.
466 strbuf_setlen(&req_buf
, 0);
470 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository
);
473 while ((oid
= negotiator
->next(negotiator
))) {
474 packet_buf_write(&req_buf
, "have %s\n", oid_to_hex(oid
));
475 print_verbose(args
, "have %s", oid_to_hex(oid
));
478 if (flush_at
<= ++count
) {
482 trace2_region_enter_printf("negotiation_v0_v1", "round",
483 the_repository
, "%d",
485 trace2_data_intmax("negotiation_v0_v1", the_repository
,
486 "haves_added", haves
);
487 trace2_data_intmax("negotiation_v0_v1", the_repository
,
490 packet_buf_flush(&req_buf
);
491 send_request(args
, fd
[1], &req_buf
);
492 strbuf_setlen(&req_buf
, state_len
);
494 flush_at
= next_flush(args
->stateless_rpc
, count
);
497 * We keep one window "ahead" of the other side, and
498 * will wait for an ACK only on the next one
500 if (!args
->stateless_rpc
&& count
== INITIAL_FLUSH
)
503 consume_shallow_list(args
, &reader
);
505 ack
= get_ack(&reader
, result_oid
);
507 print_verbose(args
, _("got %s %d %s"), "ack",
508 ack
, oid_to_hex(result_oid
));
511 trace2_region_leave_printf("negotiation_v0_v1", "round",
512 the_repository
, "%d",
521 struct commit
*commit
=
522 lookup_commit(the_repository
,
527 die(_("invalid commit %s"), oid_to_hex(result_oid
));
528 was_common
= negotiator
->ack(negotiator
, commit
);
529 if (args
->stateless_rpc
532 /* We need to replay the have for this object
533 * on the next RPC request so the peer knows
534 * it is in common with us.
536 const char *hex
= oid_to_hex(result_oid
);
537 packet_buf_write(&req_buf
, "have %s\n", hex
);
538 state_len
= req_buf
.len
;
541 * Reset in_vain because an ack
542 * for this commit has not been
546 } else if (!args
->stateless_rpc
547 || ack
!= ACK_common
)
551 if (ack
== ACK_ready
)
558 trace2_region_leave_printf("negotiation_v0_v1", "round",
559 the_repository
, "%d",
561 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
562 print_verbose(args
, _("giving up"));
570 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository
);
571 trace2_data_intmax("negotiation_v0_v1", the_repository
, "total_rounds",
573 if (!got_ready
|| !no_done
) {
574 packet_buf_write(&req_buf
, "done\n");
575 send_request(args
, fd
[1], &req_buf
);
577 print_verbose(args
, _("done"));
582 strbuf_release(&req_buf
);
584 if (!got_ready
|| !no_done
)
585 consume_shallow_list(args
, &reader
);
586 while (flushes
|| multi_ack
) {
587 int ack
= get_ack(&reader
, result_oid
);
589 print_verbose(args
, _("got %s (%d) %s"), "ack",
590 ack
, oid_to_hex(result_oid
));
598 /* it is no error to fetch into a completely empty repo */
599 return count
? retval
: 0;
602 static struct commit_list
*complete
;
604 static int mark_complete(const struct object_id
*oid
)
606 struct commit
*commit
= deref_without_lazy_fetch(oid
, 1);
608 if (commit
&& !(commit
->object
.flags
& COMPLETE
)) {
609 commit
->object
.flags
|= COMPLETE
;
610 commit_list_insert(commit
, &complete
);
615 static int mark_complete_oid(const char *refname UNUSED
,
616 const char *referent UNUSED
,
617 const struct object_id
*oid
,
619 void *cb_data UNUSED
)
621 return mark_complete(oid
);
624 static void mark_recent_complete_commits(struct fetch_pack_args
*args
,
627 while (complete
&& cutoff
<= complete
->item
->date
) {
628 print_verbose(args
, _("Marking %s as complete"),
629 oid_to_hex(&complete
->item
->object
.oid
));
630 pop_most_recent_commit(&complete
, COMPLETE
);
634 static void add_refs_to_oidset(struct oidset
*oids
, struct ref
*refs
)
636 for (; refs
; refs
= refs
->next
)
637 oidset_insert(oids
, &refs
->old_oid
);
640 static int is_unmatched_ref(const struct ref
*ref
)
642 struct object_id oid
;
644 return ref
->match_status
== REF_NOT_MATCHED
&&
645 !parse_oid_hex(ref
->name
, &oid
, &p
) &&
647 oideq(&oid
, &ref
->old_oid
);
650 static void filter_refs(struct fetch_pack_args
*args
,
652 struct ref
**sought
, int nr_sought
)
654 struct ref
*newlist
= NULL
;
655 struct ref
**newtail
= &newlist
;
656 struct ref
*unmatched
= NULL
;
657 struct ref
*ref
, *next
;
658 struct oidset tip_oids
= OIDSET_INIT
;
660 int strict
= !(allow_unadvertised_object_request
&
661 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
664 for (ref
= *refs
; ref
; ref
= next
) {
668 if (starts_with(ref
->name
, "refs/") &&
669 check_refname_format(ref
->name
, 0)) {
671 * trash or a peeled value; do not even add it to
677 while (i
< nr_sought
) {
678 int cmp
= strcmp(ref
->name
, sought
[i
]->name
);
680 break; /* definitely do not have it */
682 keep
= 1; /* definitely have it */
683 sought
[i
]->match_status
= REF_MATCHED
;
688 if (!keep
&& args
->fetch_all
&&
689 (!args
->deepen
|| !starts_with(ref
->name
, "refs/tags/")))
696 newtail
= &ref
->next
;
698 ref
->next
= unmatched
;
704 for (i
= 0; i
< nr_sought
; i
++) {
706 if (!is_unmatched_ref(ref
))
709 add_refs_to_oidset(&tip_oids
, unmatched
);
710 add_refs_to_oidset(&tip_oids
, newlist
);
715 /* Append unmatched requests to the list */
716 for (i
= 0; i
< nr_sought
; i
++) {
718 if (!is_unmatched_ref(ref
))
721 if (!strict
|| oidset_contains(&tip_oids
, &ref
->old_oid
)) {
722 ref
->match_status
= REF_MATCHED
;
723 *newtail
= copy_ref(ref
);
724 newtail
= &(*newtail
)->next
;
726 ref
->match_status
= REF_UNADVERTISED_NOT_ALLOWED
;
730 oidset_clear(&tip_oids
);
731 free_refs(unmatched
);
736 static void mark_alternate_complete(struct fetch_negotiator
*negotiator UNUSED
,
739 mark_complete(&obj
->oid
);
743 * Mark recent commits available locally and reachable from a local ref as
746 * The cutoff time for recency is determined by this heuristic: it is the
747 * earliest commit time of the objects in refs that are commits and that we know
748 * the commit time of.
750 static void mark_complete_and_common_ref(struct fetch_negotiator
*negotiator
,
751 struct fetch_pack_args
*args
,
755 int old_save_commit_buffer
= save_commit_buffer
;
756 timestamp_t cutoff
= 0;
761 save_commit_buffer
= 0;
763 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL
);
764 for (ref
= *refs
; ref
; ref
= ref
->next
) {
765 struct commit
*commit
;
767 commit
= lookup_commit_in_graph(the_repository
, &ref
->old_oid
);
771 if (!repo_has_object_file_with_flags(the_repository
, &ref
->old_oid
,
773 OBJECT_INFO_SKIP_FETCH_OBJECT
))
775 o
= parse_object(the_repository
, &ref
->old_oid
);
776 if (!o
|| o
->type
!= OBJ_COMMIT
)
779 commit
= (struct commit
*)o
;
783 * We already have it -- which may mean that we were
784 * in sync with the other side at some time after
785 * that (it is OK if we guess wrong here).
787 if (!cutoff
|| cutoff
< commit
->date
)
788 cutoff
= commit
->date
;
790 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL
);
793 * This block marks all local refs as COMPLETE, and then recursively marks all
794 * parents of those refs as COMPLETE.
796 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL
);
798 refs_for_each_rawref(get_main_ref_store(the_repository
),
799 mark_complete_oid
, NULL
);
800 for_each_cached_alternate(NULL
, mark_alternate_complete
);
801 commit_list_sort_by_date(&complete
);
803 mark_recent_complete_commits(args
, cutoff
);
805 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL
);
808 * Mark all complete remote refs as common refs.
809 * Don't mark them common yet; the server has to be told so first.
811 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL
);
812 for (ref
= *refs
; ref
; ref
= ref
->next
) {
813 struct commit
*c
= deref_without_lazy_fetch(&ref
->old_oid
, 0);
815 if (!c
|| !(c
->object
.flags
& COMPLETE
))
818 negotiator
->known_common(negotiator
, c
);
820 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL
);
822 save_commit_buffer
= old_save_commit_buffer
;
826 * Returns 1 if every object pointed to by the given remote refs is available
827 * locally and reachable from a local ref, and 0 otherwise.
829 static int everything_local(struct fetch_pack_args
*args
,
835 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
836 const struct object_id
*remote
= &ref
->old_oid
;
839 o
= lookup_object(the_repository
, remote
);
840 if (!o
|| !(o
->flags
& COMPLETE
)) {
842 print_verbose(args
, "want %s (%s)", oid_to_hex(remote
),
846 print_verbose(args
, _("already have %s (%s)"), oid_to_hex(remote
),
853 static int sideband_demux(int in UNUSED
, int out
, void *data
)
858 ret
= recv_sideband("fetch-pack", xd
[0], out
);
863 static void create_promisor_file(const char *keep_name
,
864 struct ref
**sought
, int nr_sought
)
866 struct strbuf promisor_name
= STRBUF_INIT
;
869 strbuf_addstr(&promisor_name
, keep_name
);
870 suffix_stripped
= strbuf_strip_suffix(&promisor_name
, ".keep");
871 if (!suffix_stripped
)
872 BUG("name of pack lockfile should end with .keep (was '%s')",
874 strbuf_addstr(&promisor_name
, ".promisor");
876 write_promisor_file(promisor_name
.buf
, sought
, nr_sought
);
878 strbuf_release(&promisor_name
);
881 static void parse_gitmodules_oids(int fd
, struct oidset
*gitmodules_oids
)
883 int len
= the_hash_algo
->hexsz
+ 1; /* hash + NL */
886 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
887 int read_len
= read_in_full(fd
, hex_hash
, len
);
888 struct object_id oid
;
894 die("invalid length read %d", read_len
);
895 if (parse_oid_hex(hex_hash
, &oid
, &end
) || *end
!= '\n')
897 oidset_insert(gitmodules_oids
, &oid
);
901 static void add_index_pack_keep_option(struct strvec
*args
)
903 char hostname
[HOST_NAME_MAX
+ 1];
905 if (xgethostname(hostname
, sizeof(hostname
)))
906 xsnprintf(hostname
, sizeof(hostname
), "localhost");
907 strvec_pushf(args
, "--keep=fetch-pack %"PRIuMAX
" on %s",
908 (uintmax_t)getpid(), hostname
);
912 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
913 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
914 * stored there. (It must be freed by the caller.)
916 static int get_pack(struct fetch_pack_args
*args
,
917 int xd
[2], struct string_list
*pack_lockfiles
,
918 struct strvec
*index_pack_args
,
919 struct ref
**sought
, int nr_sought
,
920 struct oidset
*gitmodules_oids
)
923 int do_keep
= args
->keep_pack
;
924 const char *cmd_name
;
925 struct pack_header header
;
927 struct child_process cmd
= CHILD_PROCESS_INIT
;
928 int fsck_objects
= 0;
931 memset(&demux
, 0, sizeof(demux
));
933 /* xd[] is talking with upload-pack; subprocess reads from
934 * xd[0], spits out band#2 to stderr, and feeds us band#1
935 * through demux->out.
937 demux
.proc
= sideband_demux
;
940 demux
.isolate_sigpipe
= 1;
941 if (start_async(&demux
))
942 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
947 if (!args
->keep_pack
&& unpack_limit
&& !index_pack_args
) {
949 if (read_pack_header(demux
.out
, &header
))
950 die(_("protocol error: bad pack header"));
952 if (ntohl(header
.hdr_entries
) < unpack_limit
)
958 if (alternate_shallow_file
) {
959 strvec_push(&cmd
.args
, "--shallow-file");
960 strvec_push(&cmd
.args
, alternate_shallow_file
);
963 fsck_objects
= fetch_pack_fsck_objects();
965 if (do_keep
|| args
->from_promisor
|| index_pack_args
|| fsck_objects
) {
966 if (pack_lockfiles
|| fsck_objects
)
968 cmd_name
= "index-pack";
969 strvec_push(&cmd
.args
, cmd_name
);
970 strvec_push(&cmd
.args
, "--stdin");
971 if (!args
->quiet
&& !args
->no_progress
)
972 strvec_push(&cmd
.args
, "-v");
973 if (args
->use_thin_pack
)
974 strvec_push(&cmd
.args
, "--fix-thin");
975 if ((do_keep
|| index_pack_args
) && (args
->lock_pack
|| unpack_limit
))
976 add_index_pack_keep_option(&cmd
.args
);
977 if (!index_pack_args
&& args
->check_self_contained_and_connected
)
978 strvec_push(&cmd
.args
, "--check-self-contained-and-connected");
981 * We cannot perform any connectivity checks because
982 * not all packs have been downloaded; let the caller
983 * have this responsibility.
985 args
->check_self_contained_and_connected
= 0;
987 if (args
->from_promisor
)
989 * create_promisor_file() may be called afterwards but
990 * we still need index-pack to know that this is a
991 * promisor pack. For example, if transfer.fsckobjects
992 * is true, index-pack needs to know that .gitmodules
993 * is a promisor object (so that it won't complain if
996 strvec_push(&cmd
.args
, "--promisor");
999 cmd_name
= "unpack-objects";
1000 strvec_push(&cmd
.args
, cmd_name
);
1001 if (args
->quiet
|| args
->no_progress
)
1002 strvec_push(&cmd
.args
, "-q");
1003 args
->check_self_contained_and_connected
= 0;
1007 strvec_pushf(&cmd
.args
, "--pack_header=%"PRIu32
",%"PRIu32
,
1008 ntohl(header
.hdr_version
),
1009 ntohl(header
.hdr_entries
));
1011 if (args
->from_promisor
|| index_pack_args
)
1013 * We cannot use --strict in index-pack because it
1014 * checks both broken objects and links, but we only
1015 * want to check for broken objects.
1017 strvec_push(&cmd
.args
, "--fsck-objects");
1019 strvec_pushf(&cmd
.args
, "--strict%s",
1020 fsck_msg_types
.buf
);
1023 if (index_pack_args
) {
1026 for (i
= 0; i
< cmd
.args
.nr
; i
++)
1027 strvec_push(index_pack_args
, cmd
.args
.v
[i
]);
1030 sigchain_push(SIGPIPE
, SIG_IGN
);
1034 if (start_command(&cmd
))
1035 die(_("fetch-pack: unable to fork off %s"), cmd_name
);
1036 if (do_keep
&& (pack_lockfiles
|| fsck_objects
)) {
1038 char *pack_lockfile
= index_pack_lockfile(cmd
.out
, &is_well_formed
);
1040 if (!is_well_formed
)
1041 die(_("fetch-pack: invalid index-pack output"));
1042 if (pack_lockfiles
&& pack_lockfile
)
1043 string_list_append_nodup(pack_lockfiles
, pack_lockfile
);
1045 free(pack_lockfile
);
1046 parse_gitmodules_oids(cmd
.out
, gitmodules_oids
);
1051 /* Closed by start_command() */
1054 ret
= finish_command(&cmd
);
1055 if (!ret
|| (args
->check_self_contained_and_connected
&& ret
== 1))
1056 args
->self_contained_and_connected
=
1057 args
->check_self_contained_and_connected
&&
1060 die(_("%s failed"), cmd_name
);
1061 if (use_sideband
&& finish_async(&demux
))
1062 die(_("error in sideband demultiplexer"));
1064 sigchain_pop(SIGPIPE
);
1067 * Now that index-pack has succeeded, write the promisor file using the
1068 * obtained .keep filename if necessary
1070 if (do_keep
&& pack_lockfiles
&& pack_lockfiles
->nr
&& args
->from_promisor
)
1071 create_promisor_file(pack_lockfiles
->items
[0].string
, sought
, nr_sought
);
1076 static int ref_compare_name(const struct ref
*a
, const struct ref
*b
)
1078 return strcmp(a
->name
, b
->name
);
1081 DEFINE_LIST_SORT(static, sort_ref_list
, struct ref
, next
);
1083 static int cmp_ref_by_name(const void *a_
, const void *b_
)
1085 const struct ref
*a
= *((const struct ref
**)a_
);
1086 const struct ref
*b
= *((const struct ref
**)b_
);
1087 return strcmp(a
->name
, b
->name
);
1090 static struct ref
*do_fetch_pack(struct fetch_pack_args
*args
,
1092 const struct ref
*orig_ref
,
1093 struct ref
**sought
, int nr_sought
,
1094 struct shallow_info
*si
,
1095 struct string_list
*pack_lockfiles
)
1097 struct repository
*r
= the_repository
;
1098 struct ref
*ref
= copy_ref_list(orig_ref
);
1099 struct object_id oid
;
1100 const char *agent_feature
;
1102 struct fetch_negotiator negotiator_alloc
;
1103 struct fetch_negotiator
*negotiator
;
1105 negotiator
= &negotiator_alloc
;
1106 if (args
->refetch
) {
1107 fetch_negotiator_init_noop(negotiator
);
1109 fetch_negotiator_init(r
, negotiator
);
1112 sort_ref_list(&ref
, ref_compare_name
);
1113 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
1115 if ((agent_feature
= server_feature_value("agent", &agent_len
))) {
1116 agent_supported
= 1;
1118 print_verbose(args
, _("Server version is %.*s"),
1119 (int)agent_len
, agent_feature
);
1122 if (!server_supports("session-id"))
1125 if (server_supports("shallow"))
1126 print_verbose(args
, _("Server supports %s"), "shallow");
1127 else if (args
->depth
> 0 || is_repository_shallow(r
))
1128 die(_("Server does not support shallow clients"));
1129 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
1131 if (server_supports("multi_ack_detailed")) {
1132 print_verbose(args
, _("Server supports %s"), "multi_ack_detailed");
1134 if (server_supports("no-done")) {
1135 print_verbose(args
, _("Server supports %s"), "no-done");
1136 if (args
->stateless_rpc
)
1140 else if (server_supports("multi_ack")) {
1141 print_verbose(args
, _("Server supports %s"), "multi_ack");
1144 if (server_supports("side-band-64k")) {
1145 print_verbose(args
, _("Server supports %s"), "side-band-64k");
1148 else if (server_supports("side-band")) {
1149 print_verbose(args
, _("Server supports %s"), "side-band");
1152 if (server_supports("allow-tip-sha1-in-want")) {
1153 print_verbose(args
, _("Server supports %s"), "allow-tip-sha1-in-want");
1154 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1156 if (server_supports("allow-reachable-sha1-in-want")) {
1157 print_verbose(args
, _("Server supports %s"), "allow-reachable-sha1-in-want");
1158 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1160 if (server_supports("thin-pack"))
1161 print_verbose(args
, _("Server supports %s"), "thin-pack");
1163 args
->use_thin_pack
= 0;
1164 if (server_supports("no-progress"))
1165 print_verbose(args
, _("Server supports %s"), "no-progress");
1167 args
->no_progress
= 0;
1168 if (server_supports("include-tag"))
1169 print_verbose(args
, _("Server supports %s"), "include-tag");
1171 args
->include_tag
= 0;
1172 if (server_supports("ofs-delta"))
1173 print_verbose(args
, _("Server supports %s"), "ofs-delta");
1175 prefer_ofs_delta
= 0;
1177 if (server_supports("filter")) {
1178 server_supports_filtering
= 1;
1179 print_verbose(args
, _("Server supports %s"), "filter");
1180 } else if (args
->filter_options
.choice
) {
1181 warning("filtering not recognized by server, ignoring");
1184 if (server_supports("deepen-since")) {
1185 print_verbose(args
, _("Server supports %s"), "deepen-since");
1186 deepen_since_ok
= 1;
1187 } else if (args
->deepen_since
)
1188 die(_("Server does not support --shallow-since"));
1189 if (server_supports("deepen-not")) {
1190 print_verbose(args
, _("Server supports %s"), "deepen-not");
1192 } else if (args
->deepen_not
)
1193 die(_("Server does not support --shallow-exclude"));
1194 if (server_supports("deepen-relative"))
1195 print_verbose(args
, _("Server supports %s"), "deepen-relative");
1196 else if (args
->deepen_relative
)
1197 die(_("Server does not support --deepen"));
1198 if (!server_supports_hash(the_hash_algo
->name
, NULL
))
1199 die(_("Server does not support this repository's object format"));
1201 mark_complete_and_common_ref(negotiator
, args
, &ref
);
1202 filter_refs(args
, &ref
, sought
, nr_sought
);
1203 if (!args
->refetch
&& everything_local(args
, &ref
)) {
1204 packet_flush(fd
[1]);
1207 if (find_common(negotiator
, args
, fd
, &oid
, ref
) < 0)
1208 if (!args
->keep_pack
)
1209 /* When cloning, it is not unusual to have
1212 warning(_("no common commits"));
1214 if (args
->stateless_rpc
)
1215 packet_flush(fd
[1]);
1217 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
1219 else if (si
->nr_ours
|| si
->nr_theirs
) {
1220 if (args
->reject_shallow_remote
)
1221 die(_("source repository is shallow, reject to clone."));
1222 alternate_shallow_file
= setup_temporary_shallow(si
->shallow
);
1224 alternate_shallow_file
= NULL
;
1225 if (get_pack(args
, fd
, pack_lockfiles
, NULL
, sought
, nr_sought
,
1226 &fsck_options
.gitmodules_found
))
1227 die(_("git fetch-pack: fetch failed."));
1228 if (fsck_finish(&fsck_options
))
1233 negotiator
->release(negotiator
);
1237 static void add_shallow_requests(struct strbuf
*req_buf
,
1238 const struct fetch_pack_args
*args
)
1240 if (is_repository_shallow(the_repository
))
1241 write_shallow_commits(req_buf
, 1, NULL
);
1242 if (args
->depth
> 0)
1243 packet_buf_write(req_buf
, "deepen %d", args
->depth
);
1244 if (args
->deepen_since
) {
1245 timestamp_t max_age
= approxidate(args
->deepen_since
);
1246 packet_buf_write(req_buf
, "deepen-since %"PRItime
, max_age
);
1248 if (args
->deepen_not
) {
1250 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
1251 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
1252 packet_buf_write(req_buf
, "deepen-not %s", s
->string
);
1255 if (args
->deepen_relative
)
1256 packet_buf_write(req_buf
, "deepen-relative\n");
1259 static void add_wants(const struct ref
*wants
, struct strbuf
*req_buf
)
1261 int use_ref_in_want
= server_supports_feature("fetch", "ref-in-want", 0);
1263 for ( ; wants
; wants
= wants
->next
) {
1264 const struct object_id
*remote
= &wants
->old_oid
;
1268 * If that object is complete (i.e. it is an ancestor of a
1269 * local ref), we tell them we have it but do not have to
1270 * tell them about its ancestors, which they already know
1273 * We use lookup_object here because we are only
1274 * interested in the case we *know* the object is
1275 * reachable and we have already scanned it.
1277 if (((o
= lookup_object(the_repository
, remote
)) != NULL
) &&
1278 (o
->flags
& COMPLETE
)) {
1282 if (!use_ref_in_want
|| wants
->exact_oid
)
1283 packet_buf_write(req_buf
, "want %s\n", oid_to_hex(remote
));
1285 packet_buf_write(req_buf
, "want-ref %s\n", wants
->name
);
1289 static void add_common(struct strbuf
*req_buf
, struct oidset
*common
)
1291 struct oidset_iter iter
;
1292 const struct object_id
*oid
;
1293 oidset_iter_init(common
, &iter
);
1295 while ((oid
= oidset_iter_next(&iter
))) {
1296 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1300 static int add_haves(struct fetch_negotiator
*negotiator
,
1301 struct strbuf
*req_buf
,
1304 int haves_added
= 0;
1305 const struct object_id
*oid
;
1307 while ((oid
= negotiator
->next(negotiator
))) {
1308 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1309 if (++haves_added
>= *haves_to_send
)
1313 /* Increase haves to send on next round */
1314 *haves_to_send
= next_flush(1, *haves_to_send
);
1319 static void write_fetch_command_and_capabilities(struct strbuf
*req_buf
,
1320 const struct string_list
*server_options
)
1322 const char *hash_name
;
1324 ensure_server_supports_v2("fetch");
1325 packet_buf_write(req_buf
, "command=fetch");
1326 if (server_supports_v2("agent"))
1327 packet_buf_write(req_buf
, "agent=%s", git_user_agent_sanitized());
1328 if (advertise_sid
&& server_supports_v2("session-id"))
1329 packet_buf_write(req_buf
, "session-id=%s", trace2_session_id());
1330 if (server_options
&& server_options
->nr
) {
1332 ensure_server_supports_v2("server-option");
1333 for (i
= 0; i
< server_options
->nr
; i
++)
1334 packet_buf_write(req_buf
, "server-option=%s",
1335 server_options
->items
[i
].string
);
1338 if (server_feature_v2("object-format", &hash_name
)) {
1339 int hash_algo
= hash_algo_by_name(hash_name
);
1340 if (hash_algo_by_ptr(the_hash_algo
) != hash_algo
)
1341 die(_("mismatched algorithms: client %s; server %s"),
1342 the_hash_algo
->name
, hash_name
);
1343 packet_buf_write(req_buf
, "object-format=%s", the_hash_algo
->name
);
1344 } else if (hash_algo_by_ptr(the_hash_algo
) != GIT_HASH_SHA1
) {
1345 die(_("the server does not support algorithm '%s'"),
1346 the_hash_algo
->name
);
1348 packet_buf_delim(req_buf
);
1351 static int send_fetch_request(struct fetch_negotiator
*negotiator
, int fd_out
,
1352 struct fetch_pack_args
*args
,
1353 const struct ref
*wants
, struct oidset
*common
,
1354 int *haves_to_send
, int *in_vain
,
1355 int sideband_all
, int seen_ack
)
1359 struct strbuf req_buf
= STRBUF_INIT
;
1361 write_fetch_command_and_capabilities(&req_buf
, args
->server_options
);
1363 if (args
->use_thin_pack
)
1364 packet_buf_write(&req_buf
, "thin-pack");
1365 if (args
->no_progress
)
1366 packet_buf_write(&req_buf
, "no-progress");
1367 if (args
->include_tag
)
1368 packet_buf_write(&req_buf
, "include-tag");
1369 if (prefer_ofs_delta
)
1370 packet_buf_write(&req_buf
, "ofs-delta");
1372 packet_buf_write(&req_buf
, "sideband-all");
1374 /* Add shallow-info and deepen request */
1375 if (server_supports_feature("fetch", "shallow", 0))
1376 add_shallow_requests(&req_buf
, args
);
1377 else if (is_repository_shallow(the_repository
) || args
->deepen
)
1378 die(_("Server does not support shallow requests"));
1381 send_filter(args
, &req_buf
,
1382 server_supports_feature("fetch", "filter", 0));
1384 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1386 struct strbuf to_send
= STRBUF_INIT
;
1388 for (i
= 0; i
< uri_protocols
.nr
; i
++) {
1389 const char *s
= uri_protocols
.items
[i
].string
;
1391 if (!strcmp(s
, "https") || !strcmp(s
, "http")) {
1393 strbuf_addch(&to_send
, ',');
1394 strbuf_addstr(&to_send
, s
);
1398 packet_buf_write(&req_buf
, "packfile-uris %s",
1400 strbuf_release(&to_send
);
1405 add_wants(wants
, &req_buf
);
1407 /* Add all of the common commits we've found in previous rounds */
1408 add_common(&req_buf
, common
);
1410 haves_added
= add_haves(negotiator
, &req_buf
, haves_to_send
);
1411 *in_vain
+= haves_added
;
1412 trace2_data_intmax("negotiation_v2", the_repository
, "haves_added", haves_added
);
1413 trace2_data_intmax("negotiation_v2", the_repository
, "in_vain", *in_vain
);
1414 if (!haves_added
|| (seen_ack
&& *in_vain
>= MAX_IN_VAIN
)) {
1416 packet_buf_write(&req_buf
, "done\n");
1421 packet_buf_flush(&req_buf
);
1422 if (write_in_full(fd_out
, req_buf
.buf
, req_buf
.len
) < 0)
1423 die_errno(_("unable to write request to remote"));
1425 strbuf_release(&req_buf
);
1430 * Processes a section header in a server's response and checks if it matches
1431 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1432 * not consumed); if 0, the line will be consumed and the function will die if
1433 * the section header doesn't match what was expected.
1435 static int process_section_header(struct packet_reader
*reader
,
1436 const char *section
, int peek
)
1440 if (packet_reader_peek(reader
) == PACKET_READ_NORMAL
&&
1441 !strcmp(reader
->line
, section
))
1447 die(_("expected '%s', received '%s'"),
1448 section
, reader
->line
);
1450 die(_("expected '%s'"), section
);
1452 packet_reader_read(reader
);
1458 static int process_ack(struct fetch_negotiator
*negotiator
,
1459 struct packet_reader
*reader
,
1460 struct object_id
*common_oid
,
1461 int *received_ready
)
1463 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1466 if (!strcmp(reader
->line
, "NAK"))
1469 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
1470 if (!get_oid_hex(arg
, common_oid
)) {
1471 struct commit
*commit
;
1472 commit
= lookup_commit(the_repository
, common_oid
);
1474 negotiator
->ack(negotiator
, commit
);
1479 if (!strcmp(reader
->line
, "ready")) {
1480 *received_ready
= 1;
1484 die(_("unexpected acknowledgment line: '%s'"), reader
->line
);
1487 if (reader
->status
!= PACKET_READ_FLUSH
&&
1488 reader
->status
!= PACKET_READ_DELIM
)
1489 die(_("error processing acks: %d"), reader
->status
);
1492 * If an "acknowledgments" section is sent, a packfile is sent if and
1493 * only if "ready" was sent in this section. The other sections
1494 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1495 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1498 if (*received_ready
&& reader
->status
!= PACKET_READ_DELIM
)
1500 * TRANSLATORS: The parameter will be 'ready', a protocol
1503 die(_("expected packfile to be sent after '%s'"), "ready");
1504 if (!*received_ready
&& reader
->status
!= PACKET_READ_FLUSH
)
1506 * TRANSLATORS: The parameter will be 'ready', a protocol
1509 die(_("expected no other sections to be sent after no '%s'"), "ready");
1514 static void receive_shallow_info(struct fetch_pack_args
*args
,
1515 struct packet_reader
*reader
,
1516 struct oid_array
*shallows
,
1517 struct shallow_info
*si
)
1519 int unshallow_received
= 0;
1521 process_section_header(reader
, "shallow-info", 0);
1522 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1524 struct object_id oid
;
1526 if (skip_prefix(reader
->line
, "shallow ", &arg
)) {
1527 if (get_oid_hex(arg
, &oid
))
1528 die(_("invalid shallow line: %s"), reader
->line
);
1529 oid_array_append(shallows
, &oid
);
1532 if (skip_prefix(reader
->line
, "unshallow ", &arg
)) {
1533 if (get_oid_hex(arg
, &oid
))
1534 die(_("invalid unshallow line: %s"), reader
->line
);
1535 if (!lookup_object(the_repository
, &oid
))
1536 die(_("object not found: %s"), reader
->line
);
1537 /* make sure that it is parsed as shallow */
1538 if (!parse_object(the_repository
, &oid
))
1539 die(_("error in object: %s"), reader
->line
);
1540 if (unregister_shallow(&oid
))
1541 die(_("no shallow found: %s"), reader
->line
);
1542 unshallow_received
= 1;
1545 die(_("expected shallow/unshallow, got %s"), reader
->line
);
1548 if (reader
->status
!= PACKET_READ_FLUSH
&&
1549 reader
->status
!= PACKET_READ_DELIM
)
1550 die(_("error processing shallow info: %d"), reader
->status
);
1552 if (args
->deepen
|| unshallow_received
) {
1554 * Treat these as shallow lines caused by our depth settings.
1555 * In v0, these lines cannot cause refs to be rejected; do the
1560 for (i
= 0; i
< shallows
->nr
; i
++)
1561 register_shallow(the_repository
, &shallows
->oid
[i
]);
1562 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
1565 } else if (shallows
->nr
) {
1567 * Treat these as shallow lines caused by the remote being
1568 * shallow. In v0, remote refs that reach these objects are
1569 * rejected (unless --update-shallow is set); do the same.
1571 prepare_shallow_info(si
, shallows
);
1572 if (si
->nr_ours
|| si
->nr_theirs
) {
1573 if (args
->reject_shallow_remote
)
1574 die(_("source repository is shallow, reject to clone."));
1575 alternate_shallow_file
=
1576 setup_temporary_shallow(si
->shallow
);
1578 alternate_shallow_file
= NULL
;
1580 alternate_shallow_file
= NULL
;
1584 static int cmp_name_ref(const void *name
, const void *ref
)
1586 return strcmp(name
, (*(struct ref
**)ref
)->name
);
1589 static void receive_wanted_refs(struct packet_reader
*reader
,
1590 struct ref
**sought
, int nr_sought
)
1592 process_section_header(reader
, "wanted-refs", 0);
1593 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1594 struct object_id oid
;
1598 if (parse_oid_hex(reader
->line
, &oid
, &end
) || *end
++ != ' ')
1599 die(_("expected wanted-ref, got '%s'"), reader
->line
);
1601 found
= bsearch(end
, sought
, nr_sought
, sizeof(*sought
),
1604 die(_("unexpected wanted-ref: '%s'"), reader
->line
);
1605 oidcpy(&(*found
)->old_oid
, &oid
);
1608 if (reader
->status
!= PACKET_READ_DELIM
)
1609 die(_("error processing wanted refs: %d"), reader
->status
);
1612 static void receive_packfile_uris(struct packet_reader
*reader
,
1613 struct string_list
*uris
)
1615 process_section_header(reader
, "packfile-uris", 0);
1616 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1617 if (reader
->pktlen
< the_hash_algo
->hexsz
||
1618 reader
->line
[the_hash_algo
->hexsz
] != ' ')
1619 die("expected '<hash> <uri>', got: %s", reader
->line
);
1621 string_list_append(uris
, reader
->line
);
1623 if (reader
->status
!= PACKET_READ_DELIM
)
1624 die("expected DELIM");
1628 FETCH_CHECK_LOCAL
= 0,
1635 static void do_check_stateless_delimiter(int stateless_rpc
,
1636 struct packet_reader
*reader
)
1638 check_stateless_delimiter(stateless_rpc
, reader
,
1639 _("git fetch-pack: expected response end packet"));
1642 static struct ref
*do_fetch_pack_v2(struct fetch_pack_args
*args
,
1644 const struct ref
*orig_ref
,
1645 struct ref
**sought
, int nr_sought
,
1646 struct oid_array
*shallows
,
1647 struct shallow_info
*si
,
1648 struct string_list
*pack_lockfiles
)
1650 struct repository
*r
= the_repository
;
1651 struct ref
*ref
= copy_ref_list(orig_ref
);
1652 enum fetch_state state
= FETCH_CHECK_LOCAL
;
1653 struct oidset common
= OIDSET_INIT
;
1654 struct packet_reader reader
;
1655 int in_vain
= 0, negotiation_started
= 0;
1656 int negotiation_round
= 0;
1657 int haves_to_send
= INITIAL_FLUSH
;
1658 struct fetch_negotiator negotiator_alloc
;
1659 struct fetch_negotiator
*negotiator
;
1661 struct object_id common_oid
;
1662 int received_ready
= 0;
1663 struct string_list packfile_uris
= STRING_LIST_INIT_DUP
;
1665 struct strvec index_pack_args
= STRVEC_INIT
;
1667 negotiator
= &negotiator_alloc
;
1669 fetch_negotiator_init_noop(negotiator
);
1671 fetch_negotiator_init(r
, negotiator
);
1673 packet_reader_init(&reader
, fd
[0], NULL
, 0,
1674 PACKET_READ_CHOMP_NEWLINE
|
1675 PACKET_READ_DIE_ON_ERR_PACKET
);
1676 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1677 server_supports_feature("fetch", "sideband-all", 0)) {
1678 reader
.use_sideband
= 1;
1679 reader
.me
= "fetch-pack";
1682 while (state
!= FETCH_DONE
) {
1684 case FETCH_CHECK_LOCAL
:
1685 sort_ref_list(&ref
, ref_compare_name
);
1686 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
1688 /* v2 supports these by default */
1689 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1691 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
1694 /* Filter 'ref' by 'sought' and those that aren't local */
1695 mark_complete_and_common_ref(negotiator
, args
, &ref
);
1696 filter_refs(args
, &ref
, sought
, nr_sought
);
1697 if (!args
->refetch
&& everything_local(args
, &ref
))
1700 state
= FETCH_SEND_REQUEST
;
1702 mark_tips(negotiator
, args
->negotiation_tips
);
1703 for_each_cached_alternate(negotiator
,
1704 insert_one_alternate_object
);
1706 case FETCH_SEND_REQUEST
:
1707 if (!negotiation_started
) {
1708 negotiation_started
= 1;
1709 trace2_region_enter("fetch-pack",
1713 negotiation_round
++;
1714 trace2_region_enter_printf("negotiation_v2", "round",
1715 the_repository
, "%d",
1717 if (send_fetch_request(negotiator
, fd
[1], args
, ref
,
1719 &haves_to_send
, &in_vain
,
1720 reader
.use_sideband
,
1722 trace2_region_leave_printf("negotiation_v2", "round",
1723 the_repository
, "%d",
1725 state
= FETCH_GET_PACK
;
1728 state
= FETCH_PROCESS_ACKS
;
1730 case FETCH_PROCESS_ACKS
:
1731 /* Process ACKs/NAKs */
1732 process_section_header(&reader
, "acknowledgments", 0);
1733 while (process_ack(negotiator
, &reader
, &common_oid
,
1737 oidset_insert(&common
, &common_oid
);
1739 trace2_region_leave_printf("negotiation_v2", "round",
1740 the_repository
, "%d",
1742 if (received_ready
) {
1744 * Don't check for response delimiter; get_pack() will
1745 * read the rest of this response.
1747 state
= FETCH_GET_PACK
;
1749 do_check_stateless_delimiter(args
->stateless_rpc
, &reader
);
1750 state
= FETCH_SEND_REQUEST
;
1753 case FETCH_GET_PACK
:
1754 trace2_region_leave("fetch-pack",
1757 trace2_data_intmax("negotiation_v2", the_repository
,
1758 "total_rounds", negotiation_round
);
1759 /* Check for shallow-info section */
1760 if (process_section_header(&reader
, "shallow-info", 1))
1761 receive_shallow_info(args
, &reader
, shallows
, si
);
1763 if (process_section_header(&reader
, "wanted-refs", 1))
1764 receive_wanted_refs(&reader
, sought
, nr_sought
);
1766 /* get the pack(s) */
1767 if (git_env_bool("GIT_TRACE_REDACT", 1))
1768 reader
.options
|= PACKET_READ_REDACT_URI_PATH
;
1769 if (process_section_header(&reader
, "packfile-uris", 1))
1770 receive_packfile_uris(&reader
, &packfile_uris
);
1771 /* We don't expect more URIs. Reset to avoid expensive URI check. */
1772 reader
.options
&= ~PACKET_READ_REDACT_URI_PATH
;
1774 process_section_header(&reader
, "packfile", 0);
1777 * this is the final request we'll make of the server;
1778 * do a half-duplex shutdown to indicate that they can
1779 * hang up as soon as the pack is sent.
1784 if (get_pack(args
, fd
, pack_lockfiles
,
1785 packfile_uris
.nr
? &index_pack_args
: NULL
,
1786 sought
, nr_sought
, &fsck_options
.gitmodules_found
))
1787 die(_("git fetch-pack: fetch failed."));
1788 do_check_stateless_delimiter(args
->stateless_rpc
, &reader
);
1797 for (i
= 0; i
< packfile_uris
.nr
; i
++) {
1799 struct child_process cmd
= CHILD_PROCESS_INIT
;
1800 char packname
[GIT_MAX_HEXSZ
+ 1];
1801 const char *uri
= packfile_uris
.items
[i
].string
+
1802 the_hash_algo
->hexsz
+ 1;
1804 strvec_push(&cmd
.args
, "http-fetch");
1805 strvec_pushf(&cmd
.args
, "--packfile=%.*s",
1806 (int) the_hash_algo
->hexsz
,
1807 packfile_uris
.items
[i
].string
);
1808 for (j
= 0; j
< index_pack_args
.nr
; j
++)
1809 strvec_pushf(&cmd
.args
, "--index-pack-arg=%s",
1810 index_pack_args
.v
[j
]);
1811 strvec_push(&cmd
.args
, uri
);
1815 if (start_command(&cmd
))
1816 die("fetch-pack: unable to spawn http-fetch");
1818 if (read_in_full(cmd
.out
, packname
, 5) < 0 ||
1819 memcmp(packname
, "keep\t", 5))
1820 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1822 if (read_in_full(cmd
.out
, packname
,
1823 the_hash_algo
->hexsz
+ 1) < 0 ||
1824 packname
[the_hash_algo
->hexsz
] != '\n')
1825 die("fetch-pack: expected hash then LF at end of http-fetch output");
1827 packname
[the_hash_algo
->hexsz
] = '\0';
1829 parse_gitmodules_oids(cmd
.out
, &fsck_options
.gitmodules_found
);
1833 if (finish_command(&cmd
))
1834 die("fetch-pack: unable to finish http-fetch");
1836 if (memcmp(packfile_uris
.items
[i
].string
, packname
,
1837 the_hash_algo
->hexsz
))
1838 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1839 uri
, (int) the_hash_algo
->hexsz
,
1840 packfile_uris
.items
[i
].string
);
1842 string_list_append_nodup(pack_lockfiles
,
1843 xstrfmt("%s/pack/pack-%s.keep",
1844 repo_get_object_directory(the_repository
),
1847 string_list_clear(&packfile_uris
, 0);
1848 strvec_clear(&index_pack_args
);
1850 if (fsck_finish(&fsck_options
))
1854 negotiator
->release(negotiator
);
1856 oidset_clear(&common
);
1860 static int fetch_pack_config_cb(const char *var
, const char *value
,
1861 const struct config_context
*ctx
, void *cb
)
1865 if (strcmp(var
, "fetch.fsck.skiplist") == 0) {
1868 if (git_config_pathname(&path
, var
, value
))
1870 strbuf_addf(&fsck_msg_types
, "%cskiplist=%s",
1871 fsck_msg_types
.len
? ',' : '=', path
);
1876 if (skip_prefix(var
, "fetch.fsck.", &msg_id
)) {
1878 return config_error_nonbool(var
);
1879 if (is_valid_msg_type(msg_id
, value
))
1880 strbuf_addf(&fsck_msg_types
, "%c%s=%s",
1881 fsck_msg_types
.len
? ',' : '=', msg_id
, value
);
1883 warning("Skipping unknown msg id '%s'", msg_id
);
1887 return git_default_config(var
, value
, ctx
, cb
);
1890 static void fetch_pack_config(void)
1892 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit
);
1893 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit
);
1894 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta
);
1895 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects
);
1896 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects
);
1897 git_config_get_bool("transfer.advertisesid", &advertise_sid
);
1898 if (!uri_protocols
.nr
) {
1901 if (!git_config_get_string("fetch.uriprotocols", &str
) && str
) {
1902 string_list_split(&uri_protocols
, str
, ',', -1);
1907 git_config(fetch_pack_config_cb
, NULL
);
1910 static void fetch_pack_setup(void)
1912 static int did_setup
;
1915 fetch_pack_config();
1916 if (0 <= fetch_unpack_limit
)
1917 unpack_limit
= fetch_unpack_limit
;
1918 else if (0 <= transfer_unpack_limit
)
1919 unpack_limit
= transfer_unpack_limit
;
1923 static int remove_duplicates_in_refs(struct ref
**ref
, int nr
)
1925 struct string_list names
= STRING_LIST_INIT_NODUP
;
1928 for (src
= dst
= 0; src
< nr
; src
++) {
1929 struct string_list_item
*item
;
1930 item
= string_list_insert(&names
, ref
[src
]->name
);
1932 continue; /* already have it */
1933 item
->util
= ref
[src
];
1935 ref
[dst
] = ref
[src
];
1938 for (src
= dst
; src
< nr
; src
++)
1940 string_list_clear(&names
, 0);
1944 static void update_shallow(struct fetch_pack_args
*args
,
1945 struct ref
**sought
, int nr_sought
,
1946 struct shallow_info
*si
)
1948 struct oid_array ref
= OID_ARRAY_INIT
;
1952 if (args
->deepen
&& alternate_shallow_file
) {
1953 if (*alternate_shallow_file
== '\0') { /* --unshallow */
1954 unlink_or_warn(git_path_shallow(the_repository
));
1955 rollback_shallow_file(the_repository
, &shallow_lock
);
1957 commit_shallow_file(the_repository
, &shallow_lock
);
1958 alternate_shallow_file
= NULL
;
1962 if (!si
->shallow
|| !si
->shallow
->nr
)
1965 if (args
->cloning
) {
1967 * remote is shallow, but this is a clone, there are
1968 * no objects in repo to worry about. Accept any
1969 * shallow points that exist in the pack (iow in repo
1970 * after get_pack() and reprepare_packed_git())
1972 struct oid_array extra
= OID_ARRAY_INIT
;
1973 struct object_id
*oid
= si
->shallow
->oid
;
1974 for (i
= 0; i
< si
->shallow
->nr
; i
++)
1975 if (repo_has_object_file(the_repository
, &oid
[i
]))
1976 oid_array_append(&extra
, &oid
[i
]);
1978 setup_alternate_shallow(&shallow_lock
,
1979 &alternate_shallow_file
,
1981 commit_shallow_file(the_repository
, &shallow_lock
);
1982 alternate_shallow_file
= NULL
;
1984 oid_array_clear(&extra
);
1988 if (!si
->nr_ours
&& !si
->nr_theirs
)
1991 remove_nonexistent_theirs_shallow(si
);
1992 if (!si
->nr_ours
&& !si
->nr_theirs
)
1994 for (i
= 0; i
< nr_sought
; i
++)
1995 oid_array_append(&ref
, &sought
[i
]->old_oid
);
1998 if (args
->update_shallow
) {
2000 * remote is also shallow, .git/shallow may be updated
2001 * so all refs can be accepted. Make sure we only add
2002 * shallow roots that are actually reachable from new
2005 struct oid_array extra
= OID_ARRAY_INIT
;
2006 struct object_id
*oid
= si
->shallow
->oid
;
2007 assign_shallow_commits_to_refs(si
, NULL
, NULL
);
2008 if (!si
->nr_ours
&& !si
->nr_theirs
) {
2009 oid_array_clear(&ref
);
2012 for (i
= 0; i
< si
->nr_ours
; i
++)
2013 oid_array_append(&extra
, &oid
[si
->ours
[i
]]);
2014 for (i
= 0; i
< si
->nr_theirs
; i
++)
2015 oid_array_append(&extra
, &oid
[si
->theirs
[i
]]);
2016 setup_alternate_shallow(&shallow_lock
,
2017 &alternate_shallow_file
,
2019 commit_shallow_file(the_repository
, &shallow_lock
);
2020 oid_array_clear(&extra
);
2021 oid_array_clear(&ref
);
2022 alternate_shallow_file
= NULL
;
2027 * remote is also shallow, check what ref is safe to update
2028 * without updating .git/shallow
2030 CALLOC_ARRAY(status
, nr_sought
);
2031 assign_shallow_commits_to_refs(si
, NULL
, status
);
2032 if (si
->nr_ours
|| si
->nr_theirs
) {
2033 for (i
= 0; i
< nr_sought
; i
++)
2035 sought
[i
]->status
= REF_STATUS_REJECT_SHALLOW
;
2038 oid_array_clear(&ref
);
2041 static const struct object_id
*iterate_ref_map(void *cb_data
)
2043 struct ref
**rm
= cb_data
;
2044 struct ref
*ref
= *rm
;
2049 return &ref
->old_oid
;
2052 int fetch_pack_fsck_objects(void)
2055 if (fetch_fsck_objects
>= 0)
2056 return fetch_fsck_objects
;
2057 if (transfer_fsck_objects
>= 0)
2058 return transfer_fsck_objects
;
2062 struct ref
*fetch_pack(struct fetch_pack_args
*args
,
2064 const struct ref
*ref
,
2065 struct ref
**sought
, int nr_sought
,
2066 struct oid_array
*shallow
,
2067 struct string_list
*pack_lockfiles
,
2068 enum protocol_version version
)
2070 struct ref
*ref_cpy
;
2071 struct shallow_info si
;
2072 struct oid_array shallows_scratch
= OID_ARRAY_INIT
;
2076 nr_sought
= remove_duplicates_in_refs(sought
, nr_sought
);
2078 if (version
!= protocol_v2
&& !ref
) {
2079 packet_flush(fd
[1]);
2080 die(_("no matching remote head"));
2082 if (version
== protocol_v2
) {
2084 BUG("Protocol V2 does not provide shallows at this point in the fetch");
2085 memset(&si
, 0, sizeof(si
));
2086 ref_cpy
= do_fetch_pack_v2(args
, fd
, ref
, sought
, nr_sought
,
2087 &shallows_scratch
, &si
,
2090 prepare_shallow_info(&si
, shallow
);
2091 ref_cpy
= do_fetch_pack(args
, fd
, ref
, sought
, nr_sought
,
2092 &si
, pack_lockfiles
);
2094 reprepare_packed_git(the_repository
);
2096 if (!args
->cloning
&& args
->deepen
) {
2097 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
2098 struct ref
*iterator
= ref_cpy
;
2099 opt
.shallow_file
= alternate_shallow_file
;
2101 opt
.is_deepening_fetch
= 1;
2102 if (check_connected(iterate_ref_map
, &iterator
, &opt
)) {
2103 error(_("remote did not send all necessary objects"));
2106 rollback_shallow_file(the_repository
, &shallow_lock
);
2109 args
->connectivity_checked
= 1;
2112 update_shallow(args
, sought
, nr_sought
, &si
);
2114 clear_shallow_info(&si
);
2115 oid_array_clear(&shallows_scratch
);
2119 static int add_to_object_array(const struct object_id
*oid
, void *data
)
2121 struct object_array
*a
= data
;
2123 add_object_array(lookup_object(the_repository
, oid
), "", a
);
2127 static void clear_common_flag(struct oidset
*s
)
2129 struct oidset_iter iter
;
2130 const struct object_id
*oid
;
2131 oidset_iter_init(s
, &iter
);
2133 while ((oid
= oidset_iter_next(&iter
))) {
2134 struct object
*obj
= lookup_object(the_repository
, oid
);
2135 obj
->flags
&= ~COMMON
;
2139 void negotiate_using_fetch(const struct oid_array
*negotiation_tips
,
2140 const struct string_list
*server_options
,
2143 struct oidset
*acked_commits
)
2145 struct fetch_negotiator negotiator
;
2146 struct packet_reader reader
;
2147 struct object_array nt_object_array
= OBJECT_ARRAY_INIT
;
2148 struct strbuf req_buf
= STRBUF_INIT
;
2149 int haves_to_send
= INITIAL_FLUSH
;
2152 int last_iteration
= 0;
2153 int negotiation_round
= 0;
2154 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
2156 fetch_negotiator_init(the_repository
, &negotiator
);
2157 mark_tips(&negotiator
, negotiation_tips
);
2159 packet_reader_init(&reader
, fd
[0], NULL
, 0,
2160 PACKET_READ_CHOMP_NEWLINE
|
2161 PACKET_READ_DIE_ON_ERR_PACKET
);
2163 oid_array_for_each((struct oid_array
*) negotiation_tips
,
2164 add_to_object_array
,
2167 trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository
);
2168 while (!last_iteration
) {
2170 struct object_id common_oid
;
2171 int received_ready
= 0;
2173 negotiation_round
++;
2175 trace2_region_enter_printf("negotiate_using_fetch", "round",
2176 the_repository
, "%d",
2178 strbuf_reset(&req_buf
);
2179 write_fetch_command_and_capabilities(&req_buf
, server_options
);
2181 packet_buf_write(&req_buf
, "wait-for-done");
2183 haves_added
= add_haves(&negotiator
, &req_buf
, &haves_to_send
);
2184 in_vain
+= haves_added
;
2185 if (!haves_added
|| (seen_ack
&& in_vain
>= MAX_IN_VAIN
))
2188 trace2_data_intmax("negotiate_using_fetch", the_repository
,
2189 "haves_added", haves_added
);
2190 trace2_data_intmax("negotiate_using_fetch", the_repository
,
2191 "in_vain", in_vain
);
2194 packet_buf_flush(&req_buf
);
2195 if (write_in_full(fd
[1], req_buf
.buf
, req_buf
.len
) < 0)
2196 die_errno(_("unable to write request to remote"));
2198 /* Process ACKs/NAKs */
2199 process_section_header(&reader
, "acknowledgments", 0);
2200 while (process_ack(&negotiator
, &reader
, &common_oid
,
2202 struct commit
*commit
= lookup_commit(the_repository
,
2205 timestamp_t generation
;
2207 parse_commit_or_die(commit
);
2208 commit
->object
.flags
|= COMMON
;
2209 generation
= commit_graph_generation(commit
);
2210 if (generation
< min_generation
)
2211 min_generation
= generation
;
2215 oidset_insert(acked_commits
, &common_oid
);
2218 die(_("unexpected 'ready' from remote"));
2220 do_check_stateless_delimiter(stateless_rpc
, &reader
);
2221 if (can_all_from_reach_with_flag(&nt_object_array
, COMMON
,
2225 trace2_region_leave_printf("negotiation", "round",
2226 the_repository
, "%d",
2229 trace2_region_leave("fetch-pack", "negotiate_using_fetch", the_repository
);
2230 trace2_data_intmax("negotiate_using_fetch", the_repository
,
2231 "total_rounds", negotiation_round
);
2233 clear_common_flag(acked_commits
);
2234 object_array_clear(&nt_object_array
);
2235 negotiator
.release(&negotiator
);
2236 strbuf_release(&req_buf
);
2239 int report_unmatched_refs(struct ref
**sought
, int nr_sought
)
2243 for (i
= 0; i
< nr_sought
; i
++) {
2246 switch (sought
[i
]->match_status
) {
2249 case REF_NOT_MATCHED
:
2250 error(_("no such remote ref %s"), sought
[i
]->name
);
2252 case REF_UNADVERTISED_NOT_ALLOWED
:
2253 error(_("Server does not allow request for unadvertised object %s"),