init-db, clone: change unnecessary global into passed parameter
[git.git] / connected.c
blobd672521da41d524f49556aefc29427b20836b8d2
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "hex.h"
4 #include "object-store.h"
5 #include "run-command.h"
6 #include "sigchain.h"
7 #include "connected.h"
8 #include "transport.h"
9 #include "packfile.h"
10 #include "promisor-remote.h"
13 * If we feed all the commits we want to verify to this command
15 * $ git rev-list --objects --stdin --not --all
17 * and if it does not error out, that means everything reachable from
18 * these commits locally exists and is connected to our existing refs.
19 * Note that this does _not_ validate the individual objects.
21 * Returns 0 if everything is connected, non-zero otherwise.
23 int check_connected(oid_iterate_fn fn, void *cb_data,
24 struct check_connected_options *opt)
26 struct child_process rev_list = CHILD_PROCESS_INIT;
27 FILE *rev_list_in;
28 struct check_connected_options defaults = CHECK_CONNECTED_INIT;
29 const struct object_id *oid;
30 int err = 0;
31 struct packed_git *new_pack = NULL;
32 struct transport *transport;
33 size_t base_len;
35 if (!opt)
36 opt = &defaults;
37 transport = opt->transport;
39 oid = fn(cb_data);
40 if (!oid) {
41 if (opt->err_fd)
42 close(opt->err_fd);
43 return err;
46 if (transport && transport->smart_options &&
47 transport->smart_options->self_contained_and_connected &&
48 transport->pack_lockfiles.nr == 1 &&
49 strip_suffix(transport->pack_lockfiles.items[0].string,
50 ".keep", &base_len)) {
51 struct strbuf idx_file = STRBUF_INIT;
52 strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
53 base_len);
54 strbuf_addstr(&idx_file, ".idx");
55 new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
56 strbuf_release(&idx_file);
59 if (repo_has_promisor_remote(the_repository)) {
61 * For partial clones, we don't want to have to do a regular
62 * connectivity check because we have to enumerate and exclude
63 * all promisor objects (slow), and then the connectivity check
64 * itself becomes a no-op because in a partial clone every
65 * object is a promisor object. Instead, just make sure we
66 * received, in a promisor packfile, the objects pointed to by
67 * each wanted ref.
69 * Before checking for promisor packs, be sure we have the
70 * latest pack-files loaded into memory.
72 reprepare_packed_git(the_repository);
73 do {
74 struct packed_git *p;
76 for (p = get_all_packs(the_repository); p; p = p->next) {
77 if (!p->pack_promisor)
78 continue;
79 if (find_pack_entry_one(oid->hash, p))
80 goto promisor_pack_found;
83 * Fallback to rev-list with oid and the rest of the
84 * object IDs provided by fn.
86 goto no_promisor_pack_found;
87 promisor_pack_found:
89 } while ((oid = fn(cb_data)) != NULL);
90 free(new_pack);
91 return 0;
94 no_promisor_pack_found:
95 if (opt->shallow_file) {
96 strvec_push(&rev_list.args, "--shallow-file");
97 strvec_push(&rev_list.args, opt->shallow_file);
99 strvec_push(&rev_list.args,"rev-list");
100 strvec_push(&rev_list.args, "--objects");
101 strvec_push(&rev_list.args, "--stdin");
102 if (repo_has_promisor_remote(the_repository))
103 strvec_push(&rev_list.args, "--exclude-promisor-objects");
104 if (!opt->is_deepening_fetch) {
105 strvec_push(&rev_list.args, "--not");
106 if (opt->exclude_hidden_refs_section)
107 strvec_pushf(&rev_list.args, "--exclude-hidden=%s",
108 opt->exclude_hidden_refs_section);
109 strvec_push(&rev_list.args, "--all");
111 strvec_push(&rev_list.args, "--quiet");
112 strvec_push(&rev_list.args, "--alternate-refs");
113 if (opt->progress)
114 strvec_pushf(&rev_list.args, "--progress=%s",
115 _("Checking connectivity"));
117 rev_list.git_cmd = 1;
118 if (opt->env)
119 strvec_pushv(&rev_list.env, opt->env);
120 rev_list.in = -1;
121 rev_list.no_stdout = 1;
122 if (opt->err_fd)
123 rev_list.err = opt->err_fd;
124 else
125 rev_list.no_stderr = opt->quiet;
127 if (start_command(&rev_list)) {
128 free(new_pack);
129 return error(_("Could not run 'git rev-list'"));
132 sigchain_push(SIGPIPE, SIG_IGN);
134 rev_list_in = xfdopen(rev_list.in, "w");
136 do {
138 * If index-pack already checked that:
139 * - there are no dangling pointers in the new pack
140 * - the pack is self contained
141 * Then if the updated ref is in the new pack, then we
142 * are sure the ref is good and not sending it to
143 * rev-list for verification.
145 if (new_pack && find_pack_entry_one(oid->hash, new_pack))
146 continue;
148 if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0)
149 break;
150 } while ((oid = fn(cb_data)) != NULL);
152 if (ferror(rev_list_in) || fflush(rev_list_in)) {
153 if (errno != EPIPE && errno != EINVAL)
154 error_errno(_("failed write to rev-list"));
155 err = -1;
158 if (fclose(rev_list_in))
159 err = error_errno(_("failed to close rev-list's stdin"));
161 sigchain_pop(SIGPIPE);
162 free(new_pack);
163 return finish_command(&rev_list) || err;