3 #include "object-store.h"
4 #include "run-command.h"
9 #include "promisor-remote.h"
12 * If we feed all the commits we want to verify to this command
14 * $ git rev-list --objects --stdin --not --all
16 * and if it does not error out, that means everything reachable from
17 * these commits locally exists and is connected to our existing refs.
18 * Note that this does _not_ validate the individual objects.
20 * Returns 0 if everything is connected, non-zero otherwise.
22 int check_connected(oid_iterate_fn fn
, void *cb_data
,
23 struct check_connected_options
*opt
)
25 struct child_process rev_list
= CHILD_PROCESS_INIT
;
27 struct check_connected_options defaults
= CHECK_CONNECTED_INIT
;
28 const struct object_id
*oid
;
30 struct packed_git
*new_pack
= NULL
;
31 struct transport
*transport
;
36 transport
= opt
->transport
;
45 if (transport
&& transport
->smart_options
&&
46 transport
->smart_options
->self_contained_and_connected
&&
47 transport
->pack_lockfiles
.nr
== 1 &&
48 strip_suffix(transport
->pack_lockfiles
.items
[0].string
,
49 ".keep", &base_len
)) {
50 struct strbuf idx_file
= STRBUF_INIT
;
51 strbuf_add(&idx_file
, transport
->pack_lockfiles
.items
[0].string
,
53 strbuf_addstr(&idx_file
, ".idx");
54 new_pack
= add_packed_git(idx_file
.buf
, idx_file
.len
, 1);
55 strbuf_release(&idx_file
);
58 if (has_promisor_remote()) {
60 * For partial clones, we don't want to have to do a regular
61 * connectivity check because we have to enumerate and exclude
62 * all promisor objects (slow), and then the connectivity check
63 * itself becomes a no-op because in a partial clone every
64 * object is a promisor object. Instead, just make sure we
65 * received, in a promisor packfile, the objects pointed to by
68 * Before checking for promisor packs, be sure we have the
69 * latest pack-files loaded into memory.
71 reprepare_packed_git(the_repository
);
75 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
76 if (!p
->pack_promisor
)
78 if (find_pack_entry_one(oid
->hash
, p
))
79 goto promisor_pack_found
;
82 * Fallback to rev-list with oid and the rest of the
83 * object IDs provided by fn.
85 goto no_promisor_pack_found
;
88 } while ((oid
= fn(cb_data
)) != NULL
);
93 no_promisor_pack_found
:
94 if (opt
->shallow_file
) {
95 strvec_push(&rev_list
.args
, "--shallow-file");
96 strvec_push(&rev_list
.args
, opt
->shallow_file
);
98 strvec_push(&rev_list
.args
,"rev-list");
99 strvec_push(&rev_list
.args
, "--objects");
100 strvec_push(&rev_list
.args
, "--stdin");
101 if (has_promisor_remote())
102 strvec_push(&rev_list
.args
, "--exclude-promisor-objects");
103 if (!opt
->is_deepening_fetch
) {
104 strvec_push(&rev_list
.args
, "--not");
105 if (opt
->exclude_hidden_refs_section
)
106 strvec_pushf(&rev_list
.args
, "--exclude-hidden=%s",
107 opt
->exclude_hidden_refs_section
);
108 strvec_push(&rev_list
.args
, "--all");
110 strvec_push(&rev_list
.args
, "--quiet");
111 strvec_push(&rev_list
.args
, "--alternate-refs");
113 strvec_pushf(&rev_list
.args
, "--progress=%s",
114 _("Checking connectivity"));
116 rev_list
.git_cmd
= 1;
118 strvec_pushv(&rev_list
.env
, opt
->env
);
120 rev_list
.no_stdout
= 1;
122 rev_list
.err
= opt
->err_fd
;
124 rev_list
.no_stderr
= opt
->quiet
;
126 if (start_command(&rev_list
)) {
128 return error(_("Could not run 'git rev-list'"));
131 sigchain_push(SIGPIPE
, SIG_IGN
);
133 rev_list_in
= xfdopen(rev_list
.in
, "w");
137 * If index-pack already checked that:
138 * - there are no dangling pointers in the new pack
139 * - the pack is self contained
140 * Then if the updated ref is in the new pack, then we
141 * are sure the ref is good and not sending it to
142 * rev-list for verification.
144 if (new_pack
&& find_pack_entry_one(oid
->hash
, new_pack
))
147 if (fprintf(rev_list_in
, "%s\n", oid_to_hex(oid
)) < 0)
149 } while ((oid
= fn(cb_data
)) != NULL
);
151 if (ferror(rev_list_in
) || fflush(rev_list_in
)) {
152 if (errno
!= EPIPE
&& errno
!= EINVAL
)
153 error_errno(_("failed write to rev-list"));
157 if (fclose(rev_list_in
))
158 err
= error_errno(_("failed to close rev-list's stdin"));
160 sigchain_pop(SIGPIPE
);
162 return finish_command(&rev_list
) || err
;