2 #include "repository.h"
12 #include "fetch-pack.h"
14 #include "run-command.h"
16 #include "transport.h"
18 #include "oid-array.h"
21 #include "object-store.h"
22 #include "connected.h"
23 #include "fetch-negotiator.h"
27 static int transfer_unpack_limit
= -1;
28 static int fetch_unpack_limit
= -1;
29 static int unpack_limit
= 100;
30 static int prefer_ofs_delta
= 1;
32 static int deepen_since_ok
;
33 static int deepen_not_ok
;
34 static int fetch_fsck_objects
= -1;
35 static int transfer_fsck_objects
= -1;
36 static int agent_supported
;
37 static int server_supports_filtering
;
38 static int advertise_sid
;
39 static struct shallow_lock shallow_lock
;
40 static const char *alternate_shallow_file
;
41 static struct fsck_options fsck_options
= FSCK_OPTIONS_MISSING_GITMODULES
;
42 static struct strbuf fsck_msg_types
= STRBUF_INIT
;
43 static struct string_list uri_protocols
= STRING_LIST_INIT_DUP
;
45 /* Remember to update object flag allocation in object.h */
46 #define COMPLETE (1U << 0)
47 #define ALTERNATE (1U << 1)
50 * After sending this many "have"s if we do not get any new ACK , we
51 * give up traversing our history.
53 #define MAX_IN_VAIN 256
55 static int multi_ack
, use_sideband
;
56 /* Allow specifying sha1 if it is a ref tip. */
57 #define ALLOW_TIP_SHA1 01
58 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
59 #define ALLOW_REACHABLE_SHA1 02
60 static unsigned int allow_unadvertised_object_request
;
62 __attribute__((format (printf
, 2, 3)))
63 static inline void print_verbose(const struct fetch_pack_args
*args
,
71 va_start(params
, fmt
);
72 vfprintf(stderr
, fmt
, params
);
77 struct alternate_object_cache
{
78 struct object
**items
;
82 static void cache_one_alternate(const struct object_id
*oid
,
85 struct alternate_object_cache
*cache
= vcache
;
86 struct object
*obj
= parse_object(the_repository
, 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(struct fetch_negotiator
*negotiator
,
97 void (*cb
)(struct fetch_negotiator
*,
100 static int initialized
;
101 static struct alternate_object_cache cache
;
105 for_each_alternate_ref(cache_one_alternate
, &cache
);
109 for (i
= 0; i
< cache
.nr
; i
++)
110 cb(negotiator
, cache
.items
[i
]);
113 static struct commit
*deref_without_lazy_fetch(const struct object_id
*oid
,
114 int mark_tags_complete
)
116 enum object_type type
;
117 struct object_info info
= { .typep
= &type
};
120 if (oid_object_info_extended(the_repository
, oid
, &info
,
121 OBJECT_INFO_SKIP_FETCH_OBJECT
| OBJECT_INFO_QUICK
))
123 if (type
== OBJ_TAG
) {
124 struct tag
*tag
= (struct tag
*)
125 parse_object(the_repository
, oid
);
129 if (mark_tags_complete
)
130 tag
->object
.flags
|= COMPLETE
;
131 oid
= &tag
->tagged
->oid
;
136 if (type
== OBJ_COMMIT
)
137 return (struct commit
*) parse_object(the_repository
, oid
);
141 static int rev_list_insert_ref(struct fetch_negotiator
*negotiator
,
142 const struct object_id
*oid
)
144 struct commit
*c
= deref_without_lazy_fetch(oid
, 0);
147 negotiator
->add_tip(negotiator
, c
);
151 static int rev_list_insert_ref_oid(const char *refname
, const struct object_id
*oid
,
152 int flag
, void *cb_data
)
154 return rev_list_insert_ref(cb_data
, oid
);
165 static void consume_shallow_list(struct fetch_pack_args
*args
,
166 struct packet_reader
*reader
)
168 if (args
->stateless_rpc
&& args
->deepen
) {
169 /* If we sent a depth we will get back "duplicate"
170 * shallow and unshallow commands every time there
171 * is a block of have lines exchanged.
173 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
174 if (starts_with(reader
->line
, "shallow "))
176 if (starts_with(reader
->line
, "unshallow "))
178 die(_("git fetch-pack: expected shallow list"));
180 if (reader
->status
!= PACKET_READ_FLUSH
)
181 die(_("git fetch-pack: expected a flush packet after shallow list"));
185 static enum ack_type
get_ack(struct packet_reader
*reader
,
186 struct object_id
*result_oid
)
191 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
192 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
193 len
= reader
->pktlen
;
195 if (!strcmp(reader
->line
, "NAK"))
197 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
199 if (!parse_oid_hex(arg
, result_oid
, &p
)) {
200 len
-= p
- reader
->line
;
203 if (strstr(p
, "continue"))
205 if (strstr(p
, "common"))
207 if (strstr(p
, "ready"))
212 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader
->line
);
215 static void send_request(struct fetch_pack_args
*args
,
216 int fd
, struct strbuf
*buf
)
218 if (args
->stateless_rpc
) {
219 send_sideband(fd
, -1, buf
->buf
, buf
->len
, LARGE_PACKET_MAX
);
222 if (write_in_full(fd
, buf
->buf
, buf
->len
) < 0)
223 die_errno(_("unable to write to remote"));
227 static void insert_one_alternate_object(struct fetch_negotiator
*negotiator
,
230 rev_list_insert_ref(negotiator
, &obj
->oid
);
233 #define INITIAL_FLUSH 16
234 #define PIPESAFE_FLUSH 32
235 #define LARGE_FLUSH 16384
237 static int next_flush(int stateless_rpc
, int count
)
240 if (count
< LARGE_FLUSH
)
243 count
= count
* 11 / 10;
245 if (count
< PIPESAFE_FLUSH
)
248 count
+= PIPESAFE_FLUSH
;
253 static void mark_tips(struct fetch_negotiator
*negotiator
,
254 const struct oid_array
*negotiation_tips
)
258 if (!negotiation_tips
) {
259 for_each_rawref(rev_list_insert_ref_oid
, negotiator
);
263 for (i
= 0; i
< negotiation_tips
->nr
; i
++)
264 rev_list_insert_ref(negotiator
, &negotiation_tips
->oid
[i
]);
268 static int find_common(struct fetch_negotiator
*negotiator
,
269 struct fetch_pack_args
*args
,
270 int fd
[2], struct object_id
*result_oid
,
274 int count
= 0, flushes
= 0, flush_at
= INITIAL_FLUSH
, retval
;
275 const struct object_id
*oid
;
276 unsigned in_vain
= 0;
277 int got_continue
= 0;
279 struct strbuf req_buf
= STRBUF_INIT
;
280 size_t state_len
= 0;
281 struct packet_reader reader
;
283 if (args
->stateless_rpc
&& multi_ack
== 1)
284 die(_("--stateless-rpc requires multi_ack_detailed"));
286 packet_reader_init(&reader
, fd
[0], NULL
, 0,
287 PACKET_READ_CHOMP_NEWLINE
|
288 PACKET_READ_DIE_ON_ERR_PACKET
);
290 mark_tips(negotiator
, args
->negotiation_tips
);
291 for_each_cached_alternate(negotiator
, insert_one_alternate_object
);
294 for ( ; refs
; refs
= refs
->next
) {
295 struct object_id
*remote
= &refs
->old_oid
;
296 const char *remote_hex
;
300 * If that object is complete (i.e. it is an ancestor of a
301 * local ref), we tell them we have it but do not have to
302 * tell them about its ancestors, which they already know
305 * We use lookup_object here because we are only
306 * interested in the case we *know* the object is
307 * reachable and we have already scanned it.
309 if (((o
= lookup_object(the_repository
, remote
)) != NULL
) &&
310 (o
->flags
& COMPLETE
)) {
314 remote_hex
= oid_to_hex(remote
);
316 struct strbuf c
= STRBUF_INIT
;
317 if (multi_ack
== 2) strbuf_addstr(&c
, " multi_ack_detailed");
318 if (multi_ack
== 1) strbuf_addstr(&c
, " multi_ack");
319 if (no_done
) strbuf_addstr(&c
, " no-done");
320 if (use_sideband
== 2) strbuf_addstr(&c
, " side-band-64k");
321 if (use_sideband
== 1) strbuf_addstr(&c
, " side-band");
322 if (args
->deepen_relative
) strbuf_addstr(&c
, " deepen-relative");
323 if (args
->use_thin_pack
) strbuf_addstr(&c
, " thin-pack");
324 if (args
->no_progress
) strbuf_addstr(&c
, " no-progress");
325 if (args
->include_tag
) strbuf_addstr(&c
, " include-tag");
326 if (prefer_ofs_delta
) strbuf_addstr(&c
, " ofs-delta");
327 if (deepen_since_ok
) strbuf_addstr(&c
, " deepen-since");
328 if (deepen_not_ok
) strbuf_addstr(&c
, " deepen-not");
329 if (agent_supported
) strbuf_addf(&c
, " agent=%s",
330 git_user_agent_sanitized());
332 strbuf_addf(&c
, " session-id=%s", trace2_session_id());
333 if (args
->filter_options
.choice
)
334 strbuf_addstr(&c
, " filter");
335 packet_buf_write(&req_buf
, "want %s%s\n", remote_hex
, c
.buf
);
338 packet_buf_write(&req_buf
, "want %s\n", remote_hex
);
343 strbuf_release(&req_buf
);
348 if (is_repository_shallow(the_repository
))
349 write_shallow_commits(&req_buf
, 1, NULL
);
351 packet_buf_write(&req_buf
, "deepen %d", args
->depth
);
352 if (args
->deepen_since
) {
353 timestamp_t max_age
= approxidate(args
->deepen_since
);
354 packet_buf_write(&req_buf
, "deepen-since %"PRItime
, max_age
);
356 if (args
->deepen_not
) {
358 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
359 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
360 packet_buf_write(&req_buf
, "deepen-not %s", s
->string
);
363 if (server_supports_filtering
&& args
->filter_options
.choice
) {
365 expand_list_objects_filter_spec(&args
->filter_options
);
366 packet_buf_write(&req_buf
, "filter %s", spec
);
368 packet_buf_flush(&req_buf
);
369 state_len
= req_buf
.len
;
373 struct object_id oid
;
375 send_request(args
, fd
[1], &req_buf
);
376 while (packet_reader_read(&reader
) == PACKET_READ_NORMAL
) {
377 if (skip_prefix(reader
.line
, "shallow ", &arg
)) {
378 if (get_oid_hex(arg
, &oid
))
379 die(_("invalid shallow line: %s"), reader
.line
);
380 register_shallow(the_repository
, &oid
);
383 if (skip_prefix(reader
.line
, "unshallow ", &arg
)) {
384 if (get_oid_hex(arg
, &oid
))
385 die(_("invalid unshallow line: %s"), reader
.line
);
386 if (!lookup_object(the_repository
, &oid
))
387 die(_("object not found: %s"), reader
.line
);
388 /* make sure that it is parsed as shallow */
389 if (!parse_object(the_repository
, &oid
))
390 die(_("error in object: %s"), reader
.line
);
391 if (unregister_shallow(&oid
))
392 die(_("no shallow found: %s"), reader
.line
);
395 die(_("expected shallow/unshallow, got %s"), reader
.line
);
397 } else if (!args
->stateless_rpc
)
398 send_request(args
, fd
[1], &req_buf
);
400 if (!args
->stateless_rpc
) {
401 /* If we aren't using the stateless-rpc interface
402 * we don't need to retain the headers.
404 strbuf_setlen(&req_buf
, 0);
408 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository
);
411 while ((oid
= negotiator
->next(negotiator
))) {
412 packet_buf_write(&req_buf
, "have %s\n", oid_to_hex(oid
));
413 print_verbose(args
, "have %s", oid_to_hex(oid
));
415 if (flush_at
<= ++count
) {
418 packet_buf_flush(&req_buf
);
419 send_request(args
, fd
[1], &req_buf
);
420 strbuf_setlen(&req_buf
, state_len
);
422 flush_at
= next_flush(args
->stateless_rpc
, count
);
425 * We keep one window "ahead" of the other side, and
426 * will wait for an ACK only on the next one
428 if (!args
->stateless_rpc
&& count
== INITIAL_FLUSH
)
431 consume_shallow_list(args
, &reader
);
433 ack
= get_ack(&reader
, result_oid
);
435 print_verbose(args
, _("got %s %d %s"), "ack",
436 ack
, oid_to_hex(result_oid
));
446 struct commit
*commit
=
447 lookup_commit(the_repository
,
452 die(_("invalid commit %s"), oid_to_hex(result_oid
));
453 was_common
= negotiator
->ack(negotiator
, commit
);
454 if (args
->stateless_rpc
457 /* We need to replay the have for this object
458 * on the next RPC request so the peer knows
459 * it is in common with us.
461 const char *hex
= oid_to_hex(result_oid
);
462 packet_buf_write(&req_buf
, "have %s\n", hex
);
463 state_len
= req_buf
.len
;
465 * Reset in_vain because an ack
466 * for this commit has not been
470 } else if (!args
->stateless_rpc
471 || ack
!= ACK_common
)
475 if (ack
== ACK_ready
)
482 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
483 print_verbose(args
, _("giving up"));
491 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository
);
492 if (!got_ready
|| !no_done
) {
493 packet_buf_write(&req_buf
, "done\n");
494 send_request(args
, fd
[1], &req_buf
);
496 print_verbose(args
, _("done"));
501 strbuf_release(&req_buf
);
503 if (!got_ready
|| !no_done
)
504 consume_shallow_list(args
, &reader
);
505 while (flushes
|| multi_ack
) {
506 int ack
= get_ack(&reader
, result_oid
);
508 print_verbose(args
, _("got %s (%d) %s"), "ack",
509 ack
, oid_to_hex(result_oid
));
517 /* it is no error to fetch into a completely empty repo */
518 return count
? retval
: 0;
521 static struct commit_list
*complete
;
523 static int mark_complete(const struct object_id
*oid
)
525 struct commit
*commit
= deref_without_lazy_fetch(oid
, 1);
527 if (commit
&& !(commit
->object
.flags
& COMPLETE
)) {
528 commit
->object
.flags
|= COMPLETE
;
529 commit_list_insert(commit
, &complete
);
534 static int mark_complete_oid(const char *refname
, const struct object_id
*oid
,
535 int flag
, void *cb_data
)
537 return mark_complete(oid
);
540 static void mark_recent_complete_commits(struct fetch_pack_args
*args
,
543 while (complete
&& cutoff
<= complete
->item
->date
) {
544 print_verbose(args
, _("Marking %s as complete"),
545 oid_to_hex(&complete
->item
->object
.oid
));
546 pop_most_recent_commit(&complete
, COMPLETE
);
550 static void add_refs_to_oidset(struct oidset
*oids
, struct ref
*refs
)
552 for (; refs
; refs
= refs
->next
)
553 oidset_insert(oids
, &refs
->old_oid
);
556 static int is_unmatched_ref(const struct ref
*ref
)
558 struct object_id oid
;
560 return ref
->match_status
== REF_NOT_MATCHED
&&
561 !parse_oid_hex(ref
->name
, &oid
, &p
) &&
563 oideq(&oid
, &ref
->old_oid
);
566 static void filter_refs(struct fetch_pack_args
*args
,
568 struct ref
**sought
, int nr_sought
)
570 struct ref
*newlist
= NULL
;
571 struct ref
**newtail
= &newlist
;
572 struct ref
*unmatched
= NULL
;
573 struct ref
*ref
, *next
;
574 struct oidset tip_oids
= OIDSET_INIT
;
576 int strict
= !(allow_unadvertised_object_request
&
577 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
580 for (ref
= *refs
; ref
; ref
= next
) {
584 if (starts_with(ref
->name
, "refs/") &&
585 check_refname_format(ref
->name
, 0)) {
587 * trash or a peeled value; do not even add it to
593 while (i
< nr_sought
) {
594 int cmp
= strcmp(ref
->name
, sought
[i
]->name
);
596 break; /* definitely do not have it */
598 keep
= 1; /* definitely have it */
599 sought
[i
]->match_status
= REF_MATCHED
;
604 if (!keep
&& args
->fetch_all
&&
605 (!args
->deepen
|| !starts_with(ref
->name
, "refs/tags/")))
612 newtail
= &ref
->next
;
614 ref
->next
= unmatched
;
620 for (i
= 0; i
< nr_sought
; i
++) {
622 if (!is_unmatched_ref(ref
))
625 add_refs_to_oidset(&tip_oids
, unmatched
);
626 add_refs_to_oidset(&tip_oids
, newlist
);
631 /* Append unmatched requests to the list */
632 for (i
= 0; i
< nr_sought
; i
++) {
634 if (!is_unmatched_ref(ref
))
637 if (!strict
|| oidset_contains(&tip_oids
, &ref
->old_oid
)) {
638 ref
->match_status
= REF_MATCHED
;
639 *newtail
= copy_ref(ref
);
640 newtail
= &(*newtail
)->next
;
642 ref
->match_status
= REF_UNADVERTISED_NOT_ALLOWED
;
646 oidset_clear(&tip_oids
);
647 free_refs(unmatched
);
652 static void mark_alternate_complete(struct fetch_negotiator
*unused
,
655 mark_complete(&obj
->oid
);
658 struct loose_object_iter
{
659 struct oidset
*loose_object_set
;
664 * Mark recent commits available locally and reachable from a local ref as
667 * The cutoff time for recency is determined by this heuristic: it is the
668 * earliest commit time of the objects in refs that are commits and that we know
669 * the commit time of.
671 static void mark_complete_and_common_ref(struct fetch_negotiator
*negotiator
,
672 struct fetch_pack_args
*args
,
676 int old_save_commit_buffer
= save_commit_buffer
;
677 timestamp_t cutoff
= 0;
679 save_commit_buffer
= 0;
681 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL
);
682 for (ref
= *refs
; ref
; ref
= ref
->next
) {
685 if (!has_object_file_with_flags(&ref
->old_oid
,
687 OBJECT_INFO_SKIP_FETCH_OBJECT
))
689 o
= parse_object(the_repository
, &ref
->old_oid
);
694 * We already have it -- which may mean that we were
695 * in sync with the other side at some time after
696 * that (it is OK if we guess wrong here).
698 if (o
->type
== OBJ_COMMIT
) {
699 struct commit
*commit
= (struct commit
*)o
;
700 if (!cutoff
|| cutoff
< commit
->date
)
701 cutoff
= commit
->date
;
704 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL
);
707 * This block marks all local refs as COMPLETE, and then recursively marks all
708 * parents of those refs as COMPLETE.
710 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL
);
712 for_each_rawref(mark_complete_oid
, NULL
);
713 for_each_cached_alternate(NULL
, mark_alternate_complete
);
714 commit_list_sort_by_date(&complete
);
716 mark_recent_complete_commits(args
, cutoff
);
718 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL
);
721 * Mark all complete remote refs as common refs.
722 * Don't mark them common yet; the server has to be told so first.
724 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL
);
725 for (ref
= *refs
; ref
; ref
= ref
->next
) {
726 struct commit
*c
= deref_without_lazy_fetch(&ref
->old_oid
, 0);
728 if (!c
|| !(c
->object
.flags
& COMPLETE
))
731 negotiator
->known_common(negotiator
, c
);
733 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL
);
735 save_commit_buffer
= old_save_commit_buffer
;
739 * Returns 1 if every object pointed to by the given remote refs is available
740 * locally and reachable from a local ref, and 0 otherwise.
742 static int everything_local(struct fetch_pack_args
*args
,
748 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
749 const struct object_id
*remote
= &ref
->old_oid
;
752 o
= lookup_object(the_repository
, remote
);
753 if (!o
|| !(o
->flags
& COMPLETE
)) {
755 print_verbose(args
, "want %s (%s)", oid_to_hex(remote
),
759 print_verbose(args
, _("already have %s (%s)"), oid_to_hex(remote
),
766 static int sideband_demux(int in
, int out
, void *data
)
771 ret
= recv_sideband("fetch-pack", xd
[0], out
);
776 static void create_promisor_file(const char *keep_name
,
777 struct ref
**sought
, int nr_sought
)
779 struct strbuf promisor_name
= STRBUF_INIT
;
782 strbuf_addstr(&promisor_name
, keep_name
);
783 suffix_stripped
= strbuf_strip_suffix(&promisor_name
, ".keep");
784 if (!suffix_stripped
)
785 BUG("name of pack lockfile should end with .keep (was '%s')",
787 strbuf_addstr(&promisor_name
, ".promisor");
789 write_promisor_file(promisor_name
.buf
, sought
, nr_sought
);
791 strbuf_release(&promisor_name
);
794 static void parse_gitmodules_oids(int fd
, struct oidset
*gitmodules_oids
)
796 int len
= the_hash_algo
->hexsz
+ 1; /* hash + NL */
799 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
800 int read_len
= read_in_full(fd
, hex_hash
, len
);
801 struct object_id oid
;
807 die("invalid length read %d", read_len
);
808 if (parse_oid_hex(hex_hash
, &oid
, &end
) || *end
!= '\n')
810 oidset_insert(gitmodules_oids
, &oid
);
815 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
816 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
817 * stored there. (It must be freed by the caller.)
819 static int get_pack(struct fetch_pack_args
*args
,
820 int xd
[2], struct string_list
*pack_lockfiles
,
821 struct strvec
*index_pack_args
,
822 struct ref
**sought
, int nr_sought
,
823 struct oidset
*gitmodules_oids
)
826 int do_keep
= args
->keep_pack
;
827 const char *cmd_name
;
828 struct pack_header header
;
830 struct child_process cmd
= CHILD_PROCESS_INIT
;
831 int fsck_objects
= 0;
834 memset(&demux
, 0, sizeof(demux
));
836 /* xd[] is talking with upload-pack; subprocess reads from
837 * xd[0], spits out band#2 to stderr, and feeds us band#1
838 * through demux->out.
840 demux
.proc
= sideband_demux
;
843 demux
.isolate_sigpipe
= 1;
844 if (start_async(&demux
))
845 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
850 if (!args
->keep_pack
&& unpack_limit
&& !index_pack_args
) {
852 if (read_pack_header(demux
.out
, &header
))
853 die(_("protocol error: bad pack header"));
855 if (ntohl(header
.hdr_entries
) < unpack_limit
)
861 if (alternate_shallow_file
) {
862 strvec_push(&cmd
.args
, "--shallow-file");
863 strvec_push(&cmd
.args
, alternate_shallow_file
);
866 if (fetch_fsck_objects
>= 0
868 : transfer_fsck_objects
>= 0
869 ? transfer_fsck_objects
873 if (do_keep
|| args
->from_promisor
|| index_pack_args
|| fsck_objects
) {
874 if (pack_lockfiles
|| fsck_objects
)
876 cmd_name
= "index-pack";
877 strvec_push(&cmd
.args
, cmd_name
);
878 strvec_push(&cmd
.args
, "--stdin");
879 if (!args
->quiet
&& !args
->no_progress
)
880 strvec_push(&cmd
.args
, "-v");
881 if (args
->use_thin_pack
)
882 strvec_push(&cmd
.args
, "--fix-thin");
883 if ((do_keep
|| index_pack_args
) && (args
->lock_pack
|| unpack_limit
)) {
884 char hostname
[HOST_NAME_MAX
+ 1];
885 if (xgethostname(hostname
, sizeof(hostname
)))
886 xsnprintf(hostname
, sizeof(hostname
), "localhost");
887 strvec_pushf(&cmd
.args
,
888 "--keep=fetch-pack %"PRIuMAX
" on %s",
889 (uintmax_t)getpid(), hostname
);
891 if (!index_pack_args
&& args
->check_self_contained_and_connected
)
892 strvec_push(&cmd
.args
, "--check-self-contained-and-connected");
895 * We cannot perform any connectivity checks because
896 * not all packs have been downloaded; let the caller
897 * have this responsibility.
899 args
->check_self_contained_and_connected
= 0;
901 if (args
->from_promisor
)
903 * create_promisor_file() may be called afterwards but
904 * we still need index-pack to know that this is a
905 * promisor pack. For example, if transfer.fsckobjects
906 * is true, index-pack needs to know that .gitmodules
907 * is a promisor object (so that it won't complain if
910 strvec_push(&cmd
.args
, "--promisor");
913 cmd_name
= "unpack-objects";
914 strvec_push(&cmd
.args
, cmd_name
);
915 if (args
->quiet
|| args
->no_progress
)
916 strvec_push(&cmd
.args
, "-q");
917 args
->check_self_contained_and_connected
= 0;
921 strvec_pushf(&cmd
.args
, "--pack_header=%"PRIu32
",%"PRIu32
,
922 ntohl(header
.hdr_version
),
923 ntohl(header
.hdr_entries
));
925 if (args
->from_promisor
|| index_pack_args
)
927 * We cannot use --strict in index-pack because it
928 * checks both broken objects and links, but we only
929 * want to check for broken objects.
931 strvec_push(&cmd
.args
, "--fsck-objects");
933 strvec_pushf(&cmd
.args
, "--strict%s",
937 if (index_pack_args
) {
940 for (i
= 0; i
< cmd
.args
.nr
; i
++)
941 strvec_push(index_pack_args
, cmd
.args
.v
[i
]);
946 if (start_command(&cmd
))
947 die(_("fetch-pack: unable to fork off %s"), cmd_name
);
948 if (do_keep
&& (pack_lockfiles
|| fsck_objects
)) {
950 char *pack_lockfile
= index_pack_lockfile(cmd
.out
, &is_well_formed
);
953 die(_("fetch-pack: invalid index-pack output"));
955 string_list_append_nodup(pack_lockfiles
, pack_lockfile
);
956 parse_gitmodules_oids(cmd
.out
, gitmodules_oids
);
961 /* Closed by start_command() */
964 ret
= finish_command(&cmd
);
965 if (!ret
|| (args
->check_self_contained_and_connected
&& ret
== 1))
966 args
->self_contained_and_connected
=
967 args
->check_self_contained_and_connected
&&
970 die(_("%s failed"), cmd_name
);
971 if (use_sideband
&& finish_async(&demux
))
972 die(_("error in sideband demultiplexer"));
975 * Now that index-pack has succeeded, write the promisor file using the
976 * obtained .keep filename if necessary
978 if (do_keep
&& pack_lockfiles
&& pack_lockfiles
->nr
&& args
->from_promisor
)
979 create_promisor_file(pack_lockfiles
->items
[0].string
, sought
, nr_sought
);
984 static int cmp_ref_by_name(const void *a_
, const void *b_
)
986 const struct ref
*a
= *((const struct ref
**)a_
);
987 const struct ref
*b
= *((const struct ref
**)b_
);
988 return strcmp(a
->name
, b
->name
);
991 static struct ref
*do_fetch_pack(struct fetch_pack_args
*args
,
993 const struct ref
*orig_ref
,
994 struct ref
**sought
, int nr_sought
,
995 struct shallow_info
*si
,
996 struct string_list
*pack_lockfiles
)
998 struct repository
*r
= the_repository
;
999 struct ref
*ref
= copy_ref_list(orig_ref
);
1000 struct object_id oid
;
1001 const char *agent_feature
;
1003 struct fetch_negotiator negotiator_alloc
;
1004 struct fetch_negotiator
*negotiator
;
1006 negotiator
= &negotiator_alloc
;
1007 fetch_negotiator_init(r
, negotiator
);
1009 sort_ref_list(&ref
, ref_compare_name
);
1010 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
1012 if ((agent_feature
= server_feature_value("agent", &agent_len
))) {
1013 agent_supported
= 1;
1015 print_verbose(args
, _("Server version is %.*s"),
1016 agent_len
, agent_feature
);
1019 if (!server_supports("session-id"))
1022 if (server_supports("shallow"))
1023 print_verbose(args
, _("Server supports %s"), "shallow");
1024 else if (args
->depth
> 0 || is_repository_shallow(r
))
1025 die(_("Server does not support shallow clients"));
1026 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
1028 if (server_supports("multi_ack_detailed")) {
1029 print_verbose(args
, _("Server supports %s"), "multi_ack_detailed");
1031 if (server_supports("no-done")) {
1032 print_verbose(args
, _("Server supports %s"), "no-done");
1033 if (args
->stateless_rpc
)
1037 else if (server_supports("multi_ack")) {
1038 print_verbose(args
, _("Server supports %s"), "multi_ack");
1041 if (server_supports("side-band-64k")) {
1042 print_verbose(args
, _("Server supports %s"), "side-band-64k");
1045 else if (server_supports("side-band")) {
1046 print_verbose(args
, _("Server supports %s"), "side-band");
1049 if (server_supports("allow-tip-sha1-in-want")) {
1050 print_verbose(args
, _("Server supports %s"), "allow-tip-sha1-in-want");
1051 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1053 if (server_supports("allow-reachable-sha1-in-want")) {
1054 print_verbose(args
, _("Server supports %s"), "allow-reachable-sha1-in-want");
1055 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1057 if (server_supports("thin-pack"))
1058 print_verbose(args
, _("Server supports %s"), "thin-pack");
1060 args
->use_thin_pack
= 0;
1061 if (server_supports("no-progress"))
1062 print_verbose(args
, _("Server supports %s"), "no-progress");
1064 args
->no_progress
= 0;
1065 if (server_supports("include-tag"))
1066 print_verbose(args
, _("Server supports %s"), "include-tag");
1068 args
->include_tag
= 0;
1069 if (server_supports("ofs-delta"))
1070 print_verbose(args
, _("Server supports %s"), "ofs-delta");
1072 prefer_ofs_delta
= 0;
1074 if (server_supports("filter")) {
1075 server_supports_filtering
= 1;
1076 print_verbose(args
, _("Server supports %s"), "filter");
1077 } else if (args
->filter_options
.choice
) {
1078 warning("filtering not recognized by server, ignoring");
1081 if (server_supports("deepen-since")) {
1082 print_verbose(args
, _("Server supports %s"), "deepen-since");
1083 deepen_since_ok
= 1;
1084 } else if (args
->deepen_since
)
1085 die(_("Server does not support --shallow-since"));
1086 if (server_supports("deepen-not")) {
1087 print_verbose(args
, _("Server supports %s"), "deepen-not");
1089 } else if (args
->deepen_not
)
1090 die(_("Server does not support --shallow-exclude"));
1091 if (server_supports("deepen-relative"))
1092 print_verbose(args
, _("Server supports %s"), "deepen-relative");
1093 else if (args
->deepen_relative
)
1094 die(_("Server does not support --deepen"));
1095 if (!server_supports_hash(the_hash_algo
->name
, NULL
))
1096 die(_("Server does not support this repository's object format"));
1098 mark_complete_and_common_ref(negotiator
, args
, &ref
);
1099 filter_refs(args
, &ref
, sought
, nr_sought
);
1100 if (everything_local(args
, &ref
)) {
1101 packet_flush(fd
[1]);
1104 if (find_common(negotiator
, args
, fd
, &oid
, ref
) < 0)
1105 if (!args
->keep_pack
)
1106 /* When cloning, it is not unusual to have
1109 warning(_("no common commits"));
1111 if (args
->stateless_rpc
)
1112 packet_flush(fd
[1]);
1114 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
1116 else if (si
->nr_ours
|| si
->nr_theirs
) {
1117 if (args
->reject_shallow_remote
)
1118 die(_("source repository is shallow, reject to clone."));
1119 alternate_shallow_file
= setup_temporary_shallow(si
->shallow
);
1121 alternate_shallow_file
= NULL
;
1122 if (get_pack(args
, fd
, pack_lockfiles
, NULL
, sought
, nr_sought
,
1123 &fsck_options
.gitmodules_found
))
1124 die(_("git fetch-pack: fetch failed."));
1125 if (fsck_finish(&fsck_options
))
1130 negotiator
->release(negotiator
);
1134 static void add_shallow_requests(struct strbuf
*req_buf
,
1135 const struct fetch_pack_args
*args
)
1137 if (is_repository_shallow(the_repository
))
1138 write_shallow_commits(req_buf
, 1, NULL
);
1139 if (args
->depth
> 0)
1140 packet_buf_write(req_buf
, "deepen %d", args
->depth
);
1141 if (args
->deepen_since
) {
1142 timestamp_t max_age
= approxidate(args
->deepen_since
);
1143 packet_buf_write(req_buf
, "deepen-since %"PRItime
, max_age
);
1145 if (args
->deepen_not
) {
1147 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
1148 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
1149 packet_buf_write(req_buf
, "deepen-not %s", s
->string
);
1152 if (args
->deepen_relative
)
1153 packet_buf_write(req_buf
, "deepen-relative\n");
1156 static void add_wants(const struct ref
*wants
, struct strbuf
*req_buf
)
1158 int use_ref_in_want
= server_supports_feature("fetch", "ref-in-want", 0);
1160 for ( ; wants
; wants
= wants
->next
) {
1161 const struct object_id
*remote
= &wants
->old_oid
;
1165 * If that object is complete (i.e. it is an ancestor of a
1166 * local ref), we tell them we have it but do not have to
1167 * tell them about its ancestors, which they already know
1170 * We use lookup_object here because we are only
1171 * interested in the case we *know* the object is
1172 * reachable and we have already scanned it.
1174 if (((o
= lookup_object(the_repository
, remote
)) != NULL
) &&
1175 (o
->flags
& COMPLETE
)) {
1179 if (!use_ref_in_want
|| wants
->exact_oid
)
1180 packet_buf_write(req_buf
, "want %s\n", oid_to_hex(remote
));
1182 packet_buf_write(req_buf
, "want-ref %s\n", wants
->name
);
1186 static void add_common(struct strbuf
*req_buf
, struct oidset
*common
)
1188 struct oidset_iter iter
;
1189 const struct object_id
*oid
;
1190 oidset_iter_init(common
, &iter
);
1192 while ((oid
= oidset_iter_next(&iter
))) {
1193 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1197 static int add_haves(struct fetch_negotiator
*negotiator
,
1199 struct strbuf
*req_buf
,
1200 int *haves_to_send
, int *in_vain
)
1203 int haves_added
= 0;
1204 const struct object_id
*oid
;
1206 while ((oid
= negotiator
->next(negotiator
))) {
1207 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1208 if (++haves_added
>= *haves_to_send
)
1212 *in_vain
+= haves_added
;
1213 if (!haves_added
|| (seen_ack
&& *in_vain
>= MAX_IN_VAIN
)) {
1215 packet_buf_write(req_buf
, "done\n");
1219 /* Increase haves to send on next round */
1220 *haves_to_send
= next_flush(1, *haves_to_send
);
1225 static int send_fetch_request(struct fetch_negotiator
*negotiator
, int fd_out
,
1226 struct fetch_pack_args
*args
,
1227 const struct ref
*wants
, struct oidset
*common
,
1228 int *haves_to_send
, int *in_vain
,
1229 int sideband_all
, int seen_ack
)
1232 const char *hash_name
;
1233 struct strbuf req_buf
= STRBUF_INIT
;
1235 if (server_supports_v2("fetch", 1))
1236 packet_buf_write(&req_buf
, "command=fetch");
1237 if (server_supports_v2("agent", 0))
1238 packet_buf_write(&req_buf
, "agent=%s", git_user_agent_sanitized());
1239 if (advertise_sid
&& server_supports_v2("session-id", 0))
1240 packet_buf_write(&req_buf
, "session-id=%s", trace2_session_id());
1241 if (args
->server_options
&& args
->server_options
->nr
&&
1242 server_supports_v2("server-option", 1)) {
1244 for (i
= 0; i
< args
->server_options
->nr
; i
++)
1245 packet_buf_write(&req_buf
, "server-option=%s",
1246 args
->server_options
->items
[i
].string
);
1249 if (server_feature_v2("object-format", &hash_name
)) {
1250 int hash_algo
= hash_algo_by_name(hash_name
);
1251 if (hash_algo_by_ptr(the_hash_algo
) != hash_algo
)
1252 die(_("mismatched algorithms: client %s; server %s"),
1253 the_hash_algo
->name
, hash_name
);
1254 packet_buf_write(&req_buf
, "object-format=%s", the_hash_algo
->name
);
1255 } else if (hash_algo_by_ptr(the_hash_algo
) != GIT_HASH_SHA1
) {
1256 die(_("the server does not support algorithm '%s'"),
1257 the_hash_algo
->name
);
1260 packet_buf_delim(&req_buf
);
1261 if (args
->use_thin_pack
)
1262 packet_buf_write(&req_buf
, "thin-pack");
1263 if (args
->no_progress
)
1264 packet_buf_write(&req_buf
, "no-progress");
1265 if (args
->include_tag
)
1266 packet_buf_write(&req_buf
, "include-tag");
1267 if (prefer_ofs_delta
)
1268 packet_buf_write(&req_buf
, "ofs-delta");
1270 packet_buf_write(&req_buf
, "sideband-all");
1272 /* Add shallow-info and deepen request */
1273 if (server_supports_feature("fetch", "shallow", 0))
1274 add_shallow_requests(&req_buf
, args
);
1275 else if (is_repository_shallow(the_repository
) || args
->deepen
)
1276 die(_("Server does not support shallow requests"));
1279 if (server_supports_feature("fetch", "filter", 0) &&
1280 args
->filter_options
.choice
) {
1282 expand_list_objects_filter_spec(&args
->filter_options
);
1283 print_verbose(args
, _("Server supports filter"));
1284 packet_buf_write(&req_buf
, "filter %s", spec
);
1285 } else if (args
->filter_options
.choice
) {
1286 warning("filtering not recognized by server, ignoring");
1289 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1291 struct strbuf to_send
= STRBUF_INIT
;
1293 for (i
= 0; i
< uri_protocols
.nr
; i
++) {
1294 const char *s
= uri_protocols
.items
[i
].string
;
1296 if (!strcmp(s
, "https") || !strcmp(s
, "http")) {
1298 strbuf_addch(&to_send
, ',');
1299 strbuf_addstr(&to_send
, s
);
1303 packet_buf_write(&req_buf
, "packfile-uris %s",
1305 strbuf_release(&to_send
);
1310 add_wants(wants
, &req_buf
);
1312 /* Add all of the common commits we've found in previous rounds */
1313 add_common(&req_buf
, common
);
1315 /* Add initial haves */
1316 ret
= add_haves(negotiator
, seen_ack
, &req_buf
,
1317 haves_to_send
, in_vain
);
1320 packet_buf_flush(&req_buf
);
1321 if (write_in_full(fd_out
, req_buf
.buf
, req_buf
.len
) < 0)
1322 die_errno(_("unable to write request to remote"));
1324 strbuf_release(&req_buf
);
1329 * Processes a section header in a server's response and checks if it matches
1330 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1331 * not consumed); if 0, the line will be consumed and the function will die if
1332 * the section header doesn't match what was expected.
1334 static int process_section_header(struct packet_reader
*reader
,
1335 const char *section
, int peek
)
1339 if (packet_reader_peek(reader
) != PACKET_READ_NORMAL
)
1340 die(_("error reading section header '%s'"), section
);
1342 ret
= !strcmp(reader
->line
, section
);
1346 die(_("expected '%s', received '%s'"),
1347 section
, reader
->line
);
1348 packet_reader_read(reader
);
1356 * No commit was found to be possessed by both the client and the
1357 * server, and "ready" was not received.
1362 * At least one commit was found to be possessed by both the client and
1363 * the server, and "ready" was not received.
1368 * "ready" was received, indicating that the server is ready to send
1369 * the packfile without any further negotiation.
1374 static enum common_found
process_acks(struct fetch_negotiator
*negotiator
,
1375 struct packet_reader
*reader
,
1376 struct oidset
*common
)
1379 int received_ready
= 0;
1380 int received_ack
= 0;
1382 process_section_header(reader
, "acknowledgments", 0);
1383 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1386 if (!strcmp(reader
->line
, "NAK"))
1389 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
1390 struct object_id oid
;
1392 if (!get_oid_hex(arg
, &oid
)) {
1393 struct commit
*commit
;
1394 oidset_insert(common
, &oid
);
1395 commit
= lookup_commit(the_repository
, &oid
);
1397 negotiator
->ack(negotiator
, commit
);
1402 if (!strcmp(reader
->line
, "ready")) {
1407 die(_("unexpected acknowledgment line: '%s'"), reader
->line
);
1410 if (reader
->status
!= PACKET_READ_FLUSH
&&
1411 reader
->status
!= PACKET_READ_DELIM
)
1412 die(_("error processing acks: %d"), reader
->status
);
1415 * If an "acknowledgments" section is sent, a packfile is sent if and
1416 * only if "ready" was sent in this section. The other sections
1417 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1418 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1421 if (received_ready
&& reader
->status
!= PACKET_READ_DELIM
)
1422 die(_("expected packfile to be sent after 'ready'"));
1423 if (!received_ready
&& reader
->status
!= PACKET_READ_FLUSH
)
1424 die(_("expected no other sections to be sent after no 'ready'"));
1426 return received_ready
? READY
:
1427 (received_ack
? COMMON_FOUND
: NO_COMMON_FOUND
);
1430 static void receive_shallow_info(struct fetch_pack_args
*args
,
1431 struct packet_reader
*reader
,
1432 struct oid_array
*shallows
,
1433 struct shallow_info
*si
)
1435 int unshallow_received
= 0;
1437 process_section_header(reader
, "shallow-info", 0);
1438 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1440 struct object_id oid
;
1442 if (skip_prefix(reader
->line
, "shallow ", &arg
)) {
1443 if (get_oid_hex(arg
, &oid
))
1444 die(_("invalid shallow line: %s"), reader
->line
);
1445 oid_array_append(shallows
, &oid
);
1448 if (skip_prefix(reader
->line
, "unshallow ", &arg
)) {
1449 if (get_oid_hex(arg
, &oid
))
1450 die(_("invalid unshallow line: %s"), reader
->line
);
1451 if (!lookup_object(the_repository
, &oid
))
1452 die(_("object not found: %s"), reader
->line
);
1453 /* make sure that it is parsed as shallow */
1454 if (!parse_object(the_repository
, &oid
))
1455 die(_("error in object: %s"), reader
->line
);
1456 if (unregister_shallow(&oid
))
1457 die(_("no shallow found: %s"), reader
->line
);
1458 unshallow_received
= 1;
1461 die(_("expected shallow/unshallow, got %s"), reader
->line
);
1464 if (reader
->status
!= PACKET_READ_FLUSH
&&
1465 reader
->status
!= PACKET_READ_DELIM
)
1466 die(_("error processing shallow info: %d"), reader
->status
);
1468 if (args
->deepen
|| unshallow_received
) {
1470 * Treat these as shallow lines caused by our depth settings.
1471 * In v0, these lines cannot cause refs to be rejected; do the
1476 for (i
= 0; i
< shallows
->nr
; i
++)
1477 register_shallow(the_repository
, &shallows
->oid
[i
]);
1478 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
1481 } else if (shallows
->nr
) {
1483 * Treat these as shallow lines caused by the remote being
1484 * shallow. In v0, remote refs that reach these objects are
1485 * rejected (unless --update-shallow is set); do the same.
1487 prepare_shallow_info(si
, shallows
);
1488 if (si
->nr_ours
|| si
->nr_theirs
) {
1489 if (args
->reject_shallow_remote
)
1490 die(_("source repository is shallow, reject to clone."));
1491 alternate_shallow_file
=
1492 setup_temporary_shallow(si
->shallow
);
1494 alternate_shallow_file
= NULL
;
1496 alternate_shallow_file
= NULL
;
1500 static int cmp_name_ref(const void *name
, const void *ref
)
1502 return strcmp(name
, (*(struct ref
**)ref
)->name
);
1505 static void receive_wanted_refs(struct packet_reader
*reader
,
1506 struct ref
**sought
, int nr_sought
)
1508 process_section_header(reader
, "wanted-refs", 0);
1509 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1510 struct object_id oid
;
1514 if (parse_oid_hex(reader
->line
, &oid
, &end
) || *end
++ != ' ')
1515 die(_("expected wanted-ref, got '%s'"), reader
->line
);
1517 found
= bsearch(end
, sought
, nr_sought
, sizeof(*sought
),
1520 die(_("unexpected wanted-ref: '%s'"), reader
->line
);
1521 oidcpy(&(*found
)->old_oid
, &oid
);
1524 if (reader
->status
!= PACKET_READ_DELIM
)
1525 die(_("error processing wanted refs: %d"), reader
->status
);
1528 static void receive_packfile_uris(struct packet_reader
*reader
,
1529 struct string_list
*uris
)
1531 process_section_header(reader
, "packfile-uris", 0);
1532 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1533 if (reader
->pktlen
< the_hash_algo
->hexsz
||
1534 reader
->line
[the_hash_algo
->hexsz
] != ' ')
1535 die("expected '<hash> <uri>', got: %s\n", reader
->line
);
1537 string_list_append(uris
, reader
->line
);
1539 if (reader
->status
!= PACKET_READ_DELIM
)
1540 die("expected DELIM");
1544 FETCH_CHECK_LOCAL
= 0,
1551 static void do_check_stateless_delimiter(const struct fetch_pack_args
*args
,
1552 struct packet_reader
*reader
)
1554 check_stateless_delimiter(args
->stateless_rpc
, reader
,
1555 _("git fetch-pack: expected response end packet"));
1558 static struct ref
*do_fetch_pack_v2(struct fetch_pack_args
*args
,
1560 const struct ref
*orig_ref
,
1561 struct ref
**sought
, int nr_sought
,
1562 struct oid_array
*shallows
,
1563 struct shallow_info
*si
,
1564 struct string_list
*pack_lockfiles
)
1566 struct repository
*r
= the_repository
;
1567 struct ref
*ref
= copy_ref_list(orig_ref
);
1568 enum fetch_state state
= FETCH_CHECK_LOCAL
;
1569 struct oidset common
= OIDSET_INIT
;
1570 struct packet_reader reader
;
1571 int in_vain
= 0, negotiation_started
= 0;
1572 int haves_to_send
= INITIAL_FLUSH
;
1573 struct fetch_negotiator negotiator_alloc
;
1574 struct fetch_negotiator
*negotiator
;
1576 struct string_list packfile_uris
= STRING_LIST_INIT_DUP
;
1578 struct strvec index_pack_args
= STRVEC_INIT
;
1580 negotiator
= &negotiator_alloc
;
1581 fetch_negotiator_init(r
, negotiator
);
1583 packet_reader_init(&reader
, fd
[0], NULL
, 0,
1584 PACKET_READ_CHOMP_NEWLINE
|
1585 PACKET_READ_DIE_ON_ERR_PACKET
);
1586 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1587 server_supports_feature("fetch", "sideband-all", 0)) {
1588 reader
.use_sideband
= 1;
1589 reader
.me
= "fetch-pack";
1592 while (state
!= FETCH_DONE
) {
1594 case FETCH_CHECK_LOCAL
:
1595 sort_ref_list(&ref
, ref_compare_name
);
1596 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
1598 /* v2 supports these by default */
1599 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1601 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
1604 /* Filter 'ref' by 'sought' and those that aren't local */
1605 mark_complete_and_common_ref(negotiator
, args
, &ref
);
1606 filter_refs(args
, &ref
, sought
, nr_sought
);
1607 if (everything_local(args
, &ref
))
1610 state
= FETCH_SEND_REQUEST
;
1612 mark_tips(negotiator
, args
->negotiation_tips
);
1613 for_each_cached_alternate(negotiator
,
1614 insert_one_alternate_object
);
1616 case FETCH_SEND_REQUEST
:
1617 if (!negotiation_started
) {
1618 negotiation_started
= 1;
1619 trace2_region_enter("fetch-pack",
1623 if (send_fetch_request(negotiator
, fd
[1], args
, ref
,
1625 &haves_to_send
, &in_vain
,
1626 reader
.use_sideband
,
1628 state
= FETCH_GET_PACK
;
1630 state
= FETCH_PROCESS_ACKS
;
1632 case FETCH_PROCESS_ACKS
:
1633 /* Process ACKs/NAKs */
1634 switch (process_acks(negotiator
, &reader
, &common
)) {
1637 * Don't check for response delimiter; get_pack() will
1638 * read the rest of this response.
1640 state
= FETCH_GET_PACK
;
1646 case NO_COMMON_FOUND
:
1647 do_check_stateless_delimiter(args
, &reader
);
1648 state
= FETCH_SEND_REQUEST
;
1652 case FETCH_GET_PACK
:
1653 trace2_region_leave("fetch-pack",
1656 /* Check for shallow-info section */
1657 if (process_section_header(&reader
, "shallow-info", 1))
1658 receive_shallow_info(args
, &reader
, shallows
, si
);
1660 if (process_section_header(&reader
, "wanted-refs", 1))
1661 receive_wanted_refs(&reader
, sought
, nr_sought
);
1663 /* get the pack(s) */
1664 if (process_section_header(&reader
, "packfile-uris", 1))
1665 receive_packfile_uris(&reader
, &packfile_uris
);
1666 process_section_header(&reader
, "packfile", 0);
1667 if (get_pack(args
, fd
, pack_lockfiles
,
1668 packfile_uris
.nr
? &index_pack_args
: NULL
,
1669 sought
, nr_sought
, &fsck_options
.gitmodules_found
))
1670 die(_("git fetch-pack: fetch failed."));
1671 do_check_stateless_delimiter(args
, &reader
);
1680 for (i
= 0; i
< packfile_uris
.nr
; i
++) {
1682 struct child_process cmd
= CHILD_PROCESS_INIT
;
1683 char packname
[GIT_MAX_HEXSZ
+ 1];
1684 const char *uri
= packfile_uris
.items
[i
].string
+
1685 the_hash_algo
->hexsz
+ 1;
1687 strvec_push(&cmd
.args
, "http-fetch");
1688 strvec_pushf(&cmd
.args
, "--packfile=%.*s",
1689 (int) the_hash_algo
->hexsz
,
1690 packfile_uris
.items
[i
].string
);
1691 for (j
= 0; j
< index_pack_args
.nr
; j
++)
1692 strvec_pushf(&cmd
.args
, "--index-pack-arg=%s",
1693 index_pack_args
.v
[j
]);
1694 strvec_push(&cmd
.args
, uri
);
1698 if (start_command(&cmd
))
1699 die("fetch-pack: unable to spawn http-fetch");
1701 if (read_in_full(cmd
.out
, packname
, 5) < 0 ||
1702 memcmp(packname
, "keep\t", 5))
1703 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1705 if (read_in_full(cmd
.out
, packname
,
1706 the_hash_algo
->hexsz
+ 1) < 0 ||
1707 packname
[the_hash_algo
->hexsz
] != '\n')
1708 die("fetch-pack: expected hash then LF at end of http-fetch output");
1710 packname
[the_hash_algo
->hexsz
] = '\0';
1712 parse_gitmodules_oids(cmd
.out
, &fsck_options
.gitmodules_found
);
1716 if (finish_command(&cmd
))
1717 die("fetch-pack: unable to finish http-fetch");
1719 if (memcmp(packfile_uris
.items
[i
].string
, packname
,
1720 the_hash_algo
->hexsz
))
1721 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1722 uri
, (int) the_hash_algo
->hexsz
,
1723 packfile_uris
.items
[i
].string
);
1725 string_list_append_nodup(pack_lockfiles
,
1726 xstrfmt("%s/pack/pack-%s.keep",
1727 get_object_directory(),
1730 string_list_clear(&packfile_uris
, 0);
1731 strvec_clear(&index_pack_args
);
1733 if (fsck_finish(&fsck_options
))
1737 negotiator
->release(negotiator
);
1739 oidset_clear(&common
);
1743 static int fetch_pack_config_cb(const char *var
, const char *value
, void *cb
)
1745 if (strcmp(var
, "fetch.fsck.skiplist") == 0) {
1748 if (git_config_pathname(&path
, var
, value
))
1750 strbuf_addf(&fsck_msg_types
, "%cskiplist=%s",
1751 fsck_msg_types
.len
? ',' : '=', path
);
1756 if (skip_prefix(var
, "fetch.fsck.", &var
)) {
1757 if (is_valid_msg_type(var
, value
))
1758 strbuf_addf(&fsck_msg_types
, "%c%s=%s",
1759 fsck_msg_types
.len
? ',' : '=', var
, value
);
1761 warning("Skipping unknown msg id '%s'", var
);
1765 return git_default_config(var
, value
, cb
);
1768 static void fetch_pack_config(void)
1770 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit
);
1771 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit
);
1772 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta
);
1773 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects
);
1774 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects
);
1775 git_config_get_bool("transfer.advertisesid", &advertise_sid
);
1776 if (!uri_protocols
.nr
) {
1779 if (!git_config_get_string("fetch.uriprotocols", &str
) && str
) {
1780 string_list_split(&uri_protocols
, str
, ',', -1);
1785 git_config(fetch_pack_config_cb
, NULL
);
1788 static void fetch_pack_setup(void)
1790 static int did_setup
;
1793 fetch_pack_config();
1794 if (0 <= transfer_unpack_limit
)
1795 unpack_limit
= transfer_unpack_limit
;
1796 else if (0 <= fetch_unpack_limit
)
1797 unpack_limit
= fetch_unpack_limit
;
1801 static int remove_duplicates_in_refs(struct ref
**ref
, int nr
)
1803 struct string_list names
= STRING_LIST_INIT_NODUP
;
1806 for (src
= dst
= 0; src
< nr
; src
++) {
1807 struct string_list_item
*item
;
1808 item
= string_list_insert(&names
, ref
[src
]->name
);
1810 continue; /* already have it */
1811 item
->util
= ref
[src
];
1813 ref
[dst
] = ref
[src
];
1816 for (src
= dst
; src
< nr
; src
++)
1818 string_list_clear(&names
, 0);
1822 static void update_shallow(struct fetch_pack_args
*args
,
1823 struct ref
**sought
, int nr_sought
,
1824 struct shallow_info
*si
)
1826 struct oid_array ref
= OID_ARRAY_INIT
;
1830 if (args
->deepen
&& alternate_shallow_file
) {
1831 if (*alternate_shallow_file
== '\0') { /* --unshallow */
1832 unlink_or_warn(git_path_shallow(the_repository
));
1833 rollback_shallow_file(the_repository
, &shallow_lock
);
1835 commit_shallow_file(the_repository
, &shallow_lock
);
1836 alternate_shallow_file
= NULL
;
1840 if (!si
->shallow
|| !si
->shallow
->nr
)
1843 if (args
->cloning
) {
1845 * remote is shallow, but this is a clone, there are
1846 * no objects in repo to worry about. Accept any
1847 * shallow points that exist in the pack (iow in repo
1848 * after get_pack() and reprepare_packed_git())
1850 struct oid_array extra
= OID_ARRAY_INIT
;
1851 struct object_id
*oid
= si
->shallow
->oid
;
1852 for (i
= 0; i
< si
->shallow
->nr
; i
++)
1853 if (has_object_file(&oid
[i
]))
1854 oid_array_append(&extra
, &oid
[i
]);
1856 setup_alternate_shallow(&shallow_lock
,
1857 &alternate_shallow_file
,
1859 commit_shallow_file(the_repository
, &shallow_lock
);
1860 alternate_shallow_file
= NULL
;
1862 oid_array_clear(&extra
);
1866 if (!si
->nr_ours
&& !si
->nr_theirs
)
1869 remove_nonexistent_theirs_shallow(si
);
1870 if (!si
->nr_ours
&& !si
->nr_theirs
)
1872 for (i
= 0; i
< nr_sought
; i
++)
1873 oid_array_append(&ref
, &sought
[i
]->old_oid
);
1876 if (args
->update_shallow
) {
1878 * remote is also shallow, .git/shallow may be updated
1879 * so all refs can be accepted. Make sure we only add
1880 * shallow roots that are actually reachable from new
1883 struct oid_array extra
= OID_ARRAY_INIT
;
1884 struct object_id
*oid
= si
->shallow
->oid
;
1885 assign_shallow_commits_to_refs(si
, NULL
, NULL
);
1886 if (!si
->nr_ours
&& !si
->nr_theirs
) {
1887 oid_array_clear(&ref
);
1890 for (i
= 0; i
< si
->nr_ours
; i
++)
1891 oid_array_append(&extra
, &oid
[si
->ours
[i
]]);
1892 for (i
= 0; i
< si
->nr_theirs
; i
++)
1893 oid_array_append(&extra
, &oid
[si
->theirs
[i
]]);
1894 setup_alternate_shallow(&shallow_lock
,
1895 &alternate_shallow_file
,
1897 commit_shallow_file(the_repository
, &shallow_lock
);
1898 oid_array_clear(&extra
);
1899 oid_array_clear(&ref
);
1900 alternate_shallow_file
= NULL
;
1905 * remote is also shallow, check what ref is safe to update
1906 * without updating .git/shallow
1908 CALLOC_ARRAY(status
, nr_sought
);
1909 assign_shallow_commits_to_refs(si
, NULL
, status
);
1910 if (si
->nr_ours
|| si
->nr_theirs
) {
1911 for (i
= 0; i
< nr_sought
; i
++)
1913 sought
[i
]->status
= REF_STATUS_REJECT_SHALLOW
;
1916 oid_array_clear(&ref
);
1919 static int iterate_ref_map(void *cb_data
, struct object_id
*oid
)
1921 struct ref
**rm
= cb_data
;
1922 struct ref
*ref
= *rm
;
1925 return -1; /* end of the list */
1927 oidcpy(oid
, &ref
->old_oid
);
1931 struct ref
*fetch_pack(struct fetch_pack_args
*args
,
1933 const struct ref
*ref
,
1934 struct ref
**sought
, int nr_sought
,
1935 struct oid_array
*shallow
,
1936 struct string_list
*pack_lockfiles
,
1937 enum protocol_version version
)
1939 struct ref
*ref_cpy
;
1940 struct shallow_info si
;
1941 struct oid_array shallows_scratch
= OID_ARRAY_INIT
;
1945 nr_sought
= remove_duplicates_in_refs(sought
, nr_sought
);
1947 if (version
!= protocol_v2
&& !ref
) {
1948 packet_flush(fd
[1]);
1949 die(_("no matching remote head"));
1951 if (version
== protocol_v2
) {
1953 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1954 memset(&si
, 0, sizeof(si
));
1955 ref_cpy
= do_fetch_pack_v2(args
, fd
, ref
, sought
, nr_sought
,
1956 &shallows_scratch
, &si
,
1959 prepare_shallow_info(&si
, shallow
);
1960 ref_cpy
= do_fetch_pack(args
, fd
, ref
, sought
, nr_sought
,
1961 &si
, pack_lockfiles
);
1963 reprepare_packed_git(the_repository
);
1965 if (!args
->cloning
&& args
->deepen
) {
1966 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
1967 struct ref
*iterator
= ref_cpy
;
1968 opt
.shallow_file
= alternate_shallow_file
;
1970 opt
.is_deepening_fetch
= 1;
1971 if (check_connected(iterate_ref_map
, &iterator
, &opt
)) {
1972 error(_("remote did not send all necessary objects"));
1975 rollback_shallow_file(the_repository
, &shallow_lock
);
1978 args
->connectivity_checked
= 1;
1981 update_shallow(args
, sought
, nr_sought
, &si
);
1983 clear_shallow_info(&si
);
1984 oid_array_clear(&shallows_scratch
);
1988 int report_unmatched_refs(struct ref
**sought
, int nr_sought
)
1992 for (i
= 0; i
< nr_sought
; i
++) {
1995 switch (sought
[i
]->match_status
) {
1998 case REF_NOT_MATCHED
:
1999 error(_("no such remote ref %s"), sought
[i
]->name
);
2001 case REF_UNADVERTISED_NOT_ALLOWED
:
2002 error(_("Server does not allow request for unadvertised object %s"),