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"
26 #include "commit-reach.h"
27 #include "commit-graph.h"
29 static int transfer_unpack_limit
= -1;
30 static int fetch_unpack_limit
= -1;
31 static int unpack_limit
= 100;
32 static int prefer_ofs_delta
= 1;
34 static int deepen_since_ok
;
35 static int deepen_not_ok
;
36 static int fetch_fsck_objects
= -1;
37 static int transfer_fsck_objects
= -1;
38 static int agent_supported
;
39 static int server_supports_filtering
;
40 static int advertise_sid
;
41 static struct shallow_lock shallow_lock
;
42 static const char *alternate_shallow_file
;
43 static struct fsck_options fsck_options
= FSCK_OPTIONS_MISSING_GITMODULES
;
44 static struct strbuf fsck_msg_types
= STRBUF_INIT
;
45 static struct string_list uri_protocols
= STRING_LIST_INIT_DUP
;
47 /* Remember to update object flag allocation in object.h */
48 #define COMPLETE (1U << 0)
49 #define ALTERNATE (1U << 1)
50 #define COMMON (1U << 6)
51 #define REACH_SCRATCH (1U << 7)
54 * After sending this many "have"s if we do not get any new ACK , we
55 * give up traversing our history.
57 #define MAX_IN_VAIN 256
59 static int multi_ack
, use_sideband
;
60 /* Allow specifying sha1 if it is a ref tip. */
61 #define ALLOW_TIP_SHA1 01
62 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
63 #define ALLOW_REACHABLE_SHA1 02
64 static unsigned int allow_unadvertised_object_request
;
66 __attribute__((format (printf
, 2, 3)))
67 static inline void print_verbose(const struct fetch_pack_args
*args
,
75 va_start(params
, fmt
);
76 vfprintf(stderr
, fmt
, params
);
81 struct alternate_object_cache
{
82 struct object
**items
;
86 static void cache_one_alternate(const struct object_id
*oid
,
89 struct alternate_object_cache
*cache
= vcache
;
90 struct object
*obj
= parse_object(the_repository
, oid
);
92 if (!obj
|| (obj
->flags
& ALTERNATE
))
95 obj
->flags
|= ALTERNATE
;
96 ALLOC_GROW(cache
->items
, cache
->nr
+ 1, cache
->alloc
);
97 cache
->items
[cache
->nr
++] = obj
;
100 static void for_each_cached_alternate(struct fetch_negotiator
*negotiator
,
101 void (*cb
)(struct fetch_negotiator
*,
104 static int initialized
;
105 static struct alternate_object_cache cache
;
109 for_each_alternate_ref(cache_one_alternate
, &cache
);
113 for (i
= 0; i
< cache
.nr
; i
++)
114 cb(negotiator
, cache
.items
[i
]);
117 static struct commit
*deref_without_lazy_fetch(const struct object_id
*oid
,
118 int mark_tags_complete
)
120 enum object_type type
;
121 struct object_info info
= { .typep
= &type
};
122 struct commit
*commit
;
124 commit
= lookup_commit_in_graph(the_repository
, oid
);
129 if (oid_object_info_extended(the_repository
, oid
, &info
,
130 OBJECT_INFO_SKIP_FETCH_OBJECT
| OBJECT_INFO_QUICK
))
132 if (type
== OBJ_TAG
) {
133 struct tag
*tag
= (struct tag
*)
134 parse_object(the_repository
, oid
);
138 if (mark_tags_complete
)
139 tag
->object
.flags
|= COMPLETE
;
140 oid
= &tag
->tagged
->oid
;
146 if (type
== OBJ_COMMIT
) {
147 struct commit
*commit
= lookup_commit(the_repository
, oid
);
148 if (!commit
|| repo_parse_commit(the_repository
, commit
))
156 static int rev_list_insert_ref(struct fetch_negotiator
*negotiator
,
157 const struct object_id
*oid
)
159 struct commit
*c
= deref_without_lazy_fetch(oid
, 0);
162 negotiator
->add_tip(negotiator
, c
);
166 static int rev_list_insert_ref_oid(const char *refname
, const struct object_id
*oid
,
167 int flag
, void *cb_data
)
169 return rev_list_insert_ref(cb_data
, oid
);
180 static void consume_shallow_list(struct fetch_pack_args
*args
,
181 struct packet_reader
*reader
)
183 if (args
->stateless_rpc
&& args
->deepen
) {
184 /* If we sent a depth we will get back "duplicate"
185 * shallow and unshallow commands every time there
186 * is a block of have lines exchanged.
188 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
189 if (starts_with(reader
->line
, "shallow "))
191 if (starts_with(reader
->line
, "unshallow "))
193 die(_("git fetch-pack: expected shallow list"));
195 if (reader
->status
!= PACKET_READ_FLUSH
)
196 die(_("git fetch-pack: expected a flush packet after shallow list"));
200 static enum ack_type
get_ack(struct packet_reader
*reader
,
201 struct object_id
*result_oid
)
206 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
207 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
208 len
= reader
->pktlen
;
210 if (!strcmp(reader
->line
, "NAK"))
212 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
214 if (!parse_oid_hex(arg
, result_oid
, &p
)) {
215 len
-= p
- reader
->line
;
218 if (strstr(p
, "continue"))
220 if (strstr(p
, "common"))
222 if (strstr(p
, "ready"))
227 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader
->line
);
230 static void send_request(struct fetch_pack_args
*args
,
231 int fd
, struct strbuf
*buf
)
233 if (args
->stateless_rpc
) {
234 send_sideband(fd
, -1, buf
->buf
, buf
->len
, LARGE_PACKET_MAX
);
237 if (write_in_full(fd
, buf
->buf
, buf
->len
) < 0)
238 die_errno(_("unable to write to remote"));
242 static void insert_one_alternate_object(struct fetch_negotiator
*negotiator
,
245 rev_list_insert_ref(negotiator
, &obj
->oid
);
248 #define INITIAL_FLUSH 16
249 #define PIPESAFE_FLUSH 32
250 #define LARGE_FLUSH 16384
252 static int next_flush(int stateless_rpc
, int count
)
255 if (count
< LARGE_FLUSH
)
258 count
= count
* 11 / 10;
260 if (count
< PIPESAFE_FLUSH
)
263 count
+= PIPESAFE_FLUSH
;
268 static void mark_tips(struct fetch_negotiator
*negotiator
,
269 const struct oid_array
*negotiation_tips
)
273 if (!negotiation_tips
) {
274 for_each_rawref(rev_list_insert_ref_oid
, negotiator
);
278 for (i
= 0; i
< negotiation_tips
->nr
; i
++)
279 rev_list_insert_ref(negotiator
, &negotiation_tips
->oid
[i
]);
283 static int find_common(struct fetch_negotiator
*negotiator
,
284 struct fetch_pack_args
*args
,
285 int fd
[2], struct object_id
*result_oid
,
289 int count
= 0, flushes
= 0, flush_at
= INITIAL_FLUSH
, retval
;
290 const struct object_id
*oid
;
291 unsigned in_vain
= 0;
292 int got_continue
= 0;
294 struct strbuf req_buf
= STRBUF_INIT
;
295 size_t state_len
= 0;
296 struct packet_reader reader
;
298 if (args
->stateless_rpc
&& multi_ack
== 1)
299 die(_("--stateless-rpc requires multi_ack_detailed"));
301 packet_reader_init(&reader
, fd
[0], NULL
, 0,
302 PACKET_READ_CHOMP_NEWLINE
|
303 PACKET_READ_DIE_ON_ERR_PACKET
);
305 mark_tips(negotiator
, args
->negotiation_tips
);
306 for_each_cached_alternate(negotiator
, insert_one_alternate_object
);
309 for ( ; refs
; refs
= refs
->next
) {
310 struct object_id
*remote
= &refs
->old_oid
;
311 const char *remote_hex
;
315 * If that object is complete (i.e. it is an ancestor of a
316 * local ref), we tell them we have it but do not have to
317 * tell them about its ancestors, which they already know
320 * We use lookup_object here because we are only
321 * interested in the case we *know* the object is
322 * reachable and we have already scanned it.
324 if (((o
= lookup_object(the_repository
, remote
)) != NULL
) &&
325 (o
->flags
& COMPLETE
)) {
329 remote_hex
= oid_to_hex(remote
);
331 struct strbuf c
= STRBUF_INIT
;
332 if (multi_ack
== 2) strbuf_addstr(&c
, " multi_ack_detailed");
333 if (multi_ack
== 1) strbuf_addstr(&c
, " multi_ack");
334 if (no_done
) strbuf_addstr(&c
, " no-done");
335 if (use_sideband
== 2) strbuf_addstr(&c
, " side-band-64k");
336 if (use_sideband
== 1) strbuf_addstr(&c
, " side-band");
337 if (args
->deepen_relative
) strbuf_addstr(&c
, " deepen-relative");
338 if (args
->use_thin_pack
) strbuf_addstr(&c
, " thin-pack");
339 if (args
->no_progress
) strbuf_addstr(&c
, " no-progress");
340 if (args
->include_tag
) strbuf_addstr(&c
, " include-tag");
341 if (prefer_ofs_delta
) strbuf_addstr(&c
, " ofs-delta");
342 if (deepen_since_ok
) strbuf_addstr(&c
, " deepen-since");
343 if (deepen_not_ok
) strbuf_addstr(&c
, " deepen-not");
344 if (agent_supported
) strbuf_addf(&c
, " agent=%s",
345 git_user_agent_sanitized());
347 strbuf_addf(&c
, " session-id=%s", trace2_session_id());
348 if (args
->filter_options
.choice
)
349 strbuf_addstr(&c
, " filter");
350 packet_buf_write(&req_buf
, "want %s%s\n", remote_hex
, c
.buf
);
353 packet_buf_write(&req_buf
, "want %s\n", remote_hex
);
358 strbuf_release(&req_buf
);
363 if (is_repository_shallow(the_repository
))
364 write_shallow_commits(&req_buf
, 1, NULL
);
366 packet_buf_write(&req_buf
, "deepen %d", args
->depth
);
367 if (args
->deepen_since
) {
368 timestamp_t max_age
= approxidate(args
->deepen_since
);
369 packet_buf_write(&req_buf
, "deepen-since %"PRItime
, max_age
);
371 if (args
->deepen_not
) {
373 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
374 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
375 packet_buf_write(&req_buf
, "deepen-not %s", s
->string
);
378 if (server_supports_filtering
&& args
->filter_options
.choice
) {
380 expand_list_objects_filter_spec(&args
->filter_options
);
381 packet_buf_write(&req_buf
, "filter %s", spec
);
383 packet_buf_flush(&req_buf
);
384 state_len
= req_buf
.len
;
388 struct object_id oid
;
390 send_request(args
, fd
[1], &req_buf
);
391 while (packet_reader_read(&reader
) == PACKET_READ_NORMAL
) {
392 if (skip_prefix(reader
.line
, "shallow ", &arg
)) {
393 if (get_oid_hex(arg
, &oid
))
394 die(_("invalid shallow line: %s"), reader
.line
);
395 register_shallow(the_repository
, &oid
);
398 if (skip_prefix(reader
.line
, "unshallow ", &arg
)) {
399 if (get_oid_hex(arg
, &oid
))
400 die(_("invalid unshallow line: %s"), reader
.line
);
401 if (!lookup_object(the_repository
, &oid
))
402 die(_("object not found: %s"), reader
.line
);
403 /* make sure that it is parsed as shallow */
404 if (!parse_object(the_repository
, &oid
))
405 die(_("error in object: %s"), reader
.line
);
406 if (unregister_shallow(&oid
))
407 die(_("no shallow found: %s"), reader
.line
);
410 die(_("expected shallow/unshallow, got %s"), reader
.line
);
412 } else if (!args
->stateless_rpc
)
413 send_request(args
, fd
[1], &req_buf
);
415 if (!args
->stateless_rpc
) {
416 /* If we aren't using the stateless-rpc interface
417 * we don't need to retain the headers.
419 strbuf_setlen(&req_buf
, 0);
423 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository
);
426 while ((oid
= negotiator
->next(negotiator
))) {
427 packet_buf_write(&req_buf
, "have %s\n", oid_to_hex(oid
));
428 print_verbose(args
, "have %s", oid_to_hex(oid
));
430 if (flush_at
<= ++count
) {
433 packet_buf_flush(&req_buf
);
434 send_request(args
, fd
[1], &req_buf
);
435 strbuf_setlen(&req_buf
, state_len
);
437 flush_at
= next_flush(args
->stateless_rpc
, count
);
440 * We keep one window "ahead" of the other side, and
441 * will wait for an ACK only on the next one
443 if (!args
->stateless_rpc
&& count
== INITIAL_FLUSH
)
446 consume_shallow_list(args
, &reader
);
448 ack
= get_ack(&reader
, result_oid
);
450 print_verbose(args
, _("got %s %d %s"), "ack",
451 ack
, oid_to_hex(result_oid
));
461 struct commit
*commit
=
462 lookup_commit(the_repository
,
467 die(_("invalid commit %s"), oid_to_hex(result_oid
));
468 was_common
= negotiator
->ack(negotiator
, commit
);
469 if (args
->stateless_rpc
472 /* We need to replay the have for this object
473 * on the next RPC request so the peer knows
474 * it is in common with us.
476 const char *hex
= oid_to_hex(result_oid
);
477 packet_buf_write(&req_buf
, "have %s\n", hex
);
478 state_len
= req_buf
.len
;
480 * Reset in_vain because an ack
481 * for this commit has not been
485 } else if (!args
->stateless_rpc
486 || ack
!= ACK_common
)
490 if (ack
== ACK_ready
)
497 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
498 print_verbose(args
, _("giving up"));
506 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository
);
507 if (!got_ready
|| !no_done
) {
508 packet_buf_write(&req_buf
, "done\n");
509 send_request(args
, fd
[1], &req_buf
);
511 print_verbose(args
, _("done"));
516 strbuf_release(&req_buf
);
518 if (!got_ready
|| !no_done
)
519 consume_shallow_list(args
, &reader
);
520 while (flushes
|| multi_ack
) {
521 int ack
= get_ack(&reader
, result_oid
);
523 print_verbose(args
, _("got %s (%d) %s"), "ack",
524 ack
, oid_to_hex(result_oid
));
532 /* it is no error to fetch into a completely empty repo */
533 return count
? retval
: 0;
536 static struct commit_list
*complete
;
538 static int mark_complete(const struct object_id
*oid
)
540 struct commit
*commit
= deref_without_lazy_fetch(oid
, 1);
542 if (commit
&& !(commit
->object
.flags
& COMPLETE
)) {
543 commit
->object
.flags
|= COMPLETE
;
544 commit_list_insert(commit
, &complete
);
549 static int mark_complete_oid(const char *refname
, const struct object_id
*oid
,
550 int flag
, void *cb_data
)
552 return mark_complete(oid
);
555 static void mark_recent_complete_commits(struct fetch_pack_args
*args
,
558 while (complete
&& cutoff
<= complete
->item
->date
) {
559 print_verbose(args
, _("Marking %s as complete"),
560 oid_to_hex(&complete
->item
->object
.oid
));
561 pop_most_recent_commit(&complete
, COMPLETE
);
565 static void add_refs_to_oidset(struct oidset
*oids
, struct ref
*refs
)
567 for (; refs
; refs
= refs
->next
)
568 oidset_insert(oids
, &refs
->old_oid
);
571 static int is_unmatched_ref(const struct ref
*ref
)
573 struct object_id oid
;
575 return ref
->match_status
== REF_NOT_MATCHED
&&
576 !parse_oid_hex(ref
->name
, &oid
, &p
) &&
578 oideq(&oid
, &ref
->old_oid
);
581 static void filter_refs(struct fetch_pack_args
*args
,
583 struct ref
**sought
, int nr_sought
)
585 struct ref
*newlist
= NULL
;
586 struct ref
**newtail
= &newlist
;
587 struct ref
*unmatched
= NULL
;
588 struct ref
*ref
, *next
;
589 struct oidset tip_oids
= OIDSET_INIT
;
591 int strict
= !(allow_unadvertised_object_request
&
592 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
595 for (ref
= *refs
; ref
; ref
= next
) {
599 if (starts_with(ref
->name
, "refs/") &&
600 check_refname_format(ref
->name
, 0)) {
602 * trash or a peeled value; do not even add it to
608 while (i
< nr_sought
) {
609 int cmp
= strcmp(ref
->name
, sought
[i
]->name
);
611 break; /* definitely do not have it */
613 keep
= 1; /* definitely have it */
614 sought
[i
]->match_status
= REF_MATCHED
;
619 if (!keep
&& args
->fetch_all
&&
620 (!args
->deepen
|| !starts_with(ref
->name
, "refs/tags/")))
627 newtail
= &ref
->next
;
629 ref
->next
= unmatched
;
635 for (i
= 0; i
< nr_sought
; i
++) {
637 if (!is_unmatched_ref(ref
))
640 add_refs_to_oidset(&tip_oids
, unmatched
);
641 add_refs_to_oidset(&tip_oids
, newlist
);
646 /* Append unmatched requests to the list */
647 for (i
= 0; i
< nr_sought
; i
++) {
649 if (!is_unmatched_ref(ref
))
652 if (!strict
|| oidset_contains(&tip_oids
, &ref
->old_oid
)) {
653 ref
->match_status
= REF_MATCHED
;
654 *newtail
= copy_ref(ref
);
655 newtail
= &(*newtail
)->next
;
657 ref
->match_status
= REF_UNADVERTISED_NOT_ALLOWED
;
661 oidset_clear(&tip_oids
);
662 free_refs(unmatched
);
667 static void mark_alternate_complete(struct fetch_negotiator
*unused
,
670 mark_complete(&obj
->oid
);
673 struct loose_object_iter
{
674 struct oidset
*loose_object_set
;
679 * Mark recent commits available locally and reachable from a local ref as
682 * The cutoff time for recency is determined by this heuristic: it is the
683 * earliest commit time of the objects in refs that are commits and that we know
684 * the commit time of.
686 static void mark_complete_and_common_ref(struct fetch_negotiator
*negotiator
,
687 struct fetch_pack_args
*args
,
691 int old_save_commit_buffer
= save_commit_buffer
;
692 timestamp_t cutoff
= 0;
694 save_commit_buffer
= 0;
696 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL
);
697 for (ref
= *refs
; ref
; ref
= ref
->next
) {
700 if (!has_object_file_with_flags(&ref
->old_oid
,
702 OBJECT_INFO_SKIP_FETCH_OBJECT
))
704 o
= parse_object(the_repository
, &ref
->old_oid
);
709 * We already have it -- which may mean that we were
710 * in sync with the other side at some time after
711 * that (it is OK if we guess wrong here).
713 if (o
->type
== OBJ_COMMIT
) {
714 struct commit
*commit
= (struct commit
*)o
;
715 if (!cutoff
|| cutoff
< commit
->date
)
716 cutoff
= commit
->date
;
719 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL
);
722 * This block marks all local refs as COMPLETE, and then recursively marks all
723 * parents of those refs as COMPLETE.
725 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL
);
727 for_each_rawref(mark_complete_oid
, NULL
);
728 for_each_cached_alternate(NULL
, mark_alternate_complete
);
729 commit_list_sort_by_date(&complete
);
731 mark_recent_complete_commits(args
, cutoff
);
733 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL
);
736 * Mark all complete remote refs as common refs.
737 * Don't mark them common yet; the server has to be told so first.
739 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL
);
740 for (ref
= *refs
; ref
; ref
= ref
->next
) {
741 struct commit
*c
= deref_without_lazy_fetch(&ref
->old_oid
, 0);
743 if (!c
|| !(c
->object
.flags
& COMPLETE
))
746 negotiator
->known_common(negotiator
, c
);
748 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL
);
750 save_commit_buffer
= old_save_commit_buffer
;
754 * Returns 1 if every object pointed to by the given remote refs is available
755 * locally and reachable from a local ref, and 0 otherwise.
757 static int everything_local(struct fetch_pack_args
*args
,
763 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
764 const struct object_id
*remote
= &ref
->old_oid
;
767 o
= lookup_object(the_repository
, remote
);
768 if (!o
|| !(o
->flags
& COMPLETE
)) {
770 print_verbose(args
, "want %s (%s)", oid_to_hex(remote
),
774 print_verbose(args
, _("already have %s (%s)"), oid_to_hex(remote
),
781 static int sideband_demux(int in
, int out
, void *data
)
786 ret
= recv_sideband("fetch-pack", xd
[0], out
);
791 static void create_promisor_file(const char *keep_name
,
792 struct ref
**sought
, int nr_sought
)
794 struct strbuf promisor_name
= STRBUF_INIT
;
797 strbuf_addstr(&promisor_name
, keep_name
);
798 suffix_stripped
= strbuf_strip_suffix(&promisor_name
, ".keep");
799 if (!suffix_stripped
)
800 BUG("name of pack lockfile should end with .keep (was '%s')",
802 strbuf_addstr(&promisor_name
, ".promisor");
804 write_promisor_file(promisor_name
.buf
, sought
, nr_sought
);
806 strbuf_release(&promisor_name
);
809 static void parse_gitmodules_oids(int fd
, struct oidset
*gitmodules_oids
)
811 int len
= the_hash_algo
->hexsz
+ 1; /* hash + NL */
814 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
815 int read_len
= read_in_full(fd
, hex_hash
, len
);
816 struct object_id oid
;
822 die("invalid length read %d", read_len
);
823 if (parse_oid_hex(hex_hash
, &oid
, &end
) || *end
!= '\n')
825 oidset_insert(gitmodules_oids
, &oid
);
830 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
831 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
832 * stored there. (It must be freed by the caller.)
834 static int get_pack(struct fetch_pack_args
*args
,
835 int xd
[2], struct string_list
*pack_lockfiles
,
836 struct strvec
*index_pack_args
,
837 struct ref
**sought
, int nr_sought
,
838 struct oidset
*gitmodules_oids
)
841 int do_keep
= args
->keep_pack
;
842 const char *cmd_name
;
843 struct pack_header header
;
845 struct child_process cmd
= CHILD_PROCESS_INIT
;
846 int fsck_objects
= 0;
849 memset(&demux
, 0, sizeof(demux
));
851 /* xd[] is talking with upload-pack; subprocess reads from
852 * xd[0], spits out band#2 to stderr, and feeds us band#1
853 * through demux->out.
855 demux
.proc
= sideband_demux
;
858 demux
.isolate_sigpipe
= 1;
859 if (start_async(&demux
))
860 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
865 if (!args
->keep_pack
&& unpack_limit
&& !index_pack_args
) {
867 if (read_pack_header(demux
.out
, &header
))
868 die(_("protocol error: bad pack header"));
870 if (ntohl(header
.hdr_entries
) < unpack_limit
)
876 if (alternate_shallow_file
) {
877 strvec_push(&cmd
.args
, "--shallow-file");
878 strvec_push(&cmd
.args
, alternate_shallow_file
);
881 if (fetch_fsck_objects
>= 0
883 : transfer_fsck_objects
>= 0
884 ? transfer_fsck_objects
888 if (do_keep
|| args
->from_promisor
|| index_pack_args
|| fsck_objects
) {
889 if (pack_lockfiles
|| fsck_objects
)
891 cmd_name
= "index-pack";
892 strvec_push(&cmd
.args
, cmd_name
);
893 strvec_push(&cmd
.args
, "--stdin");
894 if (!args
->quiet
&& !args
->no_progress
)
895 strvec_push(&cmd
.args
, "-v");
896 if (args
->use_thin_pack
)
897 strvec_push(&cmd
.args
, "--fix-thin");
898 if ((do_keep
|| index_pack_args
) && (args
->lock_pack
|| unpack_limit
)) {
899 char hostname
[HOST_NAME_MAX
+ 1];
900 if (xgethostname(hostname
, sizeof(hostname
)))
901 xsnprintf(hostname
, sizeof(hostname
), "localhost");
902 strvec_pushf(&cmd
.args
,
903 "--keep=fetch-pack %"PRIuMAX
" on %s",
904 (uintmax_t)getpid(), hostname
);
906 if (!index_pack_args
&& args
->check_self_contained_and_connected
)
907 strvec_push(&cmd
.args
, "--check-self-contained-and-connected");
910 * We cannot perform any connectivity checks because
911 * not all packs have been downloaded; let the caller
912 * have this responsibility.
914 args
->check_self_contained_and_connected
= 0;
916 if (args
->from_promisor
)
918 * create_promisor_file() may be called afterwards but
919 * we still need index-pack to know that this is a
920 * promisor pack. For example, if transfer.fsckobjects
921 * is true, index-pack needs to know that .gitmodules
922 * is a promisor object (so that it won't complain if
925 strvec_push(&cmd
.args
, "--promisor");
928 cmd_name
= "unpack-objects";
929 strvec_push(&cmd
.args
, cmd_name
);
930 if (args
->quiet
|| args
->no_progress
)
931 strvec_push(&cmd
.args
, "-q");
932 args
->check_self_contained_and_connected
= 0;
936 strvec_pushf(&cmd
.args
, "--pack_header=%"PRIu32
",%"PRIu32
,
937 ntohl(header
.hdr_version
),
938 ntohl(header
.hdr_entries
));
940 if (args
->from_promisor
|| index_pack_args
)
942 * We cannot use --strict in index-pack because it
943 * checks both broken objects and links, but we only
944 * want to check for broken objects.
946 strvec_push(&cmd
.args
, "--fsck-objects");
948 strvec_pushf(&cmd
.args
, "--strict%s",
952 if (index_pack_args
) {
955 for (i
= 0; i
< cmd
.args
.nr
; i
++)
956 strvec_push(index_pack_args
, cmd
.args
.v
[i
]);
961 if (start_command(&cmd
))
962 die(_("fetch-pack: unable to fork off %s"), cmd_name
);
963 if (do_keep
&& (pack_lockfiles
|| fsck_objects
)) {
965 char *pack_lockfile
= index_pack_lockfile(cmd
.out
, &is_well_formed
);
968 die(_("fetch-pack: invalid index-pack output"));
970 string_list_append_nodup(pack_lockfiles
, pack_lockfile
);
971 parse_gitmodules_oids(cmd
.out
, gitmodules_oids
);
976 /* Closed by start_command() */
979 ret
= finish_command(&cmd
);
980 if (!ret
|| (args
->check_self_contained_and_connected
&& ret
== 1))
981 args
->self_contained_and_connected
=
982 args
->check_self_contained_and_connected
&&
985 die(_("%s failed"), cmd_name
);
986 if (use_sideband
&& finish_async(&demux
))
987 die(_("error in sideband demultiplexer"));
990 * Now that index-pack has succeeded, write the promisor file using the
991 * obtained .keep filename if necessary
993 if (do_keep
&& pack_lockfiles
&& pack_lockfiles
->nr
&& args
->from_promisor
)
994 create_promisor_file(pack_lockfiles
->items
[0].string
, sought
, nr_sought
);
999 static int cmp_ref_by_name(const void *a_
, const void *b_
)
1001 const struct ref
*a
= *((const struct ref
**)a_
);
1002 const struct ref
*b
= *((const struct ref
**)b_
);
1003 return strcmp(a
->name
, b
->name
);
1006 static struct ref
*do_fetch_pack(struct fetch_pack_args
*args
,
1008 const struct ref
*orig_ref
,
1009 struct ref
**sought
, int nr_sought
,
1010 struct shallow_info
*si
,
1011 struct string_list
*pack_lockfiles
)
1013 struct repository
*r
= the_repository
;
1014 struct ref
*ref
= copy_ref_list(orig_ref
);
1015 struct object_id oid
;
1016 const char *agent_feature
;
1018 struct fetch_negotiator negotiator_alloc
;
1019 struct fetch_negotiator
*negotiator
;
1021 negotiator
= &negotiator_alloc
;
1022 fetch_negotiator_init(r
, negotiator
);
1024 sort_ref_list(&ref
, ref_compare_name
);
1025 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
1027 if ((agent_feature
= server_feature_value("agent", &agent_len
))) {
1028 agent_supported
= 1;
1030 print_verbose(args
, _("Server version is %.*s"),
1031 agent_len
, agent_feature
);
1034 if (!server_supports("session-id"))
1037 if (server_supports("shallow"))
1038 print_verbose(args
, _("Server supports %s"), "shallow");
1039 else if (args
->depth
> 0 || is_repository_shallow(r
))
1040 die(_("Server does not support shallow clients"));
1041 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
1043 if (server_supports("multi_ack_detailed")) {
1044 print_verbose(args
, _("Server supports %s"), "multi_ack_detailed");
1046 if (server_supports("no-done")) {
1047 print_verbose(args
, _("Server supports %s"), "no-done");
1048 if (args
->stateless_rpc
)
1052 else if (server_supports("multi_ack")) {
1053 print_verbose(args
, _("Server supports %s"), "multi_ack");
1056 if (server_supports("side-band-64k")) {
1057 print_verbose(args
, _("Server supports %s"), "side-band-64k");
1060 else if (server_supports("side-band")) {
1061 print_verbose(args
, _("Server supports %s"), "side-band");
1064 if (server_supports("allow-tip-sha1-in-want")) {
1065 print_verbose(args
, _("Server supports %s"), "allow-tip-sha1-in-want");
1066 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1068 if (server_supports("allow-reachable-sha1-in-want")) {
1069 print_verbose(args
, _("Server supports %s"), "allow-reachable-sha1-in-want");
1070 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1072 if (server_supports("thin-pack"))
1073 print_verbose(args
, _("Server supports %s"), "thin-pack");
1075 args
->use_thin_pack
= 0;
1076 if (server_supports("no-progress"))
1077 print_verbose(args
, _("Server supports %s"), "no-progress");
1079 args
->no_progress
= 0;
1080 if (server_supports("include-tag"))
1081 print_verbose(args
, _("Server supports %s"), "include-tag");
1083 args
->include_tag
= 0;
1084 if (server_supports("ofs-delta"))
1085 print_verbose(args
, _("Server supports %s"), "ofs-delta");
1087 prefer_ofs_delta
= 0;
1089 if (server_supports("filter")) {
1090 server_supports_filtering
= 1;
1091 print_verbose(args
, _("Server supports %s"), "filter");
1092 } else if (args
->filter_options
.choice
) {
1093 warning("filtering not recognized by server, ignoring");
1096 if (server_supports("deepen-since")) {
1097 print_verbose(args
, _("Server supports %s"), "deepen-since");
1098 deepen_since_ok
= 1;
1099 } else if (args
->deepen_since
)
1100 die(_("Server does not support --shallow-since"));
1101 if (server_supports("deepen-not")) {
1102 print_verbose(args
, _("Server supports %s"), "deepen-not");
1104 } else if (args
->deepen_not
)
1105 die(_("Server does not support --shallow-exclude"));
1106 if (server_supports("deepen-relative"))
1107 print_verbose(args
, _("Server supports %s"), "deepen-relative");
1108 else if (args
->deepen_relative
)
1109 die(_("Server does not support --deepen"));
1110 if (!server_supports_hash(the_hash_algo
->name
, NULL
))
1111 die(_("Server does not support this repository's object format"));
1113 mark_complete_and_common_ref(negotiator
, args
, &ref
);
1114 filter_refs(args
, &ref
, sought
, nr_sought
);
1115 if (everything_local(args
, &ref
)) {
1116 packet_flush(fd
[1]);
1119 if (find_common(negotiator
, args
, fd
, &oid
, ref
) < 0)
1120 if (!args
->keep_pack
)
1121 /* When cloning, it is not unusual to have
1124 warning(_("no common commits"));
1126 if (args
->stateless_rpc
)
1127 packet_flush(fd
[1]);
1129 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
1131 else if (si
->nr_ours
|| si
->nr_theirs
) {
1132 if (args
->reject_shallow_remote
)
1133 die(_("source repository is shallow, reject to clone."));
1134 alternate_shallow_file
= setup_temporary_shallow(si
->shallow
);
1136 alternate_shallow_file
= NULL
;
1137 if (get_pack(args
, fd
, pack_lockfiles
, NULL
, sought
, nr_sought
,
1138 &fsck_options
.gitmodules_found
))
1139 die(_("git fetch-pack: fetch failed."));
1140 if (fsck_finish(&fsck_options
))
1145 negotiator
->release(negotiator
);
1149 static void add_shallow_requests(struct strbuf
*req_buf
,
1150 const struct fetch_pack_args
*args
)
1152 if (is_repository_shallow(the_repository
))
1153 write_shallow_commits(req_buf
, 1, NULL
);
1154 if (args
->depth
> 0)
1155 packet_buf_write(req_buf
, "deepen %d", args
->depth
);
1156 if (args
->deepen_since
) {
1157 timestamp_t max_age
= approxidate(args
->deepen_since
);
1158 packet_buf_write(req_buf
, "deepen-since %"PRItime
, max_age
);
1160 if (args
->deepen_not
) {
1162 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
1163 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
1164 packet_buf_write(req_buf
, "deepen-not %s", s
->string
);
1167 if (args
->deepen_relative
)
1168 packet_buf_write(req_buf
, "deepen-relative\n");
1171 static void add_wants(const struct ref
*wants
, struct strbuf
*req_buf
)
1173 int use_ref_in_want
= server_supports_feature("fetch", "ref-in-want", 0);
1175 for ( ; wants
; wants
= wants
->next
) {
1176 const struct object_id
*remote
= &wants
->old_oid
;
1180 * If that object is complete (i.e. it is an ancestor of a
1181 * local ref), we tell them we have it but do not have to
1182 * tell them about its ancestors, which they already know
1185 * We use lookup_object here because we are only
1186 * interested in the case we *know* the object is
1187 * reachable and we have already scanned it.
1189 if (((o
= lookup_object(the_repository
, remote
)) != NULL
) &&
1190 (o
->flags
& COMPLETE
)) {
1194 if (!use_ref_in_want
|| wants
->exact_oid
)
1195 packet_buf_write(req_buf
, "want %s\n", oid_to_hex(remote
));
1197 packet_buf_write(req_buf
, "want-ref %s\n", wants
->name
);
1201 static void add_common(struct strbuf
*req_buf
, struct oidset
*common
)
1203 struct oidset_iter iter
;
1204 const struct object_id
*oid
;
1205 oidset_iter_init(common
, &iter
);
1207 while ((oid
= oidset_iter_next(&iter
))) {
1208 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1212 static int add_haves(struct fetch_negotiator
*negotiator
,
1213 struct strbuf
*req_buf
,
1216 int haves_added
= 0;
1217 const struct object_id
*oid
;
1219 while ((oid
= negotiator
->next(negotiator
))) {
1220 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1221 if (++haves_added
>= *haves_to_send
)
1225 /* Increase haves to send on next round */
1226 *haves_to_send
= next_flush(1, *haves_to_send
);
1231 static void write_fetch_command_and_capabilities(struct strbuf
*req_buf
,
1232 const struct string_list
*server_options
)
1234 const char *hash_name
;
1236 if (server_supports_v2("fetch", 1))
1237 packet_buf_write(req_buf
, "command=fetch");
1238 if (server_supports_v2("agent", 0))
1239 packet_buf_write(req_buf
, "agent=%s", git_user_agent_sanitized());
1240 if (advertise_sid
&& server_supports_v2("session-id", 0))
1241 packet_buf_write(req_buf
, "session-id=%s", trace2_session_id());
1242 if (server_options
&& server_options
->nr
&&
1243 server_supports_v2("server-option", 1)) {
1245 for (i
= 0; i
< server_options
->nr
; i
++)
1246 packet_buf_write(req_buf
, "server-option=%s",
1247 server_options
->items
[i
].string
);
1250 if (server_feature_v2("object-format", &hash_name
)) {
1251 int hash_algo
= hash_algo_by_name(hash_name
);
1252 if (hash_algo_by_ptr(the_hash_algo
) != hash_algo
)
1253 die(_("mismatched algorithms: client %s; server %s"),
1254 the_hash_algo
->name
, hash_name
);
1255 packet_buf_write(req_buf
, "object-format=%s", the_hash_algo
->name
);
1256 } else if (hash_algo_by_ptr(the_hash_algo
) != GIT_HASH_SHA1
) {
1257 die(_("the server does not support algorithm '%s'"),
1258 the_hash_algo
->name
);
1260 packet_buf_delim(req_buf
);
1263 static int send_fetch_request(struct fetch_negotiator
*negotiator
, int fd_out
,
1264 struct fetch_pack_args
*args
,
1265 const struct ref
*wants
, struct oidset
*common
,
1266 int *haves_to_send
, int *in_vain
,
1267 int sideband_all
, int seen_ack
)
1271 struct strbuf req_buf
= STRBUF_INIT
;
1273 write_fetch_command_and_capabilities(&req_buf
, args
->server_options
);
1275 if (args
->use_thin_pack
)
1276 packet_buf_write(&req_buf
, "thin-pack");
1277 if (args
->no_progress
)
1278 packet_buf_write(&req_buf
, "no-progress");
1279 if (args
->include_tag
)
1280 packet_buf_write(&req_buf
, "include-tag");
1281 if (prefer_ofs_delta
)
1282 packet_buf_write(&req_buf
, "ofs-delta");
1284 packet_buf_write(&req_buf
, "sideband-all");
1286 /* Add shallow-info and deepen request */
1287 if (server_supports_feature("fetch", "shallow", 0))
1288 add_shallow_requests(&req_buf
, args
);
1289 else if (is_repository_shallow(the_repository
) || args
->deepen
)
1290 die(_("Server does not support shallow requests"));
1293 if (server_supports_feature("fetch", "filter", 0) &&
1294 args
->filter_options
.choice
) {
1296 expand_list_objects_filter_spec(&args
->filter_options
);
1297 print_verbose(args
, _("Server supports filter"));
1298 packet_buf_write(&req_buf
, "filter %s", spec
);
1299 } else if (args
->filter_options
.choice
) {
1300 warning("filtering not recognized by server, ignoring");
1303 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1305 struct strbuf to_send
= STRBUF_INIT
;
1307 for (i
= 0; i
< uri_protocols
.nr
; i
++) {
1308 const char *s
= uri_protocols
.items
[i
].string
;
1310 if (!strcmp(s
, "https") || !strcmp(s
, "http")) {
1312 strbuf_addch(&to_send
, ',');
1313 strbuf_addstr(&to_send
, s
);
1317 packet_buf_write(&req_buf
, "packfile-uris %s",
1319 strbuf_release(&to_send
);
1324 add_wants(wants
, &req_buf
);
1326 /* Add all of the common commits we've found in previous rounds */
1327 add_common(&req_buf
, common
);
1329 haves_added
= add_haves(negotiator
, &req_buf
, haves_to_send
);
1330 *in_vain
+= haves_added
;
1331 if (!haves_added
|| (seen_ack
&& *in_vain
>= MAX_IN_VAIN
)) {
1333 packet_buf_write(&req_buf
, "done\n");
1338 packet_buf_flush(&req_buf
);
1339 if (write_in_full(fd_out
, req_buf
.buf
, req_buf
.len
) < 0)
1340 die_errno(_("unable to write request to remote"));
1342 strbuf_release(&req_buf
);
1347 * Processes a section header in a server's response and checks if it matches
1348 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1349 * not consumed); if 0, the line will be consumed and the function will die if
1350 * the section header doesn't match what was expected.
1352 static int process_section_header(struct packet_reader
*reader
,
1353 const char *section
, int peek
)
1357 if (packet_reader_peek(reader
) != PACKET_READ_NORMAL
)
1358 die(_("error reading section header '%s'"), section
);
1360 ret
= !strcmp(reader
->line
, section
);
1364 die(_("expected '%s', received '%s'"),
1365 section
, reader
->line
);
1366 packet_reader_read(reader
);
1372 static int process_ack(struct fetch_negotiator
*negotiator
,
1373 struct packet_reader
*reader
,
1374 struct object_id
*common_oid
,
1375 int *received_ready
)
1377 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1380 if (!strcmp(reader
->line
, "NAK"))
1383 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
1384 if (!get_oid_hex(arg
, common_oid
)) {
1385 struct commit
*commit
;
1386 commit
= lookup_commit(the_repository
, common_oid
);
1388 negotiator
->ack(negotiator
, commit
);
1393 if (!strcmp(reader
->line
, "ready")) {
1394 *received_ready
= 1;
1398 die(_("unexpected acknowledgment line: '%s'"), reader
->line
);
1401 if (reader
->status
!= PACKET_READ_FLUSH
&&
1402 reader
->status
!= PACKET_READ_DELIM
)
1403 die(_("error processing acks: %d"), reader
->status
);
1406 * If an "acknowledgments" section is sent, a packfile is sent if and
1407 * only if "ready" was sent in this section. The other sections
1408 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1409 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1412 if (*received_ready
&& reader
->status
!= PACKET_READ_DELIM
)
1413 die(_("expected packfile to be sent after 'ready'"));
1414 if (!*received_ready
&& reader
->status
!= PACKET_READ_FLUSH
)
1415 die(_("expected no other sections to be sent after no 'ready'"));
1420 static void receive_shallow_info(struct fetch_pack_args
*args
,
1421 struct packet_reader
*reader
,
1422 struct oid_array
*shallows
,
1423 struct shallow_info
*si
)
1425 int unshallow_received
= 0;
1427 process_section_header(reader
, "shallow-info", 0);
1428 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1430 struct object_id oid
;
1432 if (skip_prefix(reader
->line
, "shallow ", &arg
)) {
1433 if (get_oid_hex(arg
, &oid
))
1434 die(_("invalid shallow line: %s"), reader
->line
);
1435 oid_array_append(shallows
, &oid
);
1438 if (skip_prefix(reader
->line
, "unshallow ", &arg
)) {
1439 if (get_oid_hex(arg
, &oid
))
1440 die(_("invalid unshallow line: %s"), reader
->line
);
1441 if (!lookup_object(the_repository
, &oid
))
1442 die(_("object not found: %s"), reader
->line
);
1443 /* make sure that it is parsed as shallow */
1444 if (!parse_object(the_repository
, &oid
))
1445 die(_("error in object: %s"), reader
->line
);
1446 if (unregister_shallow(&oid
))
1447 die(_("no shallow found: %s"), reader
->line
);
1448 unshallow_received
= 1;
1451 die(_("expected shallow/unshallow, got %s"), reader
->line
);
1454 if (reader
->status
!= PACKET_READ_FLUSH
&&
1455 reader
->status
!= PACKET_READ_DELIM
)
1456 die(_("error processing shallow info: %d"), reader
->status
);
1458 if (args
->deepen
|| unshallow_received
) {
1460 * Treat these as shallow lines caused by our depth settings.
1461 * In v0, these lines cannot cause refs to be rejected; do the
1466 for (i
= 0; i
< shallows
->nr
; i
++)
1467 register_shallow(the_repository
, &shallows
->oid
[i
]);
1468 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
1471 } else if (shallows
->nr
) {
1473 * Treat these as shallow lines caused by the remote being
1474 * shallow. In v0, remote refs that reach these objects are
1475 * rejected (unless --update-shallow is set); do the same.
1477 prepare_shallow_info(si
, shallows
);
1478 if (si
->nr_ours
|| si
->nr_theirs
) {
1479 if (args
->reject_shallow_remote
)
1480 die(_("source repository is shallow, reject to clone."));
1481 alternate_shallow_file
=
1482 setup_temporary_shallow(si
->shallow
);
1484 alternate_shallow_file
= NULL
;
1486 alternate_shallow_file
= NULL
;
1490 static int cmp_name_ref(const void *name
, const void *ref
)
1492 return strcmp(name
, (*(struct ref
**)ref
)->name
);
1495 static void receive_wanted_refs(struct packet_reader
*reader
,
1496 struct ref
**sought
, int nr_sought
)
1498 process_section_header(reader
, "wanted-refs", 0);
1499 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1500 struct object_id oid
;
1504 if (parse_oid_hex(reader
->line
, &oid
, &end
) || *end
++ != ' ')
1505 die(_("expected wanted-ref, got '%s'"), reader
->line
);
1507 found
= bsearch(end
, sought
, nr_sought
, sizeof(*sought
),
1510 die(_("unexpected wanted-ref: '%s'"), reader
->line
);
1511 oidcpy(&(*found
)->old_oid
, &oid
);
1514 if (reader
->status
!= PACKET_READ_DELIM
)
1515 die(_("error processing wanted refs: %d"), reader
->status
);
1518 static void receive_packfile_uris(struct packet_reader
*reader
,
1519 struct string_list
*uris
)
1521 process_section_header(reader
, "packfile-uris", 0);
1522 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1523 if (reader
->pktlen
< the_hash_algo
->hexsz
||
1524 reader
->line
[the_hash_algo
->hexsz
] != ' ')
1525 die("expected '<hash> <uri>', got: %s\n", reader
->line
);
1527 string_list_append(uris
, reader
->line
);
1529 if (reader
->status
!= PACKET_READ_DELIM
)
1530 die("expected DELIM");
1534 FETCH_CHECK_LOCAL
= 0,
1541 static void do_check_stateless_delimiter(int stateless_rpc
,
1542 struct packet_reader
*reader
)
1544 check_stateless_delimiter(stateless_rpc
, reader
,
1545 _("git fetch-pack: expected response end packet"));
1548 static struct ref
*do_fetch_pack_v2(struct fetch_pack_args
*args
,
1550 const struct ref
*orig_ref
,
1551 struct ref
**sought
, int nr_sought
,
1552 struct oid_array
*shallows
,
1553 struct shallow_info
*si
,
1554 struct string_list
*pack_lockfiles
)
1556 struct repository
*r
= the_repository
;
1557 struct ref
*ref
= copy_ref_list(orig_ref
);
1558 enum fetch_state state
= FETCH_CHECK_LOCAL
;
1559 struct oidset common
= OIDSET_INIT
;
1560 struct packet_reader reader
;
1561 int in_vain
= 0, negotiation_started
= 0;
1562 int haves_to_send
= INITIAL_FLUSH
;
1563 struct fetch_negotiator negotiator_alloc
;
1564 struct fetch_negotiator
*negotiator
;
1566 struct object_id common_oid
;
1567 int received_ready
= 0;
1568 struct string_list packfile_uris
= STRING_LIST_INIT_DUP
;
1570 struct strvec index_pack_args
= STRVEC_INIT
;
1572 negotiator
= &negotiator_alloc
;
1573 fetch_negotiator_init(r
, negotiator
);
1575 packet_reader_init(&reader
, fd
[0], NULL
, 0,
1576 PACKET_READ_CHOMP_NEWLINE
|
1577 PACKET_READ_DIE_ON_ERR_PACKET
);
1578 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1579 server_supports_feature("fetch", "sideband-all", 0)) {
1580 reader
.use_sideband
= 1;
1581 reader
.me
= "fetch-pack";
1584 while (state
!= FETCH_DONE
) {
1586 case FETCH_CHECK_LOCAL
:
1587 sort_ref_list(&ref
, ref_compare_name
);
1588 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
1590 /* v2 supports these by default */
1591 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1593 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
1596 /* Filter 'ref' by 'sought' and those that aren't local */
1597 mark_complete_and_common_ref(negotiator
, args
, &ref
);
1598 filter_refs(args
, &ref
, sought
, nr_sought
);
1599 if (everything_local(args
, &ref
))
1602 state
= FETCH_SEND_REQUEST
;
1604 mark_tips(negotiator
, args
->negotiation_tips
);
1605 for_each_cached_alternate(negotiator
,
1606 insert_one_alternate_object
);
1608 case FETCH_SEND_REQUEST
:
1609 if (!negotiation_started
) {
1610 negotiation_started
= 1;
1611 trace2_region_enter("fetch-pack",
1615 if (send_fetch_request(negotiator
, fd
[1], args
, ref
,
1617 &haves_to_send
, &in_vain
,
1618 reader
.use_sideband
,
1620 state
= FETCH_GET_PACK
;
1622 state
= FETCH_PROCESS_ACKS
;
1624 case FETCH_PROCESS_ACKS
:
1625 /* Process ACKs/NAKs */
1626 process_section_header(&reader
, "acknowledgments", 0);
1627 while (process_ack(negotiator
, &reader
, &common_oid
,
1631 oidset_insert(&common
, &common_oid
);
1633 if (received_ready
) {
1635 * Don't check for response delimiter; get_pack() will
1636 * read the rest of this response.
1638 state
= FETCH_GET_PACK
;
1640 do_check_stateless_delimiter(args
->stateless_rpc
, &reader
);
1641 state
= FETCH_SEND_REQUEST
;
1644 case FETCH_GET_PACK
:
1645 trace2_region_leave("fetch-pack",
1648 /* Check for shallow-info section */
1649 if (process_section_header(&reader
, "shallow-info", 1))
1650 receive_shallow_info(args
, &reader
, shallows
, si
);
1652 if (process_section_header(&reader
, "wanted-refs", 1))
1653 receive_wanted_refs(&reader
, sought
, nr_sought
);
1655 /* get the pack(s) */
1656 if (process_section_header(&reader
, "packfile-uris", 1))
1657 receive_packfile_uris(&reader
, &packfile_uris
);
1658 process_section_header(&reader
, "packfile", 0);
1661 * this is the final request we'll make of the server;
1662 * do a half-duplex shutdown to indicate that they can
1663 * hang up as soon as the pack is sent.
1668 if (get_pack(args
, fd
, pack_lockfiles
,
1669 packfile_uris
.nr
? &index_pack_args
: NULL
,
1670 sought
, nr_sought
, &fsck_options
.gitmodules_found
))
1671 die(_("git fetch-pack: fetch failed."));
1672 do_check_stateless_delimiter(args
->stateless_rpc
, &reader
);
1681 for (i
= 0; i
< packfile_uris
.nr
; i
++) {
1683 struct child_process cmd
= CHILD_PROCESS_INIT
;
1684 char packname
[GIT_MAX_HEXSZ
+ 1];
1685 const char *uri
= packfile_uris
.items
[i
].string
+
1686 the_hash_algo
->hexsz
+ 1;
1688 strvec_push(&cmd
.args
, "http-fetch");
1689 strvec_pushf(&cmd
.args
, "--packfile=%.*s",
1690 (int) the_hash_algo
->hexsz
,
1691 packfile_uris
.items
[i
].string
);
1692 for (j
= 0; j
< index_pack_args
.nr
; j
++)
1693 strvec_pushf(&cmd
.args
, "--index-pack-arg=%s",
1694 index_pack_args
.v
[j
]);
1695 strvec_push(&cmd
.args
, uri
);
1699 if (start_command(&cmd
))
1700 die("fetch-pack: unable to spawn http-fetch");
1702 if (read_in_full(cmd
.out
, packname
, 5) < 0 ||
1703 memcmp(packname
, "keep\t", 5))
1704 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1706 if (read_in_full(cmd
.out
, packname
,
1707 the_hash_algo
->hexsz
+ 1) < 0 ||
1708 packname
[the_hash_algo
->hexsz
] != '\n')
1709 die("fetch-pack: expected hash then LF at end of http-fetch output");
1711 packname
[the_hash_algo
->hexsz
] = '\0';
1713 parse_gitmodules_oids(cmd
.out
, &fsck_options
.gitmodules_found
);
1717 if (finish_command(&cmd
))
1718 die("fetch-pack: unable to finish http-fetch");
1720 if (memcmp(packfile_uris
.items
[i
].string
, packname
,
1721 the_hash_algo
->hexsz
))
1722 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1723 uri
, (int) the_hash_algo
->hexsz
,
1724 packfile_uris
.items
[i
].string
);
1726 string_list_append_nodup(pack_lockfiles
,
1727 xstrfmt("%s/pack/pack-%s.keep",
1728 get_object_directory(),
1731 string_list_clear(&packfile_uris
, 0);
1732 strvec_clear(&index_pack_args
);
1734 if (fsck_finish(&fsck_options
))
1738 negotiator
->release(negotiator
);
1740 oidset_clear(&common
);
1744 static int fetch_pack_config_cb(const char *var
, const char *value
, void *cb
)
1746 if (strcmp(var
, "fetch.fsck.skiplist") == 0) {
1749 if (git_config_pathname(&path
, var
, value
))
1751 strbuf_addf(&fsck_msg_types
, "%cskiplist=%s",
1752 fsck_msg_types
.len
? ',' : '=', path
);
1757 if (skip_prefix(var
, "fetch.fsck.", &var
)) {
1758 if (is_valid_msg_type(var
, value
))
1759 strbuf_addf(&fsck_msg_types
, "%c%s=%s",
1760 fsck_msg_types
.len
? ',' : '=', var
, value
);
1762 warning("Skipping unknown msg id '%s'", var
);
1766 return git_default_config(var
, value
, cb
);
1769 static void fetch_pack_config(void)
1771 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit
);
1772 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit
);
1773 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta
);
1774 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects
);
1775 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects
);
1776 git_config_get_bool("transfer.advertisesid", &advertise_sid
);
1777 if (!uri_protocols
.nr
) {
1780 if (!git_config_get_string("fetch.uriprotocols", &str
) && str
) {
1781 string_list_split(&uri_protocols
, str
, ',', -1);
1786 git_config(fetch_pack_config_cb
, NULL
);
1789 static void fetch_pack_setup(void)
1791 static int did_setup
;
1794 fetch_pack_config();
1795 if (0 <= transfer_unpack_limit
)
1796 unpack_limit
= transfer_unpack_limit
;
1797 else if (0 <= fetch_unpack_limit
)
1798 unpack_limit
= fetch_unpack_limit
;
1802 static int remove_duplicates_in_refs(struct ref
**ref
, int nr
)
1804 struct string_list names
= STRING_LIST_INIT_NODUP
;
1807 for (src
= dst
= 0; src
< nr
; src
++) {
1808 struct string_list_item
*item
;
1809 item
= string_list_insert(&names
, ref
[src
]->name
);
1811 continue; /* already have it */
1812 item
->util
= ref
[src
];
1814 ref
[dst
] = ref
[src
];
1817 for (src
= dst
; src
< nr
; src
++)
1819 string_list_clear(&names
, 0);
1823 static void update_shallow(struct fetch_pack_args
*args
,
1824 struct ref
**sought
, int nr_sought
,
1825 struct shallow_info
*si
)
1827 struct oid_array ref
= OID_ARRAY_INIT
;
1831 if (args
->deepen
&& alternate_shallow_file
) {
1832 if (*alternate_shallow_file
== '\0') { /* --unshallow */
1833 unlink_or_warn(git_path_shallow(the_repository
));
1834 rollback_shallow_file(the_repository
, &shallow_lock
);
1836 commit_shallow_file(the_repository
, &shallow_lock
);
1837 alternate_shallow_file
= NULL
;
1841 if (!si
->shallow
|| !si
->shallow
->nr
)
1844 if (args
->cloning
) {
1846 * remote is shallow, but this is a clone, there are
1847 * no objects in repo to worry about. Accept any
1848 * shallow points that exist in the pack (iow in repo
1849 * after get_pack() and reprepare_packed_git())
1851 struct oid_array extra
= OID_ARRAY_INIT
;
1852 struct object_id
*oid
= si
->shallow
->oid
;
1853 for (i
= 0; i
< si
->shallow
->nr
; i
++)
1854 if (has_object_file(&oid
[i
]))
1855 oid_array_append(&extra
, &oid
[i
]);
1857 setup_alternate_shallow(&shallow_lock
,
1858 &alternate_shallow_file
,
1860 commit_shallow_file(the_repository
, &shallow_lock
);
1861 alternate_shallow_file
= NULL
;
1863 oid_array_clear(&extra
);
1867 if (!si
->nr_ours
&& !si
->nr_theirs
)
1870 remove_nonexistent_theirs_shallow(si
);
1871 if (!si
->nr_ours
&& !si
->nr_theirs
)
1873 for (i
= 0; i
< nr_sought
; i
++)
1874 oid_array_append(&ref
, &sought
[i
]->old_oid
);
1877 if (args
->update_shallow
) {
1879 * remote is also shallow, .git/shallow may be updated
1880 * so all refs can be accepted. Make sure we only add
1881 * shallow roots that are actually reachable from new
1884 struct oid_array extra
= OID_ARRAY_INIT
;
1885 struct object_id
*oid
= si
->shallow
->oid
;
1886 assign_shallow_commits_to_refs(si
, NULL
, NULL
);
1887 if (!si
->nr_ours
&& !si
->nr_theirs
) {
1888 oid_array_clear(&ref
);
1891 for (i
= 0; i
< si
->nr_ours
; i
++)
1892 oid_array_append(&extra
, &oid
[si
->ours
[i
]]);
1893 for (i
= 0; i
< si
->nr_theirs
; i
++)
1894 oid_array_append(&extra
, &oid
[si
->theirs
[i
]]);
1895 setup_alternate_shallow(&shallow_lock
,
1896 &alternate_shallow_file
,
1898 commit_shallow_file(the_repository
, &shallow_lock
);
1899 oid_array_clear(&extra
);
1900 oid_array_clear(&ref
);
1901 alternate_shallow_file
= NULL
;
1906 * remote is also shallow, check what ref is safe to update
1907 * without updating .git/shallow
1909 CALLOC_ARRAY(status
, nr_sought
);
1910 assign_shallow_commits_to_refs(si
, NULL
, status
);
1911 if (si
->nr_ours
|| si
->nr_theirs
) {
1912 for (i
= 0; i
< nr_sought
; i
++)
1914 sought
[i
]->status
= REF_STATUS_REJECT_SHALLOW
;
1917 oid_array_clear(&ref
);
1920 static const struct object_id
*iterate_ref_map(void *cb_data
)
1922 struct ref
**rm
= cb_data
;
1923 struct ref
*ref
= *rm
;
1928 return &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 static int add_to_object_array(const struct object_id
*oid
, void *data
)
1990 struct object_array
*a
= data
;
1992 add_object_array(lookup_object(the_repository
, oid
), "", a
);
1996 static void clear_common_flag(struct oidset
*s
)
1998 struct oidset_iter iter
;
1999 const struct object_id
*oid
;
2000 oidset_iter_init(s
, &iter
);
2002 while ((oid
= oidset_iter_next(&iter
))) {
2003 struct object
*obj
= lookup_object(the_repository
, oid
);
2004 obj
->flags
&= ~COMMON
;
2008 void negotiate_using_fetch(const struct oid_array
*negotiation_tips
,
2009 const struct string_list
*server_options
,
2012 struct oidset
*acked_commits
)
2014 struct fetch_negotiator negotiator
;
2015 struct packet_reader reader
;
2016 struct object_array nt_object_array
= OBJECT_ARRAY_INIT
;
2017 struct strbuf req_buf
= STRBUF_INIT
;
2018 int haves_to_send
= INITIAL_FLUSH
;
2021 int last_iteration
= 0;
2022 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
2024 fetch_negotiator_init(the_repository
, &negotiator
);
2025 mark_tips(&negotiator
, negotiation_tips
);
2027 packet_reader_init(&reader
, fd
[0], NULL
, 0,
2028 PACKET_READ_CHOMP_NEWLINE
|
2029 PACKET_READ_DIE_ON_ERR_PACKET
);
2031 oid_array_for_each((struct oid_array
*) negotiation_tips
,
2032 add_to_object_array
,
2035 while (!last_iteration
) {
2037 struct object_id common_oid
;
2038 int received_ready
= 0;
2040 strbuf_reset(&req_buf
);
2041 write_fetch_command_and_capabilities(&req_buf
, server_options
);
2043 packet_buf_write(&req_buf
, "wait-for-done");
2045 haves_added
= add_haves(&negotiator
, &req_buf
, &haves_to_send
);
2046 in_vain
+= haves_added
;
2047 if (!haves_added
|| (seen_ack
&& in_vain
>= MAX_IN_VAIN
))
2051 packet_buf_flush(&req_buf
);
2052 if (write_in_full(fd
[1], req_buf
.buf
, req_buf
.len
) < 0)
2053 die_errno(_("unable to write request to remote"));
2055 /* Process ACKs/NAKs */
2056 process_section_header(&reader
, "acknowledgments", 0);
2057 while (process_ack(&negotiator
, &reader
, &common_oid
,
2059 struct commit
*commit
= lookup_commit(the_repository
,
2062 timestamp_t generation
;
2064 parse_commit_or_die(commit
);
2065 commit
->object
.flags
|= COMMON
;
2066 generation
= commit_graph_generation(commit
);
2067 if (generation
< min_generation
)
2068 min_generation
= generation
;
2072 oidset_insert(acked_commits
, &common_oid
);
2075 die(_("unexpected 'ready' from remote"));
2077 do_check_stateless_delimiter(stateless_rpc
, &reader
);
2078 if (can_all_from_reach_with_flag(&nt_object_array
, COMMON
,
2083 clear_common_flag(acked_commits
);
2084 strbuf_release(&req_buf
);
2087 int report_unmatched_refs(struct ref
**sought
, int nr_sought
)
2091 for (i
= 0; i
< nr_sought
; i
++) {
2094 switch (sought
[i
]->match_status
) {
2097 case REF_NOT_MATCHED
:
2098 error(_("no such remote ref %s"), sought
[i
]->name
);
2100 case REF_UNADVERTISED_NOT_ALLOWED
:
2101 error(_("Server does not allow request for unadvertised object %s"),