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 "fetch-negotiator.h"
23 static int transfer_unpack_limit
= -1;
24 static int fetch_unpack_limit
= -1;
25 static int unpack_limit
= 100;
26 static int prefer_ofs_delta
= 1;
28 static int deepen_since_ok
;
29 static int deepen_not_ok
;
30 static int fetch_fsck_objects
= -1;
31 static int transfer_fsck_objects
= -1;
32 static int agent_supported
;
33 static int server_supports_filtering
;
34 static struct lock_file shallow_lock
;
35 static const char *alternate_shallow_file
;
37 /* Remember to update object flag allocation in object.h */
38 #define COMPLETE (1U << 0)
39 #define ALTERNATE (1U << 1)
42 * After sending this many "have"s if we do not get any new ACK , we
43 * give up traversing our history.
45 #define MAX_IN_VAIN 256
47 static int multi_ack
, use_sideband
;
48 /* Allow specifying sha1 if it is a ref tip. */
49 #define ALLOW_TIP_SHA1 01
50 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
51 #define ALLOW_REACHABLE_SHA1 02
52 static unsigned int allow_unadvertised_object_request
;
54 __attribute__((format (printf
, 2, 3)))
55 static inline void print_verbose(const struct fetch_pack_args
*args
,
63 va_start(params
, fmt
);
64 vfprintf(stderr
, fmt
, params
);
69 struct alternate_object_cache
{
70 struct object
**items
;
74 static void cache_one_alternate(const char *refname
,
75 const struct object_id
*oid
,
78 struct alternate_object_cache
*cache
= vcache
;
79 struct object
*obj
= parse_object(oid
);
81 if (!obj
|| (obj
->flags
& ALTERNATE
))
84 obj
->flags
|= ALTERNATE
;
85 ALLOC_GROW(cache
->items
, cache
->nr
+ 1, cache
->alloc
);
86 cache
->items
[cache
->nr
++] = obj
;
89 static void for_each_cached_alternate(struct fetch_negotiator
*negotiator
,
90 void (*cb
)(struct fetch_negotiator
*,
93 static int initialized
;
94 static struct alternate_object_cache cache
;
98 for_each_alternate_ref(cache_one_alternate
, &cache
);
102 for (i
= 0; i
< cache
.nr
; i
++)
103 cb(negotiator
, cache
.items
[i
]);
106 static int rev_list_insert_ref(struct fetch_negotiator
*negotiator
,
108 const struct object_id
*oid
)
110 struct object
*o
= deref_tag(parse_object(oid
), refname
, 0);
112 if (o
&& o
->type
== OBJ_COMMIT
)
113 negotiator
->add_tip(negotiator
, (struct commit
*)o
);
118 static int rev_list_insert_ref_oid(const char *refname
, const struct object_id
*oid
,
119 int flag
, void *cb_data
)
121 return rev_list_insert_ref(cb_data
, refname
, oid
);
132 static void consume_shallow_list(struct fetch_pack_args
*args
, int fd
)
134 if (args
->stateless_rpc
&& args
->deepen
) {
135 /* If we sent a depth we will get back "duplicate"
136 * shallow and unshallow commands every time there
137 * is a block of have lines exchanged.
140 while ((line
= packet_read_line(fd
, NULL
))) {
141 if (starts_with(line
, "shallow "))
143 if (starts_with(line
, "unshallow "))
145 die(_("git fetch-pack: expected shallow list"));
150 static enum ack_type
get_ack(int fd
, struct object_id
*result_oid
)
153 char *line
= packet_read_line(fd
, &len
);
157 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
158 if (!strcmp(line
, "NAK"))
160 if (skip_prefix(line
, "ACK ", &arg
)) {
161 if (!get_oid_hex(arg
, result_oid
)) {
166 if (strstr(arg
, "continue"))
168 if (strstr(arg
, "common"))
170 if (strstr(arg
, "ready"))
175 if (skip_prefix(line
, "ERR ", &arg
))
176 die(_("remote error: %s"), arg
);
177 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line
);
180 static void send_request(struct fetch_pack_args
*args
,
181 int fd
, struct strbuf
*buf
)
183 if (args
->stateless_rpc
) {
184 send_sideband(fd
, -1, buf
->buf
, buf
->len
, LARGE_PACKET_MAX
);
187 write_or_die(fd
, buf
->buf
, buf
->len
);
190 static void insert_one_alternate_object(struct fetch_negotiator
*negotiator
,
193 rev_list_insert_ref(negotiator
, NULL
, &obj
->oid
);
196 #define INITIAL_FLUSH 16
197 #define PIPESAFE_FLUSH 32
198 #define LARGE_FLUSH 16384
200 static int next_flush(int stateless_rpc
, int count
)
203 if (count
< LARGE_FLUSH
)
206 count
= count
* 11 / 10;
208 if (count
< PIPESAFE_FLUSH
)
211 count
+= PIPESAFE_FLUSH
;
216 static void mark_tips(struct fetch_negotiator
*negotiator
,
217 const struct oid_array
*negotiation_tips
)
221 if (!negotiation_tips
) {
222 for_each_ref(rev_list_insert_ref_oid
, negotiator
);
226 for (i
= 0; i
< negotiation_tips
->nr
; i
++)
227 rev_list_insert_ref(negotiator
, NULL
,
228 &negotiation_tips
->oid
[i
]);
232 static int find_common(struct fetch_negotiator
*negotiator
,
233 struct fetch_pack_args
*args
,
234 int fd
[2], struct object_id
*result_oid
,
238 int count
= 0, flushes
= 0, flush_at
= INITIAL_FLUSH
, retval
;
239 const struct object_id
*oid
;
240 unsigned in_vain
= 0;
241 int got_continue
= 0;
243 struct strbuf req_buf
= STRBUF_INIT
;
244 size_t state_len
= 0;
246 if (args
->stateless_rpc
&& multi_ack
== 1)
247 die(_("--stateless-rpc requires multi_ack_detailed"));
249 mark_tips(negotiator
, args
->negotiation_tips
);
250 for_each_cached_alternate(negotiator
, insert_one_alternate_object
);
253 for ( ; refs
; refs
= refs
->next
) {
254 struct object_id
*remote
= &refs
->old_oid
;
255 const char *remote_hex
;
259 * If that object is complete (i.e. it is an ancestor of a
260 * local ref), we tell them we have it but do not have to
261 * tell them about its ancestors, which they already know
264 * We use lookup_object here because we are only
265 * interested in the case we *know* the object is
266 * reachable and we have already scanned it.
268 if (((o
= lookup_object(remote
->hash
)) != NULL
) &&
269 (o
->flags
& COMPLETE
)) {
273 remote_hex
= oid_to_hex(remote
);
275 struct strbuf c
= STRBUF_INIT
;
276 if (multi_ack
== 2) strbuf_addstr(&c
, " multi_ack_detailed");
277 if (multi_ack
== 1) strbuf_addstr(&c
, " multi_ack");
278 if (no_done
) strbuf_addstr(&c
, " no-done");
279 if (use_sideband
== 2) strbuf_addstr(&c
, " side-band-64k");
280 if (use_sideband
== 1) strbuf_addstr(&c
, " side-band");
281 if (args
->deepen_relative
) strbuf_addstr(&c
, " deepen-relative");
282 if (args
->use_thin_pack
) strbuf_addstr(&c
, " thin-pack");
283 if (args
->no_progress
) strbuf_addstr(&c
, " no-progress");
284 if (args
->include_tag
) strbuf_addstr(&c
, " include-tag");
285 if (prefer_ofs_delta
) strbuf_addstr(&c
, " ofs-delta");
286 if (deepen_since_ok
) strbuf_addstr(&c
, " deepen-since");
287 if (deepen_not_ok
) strbuf_addstr(&c
, " deepen-not");
288 if (agent_supported
) strbuf_addf(&c
, " agent=%s",
289 git_user_agent_sanitized());
290 if (args
->filter_options
.choice
)
291 strbuf_addstr(&c
, " filter");
292 packet_buf_write(&req_buf
, "want %s%s\n", remote_hex
, c
.buf
);
295 packet_buf_write(&req_buf
, "want %s\n", remote_hex
);
300 strbuf_release(&req_buf
);
305 if (is_repository_shallow())
306 write_shallow_commits(&req_buf
, 1, NULL
);
308 packet_buf_write(&req_buf
, "deepen %d", args
->depth
);
309 if (args
->deepen_since
) {
310 timestamp_t max_age
= approxidate(args
->deepen_since
);
311 packet_buf_write(&req_buf
, "deepen-since %"PRItime
, max_age
);
313 if (args
->deepen_not
) {
315 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
316 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
317 packet_buf_write(&req_buf
, "deepen-not %s", s
->string
);
320 if (server_supports_filtering
&& args
->filter_options
.choice
)
321 packet_buf_write(&req_buf
, "filter %s",
322 args
->filter_options
.filter_spec
);
323 packet_buf_flush(&req_buf
);
324 state_len
= req_buf
.len
;
329 struct object_id oid
;
331 send_request(args
, fd
[1], &req_buf
);
332 while ((line
= packet_read_line(fd
[0], NULL
))) {
333 if (skip_prefix(line
, "shallow ", &arg
)) {
334 if (get_oid_hex(arg
, &oid
))
335 die(_("invalid shallow line: %s"), line
);
336 register_shallow(&oid
);
339 if (skip_prefix(line
, "unshallow ", &arg
)) {
340 if (get_oid_hex(arg
, &oid
))
341 die(_("invalid unshallow line: %s"), line
);
342 if (!lookup_object(oid
.hash
))
343 die(_("object not found: %s"), line
);
344 /* make sure that it is parsed as shallow */
345 if (!parse_object(&oid
))
346 die(_("error in object: %s"), line
);
347 if (unregister_shallow(&oid
))
348 die(_("no shallow found: %s"), line
);
351 die(_("expected shallow/unshallow, got %s"), line
);
353 } else if (!args
->stateless_rpc
)
354 send_request(args
, fd
[1], &req_buf
);
356 if (!args
->stateless_rpc
) {
357 /* If we aren't using the stateless-rpc interface
358 * we don't need to retain the headers.
360 strbuf_setlen(&req_buf
, 0);
366 if (args
->no_dependents
)
368 while ((oid
= negotiator
->next(negotiator
))) {
369 packet_buf_write(&req_buf
, "have %s\n", oid_to_hex(oid
));
370 print_verbose(args
, "have %s", oid_to_hex(oid
));
372 if (flush_at
<= ++count
) {
375 packet_buf_flush(&req_buf
);
376 send_request(args
, fd
[1], &req_buf
);
377 strbuf_setlen(&req_buf
, state_len
);
379 flush_at
= next_flush(args
->stateless_rpc
, count
);
382 * We keep one window "ahead" of the other side, and
383 * will wait for an ACK only on the next one
385 if (!args
->stateless_rpc
&& count
== INITIAL_FLUSH
)
388 consume_shallow_list(args
, fd
[0]);
390 ack
= get_ack(fd
[0], result_oid
);
392 print_verbose(args
, _("got %s %d %s"), "ack",
393 ack
, oid_to_hex(result_oid
));
403 struct commit
*commit
=
404 lookup_commit(result_oid
);
407 die(_("invalid commit %s"), oid_to_hex(result_oid
));
408 was_common
= negotiator
->ack(negotiator
, commit
);
409 if (args
->stateless_rpc
412 /* We need to replay the have for this object
413 * on the next RPC request so the peer knows
414 * it is in common with us.
416 const char *hex
= oid_to_hex(result_oid
);
417 packet_buf_write(&req_buf
, "have %s\n", hex
);
418 state_len
= req_buf
.len
;
420 * Reset in_vain because an ack
421 * for this commit has not been
425 } else if (!args
->stateless_rpc
426 || ack
!= ACK_common
)
430 if (ack
== ACK_ready
)
437 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
438 print_verbose(args
, _("giving up"));
446 if (!got_ready
|| !no_done
) {
447 packet_buf_write(&req_buf
, "done\n");
448 send_request(args
, fd
[1], &req_buf
);
450 print_verbose(args
, _("done"));
455 strbuf_release(&req_buf
);
457 if (!got_ready
|| !no_done
)
458 consume_shallow_list(args
, fd
[0]);
459 while (flushes
|| multi_ack
) {
460 int ack
= get_ack(fd
[0], result_oid
);
462 print_verbose(args
, _("got %s (%d) %s"), "ack",
463 ack
, oid_to_hex(result_oid
));
471 /* it is no error to fetch into a completely empty repo */
472 return count
? retval
: 0;
475 static struct commit_list
*complete
;
477 static int mark_complete(const struct object_id
*oid
)
479 struct object
*o
= parse_object(oid
);
481 while (o
&& o
->type
== OBJ_TAG
) {
482 struct tag
*t
= (struct tag
*) o
;
484 break; /* broken repository */
485 o
->flags
|= COMPLETE
;
486 o
= parse_object(&t
->tagged
->oid
);
488 if (o
&& o
->type
== OBJ_COMMIT
) {
489 struct commit
*commit
= (struct commit
*)o
;
490 if (!(commit
->object
.flags
& COMPLETE
)) {
491 commit
->object
.flags
|= COMPLETE
;
492 commit_list_insert(commit
, &complete
);
498 static int mark_complete_oid(const char *refname
, const struct object_id
*oid
,
499 int flag
, void *cb_data
)
501 return mark_complete(oid
);
504 static void mark_recent_complete_commits(struct fetch_pack_args
*args
,
507 while (complete
&& cutoff
<= complete
->item
->date
) {
508 print_verbose(args
, _("Marking %s as complete"),
509 oid_to_hex(&complete
->item
->object
.oid
));
510 pop_most_recent_commit(&complete
, COMPLETE
);
514 static void add_refs_to_oidset(struct oidset
*oids
, struct ref
*refs
)
516 for (; refs
; refs
= refs
->next
)
517 oidset_insert(oids
, &refs
->old_oid
);
520 static int tip_oids_contain(struct oidset
*tip_oids
,
521 struct ref
*unmatched
, struct ref
*newlist
,
522 const struct object_id
*id
)
525 * Note that this only looks at the ref lists the first time it's
526 * called. This works out in filter_refs() because even though it may
527 * add to "newlist" between calls, the additions will always be for
528 * oids that are already in the set.
530 if (!tip_oids
->map
.map
.tablesize
) {
531 add_refs_to_oidset(tip_oids
, unmatched
);
532 add_refs_to_oidset(tip_oids
, newlist
);
534 return oidset_contains(tip_oids
, id
);
537 static void filter_refs(struct fetch_pack_args
*args
,
539 struct ref
**sought
, int nr_sought
)
541 struct ref
*newlist
= NULL
;
542 struct ref
**newtail
= &newlist
;
543 struct ref
*unmatched
= NULL
;
544 struct ref
*ref
, *next
;
545 struct oidset tip_oids
= OIDSET_INIT
;
549 for (ref
= *refs
; ref
; ref
= next
) {
553 if (starts_with(ref
->name
, "refs/") &&
554 check_refname_format(ref
->name
, 0))
557 while (i
< nr_sought
) {
558 int cmp
= strcmp(ref
->name
, sought
[i
]->name
);
560 break; /* definitely do not have it */
562 keep
= 1; /* definitely have it */
563 sought
[i
]->match_status
= REF_MATCHED
;
569 if (!keep
&& args
->fetch_all
&&
570 (!args
->deepen
|| !starts_with(ref
->name
, "refs/tags/")))
576 newtail
= &ref
->next
;
578 ref
->next
= unmatched
;
583 /* Append unmatched requests to the list */
584 for (i
= 0; i
< nr_sought
; i
++) {
585 struct object_id oid
;
589 if (ref
->match_status
!= REF_NOT_MATCHED
)
591 if (parse_oid_hex(ref
->name
, &oid
, &p
) ||
593 oidcmp(&oid
, &ref
->old_oid
))
596 if ((allow_unadvertised_object_request
&
597 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
)) ||
598 tip_oids_contain(&tip_oids
, unmatched
, newlist
,
600 ref
->match_status
= REF_MATCHED
;
601 *newtail
= copy_ref(ref
);
602 newtail
= &(*newtail
)->next
;
604 ref
->match_status
= REF_UNADVERTISED_NOT_ALLOWED
;
608 oidset_clear(&tip_oids
);
609 for (ref
= unmatched
; ref
; ref
= next
) {
617 static void mark_alternate_complete(struct fetch_negotiator
*unused
,
620 mark_complete(&obj
->oid
);
623 struct loose_object_iter
{
624 struct oidset
*loose_object_set
;
629 * If the number of refs is not larger than the number of loose objects,
630 * this function stops inserting.
632 static int add_loose_objects_to_set(const struct object_id
*oid
,
636 struct loose_object_iter
*iter
= data
;
637 oidset_insert(iter
->loose_object_set
, oid
);
638 if (iter
->refs
== NULL
)
641 iter
->refs
= iter
->refs
->next
;
646 * Mark recent commits available locally and reachable from a local ref as
647 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
648 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
649 * thus do not need COMMON_REF marks).
651 * The cutoff time for recency is determined by this heuristic: it is the
652 * earliest commit time of the objects in refs that are commits and that we know
653 * the commit time of.
655 static void mark_complete_and_common_ref(struct fetch_negotiator
*negotiator
,
656 struct fetch_pack_args
*args
,
660 int old_save_commit_buffer
= save_commit_buffer
;
661 timestamp_t cutoff
= 0;
662 struct oidset loose_oid_set
= OIDSET_INIT
;
664 struct loose_object_iter iter
= {&loose_oid_set
, *refs
};
666 /* Enumerate all loose objects or know refs are not so many. */
667 use_oidset
= !for_each_loose_object(add_loose_objects_to_set
,
670 save_commit_buffer
= 0;
672 for (ref
= *refs
; ref
; ref
= ref
->next
) {
674 unsigned int flags
= OBJECT_INFO_QUICK
;
677 !oidset_contains(&loose_oid_set
, &ref
->old_oid
)) {
679 * I know this does not exist in the loose form,
680 * so check if it exists in a non-loose form.
682 flags
|= OBJECT_INFO_IGNORE_LOOSE
;
685 if (!has_object_file_with_flags(&ref
->old_oid
, flags
))
687 o
= parse_object(&ref
->old_oid
);
691 /* We already have it -- which may mean that we were
692 * in sync with the other side at some time after
693 * that (it is OK if we guess wrong here).
695 if (o
->type
== OBJ_COMMIT
) {
696 struct commit
*commit
= (struct commit
*)o
;
697 if (!cutoff
|| cutoff
< commit
->date
)
698 cutoff
= commit
->date
;
702 oidset_clear(&loose_oid_set
);
704 if (!args
->no_dependents
) {
706 for_each_ref(mark_complete_oid
, NULL
);
707 for_each_cached_alternate(NULL
, mark_alternate_complete
);
708 commit_list_sort_by_date(&complete
);
710 mark_recent_complete_commits(args
, cutoff
);
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 for (ref
= *refs
; ref
; ref
= ref
->next
) {
718 struct object
*o
= deref_tag(lookup_object(ref
->old_oid
.hash
),
721 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
724 negotiator
->known_common(negotiator
,
729 save_commit_buffer
= old_save_commit_buffer
;
733 * Returns 1 if every object pointed to by the given remote refs is available
734 * locally and reachable from a local ref, and 0 otherwise.
736 static int everything_local(struct fetch_pack_args
*args
,
742 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
743 const struct object_id
*remote
= &ref
->old_oid
;
746 o
= lookup_object(remote
->hash
);
747 if (!o
|| !(o
->flags
& COMPLETE
)) {
749 print_verbose(args
, "want %s (%s)", oid_to_hex(remote
),
753 print_verbose(args
, _("already have %s (%s)"), oid_to_hex(remote
),
760 static int sideband_demux(int in
, int out
, void *data
)
765 ret
= recv_sideband("fetch-pack", xd
[0], out
);
770 static int get_pack(struct fetch_pack_args
*args
,
771 int xd
[2], char **pack_lockfile
)
774 int do_keep
= args
->keep_pack
;
775 const char *cmd_name
;
776 struct pack_header header
;
778 struct child_process cmd
= CHILD_PROCESS_INIT
;
781 memset(&demux
, 0, sizeof(demux
));
783 /* xd[] is talking with upload-pack; subprocess reads from
784 * xd[0], spits out band#2 to stderr, and feeds us band#1
785 * through demux->out.
787 demux
.proc
= sideband_demux
;
790 demux
.isolate_sigpipe
= 1;
791 if (start_async(&demux
))
792 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
797 if (!args
->keep_pack
&& unpack_limit
) {
799 if (read_pack_header(demux
.out
, &header
))
800 die(_("protocol error: bad pack header"));
802 if (ntohl(header
.hdr_entries
) < unpack_limit
)
808 if (alternate_shallow_file
) {
809 argv_array_push(&cmd
.args
, "--shallow-file");
810 argv_array_push(&cmd
.args
, alternate_shallow_file
);
813 if (do_keep
|| args
->from_promisor
) {
816 cmd_name
= "index-pack";
817 argv_array_push(&cmd
.args
, cmd_name
);
818 argv_array_push(&cmd
.args
, "--stdin");
819 if (!args
->quiet
&& !args
->no_progress
)
820 argv_array_push(&cmd
.args
, "-v");
821 if (args
->use_thin_pack
)
822 argv_array_push(&cmd
.args
, "--fix-thin");
823 if (do_keep
&& (args
->lock_pack
|| unpack_limit
)) {
824 char hostname
[HOST_NAME_MAX
+ 1];
825 if (xgethostname(hostname
, sizeof(hostname
)))
826 xsnprintf(hostname
, sizeof(hostname
), "localhost");
827 argv_array_pushf(&cmd
.args
,
828 "--keep=fetch-pack %"PRIuMAX
" on %s",
829 (uintmax_t)getpid(), hostname
);
831 if (args
->check_self_contained_and_connected
)
832 argv_array_push(&cmd
.args
, "--check-self-contained-and-connected");
833 if (args
->from_promisor
)
834 argv_array_push(&cmd
.args
, "--promisor");
837 cmd_name
= "unpack-objects";
838 argv_array_push(&cmd
.args
, cmd_name
);
839 if (args
->quiet
|| args
->no_progress
)
840 argv_array_push(&cmd
.args
, "-q");
841 args
->check_self_contained_and_connected
= 0;
845 argv_array_pushf(&cmd
.args
, "--pack_header=%"PRIu32
",%"PRIu32
,
846 ntohl(header
.hdr_version
),
847 ntohl(header
.hdr_entries
));
848 if (fetch_fsck_objects
>= 0
850 : transfer_fsck_objects
>= 0
851 ? transfer_fsck_objects
853 if (args
->from_promisor
)
855 * We cannot use --strict in index-pack because it
856 * checks both broken objects and links, but we only
857 * want to check for broken objects.
859 argv_array_push(&cmd
.args
, "--fsck-objects");
861 argv_array_push(&cmd
.args
, "--strict");
866 if (start_command(&cmd
))
867 die(_("fetch-pack: unable to fork off %s"), cmd_name
);
868 if (do_keep
&& pack_lockfile
) {
869 *pack_lockfile
= index_pack_lockfile(cmd
.out
);
874 /* Closed by start_command() */
877 ret
= finish_command(&cmd
);
878 if (!ret
|| (args
->check_self_contained_and_connected
&& ret
== 1))
879 args
->self_contained_and_connected
=
880 args
->check_self_contained_and_connected
&&
883 die(_("%s failed"), cmd_name
);
884 if (use_sideband
&& finish_async(&demux
))
885 die(_("error in sideband demultiplexer"));
889 static int cmp_ref_by_name(const void *a_
, const void *b_
)
891 const struct ref
*a
= *((const struct ref
**)a_
);
892 const struct ref
*b
= *((const struct ref
**)b_
);
893 return strcmp(a
->name
, b
->name
);
896 static struct ref
*do_fetch_pack(struct fetch_pack_args
*args
,
898 const struct ref
*orig_ref
,
899 struct ref
**sought
, int nr_sought
,
900 struct shallow_info
*si
,
901 char **pack_lockfile
)
903 struct ref
*ref
= copy_ref_list(orig_ref
);
904 struct object_id oid
;
905 const char *agent_feature
;
907 struct fetch_negotiator negotiator
;
908 fetch_negotiator_init(&negotiator
);
910 sort_ref_list(&ref
, ref_compare_name
);
911 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
913 if ((args
->depth
> 0 || is_repository_shallow()) && !server_supports("shallow"))
914 die(_("Server does not support shallow clients"));
915 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
917 if (server_supports("multi_ack_detailed")) {
918 print_verbose(args
, _("Server supports multi_ack_detailed"));
920 if (server_supports("no-done")) {
921 print_verbose(args
, _("Server supports no-done"));
922 if (args
->stateless_rpc
)
926 else if (server_supports("multi_ack")) {
927 print_verbose(args
, _("Server supports multi_ack"));
930 if (server_supports("side-band-64k")) {
931 print_verbose(args
, _("Server supports side-band-64k"));
934 else if (server_supports("side-band")) {
935 print_verbose(args
, _("Server supports side-band"));
938 if (server_supports("allow-tip-sha1-in-want")) {
939 print_verbose(args
, _("Server supports allow-tip-sha1-in-want"));
940 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
942 if (server_supports("allow-reachable-sha1-in-want")) {
943 print_verbose(args
, _("Server supports allow-reachable-sha1-in-want"));
944 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
946 if (!server_supports("thin-pack"))
947 args
->use_thin_pack
= 0;
948 if (!server_supports("no-progress"))
949 args
->no_progress
= 0;
950 if (!server_supports("include-tag"))
951 args
->include_tag
= 0;
952 if (server_supports("ofs-delta"))
953 print_verbose(args
, _("Server supports ofs-delta"));
955 prefer_ofs_delta
= 0;
957 if (server_supports("filter")) {
958 server_supports_filtering
= 1;
959 print_verbose(args
, _("Server supports filter"));
960 } else if (args
->filter_options
.choice
) {
961 warning("filtering not recognized by server, ignoring");
964 if ((agent_feature
= server_feature_value("agent", &agent_len
))) {
967 print_verbose(args
, _("Server version is %.*s"),
968 agent_len
, agent_feature
);
970 if (server_supports("deepen-since"))
972 else if (args
->deepen_since
)
973 die(_("Server does not support --shallow-since"));
974 if (server_supports("deepen-not"))
976 else if (args
->deepen_not
)
977 die(_("Server does not support --shallow-exclude"));
978 if (!server_supports("deepen-relative") && args
->deepen_relative
)
979 die(_("Server does not support --deepen"));
981 mark_complete_and_common_ref(&negotiator
, args
, &ref
);
982 filter_refs(args
, &ref
, sought
, nr_sought
);
983 if (everything_local(args
, &ref
)) {
987 if (find_common(&negotiator
, args
, fd
, &oid
, ref
) < 0)
988 if (!args
->keep_pack
)
989 /* When cloning, it is not unusual to have
992 warning(_("no common commits"));
994 if (args
->stateless_rpc
)
997 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
999 else if (si
->nr_ours
|| si
->nr_theirs
)
1000 alternate_shallow_file
= setup_temporary_shallow(si
->shallow
);
1002 alternate_shallow_file
= NULL
;
1003 if (get_pack(args
, fd
, pack_lockfile
))
1004 die(_("git fetch-pack: fetch failed."));
1007 negotiator
.release(&negotiator
);
1011 static void add_shallow_requests(struct strbuf
*req_buf
,
1012 const struct fetch_pack_args
*args
)
1014 if (is_repository_shallow())
1015 write_shallow_commits(req_buf
, 1, NULL
);
1016 if (args
->depth
> 0)
1017 packet_buf_write(req_buf
, "deepen %d", args
->depth
);
1018 if (args
->deepen_since
) {
1019 timestamp_t max_age
= approxidate(args
->deepen_since
);
1020 packet_buf_write(req_buf
, "deepen-since %"PRItime
, max_age
);
1022 if (args
->deepen_not
) {
1024 for (i
= 0; i
< args
->deepen_not
->nr
; i
++) {
1025 struct string_list_item
*s
= args
->deepen_not
->items
+ i
;
1026 packet_buf_write(req_buf
, "deepen-not %s", s
->string
);
1031 static void add_wants(const struct ref
*wants
, struct strbuf
*req_buf
)
1033 for ( ; wants
; wants
= wants
->next
) {
1034 const struct object_id
*remote
= &wants
->old_oid
;
1035 const char *remote_hex
;
1039 * If that object is complete (i.e. it is an ancestor of a
1040 * local ref), we tell them we have it but do not have to
1041 * tell them about its ancestors, which they already know
1044 * We use lookup_object here because we are only
1045 * interested in the case we *know* the object is
1046 * reachable and we have already scanned it.
1048 if (((o
= lookup_object(remote
->hash
)) != NULL
) &&
1049 (o
->flags
& COMPLETE
)) {
1053 remote_hex
= oid_to_hex(remote
);
1054 packet_buf_write(req_buf
, "want %s\n", remote_hex
);
1058 static void add_common(struct strbuf
*req_buf
, struct oidset
*common
)
1060 struct oidset_iter iter
;
1061 const struct object_id
*oid
;
1062 oidset_iter_init(common
, &iter
);
1064 while ((oid
= oidset_iter_next(&iter
))) {
1065 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1069 static int add_haves(struct fetch_negotiator
*negotiator
,
1070 struct strbuf
*req_buf
,
1071 int *haves_to_send
, int *in_vain
)
1074 int haves_added
= 0;
1075 const struct object_id
*oid
;
1077 while ((oid
= negotiator
->next(negotiator
))) {
1078 packet_buf_write(req_buf
, "have %s\n", oid_to_hex(oid
));
1079 if (++haves_added
>= *haves_to_send
)
1083 *in_vain
+= haves_added
;
1084 if (!haves_added
|| *in_vain
>= MAX_IN_VAIN
) {
1086 packet_buf_write(req_buf
, "done\n");
1090 /* Increase haves to send on next round */
1091 *haves_to_send
= next_flush(1, *haves_to_send
);
1096 static int send_fetch_request(struct fetch_negotiator
*negotiator
, int fd_out
,
1097 const struct fetch_pack_args
*args
,
1098 const struct ref
*wants
, struct oidset
*common
,
1099 int *haves_to_send
, int *in_vain
)
1102 struct strbuf req_buf
= STRBUF_INIT
;
1104 if (server_supports_v2("fetch", 1))
1105 packet_buf_write(&req_buf
, "command=fetch");
1106 if (server_supports_v2("agent", 0))
1107 packet_buf_write(&req_buf
, "agent=%s", git_user_agent_sanitized());
1108 if (args
->server_options
&& args
->server_options
->nr
&&
1109 server_supports_v2("server-option", 1)) {
1111 for (i
= 0; i
< args
->server_options
->nr
; i
++)
1112 packet_write_fmt(fd_out
, "server-option=%s",
1113 args
->server_options
->items
[i
].string
);
1116 packet_buf_delim(&req_buf
);
1117 if (args
->use_thin_pack
)
1118 packet_buf_write(&req_buf
, "thin-pack");
1119 if (args
->no_progress
)
1120 packet_buf_write(&req_buf
, "no-progress");
1121 if (args
->include_tag
)
1122 packet_buf_write(&req_buf
, "include-tag");
1123 if (prefer_ofs_delta
)
1124 packet_buf_write(&req_buf
, "ofs-delta");
1126 /* Add shallow-info and deepen request */
1127 if (server_supports_feature("fetch", "shallow", 0))
1128 add_shallow_requests(&req_buf
, args
);
1129 else if (is_repository_shallow() || args
->deepen
)
1130 die(_("Server does not support shallow requests"));
1133 if (server_supports_feature("fetch", "filter", 0) &&
1134 args
->filter_options
.choice
) {
1135 print_verbose(args
, _("Server supports filter"));
1136 packet_buf_write(&req_buf
, "filter %s",
1137 args
->filter_options
.filter_spec
);
1138 } else if (args
->filter_options
.choice
) {
1139 warning("filtering not recognized by server, ignoring");
1143 add_wants(wants
, &req_buf
);
1145 if (args
->no_dependents
) {
1146 packet_buf_write(&req_buf
, "done");
1149 /* Add all of the common commits we've found in previous rounds */
1150 add_common(&req_buf
, common
);
1152 /* Add initial haves */
1153 ret
= add_haves(negotiator
, &req_buf
, haves_to_send
, in_vain
);
1157 packet_buf_flush(&req_buf
);
1158 write_or_die(fd_out
, req_buf
.buf
, req_buf
.len
);
1160 strbuf_release(&req_buf
);
1165 * Processes a section header in a server's response and checks if it matches
1166 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1167 * not consumed); if 0, the line will be consumed and the function will die if
1168 * the section header doesn't match what was expected.
1170 static int process_section_header(struct packet_reader
*reader
,
1171 const char *section
, int peek
)
1175 if (packet_reader_peek(reader
) != PACKET_READ_NORMAL
)
1176 die("error reading section header '%s'", section
);
1178 ret
= !strcmp(reader
->line
, section
);
1182 die("expected '%s', received '%s'",
1183 section
, reader
->line
);
1184 packet_reader_read(reader
);
1190 static int process_acks(struct fetch_negotiator
*negotiator
,
1191 struct packet_reader
*reader
,
1192 struct oidset
*common
)
1195 int received_ready
= 0;
1196 int received_ack
= 0;
1198 process_section_header(reader
, "acknowledgments", 0);
1199 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1202 if (!strcmp(reader
->line
, "NAK"))
1205 if (skip_prefix(reader
->line
, "ACK ", &arg
)) {
1206 struct object_id oid
;
1207 if (!get_oid_hex(arg
, &oid
)) {
1208 struct commit
*commit
;
1209 oidset_insert(common
, &oid
);
1210 commit
= lookup_commit(&oid
);
1211 negotiator
->ack(negotiator
, commit
);
1216 if (!strcmp(reader
->line
, "ready")) {
1221 die("unexpected acknowledgment line: '%s'", reader
->line
);
1224 if (reader
->status
!= PACKET_READ_FLUSH
&&
1225 reader
->status
!= PACKET_READ_DELIM
)
1226 die("error processing acks: %d", reader
->status
);
1228 /* return 0 if no common, 1 if there are common, or 2 if ready */
1229 return received_ready
? 2 : (received_ack
? 1 : 0);
1232 static void receive_shallow_info(struct fetch_pack_args
*args
,
1233 struct packet_reader
*reader
)
1235 process_section_header(reader
, "shallow-info", 0);
1236 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
1238 struct object_id oid
;
1240 if (skip_prefix(reader
->line
, "shallow ", &arg
)) {
1241 if (get_oid_hex(arg
, &oid
))
1242 die(_("invalid shallow line: %s"), reader
->line
);
1243 register_shallow(&oid
);
1246 if (skip_prefix(reader
->line
, "unshallow ", &arg
)) {
1247 if (get_oid_hex(arg
, &oid
))
1248 die(_("invalid unshallow line: %s"), reader
->line
);
1249 if (!lookup_object(oid
.hash
))
1250 die(_("object not found: %s"), reader
->line
);
1251 /* make sure that it is parsed as shallow */
1252 if (!parse_object(&oid
))
1253 die(_("error in object: %s"), reader
->line
);
1254 if (unregister_shallow(&oid
))
1255 die(_("no shallow found: %s"), reader
->line
);
1258 die(_("expected shallow/unshallow, got %s"), reader
->line
);
1261 if (reader
->status
!= PACKET_READ_FLUSH
&&
1262 reader
->status
!= PACKET_READ_DELIM
)
1263 die("error processing shallow info: %d", reader
->status
);
1265 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
, NULL
);
1270 FETCH_CHECK_LOCAL
= 0,
1277 static struct ref
*do_fetch_pack_v2(struct fetch_pack_args
*args
,
1279 const struct ref
*orig_ref
,
1280 struct ref
**sought
, int nr_sought
,
1281 char **pack_lockfile
)
1283 struct ref
*ref
= copy_ref_list(orig_ref
);
1284 enum fetch_state state
= FETCH_CHECK_LOCAL
;
1285 struct oidset common
= OIDSET_INIT
;
1286 struct packet_reader reader
;
1288 int haves_to_send
= INITIAL_FLUSH
;
1289 struct fetch_negotiator negotiator
;
1290 fetch_negotiator_init(&negotiator
);
1291 packet_reader_init(&reader
, fd
[0], NULL
, 0,
1292 PACKET_READ_CHOMP_NEWLINE
);
1294 while (state
!= FETCH_DONE
) {
1296 case FETCH_CHECK_LOCAL
:
1297 sort_ref_list(&ref
, ref_compare_name
);
1298 QSORT(sought
, nr_sought
, cmp_ref_by_name
);
1300 /* v2 supports these by default */
1301 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1303 if (args
->depth
> 0 || args
->deepen_since
|| args
->deepen_not
)
1306 /* Filter 'ref' by 'sought' and those that aren't local */
1307 mark_complete_and_common_ref(&negotiator
, args
, &ref
);
1308 filter_refs(args
, &ref
, sought
, nr_sought
);
1309 if (everything_local(args
, &ref
))
1312 state
= FETCH_SEND_REQUEST
;
1314 mark_tips(&negotiator
, args
->negotiation_tips
);
1315 for_each_cached_alternate(&negotiator
,
1316 insert_one_alternate_object
);
1318 case FETCH_SEND_REQUEST
:
1319 if (send_fetch_request(&negotiator
, fd
[1], args
, ref
,
1321 &haves_to_send
, &in_vain
))
1322 state
= FETCH_GET_PACK
;
1324 state
= FETCH_PROCESS_ACKS
;
1326 case FETCH_PROCESS_ACKS
:
1327 /* Process ACKs/NAKs */
1328 switch (process_acks(&negotiator
, &reader
, &common
)) {
1330 state
= FETCH_GET_PACK
;
1336 state
= FETCH_SEND_REQUEST
;
1340 case FETCH_GET_PACK
:
1341 /* Check for shallow-info section */
1342 if (process_section_header(&reader
, "shallow-info", 1))
1343 receive_shallow_info(args
, &reader
);
1346 process_section_header(&reader
, "packfile", 0);
1347 if (get_pack(args
, fd
, pack_lockfile
))
1348 die(_("git fetch-pack: fetch failed."));
1357 negotiator
.release(&negotiator
);
1358 oidset_clear(&common
);
1362 static void fetch_pack_config(void)
1364 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit
);
1365 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit
);
1366 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta
);
1367 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects
);
1368 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects
);
1370 git_config(git_default_config
, NULL
);
1373 static void fetch_pack_setup(void)
1375 static int did_setup
;
1378 fetch_pack_config();
1379 if (0 <= transfer_unpack_limit
)
1380 unpack_limit
= transfer_unpack_limit
;
1381 else if (0 <= fetch_unpack_limit
)
1382 unpack_limit
= fetch_unpack_limit
;
1386 static int remove_duplicates_in_refs(struct ref
**ref
, int nr
)
1388 struct string_list names
= STRING_LIST_INIT_NODUP
;
1391 for (src
= dst
= 0; src
< nr
; src
++) {
1392 struct string_list_item
*item
;
1393 item
= string_list_insert(&names
, ref
[src
]->name
);
1395 continue; /* already have it */
1396 item
->util
= ref
[src
];
1398 ref
[dst
] = ref
[src
];
1401 for (src
= dst
; src
< nr
; src
++)
1403 string_list_clear(&names
, 0);
1407 static void update_shallow(struct fetch_pack_args
*args
,
1408 struct ref
**sought
, int nr_sought
,
1409 struct shallow_info
*si
)
1411 struct oid_array ref
= OID_ARRAY_INIT
;
1415 if (args
->deepen
&& alternate_shallow_file
) {
1416 if (*alternate_shallow_file
== '\0') { /* --unshallow */
1417 unlink_or_warn(git_path_shallow());
1418 rollback_lock_file(&shallow_lock
);
1420 commit_lock_file(&shallow_lock
);
1424 if (!si
->shallow
|| !si
->shallow
->nr
)
1427 if (args
->cloning
) {
1429 * remote is shallow, but this is a clone, there are
1430 * no objects in repo to worry about. Accept any
1431 * shallow points that exist in the pack (iow in repo
1432 * after get_pack() and reprepare_packed_git())
1434 struct oid_array extra
= OID_ARRAY_INIT
;
1435 struct object_id
*oid
= si
->shallow
->oid
;
1436 for (i
= 0; i
< si
->shallow
->nr
; i
++)
1437 if (has_object_file(&oid
[i
]))
1438 oid_array_append(&extra
, &oid
[i
]);
1440 setup_alternate_shallow(&shallow_lock
,
1441 &alternate_shallow_file
,
1443 commit_lock_file(&shallow_lock
);
1445 oid_array_clear(&extra
);
1449 if (!si
->nr_ours
&& !si
->nr_theirs
)
1452 remove_nonexistent_theirs_shallow(si
);
1453 if (!si
->nr_ours
&& !si
->nr_theirs
)
1455 for (i
= 0; i
< nr_sought
; i
++)
1456 oid_array_append(&ref
, &sought
[i
]->old_oid
);
1459 if (args
->update_shallow
) {
1461 * remote is also shallow, .git/shallow may be updated
1462 * so all refs can be accepted. Make sure we only add
1463 * shallow roots that are actually reachable from new
1466 struct oid_array extra
= OID_ARRAY_INIT
;
1467 struct object_id
*oid
= si
->shallow
->oid
;
1468 assign_shallow_commits_to_refs(si
, NULL
, NULL
);
1469 if (!si
->nr_ours
&& !si
->nr_theirs
) {
1470 oid_array_clear(&ref
);
1473 for (i
= 0; i
< si
->nr_ours
; i
++)
1474 oid_array_append(&extra
, &oid
[si
->ours
[i
]]);
1475 for (i
= 0; i
< si
->nr_theirs
; i
++)
1476 oid_array_append(&extra
, &oid
[si
->theirs
[i
]]);
1477 setup_alternate_shallow(&shallow_lock
,
1478 &alternate_shallow_file
,
1480 commit_lock_file(&shallow_lock
);
1481 oid_array_clear(&extra
);
1482 oid_array_clear(&ref
);
1487 * remote is also shallow, check what ref is safe to update
1488 * without updating .git/shallow
1490 status
= xcalloc(nr_sought
, sizeof(*status
));
1491 assign_shallow_commits_to_refs(si
, NULL
, status
);
1492 if (si
->nr_ours
|| si
->nr_theirs
) {
1493 for (i
= 0; i
< nr_sought
; i
++)
1495 sought
[i
]->status
= REF_STATUS_REJECT_SHALLOW
;
1498 oid_array_clear(&ref
);
1501 struct ref
*fetch_pack(struct fetch_pack_args
*args
,
1502 int fd
[], struct child_process
*conn
,
1503 const struct ref
*ref
,
1505 struct ref
**sought
, int nr_sought
,
1506 struct oid_array
*shallow
,
1507 char **pack_lockfile
,
1508 enum protocol_version version
)
1510 struct ref
*ref_cpy
;
1511 struct shallow_info si
;
1515 nr_sought
= remove_duplicates_in_refs(sought
, nr_sought
);
1518 packet_flush(fd
[1]);
1519 die(_("no matching remote head"));
1521 prepare_shallow_info(&si
, shallow
);
1522 if (version
== protocol_v2
)
1523 ref_cpy
= do_fetch_pack_v2(args
, fd
, ref
, sought
, nr_sought
,
1526 ref_cpy
= do_fetch_pack(args
, fd
, ref
, sought
, nr_sought
,
1527 &si
, pack_lockfile
);
1528 reprepare_packed_git(the_repository
);
1529 update_shallow(args
, sought
, nr_sought
, &si
);
1530 clear_shallow_info(&si
);
1534 int report_unmatched_refs(struct ref
**sought
, int nr_sought
)
1538 for (i
= 0; i
< nr_sought
; i
++) {
1541 switch (sought
[i
]->match_status
) {
1544 case REF_NOT_MATCHED
:
1545 error(_("no such remote ref %s"), sought
[i
]->name
);
1547 case REF_UNADVERTISED_NOT_ALLOWED
:
1548 error(_("Server does not allow request for unadvertised object %s"),