2 #include "repository.h"
12 #include "fetch-pack.h"
14 #include "run-command.h"
16 #include "transport.h"
18 #include "sha1-array.h"
21 #include "object-store.h"
22 #include "connected.h"
23 #include "fetch-negotiator.h"
25 static int transfer_unpack_limit
= -1;
26 static int fetch_unpack_limit
= -1;
27 static int unpack_limit
= 100;
28 static int prefer_ofs_delta
= 1;
30 static int deepen_since_ok
;
31 static int deepen_not_ok
;
32 static int fetch_fsck_objects
= -1;
33 static int transfer_fsck_objects
= -1;
34 static int agent_supported
;
35 static int server_supports_filtering
;
36 static struct lock_file shallow_lock
;
37 static const char *alternate_shallow_file
;
39 /* Remember to update object flag allocation in object.h */
40 #define COMPLETE (1U << 0)
41 #define ALTERNATE (1U << 1)
44 * After sending this many "have"s if we do not get any new ACK , we
45 * give up traversing our history.
47 #define MAX_IN_VAIN 256
49 static int multi_ack
, use_sideband
;
50 /* Allow specifying sha1 if it is a ref tip. */
51 #define ALLOW_TIP_SHA1 01
52 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
53 #define ALLOW_REACHABLE_SHA1 02
54 static unsigned int allow_unadvertised_object_request
;
56 __attribute__((format (printf
, 2, 3)))
57 static inline void print_verbose(const struct fetch_pack_args
*args
,
65 va_start(params
, fmt
);
66 vfprintf(stderr
, fmt
, params
);
71 struct alternate_object_cache
{
72 struct object
**items
;
76 static void cache_one_alternate(const char *refname
,
77 const struct object_id
*oid
,
80 struct alternate_object_cache
*cache
= vcache
;
81 struct object
*obj
= parse_object(the_repository
, oid
);
83 if (!obj
|| (obj
->flags
& ALTERNATE
))
86 obj
->flags
|= ALTERNATE
;
87 ALLOC_GROW(cache
->items
, cache
->nr
+ 1, cache
->alloc
);
88 cache
->items
[cache
->nr
++] = obj
;
91 static void for_each_cached_alternate(struct fetch_negotiator
*negotiator
,
92 void (*cb
)(struct fetch_negotiator
*,
95 static int initialized
;
96 static struct alternate_object_cache cache
;
100 for_each_alternate_ref(cache_one_alternate
, &cache
);
104 for (i
= 0; i
< cache
.nr
; i
++)
105 cb(negotiator
, cache
.items
[i
]);
108 static int rev_list_insert_ref(struct fetch_negotiator
*negotiator
,
110 const struct object_id
*oid
)
112 struct object
*o
= deref_tag(the_repository
,
113 parse_object(the_repository
, oid
),
116 if (o
&& o
->type
== OBJ_COMMIT
)
117 negotiator
->add_tip(negotiator
, (struct commit
*)o
);
122 static int rev_list_insert_ref_oid(const char *refname
, const struct object_id
*oid
,
123 int flag
, void *cb_data
)
125 return rev_list_insert_ref(cb_data
, refname
, oid
);
136 static void consume_shallow_list(struct fetch_pack_args
*args
, int fd
)
138 if (args
->stateless_rpc
&& args
->deepen
) {
139 /* If we sent a depth we will get back "duplicate"
140 * shallow and unshallow commands every time there
141 * is a block of have lines exchanged.
144 while ((line
= packet_read_line(fd
, NULL
))) {
145 if (starts_with(line
, "shallow "))
147 if (starts_with(line
, "unshallow "))
149 die(_("git fetch-pack: expected shallow list"));
154 static enum ack_type
get_ack(int fd
, struct object_id
*result_oid
)
157 char *line
= packet_read_line(fd
, &len
);
161 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
162 if (!strcmp(line
, "NAK"))
164 if (skip_prefix(line
, "ACK ", &arg
)) {
165 if (!get_oid_hex(arg
, result_oid
)) {
170 if (strstr(arg
, "continue"))
172 if (strstr(arg
, "common"))
174 if (strstr(arg
, "ready"))
179 if (skip_prefix(line
, "ERR ", &arg
))
180 die(_("remote error: %s"), arg
);
181 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line
);
184 static void send_request(struct fetch_pack_args
*args
,
185 int fd
, struct strbuf
*buf
)
187 if (args
->stateless_rpc
) {
188 send_sideband(fd
, -1, buf
->buf
, buf
->len
, LARGE_PACKET_MAX
);
191 write_or_die(fd
, buf
->buf
, buf
->len
);
194 static void insert_one_alternate_object(struct fetch_negotiator
*negotiator
,
197 rev_list_insert_ref(negotiator
, NULL
, &obj
->oid
);
200 #define INITIAL_FLUSH 16
201 #define PIPESAFE_FLUSH 32
202 #define LARGE_FLUSH 16384
204 static int next_flush(int stateless_rpc
, int count
)
207 if (count
< LARGE_FLUSH
)
210 count
= count
* 11 / 10;
212 if (count
< PIPESAFE_FLUSH
)
215 count
+= PIPESAFE_FLUSH
;
220 static int find_common(struct fetch_negotiator
*negotiator
,
221 struct fetch_pack_args
*args
,
222 int fd
[2], struct object_id
*result_oid
,
226 int count
= 0, flushes
= 0, flush_at
= INITIAL_FLUSH
, retval
;
227 const struct object_id
*oid
;
228 unsigned in_vain
= 0;
229 int got_continue
= 0;
231 struct strbuf req_buf
= STRBUF_INIT
;
232 size_t state_len
= 0;
234 if (args
->stateless_rpc
&& multi_ack
== 1)
235 die(_("--stateless-rpc requires multi_ack_detailed"));
237 for_each_ref(rev_list_insert_ref_oid
, negotiator
);
238 for_each_cached_alternate(negotiator
, insert_one_alternate_object
);
241 for ( ; refs
; refs
= refs
->next
) {
242 struct object_id
*remote
= &refs
->old_oid
;
243 const char *remote_hex
;
247 * If that object is complete (i.e. it is an ancestor of a
248 * local ref), we tell them we have it but do not have to
249 * tell them about its ancestors, which they already know
252 * We use lookup_object here because we are only
253 * interested in the case we *know* the object is
254 * reachable and we have already scanned it.
256 if (((o
= lookup_object(the_repository
, remote
->hash
)) != NULL
) &&
257 (o
->flags
& COMPLETE
)) {
261 remote_hex
= oid_to_hex(remote
);
263 struct strbuf c
= STRBUF_INIT
;
264 if (multi_ack
== 2) strbuf_addstr(&c
, " multi_ack_detailed");
265 if (multi_ack
== 1) strbuf_addstr(&c
, " multi_ack");
266 if (no_done
) strbuf_addstr(&c
, " no-done");
267 if (use_sideband
== 2) strbuf_addstr(&c
, " side-band-64k");
268 if (use_sideband
== 1) strbuf_addstr(&c
, " side-band");
269 if (args
->deepen_relative
) strbuf_addstr(&c
, " deepen-relative");
270 if (args
->use_thin_pack
) strbuf_addstr(&c
, " thin-pack");
271 if (args
->no_progress
) strbuf_addstr(&c
, " no-progress");
272 if (args
->include_tag
) strbuf_addstr(&c
, " include-tag");
273 if (prefer_ofs_delta
) strbuf_addstr(&c
, " ofs-delta");
274 if (deepen_since_ok
) strbuf_addstr(&c
, " deepen-since");
275 if (deepen_not_ok
) strbuf_addstr(&c
, " deepen-not");
276 if (agent_supported
) strbuf_addf(&c
, " agent=%s",
277 git_user_agent_sanitized());
278 if (args
->filter_options
.choice
)
279 strbuf_addstr(&c
, " filter");
280 packet_buf_write(&req_buf
, "want %s%s\n", remote_hex
, c
.buf
);
283 packet_buf_write(&req_buf
, "want %s\n", remote_hex
);
288 strbuf_release(&req_buf
);
293 if (is_repository_shallow(the_repository
))
294 write_shallow_commits(&req_buf
, 1, NULL
);
296 packet_buf_write(&req_buf
, "deepen %d", args
->depth
);
297 if (args
->deepen_since
) {
298 timestamp_t max_age
= approxidate(args
->deepen_since
);
299 packet_buf_write(&req_buf
, "deepen-since %"PRItime
, max_age
);
301 if (args
->deepen_not
) {
303 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
304 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
305 packet_buf_write(&req_buf
, "deepen-not %s", s
->string
);
308 if (server_supports_filtering
&& args
->filter_options
.choice
)
309 packet_buf_write(&req_buf
, "filter %s",
310 args
->filter_options
.filter_spec
);
311 packet_buf_flush(&req_buf
);
312 state_len
= req_buf
.len
;
317 struct object_id oid
;
319 send_request(args
, fd
[1], &req_buf
);
320 while ((line
= packet_read_line(fd
[0], NULL
))) {
321 if (skip_prefix(line
, "shallow ", &arg
)) {
322 if (get_oid_hex(arg
, &oid
))
323 die(_("invalid shallow line: %s"), line
);
324 register_shallow(the_repository
, &oid
);
327 if (skip_prefix(line
, "unshallow ", &arg
)) {
328 if (get_oid_hex(arg
, &oid
))
329 die(_("invalid unshallow line: %s"), line
);
330 if (!lookup_object(the_repository
, oid
.hash
))
331 die(_("object not found: %s"), line
);
332 /* make sure that it is parsed as shallow */
333 if (!parse_object(the_repository
, &oid
))
334 die(_("error in object: %s"), line
);
335 if (unregister_shallow(&oid
))
336 die(_("no shallow found: %s"), line
);
339 die(_("expected shallow/unshallow, got %s"), line
);
341 } else if (!args
->stateless_rpc
)
342 send_request(args
, fd
[1], &req_buf
);
344 if (!args
->stateless_rpc
) {
345 /* If we aren't using the stateless-rpc interface
346 * we don't need to retain the headers.
348 strbuf_setlen(&req_buf
, 0);
354 if (args
->no_dependents
)
356 while ((oid
= negotiator
->next(negotiator
))) {
357 packet_buf_write(&req_buf
, "have %s\n", oid_to_hex(oid
));
358 print_verbose(args
, "have %s", oid_to_hex(oid
));
360 if (flush_at
<= ++count
) {
363 packet_buf_flush(&req_buf
);
364 send_request(args
, fd
[1], &req_buf
);
365 strbuf_setlen(&req_buf
, state_len
);
367 flush_at
= next_flush(args
->stateless_rpc
, count
);
370 * We keep one window "ahead" of the other side, and
371 * will wait for an ACK only on the next one
373 if (!args
->stateless_rpc
&& count
== INITIAL_FLUSH
)
376 consume_shallow_list(args
, fd
[0]);
378 ack
= get_ack(fd
[0], result_oid
);
380 print_verbose(args
, _("got %s %d %s"), "ack",
381 ack
, oid_to_hex(result_oid
));
391 struct commit
*commit
=
392 lookup_commit(the_repository
,
397 die(_("invalid commit %s"), oid_to_hex(result_oid
));
398 was_common
= negotiator
->ack(negotiator
, commit
);
399 if (args
->stateless_rpc
402 /* We need to replay the have for this object
403 * on the next RPC request so the peer knows
404 * it is in common with us.
406 const char *hex
= oid_to_hex(result_oid
);
407 packet_buf_write(&req_buf
, "have %s\n", hex
);
408 state_len
= req_buf
.len
;
410 * Reset in_vain because an ack
411 * for this commit has not been
415 } else if (!args
->stateless_rpc
416 || ack
!= ACK_common
)
420 if (ack
== ACK_ready
)
427 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
428 print_verbose(args
, _("giving up"));
436 if (!got_ready
|| !no_done
) {
437 packet_buf_write(&req_buf
, "done\n");
438 send_request(args
, fd
[1], &req_buf
);
440 print_verbose(args
, _("done"));
445 strbuf_release(&req_buf
);
447 if (!got_ready
|| !no_done
)
448 consume_shallow_list(args
, fd
[0]);
449 while (flushes
|| multi_ack
) {
450 int ack
= get_ack(fd
[0], result_oid
);
452 print_verbose(args
, _("got %s (%d) %s"), "ack",
453 ack
, oid_to_hex(result_oid
));
461 /* it is no error to fetch into a completely empty repo */
462 return count
? retval
: 0;
465 static struct commit_list
*complete
;
467 static int mark_complete(const struct object_id
*oid
)
469 struct object
*o
= parse_object(the_repository
, oid
);
471 while (o
&& o
->type
== OBJ_TAG
) {
472 struct tag
*t
= (struct tag
*) o
;
474 break; /* broken repository */
475 o
->flags
|= COMPLETE
;
476 o
= parse_object(the_repository
, &t
->tagged
->oid
);
478 if (o
&& o
->type
== OBJ_COMMIT
) {
479 struct commit
*commit
= (struct commit
*)o
;
480 if (!(commit
->object
.flags
& COMPLETE
)) {
481 commit
->object
.flags
|= COMPLETE
;
482 commit_list_insert(commit
, &complete
);
488 static int mark_complete_oid(const char *refname
, const struct object_id
*oid
,
489 int flag
, void *cb_data
)
491 return mark_complete(oid
);
494 static void mark_recent_complete_commits(struct fetch_pack_args
*args
,
497 while (complete
&& cutoff
<= complete
->item
->date
) {
498 print_verbose(args
, _("Marking %s as complete"),
499 oid_to_hex(&complete
->item
->object
.oid
));
500 pop_most_recent_commit(&complete
, COMPLETE
);
504 static void add_refs_to_oidset(struct oidset
*oids
, struct ref
*refs
)
506 for (; refs
; refs
= refs
->next
)
507 oidset_insert(oids
, &refs
->old_oid
);
510 static int tip_oids_contain(struct oidset
*tip_oids
,
511 struct ref
*unmatched
, struct ref
*newlist
,
512 const struct object_id
*id
)
515 * Note that this only looks at the ref lists the first time it's
516 * called. This works out in filter_refs() because even though it may
517 * add to "newlist" between calls, the additions will always be for
518 * oids that are already in the set.
520 if (!tip_oids
->map
.map
.tablesize
) {
521 add_refs_to_oidset(tip_oids
, unmatched
);
522 add_refs_to_oidset(tip_oids
, newlist
);
524 return oidset_contains(tip_oids
, id
);
527 static void filter_refs(struct fetch_pack_args
*args
,
529 struct ref
**sought
, int nr_sought
)
531 struct ref
*newlist
= NULL
;
532 struct ref
**newtail
= &newlist
;
533 struct ref
*unmatched
= NULL
;
534 struct ref
*ref
, *next
;
535 struct oidset tip_oids
= OIDSET_INIT
;
539 for (ref
= *refs
; ref
; ref
= next
) {
543 if (starts_with(ref
->name
, "refs/") &&
544 check_refname_format(ref
->name
, 0))
547 while (i
< nr_sought
) {
548 int cmp
= strcmp(ref
->name
, sought
[i
]->name
);
550 break; /* definitely do not have it */
552 keep
= 1; /* definitely have it */
553 sought
[i
]->match_status
= REF_MATCHED
;
558 if (!keep
&& args
->fetch_all
&&
559 (!args
->deepen
|| !starts_with(ref
->name
, "refs/tags/")))
566 newtail
= &ref
->next
;
568 ref
->next
= unmatched
;
573 /* Append unmatched requests to the list */
574 for (i
= 0; i
< nr_sought
; i
++) {
575 struct object_id oid
;
579 if (ref
->match_status
!= REF_NOT_MATCHED
)
581 if (parse_oid_hex(ref
->name
, &oid
, &p
) ||
583 oidcmp(&oid
, &ref
->old_oid
))
586 if ((allow_unadvertised_object_request
&
587 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
)) ||
588 tip_oids_contain(&tip_oids
, unmatched
, newlist
,
590 ref
->match_status
= REF_MATCHED
;
591 *newtail
= copy_ref(ref
);
592 newtail
= &(*newtail
)->next
;
594 ref
->match_status
= REF_UNADVERTISED_NOT_ALLOWED
;
598 oidset_clear(&tip_oids
);
599 for (ref
= unmatched
; ref
; ref
= next
) {
607 static void mark_alternate_complete(struct fetch_negotiator
*unused
,
610 mark_complete(&obj
->oid
);
613 struct loose_object_iter
{
614 struct oidset
*loose_object_set
;
619 * If the number of refs is not larger than the number of loose objects,
620 * this function stops inserting.
622 static int add_loose_objects_to_set(const struct object_id
*oid
,
626 struct loose_object_iter
*iter
= data
;
627 oidset_insert(iter
->loose_object_set
, oid
);
628 if (iter
->refs
== NULL
)
631 iter
->refs
= iter
->refs
->next
;
636 * Mark recent commits available locally and reachable from a local ref as
637 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
638 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
639 * thus do not need COMMON_REF marks).
641 * The cutoff time for recency is determined by this heuristic: it is the
642 * earliest commit time of the objects in refs that are commits and that we know
643 * the commit time of.
645 static void mark_complete_and_common_ref(struct fetch_negotiator
*negotiator
,
646 struct fetch_pack_args
*args
,
650 int old_save_commit_buffer
= save_commit_buffer
;
651 timestamp_t cutoff
= 0;
652 struct oidset loose_oid_set
= OIDSET_INIT
;
654 struct loose_object_iter iter
= {&loose_oid_set
, *refs
};
656 /* Enumerate all loose objects or know refs are not so many. */
657 use_oidset
= !for_each_loose_object(add_loose_objects_to_set
,
660 save_commit_buffer
= 0;
662 for (ref
= *refs
; ref
; ref
= ref
->next
) {
664 unsigned int flags
= OBJECT_INFO_QUICK
;
667 !oidset_contains(&loose_oid_set
, &ref
->old_oid
)) {
669 * I know this does not exist in the loose form,
670 * so check if it exists in a non-loose form.
672 flags
|= OBJECT_INFO_IGNORE_LOOSE
;
675 if (!has_object_file_with_flags(&ref
->old_oid
, flags
))
677 o
= parse_object(the_repository
, &ref
->old_oid
);
681 /* We already have it -- which may mean that we were
682 * in sync with the other side at some time after
683 * that (it is OK if we guess wrong here).
685 if (o
->type
== OBJ_COMMIT
) {
686 struct commit
*commit
= (struct commit
*)o
;
687 if (!cutoff
|| cutoff
< commit
->date
)
688 cutoff
= commit
->date
;
692 oidset_clear(&loose_oid_set
);
694 if (!args
->no_dependents
) {
696 for_each_ref(mark_complete_oid
, NULL
);
697 for_each_cached_alternate(NULL
, mark_alternate_complete
);
698 commit_list_sort_by_date(&complete
);
700 mark_recent_complete_commits(args
, cutoff
);
704 * Mark all complete remote refs as common refs.
705 * Don't mark them common yet; the server has to be told so first.
707 for (ref
= *refs
; ref
; ref
= ref
->next
) {
708 struct object
*o
= deref_tag(the_repository
,
709 lookup_object(the_repository
,
713 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
716 negotiator
->known_common(negotiator
,
721 save_commit_buffer
= old_save_commit_buffer
;
725 * Returns 1 if every object pointed to by the given remote refs is available
726 * locally and reachable from a local ref, and 0 otherwise.
728 static int everything_local(struct fetch_pack_args
*args
,
734 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
735 const struct object_id
*remote
= &ref
->old_oid
;
738 o
= lookup_object(the_repository
, remote
->hash
);
739 if (!o
|| !(o
->flags
& COMPLETE
)) {
741 print_verbose(args
, "want %s (%s)", oid_to_hex(remote
),
745 print_verbose(args
, _("already have %s (%s)"), oid_to_hex(remote
),
752 static int sideband_demux(int in
, int out
, void *data
)
757 ret
= recv_sideband("fetch-pack", xd
[0], out
);
762 static int get_pack(struct fetch_pack_args
*args
,
763 int xd
[2], char **pack_lockfile
)
766 int do_keep
= args
->keep_pack
;
767 const char *cmd_name
;
768 struct pack_header header
;
770 struct child_process cmd
= CHILD_PROCESS_INIT
;
773 memset(&demux
, 0, sizeof(demux
));
775 /* xd[] is talking with upload-pack; subprocess reads from
776 * xd[0], spits out band#2 to stderr, and feeds us band#1
777 * through demux->out.
779 demux
.proc
= sideband_demux
;
782 demux
.isolate_sigpipe
= 1;
783 if (start_async(&demux
))
784 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
789 if (!args
->keep_pack
&& unpack_limit
) {
791 if (read_pack_header(demux
.out
, &header
))
792 die(_("protocol error: bad pack header"));
794 if (ntohl(header
.hdr_entries
) < unpack_limit
)
800 if (alternate_shallow_file
) {
801 argv_array_push(&cmd
.args
, "--shallow-file");
802 argv_array_push(&cmd
.args
, alternate_shallow_file
);
805 if (do_keep
|| args
->from_promisor
) {
808 cmd_name
= "index-pack";
809 argv_array_push(&cmd
.args
, cmd_name
);
810 argv_array_push(&cmd
.args
, "--stdin");
811 if (!args
->quiet
&& !args
->no_progress
)
812 argv_array_push(&cmd
.args
, "-v");
813 if (args
->use_thin_pack
)
814 argv_array_push(&cmd
.args
, "--fix-thin");
815 if (do_keep
&& (args
->lock_pack
|| unpack_limit
)) {
816 char hostname
[HOST_NAME_MAX
+ 1];
817 if (xgethostname(hostname
, sizeof(hostname
)))
818 xsnprintf(hostname
, sizeof(hostname
), "localhost");
819 argv_array_pushf(&cmd
.args
,
820 "--keep=fetch-pack %"PRIuMAX
" on %s",
821 (uintmax_t)getpid(), hostname
);
823 if (args
->check_self_contained_and_connected
)
824 argv_array_push(&cmd
.args
, "--check-self-contained-and-connected");
825 if (args
->from_promisor
)
826 argv_array_push(&cmd
.args
, "--promisor");
829 cmd_name
= "unpack-objects";
830 argv_array_push(&cmd
.args
, cmd_name
);
831 if (args
->quiet
|| args
->no_progress
)
832 argv_array_push(&cmd
.args
, "-q");
833 args
->check_self_contained_and_connected
= 0;
837 argv_array_pushf(&cmd
.args
, "--pack_header=%"PRIu32
",%"PRIu32
,
838 ntohl(header
.hdr_version
),
839 ntohl(header
.hdr_entries
));
840 if (fetch_fsck_objects
>= 0
842 : transfer_fsck_objects
>= 0
843 ? transfer_fsck_objects
845 if (args
->from_promisor
)
847 * We cannot use --strict in index-pack because it
848 * checks both broken objects and links, but we only
849 * want to check for broken objects.
851 argv_array_push(&cmd
.args
, "--fsck-objects");
853 argv_array_push(&cmd
.args
, "--strict");
858 if (start_command(&cmd
))
859 die(_("fetch-pack: unable to fork off %s"), cmd_name
);
860 if (do_keep
&& pack_lockfile
) {
861 *pack_lockfile
= index_pack_lockfile(cmd
.out
);
866 /* Closed by start_command() */
869 ret
= finish_command(&cmd
);
870 if (!ret
|| (args
->check_self_contained_and_connected
&& ret
== 1))
871 args
->self_contained_and_connected
=
872 args
->check_self_contained_and_connected
&&
875 die(_("%s failed"), cmd_name
);
876 if (use_sideband
&& finish_async(&demux
))
877 die(_("error in sideband demultiplexer"));
881 static int cmp_ref_by_name(const void *a_
, const void *b_
)
883 const struct ref
*a
= *((const struct ref
**)a_
);
884 const struct ref
*b
= *((const struct ref
**)b_
);
885 return strcmp(a
->name
, b
->name
);
888 static struct ref
*do_fetch_pack(struct fetch_pack_args
*args
,
890 const struct ref
*orig_ref
,
891 struct ref
**sought
, int nr_sought
,
892 struct shallow_info
*si
,
893 char **pack_lockfile
)
895 struct ref
*ref
= copy_ref_list(orig_ref
);
896 struct object_id oid
;
897 const char *agent_feature
;
899 struct fetch_negotiator negotiator
;
900 fetch_negotiator_init(&negotiator
);
902 sort_ref_list(&ref
, ref_compare_name
);
903 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
905 if ((args
->depth
> 0 || is_repository_shallow(the_repository
)) && !server_supports("shallow"))
906 die(_("Server does not support shallow clients"));
907 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
909 if (server_supports("multi_ack_detailed")) {
910 print_verbose(args
, _("Server supports multi_ack_detailed"));
912 if (server_supports("no-done")) {
913 print_verbose(args
, _("Server supports no-done"));
914 if (args
->stateless_rpc
)
918 else if (server_supports("multi_ack")) {
919 print_verbose(args
, _("Server supports multi_ack"));
922 if (server_supports("side-band-64k")) {
923 print_verbose(args
, _("Server supports side-band-64k"));
926 else if (server_supports("side-band")) {
927 print_verbose(args
, _("Server supports side-band"));
930 if (server_supports("allow-tip-sha1-in-want")) {
931 print_verbose(args
, _("Server supports allow-tip-sha1-in-want"));
932 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
934 if (server_supports("allow-reachable-sha1-in-want")) {
935 print_verbose(args
, _("Server supports allow-reachable-sha1-in-want"));
936 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
938 if (!server_supports("thin-pack"))
939 args
->use_thin_pack
= 0;
940 if (!server_supports("no-progress"))
941 args
->no_progress
= 0;
942 if (!server_supports("include-tag"))
943 args
->include_tag
= 0;
944 if (server_supports("ofs-delta"))
945 print_verbose(args
, _("Server supports ofs-delta"));
947 prefer_ofs_delta
= 0;
949 if (server_supports("filter")) {
950 server_supports_filtering
= 1;
951 print_verbose(args
, _("Server supports filter"));
952 } else if (args
->filter_options
.choice
) {
953 warning("filtering not recognized by server, ignoring");
956 if ((agent_feature
= server_feature_value("agent", &agent_len
))) {
959 print_verbose(args
, _("Server version is %.*s"),
960 agent_len
, agent_feature
);
962 if (server_supports("deepen-since"))
964 else if (args
->deepen_since
)
965 die(_("Server does not support --shallow-since"));
966 if (server_supports("deepen-not"))
968 else if (args
->deepen_not
)
969 die(_("Server does not support --shallow-exclude"));
970 if (!server_supports("deepen-relative") && args
->deepen_relative
)
971 die(_("Server does not support --deepen"));
973 mark_complete_and_common_ref(&negotiator
, args
, &ref
);
974 filter_refs(args
, &ref
, sought
, nr_sought
);
975 if (everything_local(args
, &ref
)) {
979 if (find_common(&negotiator
, args
, fd
, &oid
, ref
) < 0)
980 if (!args
->keep_pack
)
981 /* When cloning, it is not unusual to have
984 warning(_("no common commits"));
986 if (args
->stateless_rpc
)
989 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
991 else if (si
->nr_ours
|| si
->nr_theirs
)
992 alternate_shallow_file
= setup_temporary_shallow(si
->shallow
);
994 alternate_shallow_file
= NULL
;
995 if (get_pack(args
, fd
, pack_lockfile
))
996 die(_("git fetch-pack: fetch failed."));
999 negotiator
.release(&negotiator
);
1003 static void add_shallow_requests(struct strbuf
*req_buf
,
1004 const struct fetch_pack_args
*args
)
1006 if (is_repository_shallow(the_repository
))
1007 write_shallow_commits(req_buf
, 1, NULL
);
1008 if (args
->depth
> 0)
1009 packet_buf_write(req_buf
, "deepen %d", args
->depth
);
1010 if (args
->deepen_since
) {
1011 timestamp_t max_age
= approxidate(args
->deepen_since
);
1012 packet_buf_write(req_buf
, "deepen-since %"PRItime
, max_age
);
1014 if (args
->deepen_not
) {
1016 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
1017 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
1018 packet_buf_write(req_buf
, "deepen-not %s", s
->string
);
1023 static void add_wants(const struct ref
*wants
, struct strbuf
*req_buf
)
1025 int use_ref_in_want
= server_supports_feature("fetch", "ref-in-want", 0);
1027 for ( ; wants
; wants
= wants
->next
) {
1028 const struct object_id
*remote
= &wants
->old_oid
;
1032 * If that object is complete (i.e. it is an ancestor of a
1033 * local ref), we tell them we have it but do not have to
1034 * tell them about its ancestors, which they already know
1037 * We use lookup_object here because we are only
1038 * interested in the case we *know* the object is
1039 * reachable and we have already scanned it.
1041 if (((o
= lookup_object(the_repository
, remote
->hash
)) != NULL
) &&
1042 (o
->flags
& COMPLETE
)) {
1046 if (!use_ref_in_want
|| wants
->exact_oid
)
1047 packet_buf_write(req_buf
, "want %s\n", oid_to_hex(remote
));
1049 packet_buf_write(req_buf
, "want-ref %s\n", wants
->name
);
1053 static void add_common(struct strbuf
*req_buf
, struct oidset
*common
)
1055 struct oidset_iter iter
;
1056 const struct object_id
*oid
;
1057 oidset_iter_init(common
, &iter
);
1059 while ((oid
= oidset_iter_next(&iter
))) {
1060 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1064 static int add_haves(struct fetch_negotiator
*negotiator
,
1065 struct strbuf
*req_buf
,
1066 int *haves_to_send
, int *in_vain
)
1069 int haves_added
= 0;
1070 const struct object_id
*oid
;
1072 while ((oid
= negotiator
->next(negotiator
))) {
1073 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1074 if (++haves_added
>= *haves_to_send
)
1078 *in_vain
+= haves_added
;
1079 if (!haves_added
|| *in_vain
>= MAX_IN_VAIN
) {
1081 packet_buf_write(req_buf
, "done\n");
1085 /* Increase haves to send on next round */
1086 *haves_to_send
= next_flush(1, *haves_to_send
);
1091 static int send_fetch_request(struct fetch_negotiator
*negotiator
, int fd_out
,
1092 const struct fetch_pack_args
*args
,
1093 const struct ref
*wants
, struct oidset
*common
,
1094 int *haves_to_send
, int *in_vain
)
1097 struct strbuf req_buf
= STRBUF_INIT
;
1099 if (server_supports_v2("fetch", 1))
1100 packet_buf_write(&req_buf
, "command=fetch");
1101 if (server_supports_v2("agent", 0))
1102 packet_buf_write(&req_buf
, "agent=%s", git_user_agent_sanitized());
1103 if (args
->server_options
&& args
->server_options
->nr
&&
1104 server_supports_v2("server-option", 1)) {
1106 for (i
= 0; i
< args
->server_options
->nr
; i
++)
1107 packet_write_fmt(fd_out
, "server-option=%s",
1108 args
->server_options
->items
[i
].string
);
1111 packet_buf_delim(&req_buf
);
1112 if (args
->use_thin_pack
)
1113 packet_buf_write(&req_buf
, "thin-pack");
1114 if (args
->no_progress
)
1115 packet_buf_write(&req_buf
, "no-progress");
1116 if (args
->include_tag
)
1117 packet_buf_write(&req_buf
, "include-tag");
1118 if (prefer_ofs_delta
)
1119 packet_buf_write(&req_buf
, "ofs-delta");
1121 /* Add shallow-info and deepen request */
1122 if (server_supports_feature("fetch", "shallow", 0))
1123 add_shallow_requests(&req_buf
, args
);
1124 else if (is_repository_shallow(the_repository
) || args
->deepen
)
1125 die(_("Server does not support shallow requests"));
1128 if (server_supports_feature("fetch", "filter", 0) &&
1129 args
->filter_options
.choice
) {
1130 print_verbose(args
, _("Server supports filter"));
1131 packet_buf_write(&req_buf
, "filter %s",
1132 args
->filter_options
.filter_spec
);
1133 } else if (args
->filter_options
.choice
) {
1134 warning("filtering not recognized by server, ignoring");
1138 add_wants(wants
, &req_buf
);
1140 if (args
->no_dependents
) {
1141 packet_buf_write(&req_buf
, "done");
1144 /* Add all of the common commits we've found in previous rounds */
1145 add_common(&req_buf
, common
);
1147 /* Add initial haves */
1148 ret
= add_haves(negotiator
, &req_buf
, haves_to_send
, in_vain
);
1152 packet_buf_flush(&req_buf
);
1153 write_or_die(fd_out
, req_buf
.buf
, req_buf
.len
);
1155 strbuf_release(&req_buf
);
1160 * Processes a section header in a server's response and checks if it matches
1161 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1162 * not consumed); if 0, the line will be consumed and the function will die if
1163 * the section header doesn't match what was expected.
1165 static int process_section_header(struct packet_reader
*reader
,
1166 const char *section
, int peek
)
1170 if (packet_reader_peek(reader
) != PACKET_READ_NORMAL
)
1171 die("error reading section header '%s'", section
);
1173 ret
= !strcmp(reader
->line
, section
);
1177 die("expected '%s', received '%s'",
1178 section
, reader
->line
);
1179 packet_reader_read(reader
);
1185 static int process_acks(struct fetch_negotiator
*negotiator
,
1186 struct packet_reader
*reader
,
1187 struct oidset
*common
)
1190 int received_ready
= 0;
1191 int received_ack
= 0;
1193 process_section_header(reader
, "acknowledgments", 0);
1194 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1197 if (!strcmp(reader
->line
, "NAK"))
1200 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
1201 struct object_id oid
;
1202 if (!get_oid_hex(arg
, &oid
)) {
1203 struct commit
*commit
;
1204 oidset_insert(common
, &oid
);
1205 commit
= lookup_commit(the_repository
, &oid
);
1206 negotiator
->ack(negotiator
, commit
);
1211 if (!strcmp(reader
->line
, "ready")) {
1216 die("unexpected acknowledgment line: '%s'", reader
->line
);
1219 if (reader
->status
!= PACKET_READ_FLUSH
&&
1220 reader
->status
!= PACKET_READ_DELIM
)
1221 die("error processing acks: %d", reader
->status
);
1223 /* return 0 if no common, 1 if there are common, or 2 if ready */
1224 return received_ready
? 2 : (received_ack
? 1 : 0);
1227 static void receive_shallow_info(struct fetch_pack_args
*args
,
1228 struct packet_reader
*reader
)
1230 process_section_header(reader
, "shallow-info", 0);
1231 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1233 struct object_id oid
;
1235 if (skip_prefix(reader
->line
, "shallow ", &arg
)) {
1236 if (get_oid_hex(arg
, &oid
))
1237 die(_("invalid shallow line: %s"), reader
->line
);
1238 register_shallow(the_repository
, &oid
);
1241 if (skip_prefix(reader
->line
, "unshallow ", &arg
)) {
1242 if (get_oid_hex(arg
, &oid
))
1243 die(_("invalid unshallow line: %s"), reader
->line
);
1244 if (!lookup_object(the_repository
, oid
.hash
))
1245 die(_("object not found: %s"), reader
->line
);
1246 /* make sure that it is parsed as shallow */
1247 if (!parse_object(the_repository
, &oid
))
1248 die(_("error in object: %s"), reader
->line
);
1249 if (unregister_shallow(&oid
))
1250 die(_("no shallow found: %s"), reader
->line
);
1253 die(_("expected shallow/unshallow, got %s"), reader
->line
);
1256 if (reader
->status
!= PACKET_READ_FLUSH
&&
1257 reader
->status
!= PACKET_READ_DELIM
)
1258 die("error processing shallow info: %d", reader
->status
);
1260 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
, NULL
);
1264 static void receive_wanted_refs(struct packet_reader
*reader
, struct ref
*refs
)
1266 process_section_header(reader
, "wanted-refs", 0);
1267 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1268 struct object_id oid
;
1270 struct ref
*r
= NULL
;
1272 if (parse_oid_hex(reader
->line
, &oid
, &end
) || *end
++ != ' ')
1273 die("expected wanted-ref, got '%s'", reader
->line
);
1275 for (r
= refs
; r
; r
= r
->next
) {
1276 if (!strcmp(end
, r
->name
)) {
1277 oidcpy(&r
->old_oid
, &oid
);
1283 die("unexpected wanted-ref: '%s'", reader
->line
);
1286 if (reader
->status
!= PACKET_READ_DELIM
)
1287 die("error processing wanted refs: %d", reader
->status
);
1291 FETCH_CHECK_LOCAL
= 0,
1298 static struct ref
*do_fetch_pack_v2(struct fetch_pack_args
*args
,
1300 const struct ref
*orig_ref
,
1301 struct ref
**sought
, int nr_sought
,
1302 char **pack_lockfile
)
1304 struct ref
*ref
= copy_ref_list(orig_ref
);
1305 enum fetch_state state
= FETCH_CHECK_LOCAL
;
1306 struct oidset common
= OIDSET_INIT
;
1307 struct packet_reader reader
;
1309 int haves_to_send
= INITIAL_FLUSH
;
1310 struct fetch_negotiator negotiator
;
1311 fetch_negotiator_init(&negotiator
);
1312 packet_reader_init(&reader
, fd
[0], NULL
, 0,
1313 PACKET_READ_CHOMP_NEWLINE
);
1315 while (state
!= FETCH_DONE
) {
1317 case FETCH_CHECK_LOCAL
:
1318 sort_ref_list(&ref
, ref_compare_name
);
1319 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
1321 /* v2 supports these by default */
1322 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1324 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
1327 /* Filter 'ref' by 'sought' and those that aren't local */
1328 mark_complete_and_common_ref(&negotiator
, args
, &ref
);
1329 filter_refs(args
, &ref
, sought
, nr_sought
);
1330 if (everything_local(args
, &ref
))
1333 state
= FETCH_SEND_REQUEST
;
1335 for_each_ref(rev_list_insert_ref_oid
, &negotiator
);
1336 for_each_cached_alternate(&negotiator
,
1337 insert_one_alternate_object
);
1339 case FETCH_SEND_REQUEST
:
1340 if (send_fetch_request(&negotiator
, fd
[1], args
, ref
,
1342 &haves_to_send
, &in_vain
))
1343 state
= FETCH_GET_PACK
;
1345 state
= FETCH_PROCESS_ACKS
;
1347 case FETCH_PROCESS_ACKS
:
1348 /* Process ACKs/NAKs */
1349 switch (process_acks(&negotiator
, &reader
, &common
)) {
1351 state
= FETCH_GET_PACK
;
1357 state
= FETCH_SEND_REQUEST
;
1361 case FETCH_GET_PACK
:
1362 /* Check for shallow-info section */
1363 if (process_section_header(&reader
, "shallow-info", 1))
1364 receive_shallow_info(args
, &reader
);
1366 if (process_section_header(&reader
, "wanted-refs", 1))
1367 receive_wanted_refs(&reader
, ref
);
1370 process_section_header(&reader
, "packfile", 0);
1371 if (get_pack(args
, fd
, pack_lockfile
))
1372 die(_("git fetch-pack: fetch failed."));
1381 negotiator
.release(&negotiator
);
1382 oidset_clear(&common
);
1386 static void fetch_pack_config(void)
1388 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit
);
1389 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit
);
1390 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta
);
1391 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects
);
1392 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects
);
1394 git_config(git_default_config
, NULL
);
1397 static void fetch_pack_setup(void)
1399 static int did_setup
;
1402 fetch_pack_config();
1403 if (0 <= transfer_unpack_limit
)
1404 unpack_limit
= transfer_unpack_limit
;
1405 else if (0 <= fetch_unpack_limit
)
1406 unpack_limit
= fetch_unpack_limit
;
1410 static int remove_duplicates_in_refs(struct ref
**ref
, int nr
)
1412 struct string_list names
= STRING_LIST_INIT_NODUP
;
1415 for (src
= dst
= 0; src
< nr
; src
++) {
1416 struct string_list_item
*item
;
1417 item
= string_list_insert(&names
, ref
[src
]->name
);
1419 continue; /* already have it */
1420 item
->util
= ref
[src
];
1422 ref
[dst
] = ref
[src
];
1425 for (src
= dst
; src
< nr
; src
++)
1427 string_list_clear(&names
, 0);
1431 static void update_shallow(struct fetch_pack_args
*args
,
1433 struct shallow_info
*si
)
1435 struct oid_array ref
= OID_ARRAY_INIT
;
1440 if (args
->deepen
&& alternate_shallow_file
) {
1441 if (*alternate_shallow_file
== '\0') { /* --unshallow */
1442 unlink_or_warn(git_path_shallow(the_repository
));
1443 rollback_lock_file(&shallow_lock
);
1445 commit_lock_file(&shallow_lock
);
1449 if (!si
->shallow
|| !si
->shallow
->nr
)
1452 if (args
->cloning
) {
1454 * remote is shallow, but this is a clone, there are
1455 * no objects in repo to worry about. Accept any
1456 * shallow points that exist in the pack (iow in repo
1457 * after get_pack() and reprepare_packed_git())
1459 struct oid_array extra
= OID_ARRAY_INIT
;
1460 struct object_id
*oid
= si
->shallow
->oid
;
1461 for (i
= 0; i
< si
->shallow
->nr
; i
++)
1462 if (has_object_file(&oid
[i
]))
1463 oid_array_append(&extra
, &oid
[i
]);
1465 setup_alternate_shallow(&shallow_lock
,
1466 &alternate_shallow_file
,
1468 commit_lock_file(&shallow_lock
);
1470 oid_array_clear(&extra
);
1474 if (!si
->nr_ours
&& !si
->nr_theirs
)
1477 remove_nonexistent_theirs_shallow(si
);
1478 if (!si
->nr_ours
&& !si
->nr_theirs
)
1480 for (r
= refs
; r
; r
= r
->next
)
1481 oid_array_append(&ref
, &r
->old_oid
);
1484 if (args
->update_shallow
) {
1486 * remote is also shallow, .git/shallow may be updated
1487 * so all refs can be accepted. Make sure we only add
1488 * shallow roots that are actually reachable from new
1491 struct oid_array extra
= OID_ARRAY_INIT
;
1492 struct object_id
*oid
= si
->shallow
->oid
;
1493 assign_shallow_commits_to_refs(si
, NULL
, NULL
);
1494 if (!si
->nr_ours
&& !si
->nr_theirs
) {
1495 oid_array_clear(&ref
);
1498 for (i
= 0; i
< si
->nr_ours
; i
++)
1499 oid_array_append(&extra
, &oid
[si
->ours
[i
]]);
1500 for (i
= 0; i
< si
->nr_theirs
; i
++)
1501 oid_array_append(&extra
, &oid
[si
->theirs
[i
]]);
1502 setup_alternate_shallow(&shallow_lock
,
1503 &alternate_shallow_file
,
1505 commit_lock_file(&shallow_lock
);
1506 oid_array_clear(&extra
);
1507 oid_array_clear(&ref
);
1512 * remote is also shallow, check what ref is safe to update
1513 * without updating .git/shallow
1515 status
= xcalloc(ref
.nr
, sizeof(*status
));
1516 assign_shallow_commits_to_refs(si
, NULL
, status
);
1517 if (si
->nr_ours
|| si
->nr_theirs
) {
1518 for (r
= refs
, i
= 0; r
; r
= r
->next
, i
++)
1520 r
->status
= REF_STATUS_REJECT_SHALLOW
;
1523 oid_array_clear(&ref
);
1526 static int iterate_ref_map(void *cb_data
, struct object_id
*oid
)
1528 struct ref
**rm
= cb_data
;
1529 struct ref
*ref
= *rm
;
1532 return -1; /* end of the list */
1534 oidcpy(oid
, &ref
->old_oid
);
1538 struct ref
*fetch_pack(struct fetch_pack_args
*args
,
1539 int fd
[], struct child_process
*conn
,
1540 const struct ref
*ref
,
1542 struct ref
**sought
, int nr_sought
,
1543 struct oid_array
*shallow
,
1544 char **pack_lockfile
,
1545 enum protocol_version version
)
1547 struct ref
*ref_cpy
;
1548 struct shallow_info si
;
1552 nr_sought
= remove_duplicates_in_refs(sought
, nr_sought
);
1555 packet_flush(fd
[1]);
1556 die(_("no matching remote head"));
1558 prepare_shallow_info(&si
, shallow
);
1559 if (version
== protocol_v2
)
1560 ref_cpy
= do_fetch_pack_v2(args
, fd
, ref
, sought
, nr_sought
,
1563 ref_cpy
= do_fetch_pack(args
, fd
, ref
, sought
, nr_sought
,
1564 &si
, pack_lockfile
);
1565 reprepare_packed_git(the_repository
);
1567 if (!args
->cloning
&& args
->deepen
) {
1568 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
1569 struct ref
*iterator
= ref_cpy
;
1570 opt
.shallow_file
= alternate_shallow_file
;
1572 opt
.is_deepening_fetch
= 1;
1573 if (check_connected(iterate_ref_map
, &iterator
, &opt
)) {
1574 error(_("remote did not send all necessary objects"));
1577 rollback_lock_file(&shallow_lock
);
1580 args
->connectivity_checked
= 1;
1583 update_shallow(args
, ref_cpy
, &si
);
1585 clear_shallow_info(&si
);
1589 int report_unmatched_refs(struct ref
**sought
, int nr_sought
)
1593 for (i
= 0; i
< nr_sought
; i
++) {
1596 switch (sought
[i
]->match_status
) {
1599 case REF_NOT_MATCHED
:
1600 error(_("no such remote ref %s"), sought
[i
]->name
);
1602 case REF_UNADVERTISED_NOT_ALLOWED
:
1603 error(_("Server does not allow request for unadvertised object %s"),