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 static int transfer_unpack_limit
= -1;
27 static int fetch_unpack_limit
= -1;
28 static int unpack_limit
= 100;
29 static int prefer_ofs_delta
= 1;
31 static int deepen_since_ok
;
32 static int deepen_not_ok
;
33 static int fetch_fsck_objects
= -1;
34 static int transfer_fsck_objects
= -1;
35 static int agent_supported
;
36 static int server_supports_filtering
;
37 static struct lock_file shallow_lock
;
38 static const char *alternate_shallow_file
;
39 static struct strbuf fsck_msg_types
= STRBUF_INIT
;
41 /* Remember to update object flag allocation in object.h */
42 #define COMPLETE (1U << 0)
43 #define ALTERNATE (1U << 1)
46 * After sending this many "have"s if we do not get any new ACK , we
47 * give up traversing our history.
49 #define MAX_IN_VAIN 256
51 static int multi_ack
, use_sideband
;
52 /* Allow specifying sha1 if it is a ref tip. */
53 #define ALLOW_TIP_SHA1 01
54 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
55 #define ALLOW_REACHABLE_SHA1 02
56 static unsigned int allow_unadvertised_object_request
;
58 __attribute__((format (printf
, 2, 3)))
59 static inline void print_verbose(const struct fetch_pack_args
*args
,
67 va_start(params
, fmt
);
68 vfprintf(stderr
, fmt
, params
);
73 struct alternate_object_cache
{
74 struct object
**items
;
78 static void cache_one_alternate(const struct object_id
*oid
,
81 struct alternate_object_cache
*cache
= vcache
;
82 struct object
*obj
= parse_object(the_repository
, oid
);
84 if (!obj
|| (obj
->flags
& ALTERNATE
))
87 obj
->flags
|= ALTERNATE
;
88 ALLOC_GROW(cache
->items
, cache
->nr
+ 1, cache
->alloc
);
89 cache
->items
[cache
->nr
++] = obj
;
92 static void for_each_cached_alternate(struct fetch_negotiator
*negotiator
,
93 void (*cb
)(struct fetch_negotiator
*,
96 static int initialized
;
97 static struct alternate_object_cache cache
;
101 for_each_alternate_ref(cache_one_alternate
, &cache
);
105 for (i
= 0; i
< cache
.nr
; i
++)
106 cb(negotiator
, cache
.items
[i
]);
109 static int rev_list_insert_ref(struct fetch_negotiator
*negotiator
,
111 const struct object_id
*oid
)
113 struct object
*o
= deref_tag(the_repository
,
114 parse_object(the_repository
, oid
),
117 if (o
&& o
->type
== OBJ_COMMIT
)
118 negotiator
->add_tip(negotiator
, (struct commit
*)o
);
123 static int rev_list_insert_ref_oid(const char *refname
, const struct object_id
*oid
,
124 int flag
, void *cb_data
)
126 return rev_list_insert_ref(cb_data
, refname
, oid
);
137 static void consume_shallow_list(struct fetch_pack_args
*args
,
138 struct packet_reader
*reader
)
140 if (args
->stateless_rpc
&& args
->deepen
) {
141 /* If we sent a depth we will get back "duplicate"
142 * shallow and unshallow commands every time there
143 * is a block of have lines exchanged.
145 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
146 if (starts_with(reader
->line
, "shallow "))
148 if (starts_with(reader
->line
, "unshallow "))
150 die(_("git fetch-pack: expected shallow list"));
152 if (reader
->status
!= PACKET_READ_FLUSH
)
153 die(_("git fetch-pack: expected a flush packet after shallow list"));
157 static enum ack_type
get_ack(struct packet_reader
*reader
,
158 struct object_id
*result_oid
)
163 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
164 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
165 len
= reader
->pktlen
;
167 if (!strcmp(reader
->line
, "NAK"))
169 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
171 if (!parse_oid_hex(arg
, result_oid
, &p
)) {
172 len
-= p
- reader
->line
;
175 if (strstr(p
, "continue"))
177 if (strstr(p
, "common"))
179 if (strstr(p
, "ready"))
184 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader
->line
);
187 static void send_request(struct fetch_pack_args
*args
,
188 int fd
, struct strbuf
*buf
)
190 if (args
->stateless_rpc
) {
191 send_sideband(fd
, -1, buf
->buf
, buf
->len
, LARGE_PACKET_MAX
);
194 if (write_in_full(fd
, buf
->buf
, buf
->len
) < 0)
195 die_errno(_("unable to write to remote"));
199 static void insert_one_alternate_object(struct fetch_negotiator
*negotiator
,
202 rev_list_insert_ref(negotiator
, NULL
, &obj
->oid
);
205 #define INITIAL_FLUSH 16
206 #define PIPESAFE_FLUSH 32
207 #define LARGE_FLUSH 16384
209 static int next_flush(int stateless_rpc
, int count
)
212 if (count
< LARGE_FLUSH
)
215 count
= count
* 11 / 10;
217 if (count
< PIPESAFE_FLUSH
)
220 count
+= PIPESAFE_FLUSH
;
225 static void mark_tips(struct fetch_negotiator
*negotiator
,
226 const struct oid_array
*negotiation_tips
)
230 if (!negotiation_tips
) {
231 for_each_ref(rev_list_insert_ref_oid
, negotiator
);
235 for (i
= 0; i
< negotiation_tips
->nr
; i
++)
236 rev_list_insert_ref(negotiator
, NULL
,
237 &negotiation_tips
->oid
[i
]);
241 static int find_common(struct fetch_negotiator
*negotiator
,
242 struct fetch_pack_args
*args
,
243 int fd
[2], struct object_id
*result_oid
,
247 int count
= 0, flushes
= 0, flush_at
= INITIAL_FLUSH
, retval
;
248 const struct object_id
*oid
;
249 unsigned in_vain
= 0;
250 int got_continue
= 0;
252 struct strbuf req_buf
= STRBUF_INIT
;
253 size_t state_len
= 0;
254 struct packet_reader reader
;
256 if (args
->stateless_rpc
&& multi_ack
== 1)
257 die(_("--stateless-rpc requires multi_ack_detailed"));
259 packet_reader_init(&reader
, fd
[0], NULL
, 0,
260 PACKET_READ_CHOMP_NEWLINE
|
261 PACKET_READ_DIE_ON_ERR_PACKET
);
263 if (!args
->no_dependents
) {
264 mark_tips(negotiator
, args
->negotiation_tips
);
265 for_each_cached_alternate(negotiator
, insert_one_alternate_object
);
269 for ( ; refs
; refs
= refs
->next
) {
270 struct object_id
*remote
= &refs
->old_oid
;
271 const char *remote_hex
;
275 * If that object is complete (i.e. it is an ancestor of a
276 * local ref), we tell them we have it but do not have to
277 * tell them about its ancestors, which they already know
280 * We use lookup_object here because we are only
281 * interested in the case we *know* the object is
282 * reachable and we have already scanned it.
284 * Do this only if args->no_dependents is false (if it is true,
285 * we cannot trust the object flags).
287 if (!args
->no_dependents
&&
288 ((o
= lookup_object(the_repository
, remote
)) != NULL
) &&
289 (o
->flags
& COMPLETE
)) {
293 remote_hex
= oid_to_hex(remote
);
295 struct strbuf c
= STRBUF_INIT
;
296 if (multi_ack
== 2) strbuf_addstr(&c
, " multi_ack_detailed");
297 if (multi_ack
== 1) strbuf_addstr(&c
, " multi_ack");
298 if (no_done
) strbuf_addstr(&c
, " no-done");
299 if (use_sideband
== 2) strbuf_addstr(&c
, " side-band-64k");
300 if (use_sideband
== 1) strbuf_addstr(&c
, " side-band");
301 if (args
->deepen_relative
) strbuf_addstr(&c
, " deepen-relative");
302 if (args
->use_thin_pack
) strbuf_addstr(&c
, " thin-pack");
303 if (args
->no_progress
) strbuf_addstr(&c
, " no-progress");
304 if (args
->include_tag
) strbuf_addstr(&c
, " include-tag");
305 if (prefer_ofs_delta
) strbuf_addstr(&c
, " ofs-delta");
306 if (deepen_since_ok
) strbuf_addstr(&c
, " deepen-since");
307 if (deepen_not_ok
) strbuf_addstr(&c
, " deepen-not");
308 if (agent_supported
) strbuf_addf(&c
, " agent=%s",
309 git_user_agent_sanitized());
310 if (args
->filter_options
.choice
)
311 strbuf_addstr(&c
, " filter");
312 packet_buf_write(&req_buf
, "want %s%s\n", remote_hex
, c
.buf
);
315 packet_buf_write(&req_buf
, "want %s\n", remote_hex
);
320 strbuf_release(&req_buf
);
325 if (is_repository_shallow(the_repository
))
326 write_shallow_commits(&req_buf
, 1, NULL
);
328 packet_buf_write(&req_buf
, "deepen %d", args
->depth
);
329 if (args
->deepen_since
) {
330 timestamp_t max_age
= approxidate(args
->deepen_since
);
331 packet_buf_write(&req_buf
, "deepen-since %"PRItime
, max_age
);
333 if (args
->deepen_not
) {
335 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
336 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
337 packet_buf_write(&req_buf
, "deepen-not %s", s
->string
);
340 if (server_supports_filtering
&& args
->filter_options
.choice
) {
342 expand_list_objects_filter_spec(&args
->filter_options
);
343 packet_buf_write(&req_buf
, "filter %s", spec
);
345 packet_buf_flush(&req_buf
);
346 state_len
= req_buf
.len
;
350 struct object_id oid
;
352 send_request(args
, fd
[1], &req_buf
);
353 while (packet_reader_read(&reader
) == PACKET_READ_NORMAL
) {
354 if (skip_prefix(reader
.line
, "shallow ", &arg
)) {
355 if (get_oid_hex(arg
, &oid
))
356 die(_("invalid shallow line: %s"), reader
.line
);
357 register_shallow(the_repository
, &oid
);
360 if (skip_prefix(reader
.line
, "unshallow ", &arg
)) {
361 if (get_oid_hex(arg
, &oid
))
362 die(_("invalid unshallow line: %s"), reader
.line
);
363 if (!lookup_object(the_repository
, &oid
))
364 die(_("object not found: %s"), reader
.line
);
365 /* make sure that it is parsed as shallow */
366 if (!parse_object(the_repository
, &oid
))
367 die(_("error in object: %s"), reader
.line
);
368 if (unregister_shallow(&oid
))
369 die(_("no shallow found: %s"), reader
.line
);
372 die(_("expected shallow/unshallow, got %s"), reader
.line
);
374 } else if (!args
->stateless_rpc
)
375 send_request(args
, fd
[1], &req_buf
);
377 if (!args
->stateless_rpc
) {
378 /* If we aren't using the stateless-rpc interface
379 * we don't need to retain the headers.
381 strbuf_setlen(&req_buf
, 0);
385 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository
);
388 if (args
->no_dependents
)
390 while ((oid
= negotiator
->next(negotiator
))) {
391 packet_buf_write(&req_buf
, "have %s\n", oid_to_hex(oid
));
392 print_verbose(args
, "have %s", oid_to_hex(oid
));
394 if (flush_at
<= ++count
) {
397 packet_buf_flush(&req_buf
);
398 send_request(args
, fd
[1], &req_buf
);
399 strbuf_setlen(&req_buf
, state_len
);
401 flush_at
= next_flush(args
->stateless_rpc
, count
);
404 * We keep one window "ahead" of the other side, and
405 * will wait for an ACK only on the next one
407 if (!args
->stateless_rpc
&& count
== INITIAL_FLUSH
)
410 consume_shallow_list(args
, &reader
);
412 ack
= get_ack(&reader
, result_oid
);
414 print_verbose(args
, _("got %s %d %s"), "ack",
415 ack
, oid_to_hex(result_oid
));
425 struct commit
*commit
=
426 lookup_commit(the_repository
,
431 die(_("invalid commit %s"), oid_to_hex(result_oid
));
432 was_common
= negotiator
->ack(negotiator
, commit
);
433 if (args
->stateless_rpc
436 /* We need to replay the have for this object
437 * on the next RPC request so the peer knows
438 * it is in common with us.
440 const char *hex
= oid_to_hex(result_oid
);
441 packet_buf_write(&req_buf
, "have %s\n", hex
);
442 state_len
= req_buf
.len
;
444 * Reset in_vain because an ack
445 * for this commit has not been
449 } else if (!args
->stateless_rpc
450 || ack
!= ACK_common
)
454 if (ack
== ACK_ready
)
461 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
462 print_verbose(args
, _("giving up"));
470 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository
);
471 if (!got_ready
|| !no_done
) {
472 packet_buf_write(&req_buf
, "done\n");
473 send_request(args
, fd
[1], &req_buf
);
475 print_verbose(args
, _("done"));
480 strbuf_release(&req_buf
);
482 if (!got_ready
|| !no_done
)
483 consume_shallow_list(args
, &reader
);
484 while (flushes
|| multi_ack
) {
485 int ack
= get_ack(&reader
, result_oid
);
487 print_verbose(args
, _("got %s (%d) %s"), "ack",
488 ack
, oid_to_hex(result_oid
));
496 /* it is no error to fetch into a completely empty repo */
497 return count
? retval
: 0;
500 static struct commit_list
*complete
;
502 static int mark_complete(const struct object_id
*oid
)
504 struct object
*o
= parse_object(the_repository
, oid
);
506 while (o
&& o
->type
== OBJ_TAG
) {
507 struct tag
*t
= (struct tag
*) o
;
509 break; /* broken repository */
510 o
->flags
|= COMPLETE
;
511 o
= parse_object(the_repository
, &t
->tagged
->oid
);
513 if (o
&& o
->type
== OBJ_COMMIT
) {
514 struct commit
*commit
= (struct commit
*)o
;
515 if (!(commit
->object
.flags
& COMPLETE
)) {
516 commit
->object
.flags
|= COMPLETE
;
517 commit_list_insert(commit
, &complete
);
523 static int mark_complete_oid(const char *refname
, const struct object_id
*oid
,
524 int flag
, void *cb_data
)
526 return mark_complete(oid
);
529 static void mark_recent_complete_commits(struct fetch_pack_args
*args
,
532 while (complete
&& cutoff
<= complete
->item
->date
) {
533 print_verbose(args
, _("Marking %s as complete"),
534 oid_to_hex(&complete
->item
->object
.oid
));
535 pop_most_recent_commit(&complete
, COMPLETE
);
539 static void add_refs_to_oidset(struct oidset
*oids
, struct ref
*refs
)
541 for (; refs
; refs
= refs
->next
)
542 oidset_insert(oids
, &refs
->old_oid
);
545 static int is_unmatched_ref(const struct ref
*ref
)
547 struct object_id oid
;
549 return ref
->match_status
== REF_NOT_MATCHED
&&
550 !parse_oid_hex(ref
->name
, &oid
, &p
) &&
552 oideq(&oid
, &ref
->old_oid
);
555 static void filter_refs(struct fetch_pack_args
*args
,
557 struct ref
**sought
, int nr_sought
)
559 struct ref
*newlist
= NULL
;
560 struct ref
**newtail
= &newlist
;
561 struct ref
*unmatched
= NULL
;
562 struct ref
*ref
, *next
;
563 struct oidset tip_oids
= OIDSET_INIT
;
565 int strict
= !(allow_unadvertised_object_request
&
566 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
569 for (ref
= *refs
; ref
; ref
= next
) {
573 if (starts_with(ref
->name
, "refs/") &&
574 check_refname_format(ref
->name
, 0)) {
576 * trash or a peeled value; do not even add it to
582 while (i
< nr_sought
) {
583 int cmp
= strcmp(ref
->name
, sought
[i
]->name
);
585 break; /* definitely do not have it */
587 keep
= 1; /* definitely have it */
588 sought
[i
]->match_status
= REF_MATCHED
;
593 if (!keep
&& args
->fetch_all
&&
594 (!args
->deepen
|| !starts_with(ref
->name
, "refs/tags/")))
601 newtail
= &ref
->next
;
603 ref
->next
= unmatched
;
609 for (i
= 0; i
< nr_sought
; i
++) {
611 if (!is_unmatched_ref(ref
))
614 add_refs_to_oidset(&tip_oids
, unmatched
);
615 add_refs_to_oidset(&tip_oids
, newlist
);
620 /* Append unmatched requests to the list */
621 for (i
= 0; i
< nr_sought
; i
++) {
623 if (!is_unmatched_ref(ref
))
626 if (!strict
|| oidset_contains(&tip_oids
, &ref
->old_oid
)) {
627 ref
->match_status
= REF_MATCHED
;
628 *newtail
= copy_ref(ref
);
629 newtail
= &(*newtail
)->next
;
631 ref
->match_status
= REF_UNADVERTISED_NOT_ALLOWED
;
635 oidset_clear(&tip_oids
);
636 free_refs(unmatched
);
641 static void mark_alternate_complete(struct fetch_negotiator
*unused
,
644 mark_complete(&obj
->oid
);
647 struct loose_object_iter
{
648 struct oidset
*loose_object_set
;
653 * Mark recent commits available locally and reachable from a local ref as
654 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
655 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
656 * thus do not need COMMON_REF marks).
658 * The cutoff time for recency is determined by this heuristic: it is the
659 * earliest commit time of the objects in refs that are commits and that we know
660 * the commit time of.
662 static void mark_complete_and_common_ref(struct fetch_negotiator
*negotiator
,
663 struct fetch_pack_args
*args
,
667 int old_save_commit_buffer
= save_commit_buffer
;
668 timestamp_t cutoff
= 0;
670 save_commit_buffer
= 0;
672 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL
);
673 for (ref
= *refs
; ref
; ref
= ref
->next
) {
676 if (!has_object_file_with_flags(&ref
->old_oid
,
678 OBJECT_INFO_SKIP_FETCH_OBJECT
))
680 o
= parse_object(the_repository
, &ref
->old_oid
);
685 * We already have it -- which may mean that we were
686 * in sync with the other side at some time after
687 * that (it is OK if we guess wrong here).
689 if (o
->type
== OBJ_COMMIT
) {
690 struct commit
*commit
= (struct commit
*)o
;
691 if (!cutoff
|| cutoff
< commit
->date
)
692 cutoff
= commit
->date
;
695 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL
);
698 * This block marks all local refs as COMPLETE, and then recursively marks all
699 * parents of those refs as COMPLETE.
701 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL
);
703 for_each_ref(mark_complete_oid
, NULL
);
704 for_each_cached_alternate(NULL
, mark_alternate_complete
);
705 commit_list_sort_by_date(&complete
);
707 mark_recent_complete_commits(args
, cutoff
);
709 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL
);
712 * Mark all complete remote refs as common refs.
713 * Don't mark them common yet; the server has to be told so first.
715 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL
);
716 for (ref
= *refs
; ref
; ref
= ref
->next
) {
717 struct object
*o
= deref_tag(the_repository
,
718 lookup_object(the_repository
,
722 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
725 negotiator
->known_common(negotiator
,
728 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL
);
730 save_commit_buffer
= old_save_commit_buffer
;
734 * Returns 1 if every object pointed to by the given remote refs is available
735 * locally and reachable from a local ref, and 0 otherwise.
737 static int everything_local(struct fetch_pack_args
*args
,
743 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
744 const struct object_id
*remote
= &ref
->old_oid
;
747 o
= lookup_object(the_repository
, remote
);
748 if (!o
|| !(o
->flags
& COMPLETE
)) {
750 print_verbose(args
, "want %s (%s)", oid_to_hex(remote
),
754 print_verbose(args
, _("already have %s (%s)"), oid_to_hex(remote
),
761 static int sideband_demux(int in
, int out
, void *data
)
766 ret
= recv_sideband("fetch-pack", xd
[0], out
);
771 static void write_promisor_file(const char *keep_name
,
772 struct ref
**sought
, int nr_sought
)
774 struct strbuf promisor_name
= STRBUF_INIT
;
779 strbuf_addstr(&promisor_name
, keep_name
);
780 suffix_stripped
= strbuf_strip_suffix(&promisor_name
, ".keep");
781 if (!suffix_stripped
)
782 BUG("name of pack lockfile should end with .keep (was '%s')",
784 strbuf_addstr(&promisor_name
, ".promisor");
786 output
= xfopen(promisor_name
.buf
, "w");
787 for (i
= 0; i
< nr_sought
; i
++)
788 fprintf(output
, "%s %s\n", oid_to_hex(&sought
[i
]->old_oid
),
792 strbuf_release(&promisor_name
);
795 static int get_pack(struct fetch_pack_args
*args
,
796 int xd
[2], char **pack_lockfile
,
797 struct ref
**sought
, int nr_sought
)
800 int do_keep
= args
->keep_pack
;
801 const char *cmd_name
;
802 struct pack_header header
;
804 struct child_process cmd
= CHILD_PROCESS_INIT
;
807 memset(&demux
, 0, sizeof(demux
));
809 /* xd[] is talking with upload-pack; subprocess reads from
810 * xd[0], spits out band#2 to stderr, and feeds us band#1
811 * through demux->out.
813 demux
.proc
= sideband_demux
;
816 demux
.isolate_sigpipe
= 1;
817 if (start_async(&demux
))
818 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
823 if (!args
->keep_pack
&& unpack_limit
) {
825 if (read_pack_header(demux
.out
, &header
))
826 die(_("protocol error: bad pack header"));
828 if (ntohl(header
.hdr_entries
) < unpack_limit
)
834 if (alternate_shallow_file
) {
835 argv_array_push(&cmd
.args
, "--shallow-file");
836 argv_array_push(&cmd
.args
, alternate_shallow_file
);
839 if (do_keep
|| args
->from_promisor
) {
842 cmd_name
= "index-pack";
843 argv_array_push(&cmd
.args
, cmd_name
);
844 argv_array_push(&cmd
.args
, "--stdin");
845 if (!args
->quiet
&& !args
->no_progress
)
846 argv_array_push(&cmd
.args
, "-v");
847 if (args
->use_thin_pack
)
848 argv_array_push(&cmd
.args
, "--fix-thin");
849 if (do_keep
&& (args
->lock_pack
|| unpack_limit
)) {
850 char hostname
[HOST_NAME_MAX
+ 1];
851 if (xgethostname(hostname
, sizeof(hostname
)))
852 xsnprintf(hostname
, sizeof(hostname
), "localhost");
853 argv_array_pushf(&cmd
.args
,
854 "--keep=fetch-pack %"PRIuMAX
" on %s",
855 (uintmax_t)getpid(), hostname
);
857 if (args
->check_self_contained_and_connected
)
858 argv_array_push(&cmd
.args
, "--check-self-contained-and-connected");
860 * If we're obtaining the filename of a lockfile, we'll use
861 * that filename to write a .promisor file with more
862 * information below. If not, we need index-pack to do it for
865 if (!(do_keep
&& pack_lockfile
) && args
->from_promisor
)
866 argv_array_push(&cmd
.args
, "--promisor");
869 cmd_name
= "unpack-objects";
870 argv_array_push(&cmd
.args
, cmd_name
);
871 if (args
->quiet
|| args
->no_progress
)
872 argv_array_push(&cmd
.args
, "-q");
873 args
->check_self_contained_and_connected
= 0;
877 argv_array_pushf(&cmd
.args
, "--pack_header=%"PRIu32
",%"PRIu32
,
878 ntohl(header
.hdr_version
),
879 ntohl(header
.hdr_entries
));
880 if (fetch_fsck_objects
>= 0
882 : transfer_fsck_objects
>= 0
883 ? transfer_fsck_objects
885 if (args
->from_promisor
)
887 * We cannot use --strict in index-pack because it
888 * checks both broken objects and links, but we only
889 * want to check for broken objects.
891 argv_array_push(&cmd
.args
, "--fsck-objects");
893 argv_array_pushf(&cmd
.args
, "--strict%s",
899 if (start_command(&cmd
))
900 die(_("fetch-pack: unable to fork off %s"), cmd_name
);
901 if (do_keep
&& pack_lockfile
) {
902 *pack_lockfile
= index_pack_lockfile(cmd
.out
);
907 /* Closed by start_command() */
910 ret
= finish_command(&cmd
);
911 if (!ret
|| (args
->check_self_contained_and_connected
&& ret
== 1))
912 args
->self_contained_and_connected
=
913 args
->check_self_contained_and_connected
&&
916 die(_("%s failed"), cmd_name
);
917 if (use_sideband
&& finish_async(&demux
))
918 die(_("error in sideband demultiplexer"));
921 * Now that index-pack has succeeded, write the promisor file using the
922 * obtained .keep filename if necessary
924 if (do_keep
&& pack_lockfile
&& args
->from_promisor
)
925 write_promisor_file(*pack_lockfile
, sought
, nr_sought
);
930 static int cmp_ref_by_name(const void *a_
, const void *b_
)
932 const struct ref
*a
= *((const struct ref
**)a_
);
933 const struct ref
*b
= *((const struct ref
**)b_
);
934 return strcmp(a
->name
, b
->name
);
937 static struct ref
*do_fetch_pack(struct fetch_pack_args
*args
,
939 const struct ref
*orig_ref
,
940 struct ref
**sought
, int nr_sought
,
941 struct shallow_info
*si
,
942 char **pack_lockfile
)
944 struct repository
*r
= the_repository
;
945 struct ref
*ref
= copy_ref_list(orig_ref
);
946 struct object_id oid
;
947 const char *agent_feature
;
949 struct fetch_negotiator negotiator_alloc
;
950 struct fetch_negotiator
*negotiator
;
952 if (args
->no_dependents
) {
955 negotiator
= &negotiator_alloc
;
956 fetch_negotiator_init(r
, negotiator
);
959 sort_ref_list(&ref
, ref_compare_name
);
960 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
962 if ((agent_feature
= server_feature_value("agent", &agent_len
))) {
965 print_verbose(args
, _("Server version is %.*s"),
966 agent_len
, agent_feature
);
969 if (server_supports("shallow"))
970 print_verbose(args
, _("Server supports %s"), "shallow");
971 else if (args
->depth
> 0 || is_repository_shallow(r
))
972 die(_("Server does not support shallow clients"));
973 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
975 if (server_supports("multi_ack_detailed")) {
976 print_verbose(args
, _("Server supports %s"), "multi_ack_detailed");
978 if (server_supports("no-done")) {
979 print_verbose(args
, _("Server supports %s"), "no-done");
980 if (args
->stateless_rpc
)
984 else if (server_supports("multi_ack")) {
985 print_verbose(args
, _("Server supports %s"), "multi_ack");
988 if (server_supports("side-band-64k")) {
989 print_verbose(args
, _("Server supports %s"), "side-band-64k");
992 else if (server_supports("side-band")) {
993 print_verbose(args
, _("Server supports %s"), "side-band");
996 if (server_supports("allow-tip-sha1-in-want")) {
997 print_verbose(args
, _("Server supports %s"), "allow-tip-sha1-in-want");
998 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1000 if (server_supports("allow-reachable-sha1-in-want")) {
1001 print_verbose(args
, _("Server supports %s"), "allow-reachable-sha1-in-want");
1002 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1004 if (server_supports("thin-pack"))
1005 print_verbose(args
, _("Server supports %s"), "thin-pack");
1007 args
->use_thin_pack
= 0;
1008 if (server_supports("no-progress"))
1009 print_verbose(args
, _("Server supports %s"), "no-progress");
1011 args
->no_progress
= 0;
1012 if (server_supports("include-tag"))
1013 print_verbose(args
, _("Server supports %s"), "include-tag");
1015 args
->include_tag
= 0;
1016 if (server_supports("ofs-delta"))
1017 print_verbose(args
, _("Server supports %s"), "ofs-delta");
1019 prefer_ofs_delta
= 0;
1021 if (server_supports("filter")) {
1022 server_supports_filtering
= 1;
1023 print_verbose(args
, _("Server supports %s"), "filter");
1024 } else if (args
->filter_options
.choice
) {
1025 warning("filtering not recognized by server, ignoring");
1028 if (server_supports("deepen-since")) {
1029 print_verbose(args
, _("Server supports %s"), "deepen-since");
1030 deepen_since_ok
= 1;
1031 } else if (args
->deepen_since
)
1032 die(_("Server does not support --shallow-since"));
1033 if (server_supports("deepen-not")) {
1034 print_verbose(args
, _("Server supports %s"), "deepen-not");
1036 } else if (args
->deepen_not
)
1037 die(_("Server does not support --shallow-exclude"));
1038 if (server_supports("deepen-relative"))
1039 print_verbose(args
, _("Server supports %s"), "deepen-relative");
1040 else if (args
->deepen_relative
)
1041 die(_("Server does not support --deepen"));
1042 if (!server_supports_hash(the_hash_algo
->name
, NULL
))
1043 die(_("Server does not support this repository's object format"));
1045 if (!args
->no_dependents
) {
1046 mark_complete_and_common_ref(negotiator
, args
, &ref
);
1047 filter_refs(args
, &ref
, sought
, nr_sought
);
1048 if (everything_local(args
, &ref
)) {
1049 packet_flush(fd
[1]);
1053 filter_refs(args
, &ref
, sought
, nr_sought
);
1055 if (find_common(negotiator
, args
, fd
, &oid
, ref
) < 0)
1056 if (!args
->keep_pack
)
1057 /* When cloning, it is not unusual to have
1060 warning(_("no common commits"));
1062 if (args
->stateless_rpc
)
1063 packet_flush(fd
[1]);
1065 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
1067 else if (si
->nr_ours
|| si
->nr_theirs
)
1068 alternate_shallow_file
= setup_temporary_shallow(si
->shallow
);
1070 alternate_shallow_file
= NULL
;
1071 if (get_pack(args
, fd
, pack_lockfile
, sought
, nr_sought
))
1072 die(_("git fetch-pack: fetch failed."));
1076 negotiator
->release(negotiator
);
1080 static void add_shallow_requests(struct strbuf
*req_buf
,
1081 const struct fetch_pack_args
*args
)
1083 if (is_repository_shallow(the_repository
))
1084 write_shallow_commits(req_buf
, 1, NULL
);
1085 if (args
->depth
> 0)
1086 packet_buf_write(req_buf
, "deepen %d", args
->depth
);
1087 if (args
->deepen_since
) {
1088 timestamp_t max_age
= approxidate(args
->deepen_since
);
1089 packet_buf_write(req_buf
, "deepen-since %"PRItime
, max_age
);
1091 if (args
->deepen_not
) {
1093 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
1094 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
1095 packet_buf_write(req_buf
, "deepen-not %s", s
->string
);
1098 if (args
->deepen_relative
)
1099 packet_buf_write(req_buf
, "deepen-relative\n");
1102 static void add_wants(int no_dependents
, const struct ref
*wants
, struct strbuf
*req_buf
)
1104 int use_ref_in_want
= server_supports_feature("fetch", "ref-in-want", 0);
1106 for ( ; wants
; wants
= wants
->next
) {
1107 const struct object_id
*remote
= &wants
->old_oid
;
1111 * If that object is complete (i.e. it is an ancestor of a
1112 * local ref), we tell them we have it but do not have to
1113 * tell them about its ancestors, which they already know
1116 * We use lookup_object here because we are only
1117 * interested in the case we *know* the object is
1118 * reachable and we have already scanned it.
1120 * Do this only if args->no_dependents is false (if it is true,
1121 * we cannot trust the object flags).
1123 if (!no_dependents
&&
1124 ((o
= lookup_object(the_repository
, remote
)) != NULL
) &&
1125 (o
->flags
& COMPLETE
)) {
1129 if (!use_ref_in_want
|| wants
->exact_oid
)
1130 packet_buf_write(req_buf
, "want %s\n", oid_to_hex(remote
));
1132 packet_buf_write(req_buf
, "want-ref %s\n", wants
->name
);
1136 static void add_common(struct strbuf
*req_buf
, struct oidset
*common
)
1138 struct oidset_iter iter
;
1139 const struct object_id
*oid
;
1140 oidset_iter_init(common
, &iter
);
1142 while ((oid
= oidset_iter_next(&iter
))) {
1143 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1147 static int add_haves(struct fetch_negotiator
*negotiator
,
1149 struct strbuf
*req_buf
,
1150 int *haves_to_send
, int *in_vain
)
1153 int haves_added
= 0;
1154 const struct object_id
*oid
;
1156 while ((oid
= negotiator
->next(negotiator
))) {
1157 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1158 if (++haves_added
>= *haves_to_send
)
1162 *in_vain
+= haves_added
;
1163 if (!haves_added
|| (seen_ack
&& *in_vain
>= MAX_IN_VAIN
)) {
1165 packet_buf_write(req_buf
, "done\n");
1169 /* Increase haves to send on next round */
1170 *haves_to_send
= next_flush(1, *haves_to_send
);
1175 static int send_fetch_request(struct fetch_negotiator
*negotiator
, int fd_out
,
1176 struct fetch_pack_args
*args
,
1177 const struct ref
*wants
, struct oidset
*common
,
1178 int *haves_to_send
, int *in_vain
,
1179 int sideband_all
, int seen_ack
)
1182 const char *hash_name
;
1183 struct strbuf req_buf
= STRBUF_INIT
;
1185 if (server_supports_v2("fetch", 1))
1186 packet_buf_write(&req_buf
, "command=fetch");
1187 if (server_supports_v2("agent", 0))
1188 packet_buf_write(&req_buf
, "agent=%s", git_user_agent_sanitized());
1189 if (args
->server_options
&& args
->server_options
->nr
&&
1190 server_supports_v2("server-option", 1)) {
1192 for (i
= 0; i
< args
->server_options
->nr
; i
++)
1193 packet_buf_write(&req_buf
, "server-option=%s",
1194 args
->server_options
->items
[i
].string
);
1197 if (server_feature_v2("object-format", &hash_name
)) {
1198 int hash_algo
= hash_algo_by_name(hash_name
);
1199 if (hash_algo_by_ptr(the_hash_algo
) != hash_algo
)
1200 die(_("mismatched algorithms: client %s; server %s"),
1201 the_hash_algo
->name
, hash_name
);
1202 packet_write_fmt(fd_out
, "object-format=%s", the_hash_algo
->name
);
1203 } else if (hash_algo_by_ptr(the_hash_algo
) != GIT_HASH_SHA1
) {
1204 die(_("the server does not support algorithm '%s'"),
1205 the_hash_algo
->name
);
1208 packet_buf_delim(&req_buf
);
1209 if (args
->use_thin_pack
)
1210 packet_buf_write(&req_buf
, "thin-pack");
1211 if (args
->no_progress
)
1212 packet_buf_write(&req_buf
, "no-progress");
1213 if (args
->include_tag
)
1214 packet_buf_write(&req_buf
, "include-tag");
1215 if (prefer_ofs_delta
)
1216 packet_buf_write(&req_buf
, "ofs-delta");
1218 packet_buf_write(&req_buf
, "sideband-all");
1220 /* Add shallow-info and deepen request */
1221 if (server_supports_feature("fetch", "shallow", 0))
1222 add_shallow_requests(&req_buf
, args
);
1223 else if (is_repository_shallow(the_repository
) || args
->deepen
)
1224 die(_("Server does not support shallow requests"));
1227 if (server_supports_feature("fetch", "filter", 0) &&
1228 args
->filter_options
.choice
) {
1230 expand_list_objects_filter_spec(&args
->filter_options
);
1231 print_verbose(args
, _("Server supports filter"));
1232 packet_buf_write(&req_buf
, "filter %s", spec
);
1233 } else if (args
->filter_options
.choice
) {
1234 warning("filtering not recognized by server, ignoring");
1238 add_wants(args
->no_dependents
, wants
, &req_buf
);
1240 if (args
->no_dependents
) {
1241 packet_buf_write(&req_buf
, "done");
1244 /* Add all of the common commits we've found in previous rounds */
1245 add_common(&req_buf
, common
);
1247 /* Add initial haves */
1248 ret
= add_haves(negotiator
, seen_ack
, &req_buf
,
1249 haves_to_send
, in_vain
);
1253 packet_buf_flush(&req_buf
);
1254 if (write_in_full(fd_out
, req_buf
.buf
, req_buf
.len
) < 0)
1255 die_errno(_("unable to write request to remote"));
1257 strbuf_release(&req_buf
);
1262 * Processes a section header in a server's response and checks if it matches
1263 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1264 * not consumed); if 0, the line will be consumed and the function will die if
1265 * the section header doesn't match what was expected.
1267 static int process_section_header(struct packet_reader
*reader
,
1268 const char *section
, int peek
)
1272 if (packet_reader_peek(reader
) != PACKET_READ_NORMAL
)
1273 die(_("error reading section header '%s'"), section
);
1275 ret
= !strcmp(reader
->line
, section
);
1279 die(_("expected '%s', received '%s'"),
1280 section
, reader
->line
);
1281 packet_reader_read(reader
);
1289 * No commit was found to be possessed by both the client and the
1290 * server, and "ready" was not received.
1295 * At least one commit was found to be possessed by both the client and
1296 * the server, and "ready" was not received.
1301 * "ready" was received, indicating that the server is ready to send
1302 * the packfile without any further negotiation.
1307 static enum common_found
process_acks(struct fetch_negotiator
*negotiator
,
1308 struct packet_reader
*reader
,
1309 struct oidset
*common
)
1312 int received_ready
= 0;
1313 int received_ack
= 0;
1315 process_section_header(reader
, "acknowledgments", 0);
1316 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1319 if (!strcmp(reader
->line
, "NAK"))
1322 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
1323 struct object_id oid
;
1325 if (!get_oid_hex(arg
, &oid
)) {
1326 struct commit
*commit
;
1327 oidset_insert(common
, &oid
);
1328 commit
= lookup_commit(the_repository
, &oid
);
1330 negotiator
->ack(negotiator
, commit
);
1335 if (!strcmp(reader
->line
, "ready")) {
1340 die(_("unexpected acknowledgment line: '%s'"), reader
->line
);
1343 if (reader
->status
!= PACKET_READ_FLUSH
&&
1344 reader
->status
!= PACKET_READ_DELIM
)
1345 die(_("error processing acks: %d"), reader
->status
);
1348 * If an "acknowledgments" section is sent, a packfile is sent if and
1349 * only if "ready" was sent in this section. The other sections
1350 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1351 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1354 if (received_ready
&& reader
->status
!= PACKET_READ_DELIM
)
1355 die(_("expected packfile to be sent after 'ready'"));
1356 if (!received_ready
&& reader
->status
!= PACKET_READ_FLUSH
)
1357 die(_("expected no other sections to be sent after no 'ready'"));
1359 return received_ready
? READY
:
1360 (received_ack
? COMMON_FOUND
: NO_COMMON_FOUND
);
1363 static void receive_shallow_info(struct fetch_pack_args
*args
,
1364 struct packet_reader
*reader
,
1365 struct oid_array
*shallows
,
1366 struct shallow_info
*si
)
1368 int unshallow_received
= 0;
1370 process_section_header(reader
, "shallow-info", 0);
1371 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1373 struct object_id oid
;
1375 if (skip_prefix(reader
->line
, "shallow ", &arg
)) {
1376 if (get_oid_hex(arg
, &oid
))
1377 die(_("invalid shallow line: %s"), reader
->line
);
1378 oid_array_append(shallows
, &oid
);
1381 if (skip_prefix(reader
->line
, "unshallow ", &arg
)) {
1382 if (get_oid_hex(arg
, &oid
))
1383 die(_("invalid unshallow line: %s"), reader
->line
);
1384 if (!lookup_object(the_repository
, &oid
))
1385 die(_("object not found: %s"), reader
->line
);
1386 /* make sure that it is parsed as shallow */
1387 if (!parse_object(the_repository
, &oid
))
1388 die(_("error in object: %s"), reader
->line
);
1389 if (unregister_shallow(&oid
))
1390 die(_("no shallow found: %s"), reader
->line
);
1391 unshallow_received
= 1;
1394 die(_("expected shallow/unshallow, got %s"), reader
->line
);
1397 if (reader
->status
!= PACKET_READ_FLUSH
&&
1398 reader
->status
!= PACKET_READ_DELIM
)
1399 die(_("error processing shallow info: %d"), reader
->status
);
1401 if (args
->deepen
|| unshallow_received
) {
1403 * Treat these as shallow lines caused by our depth settings.
1404 * In v0, these lines cannot cause refs to be rejected; do the
1409 for (i
= 0; i
< shallows
->nr
; i
++)
1410 register_shallow(the_repository
, &shallows
->oid
[i
]);
1411 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
1414 } else if (shallows
->nr
) {
1416 * Treat these as shallow lines caused by the remote being
1417 * shallow. In v0, remote refs that reach these objects are
1418 * rejected (unless --update-shallow is set); do the same.
1420 prepare_shallow_info(si
, shallows
);
1421 if (si
->nr_ours
|| si
->nr_theirs
)
1422 alternate_shallow_file
=
1423 setup_temporary_shallow(si
->shallow
);
1425 alternate_shallow_file
= NULL
;
1427 alternate_shallow_file
= NULL
;
1431 static int cmp_name_ref(const void *name
, const void *ref
)
1433 return strcmp(name
, (*(struct ref
**)ref
)->name
);
1436 static void receive_wanted_refs(struct packet_reader
*reader
,
1437 struct ref
**sought
, int nr_sought
)
1439 process_section_header(reader
, "wanted-refs", 0);
1440 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1441 struct object_id oid
;
1445 if (parse_oid_hex(reader
->line
, &oid
, &end
) || *end
++ != ' ')
1446 die(_("expected wanted-ref, got '%s'"), reader
->line
);
1448 found
= bsearch(end
, sought
, nr_sought
, sizeof(*sought
),
1451 die(_("unexpected wanted-ref: '%s'"), reader
->line
);
1452 oidcpy(&(*found
)->old_oid
, &oid
);
1455 if (reader
->status
!= PACKET_READ_DELIM
)
1456 die(_("error processing wanted refs: %d"), reader
->status
);
1460 FETCH_CHECK_LOCAL
= 0,
1467 static struct ref
*do_fetch_pack_v2(struct fetch_pack_args
*args
,
1469 const struct ref
*orig_ref
,
1470 struct ref
**sought
, int nr_sought
,
1471 struct oid_array
*shallows
,
1472 struct shallow_info
*si
,
1473 char **pack_lockfile
)
1475 struct repository
*r
= the_repository
;
1476 struct ref
*ref
= copy_ref_list(orig_ref
);
1477 enum fetch_state state
= FETCH_CHECK_LOCAL
;
1478 struct oidset common
= OIDSET_INIT
;
1479 struct packet_reader reader
;
1480 int in_vain
= 0, negotiation_started
= 0;
1481 int haves_to_send
= INITIAL_FLUSH
;
1482 struct fetch_negotiator negotiator_alloc
;
1483 struct fetch_negotiator
*negotiator
;
1486 if (args
->no_dependents
) {
1489 negotiator
= &negotiator_alloc
;
1490 fetch_negotiator_init(r
, negotiator
);
1493 packet_reader_init(&reader
, fd
[0], NULL
, 0,
1494 PACKET_READ_CHOMP_NEWLINE
|
1495 PACKET_READ_DIE_ON_ERR_PACKET
);
1496 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1497 server_supports_feature("fetch", "sideband-all", 0)) {
1498 reader
.use_sideband
= 1;
1499 reader
.me
= "fetch-pack";
1502 while (state
!= FETCH_DONE
) {
1504 case FETCH_CHECK_LOCAL
:
1505 sort_ref_list(&ref
, ref_compare_name
);
1506 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
1508 /* v2 supports these by default */
1509 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1511 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
1514 /* Filter 'ref' by 'sought' and those that aren't local */
1515 if (!args
->no_dependents
) {
1516 mark_complete_and_common_ref(negotiator
, args
, &ref
);
1517 filter_refs(args
, &ref
, sought
, nr_sought
);
1518 if (everything_local(args
, &ref
))
1521 state
= FETCH_SEND_REQUEST
;
1523 mark_tips(negotiator
, args
->negotiation_tips
);
1524 for_each_cached_alternate(negotiator
,
1525 insert_one_alternate_object
);
1527 filter_refs(args
, &ref
, sought
, nr_sought
);
1528 state
= FETCH_SEND_REQUEST
;
1531 case FETCH_SEND_REQUEST
:
1532 if (!negotiation_started
) {
1533 negotiation_started
= 1;
1534 trace2_region_enter("fetch-pack",
1538 if (send_fetch_request(negotiator
, fd
[1], args
, ref
,
1540 &haves_to_send
, &in_vain
,
1541 reader
.use_sideband
,
1543 state
= FETCH_GET_PACK
;
1545 state
= FETCH_PROCESS_ACKS
;
1547 case FETCH_PROCESS_ACKS
:
1548 /* Process ACKs/NAKs */
1549 switch (process_acks(negotiator
, &reader
, &common
)) {
1551 state
= FETCH_GET_PACK
;
1557 case NO_COMMON_FOUND
:
1558 state
= FETCH_SEND_REQUEST
;
1562 case FETCH_GET_PACK
:
1563 trace2_region_leave("fetch-pack",
1566 /* Check for shallow-info section */
1567 if (process_section_header(&reader
, "shallow-info", 1))
1568 receive_shallow_info(args
, &reader
, shallows
, si
);
1570 if (process_section_header(&reader
, "wanted-refs", 1))
1571 receive_wanted_refs(&reader
, sought
, nr_sought
);
1574 process_section_header(&reader
, "packfile", 0);
1575 if (get_pack(args
, fd
, pack_lockfile
, sought
, nr_sought
))
1576 die(_("git fetch-pack: fetch failed."));
1586 negotiator
->release(negotiator
);
1587 oidset_clear(&common
);
1591 static int fetch_pack_config_cb(const char *var
, const char *value
, void *cb
)
1593 if (strcmp(var
, "fetch.fsck.skiplist") == 0) {
1596 if (git_config_pathname(&path
, var
, value
))
1598 strbuf_addf(&fsck_msg_types
, "%cskiplist=%s",
1599 fsck_msg_types
.len
? ',' : '=', path
);
1604 if (skip_prefix(var
, "fetch.fsck.", &var
)) {
1605 if (is_valid_msg_type(var
, value
))
1606 strbuf_addf(&fsck_msg_types
, "%c%s=%s",
1607 fsck_msg_types
.len
? ',' : '=', var
, value
);
1609 warning("Skipping unknown msg id '%s'", var
);
1613 return git_default_config(var
, value
, cb
);
1616 static void fetch_pack_config(void)
1618 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit
);
1619 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit
);
1620 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta
);
1621 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects
);
1622 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects
);
1624 git_config(fetch_pack_config_cb
, NULL
);
1627 static void fetch_pack_setup(void)
1629 static int did_setup
;
1632 fetch_pack_config();
1633 if (0 <= transfer_unpack_limit
)
1634 unpack_limit
= transfer_unpack_limit
;
1635 else if (0 <= fetch_unpack_limit
)
1636 unpack_limit
= fetch_unpack_limit
;
1640 static int remove_duplicates_in_refs(struct ref
**ref
, int nr
)
1642 struct string_list names
= STRING_LIST_INIT_NODUP
;
1645 for (src
= dst
= 0; src
< nr
; src
++) {
1646 struct string_list_item
*item
;
1647 item
= string_list_insert(&names
, ref
[src
]->name
);
1649 continue; /* already have it */
1650 item
->util
= ref
[src
];
1652 ref
[dst
] = ref
[src
];
1655 for (src
= dst
; src
< nr
; src
++)
1657 string_list_clear(&names
, 0);
1661 static void update_shallow(struct fetch_pack_args
*args
,
1662 struct ref
**sought
, int nr_sought
,
1663 struct shallow_info
*si
)
1665 struct oid_array ref
= OID_ARRAY_INIT
;
1669 if (args
->deepen
&& alternate_shallow_file
) {
1670 if (*alternate_shallow_file
== '\0') { /* --unshallow */
1671 unlink_or_warn(git_path_shallow(the_repository
));
1672 rollback_shallow_file(the_repository
, &shallow_lock
);
1674 commit_shallow_file(the_repository
, &shallow_lock
);
1675 alternate_shallow_file
= NULL
;
1679 if (!si
->shallow
|| !si
->shallow
->nr
)
1682 if (args
->cloning
) {
1684 * remote is shallow, but this is a clone, there are
1685 * no objects in repo to worry about. Accept any
1686 * shallow points that exist in the pack (iow in repo
1687 * after get_pack() and reprepare_packed_git())
1689 struct oid_array extra
= OID_ARRAY_INIT
;
1690 struct object_id
*oid
= si
->shallow
->oid
;
1691 for (i
= 0; i
< si
->shallow
->nr
; i
++)
1692 if (has_object_file(&oid
[i
]))
1693 oid_array_append(&extra
, &oid
[i
]);
1695 setup_alternate_shallow(&shallow_lock
,
1696 &alternate_shallow_file
,
1698 commit_shallow_file(the_repository
, &shallow_lock
);
1699 alternate_shallow_file
= NULL
;
1701 oid_array_clear(&extra
);
1705 if (!si
->nr_ours
&& !si
->nr_theirs
)
1708 remove_nonexistent_theirs_shallow(si
);
1709 if (!si
->nr_ours
&& !si
->nr_theirs
)
1711 for (i
= 0; i
< nr_sought
; i
++)
1712 oid_array_append(&ref
, &sought
[i
]->old_oid
);
1715 if (args
->update_shallow
) {
1717 * remote is also shallow, .git/shallow may be updated
1718 * so all refs can be accepted. Make sure we only add
1719 * shallow roots that are actually reachable from new
1722 struct oid_array extra
= OID_ARRAY_INIT
;
1723 struct object_id
*oid
= si
->shallow
->oid
;
1724 assign_shallow_commits_to_refs(si
, NULL
, NULL
);
1725 if (!si
->nr_ours
&& !si
->nr_theirs
) {
1726 oid_array_clear(&ref
);
1729 for (i
= 0; i
< si
->nr_ours
; i
++)
1730 oid_array_append(&extra
, &oid
[si
->ours
[i
]]);
1731 for (i
= 0; i
< si
->nr_theirs
; i
++)
1732 oid_array_append(&extra
, &oid
[si
->theirs
[i
]]);
1733 setup_alternate_shallow(&shallow_lock
,
1734 &alternate_shallow_file
,
1736 commit_shallow_file(the_repository
, &shallow_lock
);
1737 oid_array_clear(&extra
);
1738 oid_array_clear(&ref
);
1739 alternate_shallow_file
= NULL
;
1744 * remote is also shallow, check what ref is safe to update
1745 * without updating .git/shallow
1747 status
= xcalloc(nr_sought
, sizeof(*status
));
1748 assign_shallow_commits_to_refs(si
, NULL
, status
);
1749 if (si
->nr_ours
|| si
->nr_theirs
) {
1750 for (i
= 0; i
< nr_sought
; i
++)
1752 sought
[i
]->status
= REF_STATUS_REJECT_SHALLOW
;
1755 oid_array_clear(&ref
);
1758 static int iterate_ref_map(void *cb_data
, struct object_id
*oid
)
1760 struct ref
**rm
= cb_data
;
1761 struct ref
*ref
= *rm
;
1764 return -1; /* end of the list */
1766 oidcpy(oid
, &ref
->old_oid
);
1770 struct ref
*fetch_pack(struct fetch_pack_args
*args
,
1772 const struct ref
*ref
,
1773 struct ref
**sought
, int nr_sought
,
1774 struct oid_array
*shallow
,
1775 char **pack_lockfile
,
1776 enum protocol_version version
)
1778 struct ref
*ref_cpy
;
1779 struct shallow_info si
;
1780 struct oid_array shallows_scratch
= OID_ARRAY_INIT
;
1784 nr_sought
= remove_duplicates_in_refs(sought
, nr_sought
);
1786 if (args
->no_dependents
&& !args
->filter_options
.choice
) {
1788 * The protocol does not support requesting that only the
1789 * wanted objects be sent, so approximate this by setting a
1790 * "blob:none" filter if no filter is already set. This works
1791 * for all object types: note that wanted blobs will still be
1792 * sent because they are directly specified as a "want".
1794 * NEEDSWORK: Add an option in the protocol to request that
1795 * only the wanted objects be sent, and implement it.
1797 parse_list_objects_filter(&args
->filter_options
, "blob:none");
1800 if (version
!= protocol_v2
&& !ref
) {
1801 packet_flush(fd
[1]);
1802 die(_("no matching remote head"));
1804 if (version
== protocol_v2
) {
1806 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1807 memset(&si
, 0, sizeof(si
));
1808 ref_cpy
= do_fetch_pack_v2(args
, fd
, ref
, sought
, nr_sought
,
1809 &shallows_scratch
, &si
,
1812 prepare_shallow_info(&si
, shallow
);
1813 ref_cpy
= do_fetch_pack(args
, fd
, ref
, sought
, nr_sought
,
1814 &si
, pack_lockfile
);
1816 reprepare_packed_git(the_repository
);
1818 if (!args
->cloning
&& args
->deepen
) {
1819 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
1820 struct ref
*iterator
= ref_cpy
;
1821 opt
.shallow_file
= alternate_shallow_file
;
1823 opt
.is_deepening_fetch
= 1;
1824 if (check_connected(iterate_ref_map
, &iterator
, &opt
)) {
1825 error(_("remote did not send all necessary objects"));
1828 rollback_shallow_file(the_repository
, &shallow_lock
);
1831 args
->connectivity_checked
= 1;
1834 update_shallow(args
, sought
, nr_sought
, &si
);
1836 clear_shallow_info(&si
);
1837 oid_array_clear(&shallows_scratch
);
1841 int report_unmatched_refs(struct ref
**sought
, int nr_sought
)
1845 for (i
= 0; i
< nr_sought
; i
++) {
1848 switch (sought
[i
]->match_status
) {
1851 case REF_NOT_MATCHED
:
1852 error(_("no such remote ref %s"), sought
[i
]->name
);
1854 case REF_UNADVERTISED_NOT_ALLOWED
:
1855 error(_("Server does not allow request for unadvertised object %s"),