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 struct shallow_lock shallow_lock
;
39 static const char *alternate_shallow_file
;
40 static struct strbuf fsck_msg_types
= STRBUF_INIT
;
41 static struct string_list uri_protocols
= STRING_LIST_INIT_DUP
;
43 /* Remember to update object flag allocation in object.h */
44 #define COMPLETE (1U << 0)
45 #define ALTERNATE (1U << 1)
48 * After sending this many "have"s if we do not get any new ACK , we
49 * give up traversing our history.
51 #define MAX_IN_VAIN 256
53 static int multi_ack
, use_sideband
;
54 /* Allow specifying sha1 if it is a ref tip. */
55 #define ALLOW_TIP_SHA1 01
56 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
57 #define ALLOW_REACHABLE_SHA1 02
58 static unsigned int allow_unadvertised_object_request
;
60 __attribute__((format (printf
, 2, 3)))
61 static inline void print_verbose(const struct fetch_pack_args
*args
,
69 va_start(params
, fmt
);
70 vfprintf(stderr
, fmt
, params
);
75 struct alternate_object_cache
{
76 struct object
**items
;
80 static void cache_one_alternate(const struct object_id
*oid
,
83 struct alternate_object_cache
*cache
= vcache
;
84 struct object
*obj
= parse_object(the_repository
, oid
);
86 if (!obj
|| (obj
->flags
& ALTERNATE
))
89 obj
->flags
|= ALTERNATE
;
90 ALLOC_GROW(cache
->items
, cache
->nr
+ 1, cache
->alloc
);
91 cache
->items
[cache
->nr
++] = obj
;
94 static void for_each_cached_alternate(struct fetch_negotiator
*negotiator
,
95 void (*cb
)(struct fetch_negotiator
*,
98 static int initialized
;
99 static struct alternate_object_cache cache
;
103 for_each_alternate_ref(cache_one_alternate
, &cache
);
107 for (i
= 0; i
< cache
.nr
; i
++)
108 cb(negotiator
, cache
.items
[i
]);
111 static int rev_list_insert_ref(struct fetch_negotiator
*negotiator
,
113 const struct object_id
*oid
)
115 struct object
*o
= deref_tag(the_repository
,
116 parse_object(the_repository
, oid
),
119 if (o
&& o
->type
== OBJ_COMMIT
)
120 negotiator
->add_tip(negotiator
, (struct commit
*)o
);
125 static int rev_list_insert_ref_oid(const char *refname
, const struct object_id
*oid
,
126 int flag
, void *cb_data
)
128 return rev_list_insert_ref(cb_data
, refname
, oid
);
139 static void consume_shallow_list(struct fetch_pack_args
*args
,
140 struct packet_reader
*reader
)
142 if (args
->stateless_rpc
&& args
->deepen
) {
143 /* If we sent a depth we will get back "duplicate"
144 * shallow and unshallow commands every time there
145 * is a block of have lines exchanged.
147 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
148 if (starts_with(reader
->line
, "shallow "))
150 if (starts_with(reader
->line
, "unshallow "))
152 die(_("git fetch-pack: expected shallow list"));
154 if (reader
->status
!= PACKET_READ_FLUSH
)
155 die(_("git fetch-pack: expected a flush packet after shallow list"));
159 static enum ack_type
get_ack(struct packet_reader
*reader
,
160 struct object_id
*result_oid
)
165 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
166 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
167 len
= reader
->pktlen
;
169 if (!strcmp(reader
->line
, "NAK"))
171 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
173 if (!parse_oid_hex(arg
, result_oid
, &p
)) {
174 len
-= p
- reader
->line
;
177 if (strstr(p
, "continue"))
179 if (strstr(p
, "common"))
181 if (strstr(p
, "ready"))
186 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader
->line
);
189 static void send_request(struct fetch_pack_args
*args
,
190 int fd
, struct strbuf
*buf
)
192 if (args
->stateless_rpc
) {
193 send_sideband(fd
, -1, buf
->buf
, buf
->len
, LARGE_PACKET_MAX
);
196 if (write_in_full(fd
, buf
->buf
, buf
->len
) < 0)
197 die_errno(_("unable to write to remote"));
201 static void insert_one_alternate_object(struct fetch_negotiator
*negotiator
,
204 rev_list_insert_ref(negotiator
, NULL
, &obj
->oid
);
207 #define INITIAL_FLUSH 16
208 #define PIPESAFE_FLUSH 32
209 #define LARGE_FLUSH 16384
211 static int next_flush(int stateless_rpc
, int count
)
214 if (count
< LARGE_FLUSH
)
217 count
= count
* 11 / 10;
219 if (count
< PIPESAFE_FLUSH
)
222 count
+= PIPESAFE_FLUSH
;
227 static void mark_tips(struct fetch_negotiator
*negotiator
,
228 const struct oid_array
*negotiation_tips
)
232 if (!negotiation_tips
) {
233 for_each_ref(rev_list_insert_ref_oid
, negotiator
);
237 for (i
= 0; i
< negotiation_tips
->nr
; i
++)
238 rev_list_insert_ref(negotiator
, NULL
,
239 &negotiation_tips
->oid
[i
]);
243 static int find_common(struct fetch_negotiator
*negotiator
,
244 struct fetch_pack_args
*args
,
245 int fd
[2], struct object_id
*result_oid
,
249 int count
= 0, flushes
= 0, flush_at
= INITIAL_FLUSH
, retval
;
250 const struct object_id
*oid
;
251 unsigned in_vain
= 0;
252 int got_continue
= 0;
254 struct strbuf req_buf
= STRBUF_INIT
;
255 size_t state_len
= 0;
256 struct packet_reader reader
;
258 if (args
->stateless_rpc
&& multi_ack
== 1)
259 die(_("--stateless-rpc requires multi_ack_detailed"));
261 packet_reader_init(&reader
, fd
[0], NULL
, 0,
262 PACKET_READ_CHOMP_NEWLINE
|
263 PACKET_READ_DIE_ON_ERR_PACKET
);
265 if (!args
->no_dependents
) {
266 mark_tips(negotiator
, args
->negotiation_tips
);
267 for_each_cached_alternate(negotiator
, insert_one_alternate_object
);
271 for ( ; refs
; refs
= refs
->next
) {
272 struct object_id
*remote
= &refs
->old_oid
;
273 const char *remote_hex
;
277 * If that object is complete (i.e. it is an ancestor of a
278 * local ref), we tell them we have it but do not have to
279 * tell them about its ancestors, which they already know
282 * We use lookup_object here because we are only
283 * interested in the case we *know* the object is
284 * reachable and we have already scanned it.
286 * Do this only if args->no_dependents is false (if it is true,
287 * we cannot trust the object flags).
289 if (!args
->no_dependents
&&
290 ((o
= lookup_object(the_repository
, remote
)) != NULL
) &&
291 (o
->flags
& COMPLETE
)) {
295 remote_hex
= oid_to_hex(remote
);
297 struct strbuf c
= STRBUF_INIT
;
298 if (multi_ack
== 2) strbuf_addstr(&c
, " multi_ack_detailed");
299 if (multi_ack
== 1) strbuf_addstr(&c
, " multi_ack");
300 if (no_done
) strbuf_addstr(&c
, " no-done");
301 if (use_sideband
== 2) strbuf_addstr(&c
, " side-band-64k");
302 if (use_sideband
== 1) strbuf_addstr(&c
, " side-band");
303 if (args
->deepen_relative
) strbuf_addstr(&c
, " deepen-relative");
304 if (args
->use_thin_pack
) strbuf_addstr(&c
, " thin-pack");
305 if (args
->no_progress
) strbuf_addstr(&c
, " no-progress");
306 if (args
->include_tag
) strbuf_addstr(&c
, " include-tag");
307 if (prefer_ofs_delta
) strbuf_addstr(&c
, " ofs-delta");
308 if (deepen_since_ok
) strbuf_addstr(&c
, " deepen-since");
309 if (deepen_not_ok
) strbuf_addstr(&c
, " deepen-not");
310 if (agent_supported
) strbuf_addf(&c
, " agent=%s",
311 git_user_agent_sanitized());
312 if (args
->filter_options
.choice
)
313 strbuf_addstr(&c
, " filter");
314 packet_buf_write(&req_buf
, "want %s%s\n", remote_hex
, c
.buf
);
317 packet_buf_write(&req_buf
, "want %s\n", remote_hex
);
322 strbuf_release(&req_buf
);
327 if (is_repository_shallow(the_repository
))
328 write_shallow_commits(&req_buf
, 1, NULL
);
330 packet_buf_write(&req_buf
, "deepen %d", args
->depth
);
331 if (args
->deepen_since
) {
332 timestamp_t max_age
= approxidate(args
->deepen_since
);
333 packet_buf_write(&req_buf
, "deepen-since %"PRItime
, max_age
);
335 if (args
->deepen_not
) {
337 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
338 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
339 packet_buf_write(&req_buf
, "deepen-not %s", s
->string
);
342 if (server_supports_filtering
&& args
->filter_options
.choice
) {
344 expand_list_objects_filter_spec(&args
->filter_options
);
345 packet_buf_write(&req_buf
, "filter %s", spec
);
347 packet_buf_flush(&req_buf
);
348 state_len
= req_buf
.len
;
352 struct object_id oid
;
354 send_request(args
, fd
[1], &req_buf
);
355 while (packet_reader_read(&reader
) == PACKET_READ_NORMAL
) {
356 if (skip_prefix(reader
.line
, "shallow ", &arg
)) {
357 if (get_oid_hex(arg
, &oid
))
358 die(_("invalid shallow line: %s"), reader
.line
);
359 register_shallow(the_repository
, &oid
);
362 if (skip_prefix(reader
.line
, "unshallow ", &arg
)) {
363 if (get_oid_hex(arg
, &oid
))
364 die(_("invalid unshallow line: %s"), reader
.line
);
365 if (!lookup_object(the_repository
, &oid
))
366 die(_("object not found: %s"), reader
.line
);
367 /* make sure that it is parsed as shallow */
368 if (!parse_object(the_repository
, &oid
))
369 die(_("error in object: %s"), reader
.line
);
370 if (unregister_shallow(&oid
))
371 die(_("no shallow found: %s"), reader
.line
);
374 die(_("expected shallow/unshallow, got %s"), reader
.line
);
376 } else if (!args
->stateless_rpc
)
377 send_request(args
, fd
[1], &req_buf
);
379 if (!args
->stateless_rpc
) {
380 /* If we aren't using the stateless-rpc interface
381 * we don't need to retain the headers.
383 strbuf_setlen(&req_buf
, 0);
387 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository
);
390 if (args
->no_dependents
)
392 while ((oid
= negotiator
->next(negotiator
))) {
393 packet_buf_write(&req_buf
, "have %s\n", oid_to_hex(oid
));
394 print_verbose(args
, "have %s", oid_to_hex(oid
));
396 if (flush_at
<= ++count
) {
399 packet_buf_flush(&req_buf
);
400 send_request(args
, fd
[1], &req_buf
);
401 strbuf_setlen(&req_buf
, state_len
);
403 flush_at
= next_flush(args
->stateless_rpc
, count
);
406 * We keep one window "ahead" of the other side, and
407 * will wait for an ACK only on the next one
409 if (!args
->stateless_rpc
&& count
== INITIAL_FLUSH
)
412 consume_shallow_list(args
, &reader
);
414 ack
= get_ack(&reader
, result_oid
);
416 print_verbose(args
, _("got %s %d %s"), "ack",
417 ack
, oid_to_hex(result_oid
));
427 struct commit
*commit
=
428 lookup_commit(the_repository
,
433 die(_("invalid commit %s"), oid_to_hex(result_oid
));
434 was_common
= negotiator
->ack(negotiator
, commit
);
435 if (args
->stateless_rpc
438 /* We need to replay the have for this object
439 * on the next RPC request so the peer knows
440 * it is in common with us.
442 const char *hex
= oid_to_hex(result_oid
);
443 packet_buf_write(&req_buf
, "have %s\n", hex
);
444 state_len
= req_buf
.len
;
446 * Reset in_vain because an ack
447 * for this commit has not been
451 } else if (!args
->stateless_rpc
452 || ack
!= ACK_common
)
456 if (ack
== ACK_ready
)
463 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
464 print_verbose(args
, _("giving up"));
472 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository
);
473 if (!got_ready
|| !no_done
) {
474 packet_buf_write(&req_buf
, "done\n");
475 send_request(args
, fd
[1], &req_buf
);
477 print_verbose(args
, _("done"));
482 strbuf_release(&req_buf
);
484 if (!got_ready
|| !no_done
)
485 consume_shallow_list(args
, &reader
);
486 while (flushes
|| multi_ack
) {
487 int ack
= get_ack(&reader
, result_oid
);
489 print_verbose(args
, _("got %s (%d) %s"), "ack",
490 ack
, oid_to_hex(result_oid
));
498 /* it is no error to fetch into a completely empty repo */
499 return count
? retval
: 0;
502 static struct commit_list
*complete
;
504 static int mark_complete(const struct object_id
*oid
)
506 struct object
*o
= parse_object(the_repository
, oid
);
508 while (o
&& o
->type
== OBJ_TAG
) {
509 struct tag
*t
= (struct tag
*) o
;
511 break; /* broken repository */
512 o
->flags
|= COMPLETE
;
513 o
= parse_object(the_repository
, &t
->tagged
->oid
);
515 if (o
&& o
->type
== OBJ_COMMIT
) {
516 struct commit
*commit
= (struct commit
*)o
;
517 if (!(commit
->object
.flags
& COMPLETE
)) {
518 commit
->object
.flags
|= COMPLETE
;
519 commit_list_insert(commit
, &complete
);
525 static int mark_complete_oid(const char *refname
, const struct object_id
*oid
,
526 int flag
, void *cb_data
)
528 return mark_complete(oid
);
531 static void mark_recent_complete_commits(struct fetch_pack_args
*args
,
534 while (complete
&& cutoff
<= complete
->item
->date
) {
535 print_verbose(args
, _("Marking %s as complete"),
536 oid_to_hex(&complete
->item
->object
.oid
));
537 pop_most_recent_commit(&complete
, COMPLETE
);
541 static void add_refs_to_oidset(struct oidset
*oids
, struct ref
*refs
)
543 for (; refs
; refs
= refs
->next
)
544 oidset_insert(oids
, &refs
->old_oid
);
547 static int is_unmatched_ref(const struct ref
*ref
)
549 struct object_id oid
;
551 return ref
->match_status
== REF_NOT_MATCHED
&&
552 !parse_oid_hex(ref
->name
, &oid
, &p
) &&
554 oideq(&oid
, &ref
->old_oid
);
557 static void filter_refs(struct fetch_pack_args
*args
,
559 struct ref
**sought
, int nr_sought
)
561 struct ref
*newlist
= NULL
;
562 struct ref
**newtail
= &newlist
;
563 struct ref
*unmatched
= NULL
;
564 struct ref
*ref
, *next
;
565 struct oidset tip_oids
= OIDSET_INIT
;
567 int strict
= !(allow_unadvertised_object_request
&
568 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
571 for (ref
= *refs
; ref
; ref
= next
) {
575 if (starts_with(ref
->name
, "refs/") &&
576 check_refname_format(ref
->name
, 0)) {
578 * trash or a peeled value; do not even add it to
584 while (i
< nr_sought
) {
585 int cmp
= strcmp(ref
->name
, sought
[i
]->name
);
587 break; /* definitely do not have it */
589 keep
= 1; /* definitely have it */
590 sought
[i
]->match_status
= REF_MATCHED
;
595 if (!keep
&& args
->fetch_all
&&
596 (!args
->deepen
|| !starts_with(ref
->name
, "refs/tags/")))
603 newtail
= &ref
->next
;
605 ref
->next
= unmatched
;
611 for (i
= 0; i
< nr_sought
; i
++) {
613 if (!is_unmatched_ref(ref
))
616 add_refs_to_oidset(&tip_oids
, unmatched
);
617 add_refs_to_oidset(&tip_oids
, newlist
);
622 /* Append unmatched requests to the list */
623 for (i
= 0; i
< nr_sought
; i
++) {
625 if (!is_unmatched_ref(ref
))
628 if (!strict
|| oidset_contains(&tip_oids
, &ref
->old_oid
)) {
629 ref
->match_status
= REF_MATCHED
;
630 *newtail
= copy_ref(ref
);
631 newtail
= &(*newtail
)->next
;
633 ref
->match_status
= REF_UNADVERTISED_NOT_ALLOWED
;
637 oidset_clear(&tip_oids
);
638 free_refs(unmatched
);
643 static void mark_alternate_complete(struct fetch_negotiator
*unused
,
646 mark_complete(&obj
->oid
);
649 struct loose_object_iter
{
650 struct oidset
*loose_object_set
;
655 * Mark recent commits available locally and reachable from a local ref as
656 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
657 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
658 * thus do not need COMMON_REF marks).
660 * The cutoff time for recency is determined by this heuristic: it is the
661 * earliest commit time of the objects in refs that are commits and that we know
662 * the commit time of.
664 static void mark_complete_and_common_ref(struct fetch_negotiator
*negotiator
,
665 struct fetch_pack_args
*args
,
669 int old_save_commit_buffer
= save_commit_buffer
;
670 timestamp_t cutoff
= 0;
672 save_commit_buffer
= 0;
674 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL
);
675 for (ref
= *refs
; ref
; ref
= ref
->next
) {
678 if (!has_object_file_with_flags(&ref
->old_oid
,
680 OBJECT_INFO_SKIP_FETCH_OBJECT
))
682 o
= parse_object(the_repository
, &ref
->old_oid
);
687 * We already have it -- which may mean that we were
688 * in sync with the other side at some time after
689 * that (it is OK if we guess wrong here).
691 if (o
->type
== OBJ_COMMIT
) {
692 struct commit
*commit
= (struct commit
*)o
;
693 if (!cutoff
|| cutoff
< commit
->date
)
694 cutoff
= commit
->date
;
697 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL
);
700 * This block marks all local refs as COMPLETE, and then recursively marks all
701 * parents of those refs as COMPLETE.
703 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL
);
705 for_each_ref(mark_complete_oid
, NULL
);
706 for_each_cached_alternate(NULL
, mark_alternate_complete
);
707 commit_list_sort_by_date(&complete
);
709 mark_recent_complete_commits(args
, cutoff
);
711 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL
);
714 * Mark all complete remote refs as common refs.
715 * Don't mark them common yet; the server has to be told so first.
717 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL
);
718 for (ref
= *refs
; ref
; ref
= ref
->next
) {
719 struct object
*o
= deref_tag(the_repository
,
720 lookup_object(the_repository
,
724 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
727 negotiator
->known_common(negotiator
,
730 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL
);
732 save_commit_buffer
= old_save_commit_buffer
;
736 * Returns 1 if every object pointed to by the given remote refs is available
737 * locally and reachable from a local ref, and 0 otherwise.
739 static int everything_local(struct fetch_pack_args
*args
,
745 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
746 const struct object_id
*remote
= &ref
->old_oid
;
749 o
= lookup_object(the_repository
, remote
);
750 if (!o
|| !(o
->flags
& COMPLETE
)) {
752 print_verbose(args
, "want %s (%s)", oid_to_hex(remote
),
756 print_verbose(args
, _("already have %s (%s)"), oid_to_hex(remote
),
763 static int sideband_demux(int in
, int out
, void *data
)
768 ret
= recv_sideband("fetch-pack", xd
[0], out
);
773 static void write_promisor_file(const char *keep_name
,
774 struct ref
**sought
, int nr_sought
)
776 struct strbuf promisor_name
= STRBUF_INIT
;
781 strbuf_addstr(&promisor_name
, keep_name
);
782 suffix_stripped
= strbuf_strip_suffix(&promisor_name
, ".keep");
783 if (!suffix_stripped
)
784 BUG("name of pack lockfile should end with .keep (was '%s')",
786 strbuf_addstr(&promisor_name
, ".promisor");
788 output
= xfopen(promisor_name
.buf
, "w");
789 for (i
= 0; i
< nr_sought
; i
++)
790 fprintf(output
, "%s %s\n", oid_to_hex(&sought
[i
]->old_oid
),
794 strbuf_release(&promisor_name
);
797 static int get_pack(struct fetch_pack_args
*args
,
798 int xd
[2], struct string_list
*pack_lockfiles
,
800 struct ref
**sought
, int nr_sought
)
803 int do_keep
= args
->keep_pack
;
804 const char *cmd_name
;
805 struct pack_header header
;
807 struct child_process cmd
= CHILD_PROCESS_INIT
;
810 memset(&demux
, 0, sizeof(demux
));
812 /* xd[] is talking with upload-pack; subprocess reads from
813 * xd[0], spits out band#2 to stderr, and feeds us band#1
814 * through demux->out.
816 demux
.proc
= sideband_demux
;
819 demux
.isolate_sigpipe
= 1;
820 if (start_async(&demux
))
821 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
826 if (!args
->keep_pack
&& unpack_limit
) {
828 if (read_pack_header(demux
.out
, &header
))
829 die(_("protocol error: bad pack header"));
831 if (ntohl(header
.hdr_entries
) < unpack_limit
)
837 if (alternate_shallow_file
) {
838 argv_array_push(&cmd
.args
, "--shallow-file");
839 argv_array_push(&cmd
.args
, alternate_shallow_file
);
842 if (do_keep
|| args
->from_promisor
) {
845 cmd_name
= "index-pack";
846 argv_array_push(&cmd
.args
, cmd_name
);
847 argv_array_push(&cmd
.args
, "--stdin");
848 if (!args
->quiet
&& !args
->no_progress
)
849 argv_array_push(&cmd
.args
, "-v");
850 if (args
->use_thin_pack
)
851 argv_array_push(&cmd
.args
, "--fix-thin");
852 if (do_keep
&& (args
->lock_pack
|| unpack_limit
)) {
853 char hostname
[HOST_NAME_MAX
+ 1];
854 if (xgethostname(hostname
, sizeof(hostname
)))
855 xsnprintf(hostname
, sizeof(hostname
), "localhost");
856 argv_array_pushf(&cmd
.args
,
857 "--keep=fetch-pack %"PRIuMAX
" on %s",
858 (uintmax_t)getpid(), hostname
);
860 if (only_packfile
&& args
->check_self_contained_and_connected
)
861 argv_array_push(&cmd
.args
, "--check-self-contained-and-connected");
864 * We cannot perform any connectivity checks because
865 * not all packs have been downloaded; let the caller
866 * have this responsibility.
868 args
->check_self_contained_and_connected
= 0;
870 * If we're obtaining the filename of a lockfile, we'll use
871 * that filename to write a .promisor file with more
872 * information below. If not, we need index-pack to do it for
875 if (!(do_keep
&& pack_lockfiles
) && args
->from_promisor
)
876 argv_array_push(&cmd
.args
, "--promisor");
879 cmd_name
= "unpack-objects";
880 argv_array_push(&cmd
.args
, cmd_name
);
881 if (args
->quiet
|| args
->no_progress
)
882 argv_array_push(&cmd
.args
, "-q");
883 args
->check_self_contained_and_connected
= 0;
887 argv_array_pushf(&cmd
.args
, "--pack_header=%"PRIu32
",%"PRIu32
,
888 ntohl(header
.hdr_version
),
889 ntohl(header
.hdr_entries
));
890 if (fetch_fsck_objects
>= 0
892 : transfer_fsck_objects
>= 0
893 ? transfer_fsck_objects
895 if (args
->from_promisor
)
897 * We cannot use --strict in index-pack because it
898 * checks both broken objects and links, but we only
899 * want to check for broken objects.
901 argv_array_push(&cmd
.args
, "--fsck-objects");
903 argv_array_pushf(&cmd
.args
, "--strict%s",
909 if (start_command(&cmd
))
910 die(_("fetch-pack: unable to fork off %s"), cmd_name
);
911 if (do_keep
&& pack_lockfiles
) {
912 string_list_append_nodup(pack_lockfiles
,
913 index_pack_lockfile(cmd
.out
));
918 /* Closed by start_command() */
921 ret
= finish_command(&cmd
);
922 if (!ret
|| (args
->check_self_contained_and_connected
&& ret
== 1))
923 args
->self_contained_and_connected
=
924 args
->check_self_contained_and_connected
&&
927 die(_("%s failed"), cmd_name
);
928 if (use_sideband
&& finish_async(&demux
))
929 die(_("error in sideband demultiplexer"));
932 * Now that index-pack has succeeded, write the promisor file using the
933 * obtained .keep filename if necessary
935 if (do_keep
&& pack_lockfiles
&& pack_lockfiles
->nr
&& args
->from_promisor
)
936 write_promisor_file(pack_lockfiles
->items
[0].string
, sought
, nr_sought
);
941 static int cmp_ref_by_name(const void *a_
, const void *b_
)
943 const struct ref
*a
= *((const struct ref
**)a_
);
944 const struct ref
*b
= *((const struct ref
**)b_
);
945 return strcmp(a
->name
, b
->name
);
948 static struct ref
*do_fetch_pack(struct fetch_pack_args
*args
,
950 const struct ref
*orig_ref
,
951 struct ref
**sought
, int nr_sought
,
952 struct shallow_info
*si
,
953 struct string_list
*pack_lockfiles
)
955 struct repository
*r
= the_repository
;
956 struct ref
*ref
= copy_ref_list(orig_ref
);
957 struct object_id oid
;
958 const char *agent_feature
;
960 struct fetch_negotiator negotiator_alloc
;
961 struct fetch_negotiator
*negotiator
;
963 if (args
->no_dependents
) {
966 negotiator
= &negotiator_alloc
;
967 fetch_negotiator_init(r
, negotiator
);
970 sort_ref_list(&ref
, ref_compare_name
);
971 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
973 if ((agent_feature
= server_feature_value("agent", &agent_len
))) {
976 print_verbose(args
, _("Server version is %.*s"),
977 agent_len
, agent_feature
);
980 if (server_supports("shallow"))
981 print_verbose(args
, _("Server supports %s"), "shallow");
982 else if (args
->depth
> 0 || is_repository_shallow(r
))
983 die(_("Server does not support shallow clients"));
984 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
986 if (server_supports("multi_ack_detailed")) {
987 print_verbose(args
, _("Server supports %s"), "multi_ack_detailed");
989 if (server_supports("no-done")) {
990 print_verbose(args
, _("Server supports %s"), "no-done");
991 if (args
->stateless_rpc
)
995 else if (server_supports("multi_ack")) {
996 print_verbose(args
, _("Server supports %s"), "multi_ack");
999 if (server_supports("side-band-64k")) {
1000 print_verbose(args
, _("Server supports %s"), "side-band-64k");
1003 else if (server_supports("side-band")) {
1004 print_verbose(args
, _("Server supports %s"), "side-band");
1007 if (server_supports("allow-tip-sha1-in-want")) {
1008 print_verbose(args
, _("Server supports %s"), "allow-tip-sha1-in-want");
1009 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1011 if (server_supports("allow-reachable-sha1-in-want")) {
1012 print_verbose(args
, _("Server supports %s"), "allow-reachable-sha1-in-want");
1013 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1015 if (server_supports("thin-pack"))
1016 print_verbose(args
, _("Server supports %s"), "thin-pack");
1018 args
->use_thin_pack
= 0;
1019 if (server_supports("no-progress"))
1020 print_verbose(args
, _("Server supports %s"), "no-progress");
1022 args
->no_progress
= 0;
1023 if (server_supports("include-tag"))
1024 print_verbose(args
, _("Server supports %s"), "include-tag");
1026 args
->include_tag
= 0;
1027 if (server_supports("ofs-delta"))
1028 print_verbose(args
, _("Server supports %s"), "ofs-delta");
1030 prefer_ofs_delta
= 0;
1032 if (server_supports("filter")) {
1033 server_supports_filtering
= 1;
1034 print_verbose(args
, _("Server supports %s"), "filter");
1035 } else if (args
->filter_options
.choice
) {
1036 warning("filtering not recognized by server, ignoring");
1039 if (server_supports("deepen-since")) {
1040 print_verbose(args
, _("Server supports %s"), "deepen-since");
1041 deepen_since_ok
= 1;
1042 } else if (args
->deepen_since
)
1043 die(_("Server does not support --shallow-since"));
1044 if (server_supports("deepen-not")) {
1045 print_verbose(args
, _("Server supports %s"), "deepen-not");
1047 } else if (args
->deepen_not
)
1048 die(_("Server does not support --shallow-exclude"));
1049 if (server_supports("deepen-relative"))
1050 print_verbose(args
, _("Server supports %s"), "deepen-relative");
1051 else if (args
->deepen_relative
)
1052 die(_("Server does not support --deepen"));
1054 if (!args
->no_dependents
) {
1055 mark_complete_and_common_ref(negotiator
, args
, &ref
);
1056 filter_refs(args
, &ref
, sought
, nr_sought
);
1057 if (everything_local(args
, &ref
)) {
1058 packet_flush(fd
[1]);
1062 filter_refs(args
, &ref
, sought
, nr_sought
);
1064 if (find_common(negotiator
, args
, fd
, &oid
, ref
) < 0)
1065 if (!args
->keep_pack
)
1066 /* When cloning, it is not unusual to have
1069 warning(_("no common commits"));
1071 if (args
->stateless_rpc
)
1072 packet_flush(fd
[1]);
1074 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
1076 else if (si
->nr_ours
|| si
->nr_theirs
)
1077 alternate_shallow_file
= setup_temporary_shallow(si
->shallow
);
1079 alternate_shallow_file
= NULL
;
1080 if (get_pack(args
, fd
, pack_lockfiles
, 1, sought
, nr_sought
))
1081 die(_("git fetch-pack: fetch failed."));
1085 negotiator
->release(negotiator
);
1089 static void add_shallow_requests(struct strbuf
*req_buf
,
1090 const struct fetch_pack_args
*args
)
1092 if (is_repository_shallow(the_repository
))
1093 write_shallow_commits(req_buf
, 1, NULL
);
1094 if (args
->depth
> 0)
1095 packet_buf_write(req_buf
, "deepen %d", args
->depth
);
1096 if (args
->deepen_since
) {
1097 timestamp_t max_age
= approxidate(args
->deepen_since
);
1098 packet_buf_write(req_buf
, "deepen-since %"PRItime
, max_age
);
1100 if (args
->deepen_not
) {
1102 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
1103 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
1104 packet_buf_write(req_buf
, "deepen-not %s", s
->string
);
1107 if (args
->deepen_relative
)
1108 packet_buf_write(req_buf
, "deepen-relative\n");
1111 static void add_wants(int no_dependents
, const struct ref
*wants
, struct strbuf
*req_buf
)
1113 int use_ref_in_want
= server_supports_feature("fetch", "ref-in-want", 0);
1115 for ( ; wants
; wants
= wants
->next
) {
1116 const struct object_id
*remote
= &wants
->old_oid
;
1120 * If that object is complete (i.e. it is an ancestor of a
1121 * local ref), we tell them we have it but do not have to
1122 * tell them about its ancestors, which they already know
1125 * We use lookup_object here because we are only
1126 * interested in the case we *know* the object is
1127 * reachable and we have already scanned it.
1129 * Do this only if args->no_dependents is false (if it is true,
1130 * we cannot trust the object flags).
1132 if (!no_dependents
&&
1133 ((o
= lookup_object(the_repository
, remote
)) != NULL
) &&
1134 (o
->flags
& COMPLETE
)) {
1138 if (!use_ref_in_want
|| wants
->exact_oid
)
1139 packet_buf_write(req_buf
, "want %s\n", oid_to_hex(remote
));
1141 packet_buf_write(req_buf
, "want-ref %s\n", wants
->name
);
1145 static void add_common(struct strbuf
*req_buf
, struct oidset
*common
)
1147 struct oidset_iter iter
;
1148 const struct object_id
*oid
;
1149 oidset_iter_init(common
, &iter
);
1151 while ((oid
= oidset_iter_next(&iter
))) {
1152 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1156 static int add_haves(struct fetch_negotiator
*negotiator
,
1158 struct strbuf
*req_buf
,
1159 int *haves_to_send
, int *in_vain
)
1162 int haves_added
= 0;
1163 const struct object_id
*oid
;
1165 while ((oid
= negotiator
->next(negotiator
))) {
1166 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1167 if (++haves_added
>= *haves_to_send
)
1171 *in_vain
+= haves_added
;
1172 if (!haves_added
|| (seen_ack
&& *in_vain
>= MAX_IN_VAIN
)) {
1174 packet_buf_write(req_buf
, "done\n");
1178 /* Increase haves to send on next round */
1179 *haves_to_send
= next_flush(1, *haves_to_send
);
1184 static int send_fetch_request(struct fetch_negotiator
*negotiator
, int fd_out
,
1185 struct fetch_pack_args
*args
,
1186 const struct ref
*wants
, struct oidset
*common
,
1187 int *haves_to_send
, int *in_vain
,
1188 int sideband_all
, int seen_ack
)
1191 struct strbuf req_buf
= STRBUF_INIT
;
1193 if (server_supports_v2("fetch", 1))
1194 packet_buf_write(&req_buf
, "command=fetch");
1195 if (server_supports_v2("agent", 0))
1196 packet_buf_write(&req_buf
, "agent=%s", git_user_agent_sanitized());
1197 if (args
->server_options
&& args
->server_options
->nr
&&
1198 server_supports_v2("server-option", 1)) {
1200 for (i
= 0; i
< args
->server_options
->nr
; i
++)
1201 packet_buf_write(&req_buf
, "server-option=%s",
1202 args
->server_options
->items
[i
].string
);
1205 packet_buf_delim(&req_buf
);
1206 if (args
->use_thin_pack
)
1207 packet_buf_write(&req_buf
, "thin-pack");
1208 if (args
->no_progress
)
1209 packet_buf_write(&req_buf
, "no-progress");
1210 if (args
->include_tag
)
1211 packet_buf_write(&req_buf
, "include-tag");
1212 if (prefer_ofs_delta
)
1213 packet_buf_write(&req_buf
, "ofs-delta");
1215 packet_buf_write(&req_buf
, "sideband-all");
1217 /* Add shallow-info and deepen request */
1218 if (server_supports_feature("fetch", "shallow", 0))
1219 add_shallow_requests(&req_buf
, args
);
1220 else if (is_repository_shallow(the_repository
) || args
->deepen
)
1221 die(_("Server does not support shallow requests"));
1224 if (server_supports_feature("fetch", "filter", 0) &&
1225 args
->filter_options
.choice
) {
1227 expand_list_objects_filter_spec(&args
->filter_options
);
1228 print_verbose(args
, _("Server supports filter"));
1229 packet_buf_write(&req_buf
, "filter %s", spec
);
1230 } else if (args
->filter_options
.choice
) {
1231 warning("filtering not recognized by server, ignoring");
1234 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1236 struct strbuf to_send
= STRBUF_INIT
;
1238 for (i
= 0; i
< uri_protocols
.nr
; i
++) {
1239 const char *s
= uri_protocols
.items
[i
].string
;
1241 if (!strcmp(s
, "https") || !strcmp(s
, "http")) {
1243 strbuf_addch(&to_send
, ',');
1244 strbuf_addstr(&to_send
, s
);
1248 packet_buf_write(&req_buf
, "packfile-uris %s",
1250 strbuf_release(&to_send
);
1255 add_wants(args
->no_dependents
, wants
, &req_buf
);
1257 if (args
->no_dependents
) {
1258 packet_buf_write(&req_buf
, "done");
1261 /* Add all of the common commits we've found in previous rounds */
1262 add_common(&req_buf
, common
);
1264 /* Add initial haves */
1265 ret
= add_haves(negotiator
, seen_ack
, &req_buf
,
1266 haves_to_send
, in_vain
);
1270 packet_buf_flush(&req_buf
);
1271 if (write_in_full(fd_out
, req_buf
.buf
, req_buf
.len
) < 0)
1272 die_errno(_("unable to write request to remote"));
1274 strbuf_release(&req_buf
);
1279 * Processes a section header in a server's response and checks if it matches
1280 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1281 * not consumed); if 0, the line will be consumed and the function will die if
1282 * the section header doesn't match what was expected.
1284 static int process_section_header(struct packet_reader
*reader
,
1285 const char *section
, int peek
)
1289 if (packet_reader_peek(reader
) != PACKET_READ_NORMAL
)
1290 die(_("error reading section header '%s'"), section
);
1292 ret
= !strcmp(reader
->line
, section
);
1296 die(_("expected '%s', received '%s'"),
1297 section
, reader
->line
);
1298 packet_reader_read(reader
);
1306 * No commit was found to be possessed by both the client and the
1307 * server, and "ready" was not received.
1312 * At least one commit was found to be possessed by both the client and
1313 * the server, and "ready" was not received.
1318 * "ready" was received, indicating that the server is ready to send
1319 * the packfile without any further negotiation.
1324 static enum common_found
process_acks(struct fetch_negotiator
*negotiator
,
1325 struct packet_reader
*reader
,
1326 struct oidset
*common
)
1329 int received_ready
= 0;
1330 int received_ack
= 0;
1332 process_section_header(reader
, "acknowledgments", 0);
1333 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1336 if (!strcmp(reader
->line
, "NAK"))
1339 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
1340 struct object_id oid
;
1342 if (!get_oid_hex(arg
, &oid
)) {
1343 struct commit
*commit
;
1344 oidset_insert(common
, &oid
);
1345 commit
= lookup_commit(the_repository
, &oid
);
1347 negotiator
->ack(negotiator
, commit
);
1352 if (!strcmp(reader
->line
, "ready")) {
1357 die(_("unexpected acknowledgment line: '%s'"), reader
->line
);
1360 if (reader
->status
!= PACKET_READ_FLUSH
&&
1361 reader
->status
!= PACKET_READ_DELIM
)
1362 die(_("error processing acks: %d"), reader
->status
);
1365 * If an "acknowledgments" section is sent, a packfile is sent if and
1366 * only if "ready" was sent in this section. The other sections
1367 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1368 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1371 if (received_ready
&& reader
->status
!= PACKET_READ_DELIM
)
1372 die(_("expected packfile to be sent after 'ready'"));
1373 if (!received_ready
&& reader
->status
!= PACKET_READ_FLUSH
)
1374 die(_("expected no other sections to be sent after no 'ready'"));
1376 return received_ready
? READY
:
1377 (received_ack
? COMMON_FOUND
: NO_COMMON_FOUND
);
1380 static void receive_shallow_info(struct fetch_pack_args
*args
,
1381 struct packet_reader
*reader
,
1382 struct oid_array
*shallows
,
1383 struct shallow_info
*si
)
1385 int unshallow_received
= 0;
1387 process_section_header(reader
, "shallow-info", 0);
1388 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1390 struct object_id oid
;
1392 if (skip_prefix(reader
->line
, "shallow ", &arg
)) {
1393 if (get_oid_hex(arg
, &oid
))
1394 die(_("invalid shallow line: %s"), reader
->line
);
1395 oid_array_append(shallows
, &oid
);
1398 if (skip_prefix(reader
->line
, "unshallow ", &arg
)) {
1399 if (get_oid_hex(arg
, &oid
))
1400 die(_("invalid unshallow line: %s"), reader
->line
);
1401 if (!lookup_object(the_repository
, &oid
))
1402 die(_("object not found: %s"), reader
->line
);
1403 /* make sure that it is parsed as shallow */
1404 if (!parse_object(the_repository
, &oid
))
1405 die(_("error in object: %s"), reader
->line
);
1406 if (unregister_shallow(&oid
))
1407 die(_("no shallow found: %s"), reader
->line
);
1408 unshallow_received
= 1;
1411 die(_("expected shallow/unshallow, got %s"), reader
->line
);
1414 if (reader
->status
!= PACKET_READ_FLUSH
&&
1415 reader
->status
!= PACKET_READ_DELIM
)
1416 die(_("error processing shallow info: %d"), reader
->status
);
1418 if (args
->deepen
|| unshallow_received
) {
1420 * Treat these as shallow lines caused by our depth settings.
1421 * In v0, these lines cannot cause refs to be rejected; do the
1426 for (i
= 0; i
< shallows
->nr
; i
++)
1427 register_shallow(the_repository
, &shallows
->oid
[i
]);
1428 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
1431 } else if (shallows
->nr
) {
1433 * Treat these as shallow lines caused by the remote being
1434 * shallow. In v0, remote refs that reach these objects are
1435 * rejected (unless --update-shallow is set); do the same.
1437 prepare_shallow_info(si
, shallows
);
1438 if (si
->nr_ours
|| si
->nr_theirs
)
1439 alternate_shallow_file
=
1440 setup_temporary_shallow(si
->shallow
);
1442 alternate_shallow_file
= NULL
;
1444 alternate_shallow_file
= NULL
;
1448 static int cmp_name_ref(const void *name
, const void *ref
)
1450 return strcmp(name
, (*(struct ref
**)ref
)->name
);
1453 static void receive_wanted_refs(struct packet_reader
*reader
,
1454 struct ref
**sought
, int nr_sought
)
1456 process_section_header(reader
, "wanted-refs", 0);
1457 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1458 struct object_id oid
;
1462 if (parse_oid_hex(reader
->line
, &oid
, &end
) || *end
++ != ' ')
1463 die(_("expected wanted-ref, got '%s'"), reader
->line
);
1465 found
= bsearch(end
, sought
, nr_sought
, sizeof(*sought
),
1468 die(_("unexpected wanted-ref: '%s'"), reader
->line
);
1469 oidcpy(&(*found
)->old_oid
, &oid
);
1472 if (reader
->status
!= PACKET_READ_DELIM
)
1473 die(_("error processing wanted refs: %d"), reader
->status
);
1476 static void receive_packfile_uris(struct packet_reader
*reader
,
1477 struct string_list
*uris
)
1479 process_section_header(reader
, "packfile-uris", 0);
1480 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1481 if (reader
->pktlen
< the_hash_algo
->hexsz
||
1482 reader
->line
[the_hash_algo
->hexsz
] != ' ')
1483 die("expected '<hash> <uri>', got: %s\n", reader
->line
);
1485 string_list_append(uris
, reader
->line
);
1487 if (reader
->status
!= PACKET_READ_DELIM
)
1488 die("expected DELIM");
1492 FETCH_CHECK_LOCAL
= 0,
1499 static struct ref
*do_fetch_pack_v2(struct fetch_pack_args
*args
,
1501 const struct ref
*orig_ref
,
1502 struct ref
**sought
, int nr_sought
,
1503 struct oid_array
*shallows
,
1504 struct shallow_info
*si
,
1505 struct string_list
*pack_lockfiles
)
1507 struct repository
*r
= the_repository
;
1508 struct ref
*ref
= copy_ref_list(orig_ref
);
1509 enum fetch_state state
= FETCH_CHECK_LOCAL
;
1510 struct oidset common
= OIDSET_INIT
;
1511 struct packet_reader reader
;
1512 int in_vain
= 0, negotiation_started
= 0;
1513 int haves_to_send
= INITIAL_FLUSH
;
1514 struct fetch_negotiator negotiator_alloc
;
1515 struct fetch_negotiator
*negotiator
;
1517 struct string_list packfile_uris
= STRING_LIST_INIT_DUP
;
1520 if (args
->no_dependents
) {
1523 negotiator
= &negotiator_alloc
;
1524 fetch_negotiator_init(r
, negotiator
);
1527 packet_reader_init(&reader
, fd
[0], NULL
, 0,
1528 PACKET_READ_CHOMP_NEWLINE
|
1529 PACKET_READ_DIE_ON_ERR_PACKET
);
1530 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1531 server_supports_feature("fetch", "sideband-all", 0)) {
1532 reader
.use_sideband
= 1;
1533 reader
.me
= "fetch-pack";
1536 while (state
!= FETCH_DONE
) {
1538 case FETCH_CHECK_LOCAL
:
1539 sort_ref_list(&ref
, ref_compare_name
);
1540 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
1542 /* v2 supports these by default */
1543 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1545 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
1548 /* Filter 'ref' by 'sought' and those that aren't local */
1549 if (!args
->no_dependents
) {
1550 mark_complete_and_common_ref(negotiator
, args
, &ref
);
1551 filter_refs(args
, &ref
, sought
, nr_sought
);
1552 if (everything_local(args
, &ref
))
1555 state
= FETCH_SEND_REQUEST
;
1557 mark_tips(negotiator
, args
->negotiation_tips
);
1558 for_each_cached_alternate(negotiator
,
1559 insert_one_alternate_object
);
1561 filter_refs(args
, &ref
, sought
, nr_sought
);
1562 state
= FETCH_SEND_REQUEST
;
1565 case FETCH_SEND_REQUEST
:
1566 if (!negotiation_started
) {
1567 negotiation_started
= 1;
1568 trace2_region_enter("fetch-pack",
1572 if (send_fetch_request(negotiator
, fd
[1], args
, ref
,
1574 &haves_to_send
, &in_vain
,
1575 reader
.use_sideband
,
1577 state
= FETCH_GET_PACK
;
1579 state
= FETCH_PROCESS_ACKS
;
1581 case FETCH_PROCESS_ACKS
:
1582 /* Process ACKs/NAKs */
1583 switch (process_acks(negotiator
, &reader
, &common
)) {
1585 state
= FETCH_GET_PACK
;
1591 case NO_COMMON_FOUND
:
1592 state
= FETCH_SEND_REQUEST
;
1596 case FETCH_GET_PACK
:
1597 trace2_region_leave("fetch-pack",
1600 /* Check for shallow-info section */
1601 if (process_section_header(&reader
, "shallow-info", 1))
1602 receive_shallow_info(args
, &reader
, shallows
, si
);
1604 if (process_section_header(&reader
, "wanted-refs", 1))
1605 receive_wanted_refs(&reader
, sought
, nr_sought
);
1607 /* get the pack(s) */
1608 if (process_section_header(&reader
, "packfile-uris", 1))
1609 receive_packfile_uris(&reader
, &packfile_uris
);
1610 process_section_header(&reader
, "packfile", 0);
1611 if (get_pack(args
, fd
, pack_lockfiles
,
1612 !packfile_uris
.nr
, sought
, nr_sought
))
1613 die(_("git fetch-pack: fetch failed."));
1622 for (i
= 0; i
< packfile_uris
.nr
; i
++) {
1623 struct child_process cmd
= CHILD_PROCESS_INIT
;
1624 char packname
[GIT_MAX_HEXSZ
+ 1];
1625 const char *uri
= packfile_uris
.items
[i
].string
+
1626 the_hash_algo
->hexsz
+ 1;
1628 argv_array_push(&cmd
.args
, "http-fetch");
1629 argv_array_pushf(&cmd
.args
, "--packfile=%.*s",
1630 (int) the_hash_algo
->hexsz
,
1631 packfile_uris
.items
[i
].string
);
1632 argv_array_push(&cmd
.args
, uri
);
1636 if (start_command(&cmd
))
1637 die("fetch-pack: unable to spawn http-fetch");
1639 if (read_in_full(cmd
.out
, packname
, 5) < 0 ||
1640 memcmp(packname
, "keep\t", 5))
1641 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1643 if (read_in_full(cmd
.out
, packname
,
1644 the_hash_algo
->hexsz
+ 1) < 0 ||
1645 packname
[the_hash_algo
->hexsz
] != '\n')
1646 die("fetch-pack: expected hash then LF at end of http-fetch output");
1648 packname
[the_hash_algo
->hexsz
] = '\0';
1652 if (finish_command(&cmd
))
1653 die("fetch-pack: unable to finish http-fetch");
1655 if (memcmp(packfile_uris
.items
[i
].string
, packname
,
1656 the_hash_algo
->hexsz
))
1657 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1658 uri
, (int) the_hash_algo
->hexsz
,
1659 packfile_uris
.items
[i
].string
);
1661 string_list_append_nodup(pack_lockfiles
,
1662 xstrfmt("%s/pack/pack-%s.keep",
1663 get_object_directory(),
1666 string_list_clear(&packfile_uris
, 0);
1669 negotiator
->release(negotiator
);
1671 oidset_clear(&common
);
1675 static int fetch_pack_config_cb(const char *var
, const char *value
, void *cb
)
1677 if (strcmp(var
, "fetch.fsck.skiplist") == 0) {
1680 if (git_config_pathname(&path
, var
, value
))
1682 strbuf_addf(&fsck_msg_types
, "%cskiplist=%s",
1683 fsck_msg_types
.len
? ',' : '=', path
);
1688 if (skip_prefix(var
, "fetch.fsck.", &var
)) {
1689 if (is_valid_msg_type(var
, value
))
1690 strbuf_addf(&fsck_msg_types
, "%c%s=%s",
1691 fsck_msg_types
.len
? ',' : '=', var
, value
);
1693 warning("Skipping unknown msg id '%s'", var
);
1697 return git_default_config(var
, value
, cb
);
1700 static void fetch_pack_config(void)
1702 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit
);
1703 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit
);
1704 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta
);
1705 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects
);
1706 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects
);
1707 if (!uri_protocols
.nr
) {
1710 if (!git_config_get_string("fetch.uriprotocols", &str
) && str
) {
1711 string_list_split(&uri_protocols
, str
, ',', -1);
1716 git_config(fetch_pack_config_cb
, NULL
);
1719 static void fetch_pack_setup(void)
1721 static int did_setup
;
1724 fetch_pack_config();
1725 if (0 <= transfer_unpack_limit
)
1726 unpack_limit
= transfer_unpack_limit
;
1727 else if (0 <= fetch_unpack_limit
)
1728 unpack_limit
= fetch_unpack_limit
;
1732 static int remove_duplicates_in_refs(struct ref
**ref
, int nr
)
1734 struct string_list names
= STRING_LIST_INIT_NODUP
;
1737 for (src
= dst
= 0; src
< nr
; src
++) {
1738 struct string_list_item
*item
;
1739 item
= string_list_insert(&names
, ref
[src
]->name
);
1741 continue; /* already have it */
1742 item
->util
= ref
[src
];
1744 ref
[dst
] = ref
[src
];
1747 for (src
= dst
; src
< nr
; src
++)
1749 string_list_clear(&names
, 0);
1753 static void update_shallow(struct fetch_pack_args
*args
,
1754 struct ref
**sought
, int nr_sought
,
1755 struct shallow_info
*si
)
1757 struct oid_array ref
= OID_ARRAY_INIT
;
1761 if (args
->deepen
&& alternate_shallow_file
) {
1762 if (*alternate_shallow_file
== '\0') { /* --unshallow */
1763 unlink_or_warn(git_path_shallow(the_repository
));
1764 rollback_shallow_file(the_repository
, &shallow_lock
);
1766 commit_shallow_file(the_repository
, &shallow_lock
);
1767 alternate_shallow_file
= NULL
;
1771 if (!si
->shallow
|| !si
->shallow
->nr
)
1774 if (args
->cloning
) {
1776 * remote is shallow, but this is a clone, there are
1777 * no objects in repo to worry about. Accept any
1778 * shallow points that exist in the pack (iow in repo
1779 * after get_pack() and reprepare_packed_git())
1781 struct oid_array extra
= OID_ARRAY_INIT
;
1782 struct object_id
*oid
= si
->shallow
->oid
;
1783 for (i
= 0; i
< si
->shallow
->nr
; i
++)
1784 if (has_object_file(&oid
[i
]))
1785 oid_array_append(&extra
, &oid
[i
]);
1787 setup_alternate_shallow(&shallow_lock
,
1788 &alternate_shallow_file
,
1790 commit_shallow_file(the_repository
, &shallow_lock
);
1791 alternate_shallow_file
= NULL
;
1793 oid_array_clear(&extra
);
1797 if (!si
->nr_ours
&& !si
->nr_theirs
)
1800 remove_nonexistent_theirs_shallow(si
);
1801 if (!si
->nr_ours
&& !si
->nr_theirs
)
1803 for (i
= 0; i
< nr_sought
; i
++)
1804 oid_array_append(&ref
, &sought
[i
]->old_oid
);
1807 if (args
->update_shallow
) {
1809 * remote is also shallow, .git/shallow may be updated
1810 * so all refs can be accepted. Make sure we only add
1811 * shallow roots that are actually reachable from new
1814 struct oid_array extra
= OID_ARRAY_INIT
;
1815 struct object_id
*oid
= si
->shallow
->oid
;
1816 assign_shallow_commits_to_refs(si
, NULL
, NULL
);
1817 if (!si
->nr_ours
&& !si
->nr_theirs
) {
1818 oid_array_clear(&ref
);
1821 for (i
= 0; i
< si
->nr_ours
; i
++)
1822 oid_array_append(&extra
, &oid
[si
->ours
[i
]]);
1823 for (i
= 0; i
< si
->nr_theirs
; i
++)
1824 oid_array_append(&extra
, &oid
[si
->theirs
[i
]]);
1825 setup_alternate_shallow(&shallow_lock
,
1826 &alternate_shallow_file
,
1828 commit_shallow_file(the_repository
, &shallow_lock
);
1829 oid_array_clear(&extra
);
1830 oid_array_clear(&ref
);
1831 alternate_shallow_file
= NULL
;
1836 * remote is also shallow, check what ref is safe to update
1837 * without updating .git/shallow
1839 status
= xcalloc(nr_sought
, sizeof(*status
));
1840 assign_shallow_commits_to_refs(si
, NULL
, status
);
1841 if (si
->nr_ours
|| si
->nr_theirs
) {
1842 for (i
= 0; i
< nr_sought
; i
++)
1844 sought
[i
]->status
= REF_STATUS_REJECT_SHALLOW
;
1847 oid_array_clear(&ref
);
1850 static int iterate_ref_map(void *cb_data
, struct object_id
*oid
)
1852 struct ref
**rm
= cb_data
;
1853 struct ref
*ref
= *rm
;
1856 return -1; /* end of the list */
1858 oidcpy(oid
, &ref
->old_oid
);
1862 struct ref
*fetch_pack(struct fetch_pack_args
*args
,
1864 const struct ref
*ref
,
1865 struct ref
**sought
, int nr_sought
,
1866 struct oid_array
*shallow
,
1867 struct string_list
*pack_lockfiles
,
1868 enum protocol_version version
)
1870 struct ref
*ref_cpy
;
1871 struct shallow_info si
;
1872 struct oid_array shallows_scratch
= OID_ARRAY_INIT
;
1876 nr_sought
= remove_duplicates_in_refs(sought
, nr_sought
);
1878 if (args
->no_dependents
&& !args
->filter_options
.choice
) {
1880 * The protocol does not support requesting that only the
1881 * wanted objects be sent, so approximate this by setting a
1882 * "blob:none" filter if no filter is already set. This works
1883 * for all object types: note that wanted blobs will still be
1884 * sent because they are directly specified as a "want".
1886 * NEEDSWORK: Add an option in the protocol to request that
1887 * only the wanted objects be sent, and implement it.
1889 parse_list_objects_filter(&args
->filter_options
, "blob:none");
1892 if (version
!= protocol_v2
&& !ref
) {
1893 packet_flush(fd
[1]);
1894 die(_("no matching remote head"));
1896 if (version
== protocol_v2
) {
1898 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1899 memset(&si
, 0, sizeof(si
));
1900 ref_cpy
= do_fetch_pack_v2(args
, fd
, ref
, sought
, nr_sought
,
1901 &shallows_scratch
, &si
,
1904 prepare_shallow_info(&si
, shallow
);
1905 ref_cpy
= do_fetch_pack(args
, fd
, ref
, sought
, nr_sought
,
1906 &si
, pack_lockfiles
);
1908 reprepare_packed_git(the_repository
);
1910 if (!args
->cloning
&& args
->deepen
) {
1911 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
1912 struct ref
*iterator
= ref_cpy
;
1913 opt
.shallow_file
= alternate_shallow_file
;
1915 opt
.is_deepening_fetch
= 1;
1916 if (check_connected(iterate_ref_map
, &iterator
, &opt
)) {
1917 error(_("remote did not send all necessary objects"));
1920 rollback_shallow_file(the_repository
, &shallow_lock
);
1923 args
->connectivity_checked
= 1;
1926 update_shallow(args
, sought
, nr_sought
, &si
);
1928 clear_shallow_info(&si
);
1929 oid_array_clear(&shallows_scratch
);
1933 int report_unmatched_refs(struct ref
**sought
, int nr_sought
)
1937 for (i
= 0; i
< nr_sought
; i
++) {
1940 switch (sought
[i
]->match_status
) {
1943 case REF_NOT_MATCHED
:
1944 error(_("no such remote ref %s"), sought
[i
]->name
);
1946 case REF_UNADVERTISED_NOT_ALLOWED
:
1947 error(_("Server does not allow request for unadvertised object %s"),