]> git.ipfire.org Git - thirdparty/git.git/blob - connected.c
Merge branch 'ds/ahead-behind'
[thirdparty/git.git] / connected.c
1 #include "cache.h"
2 #include "hex.h"
3 #include "object-store.h"
4 #include "run-command.h"
5 #include "sigchain.h"
6 #include "connected.h"
7 #include "transport.h"
8 #include "packfile.h"
9 #include "promisor-remote.h"
10
11 /*
12 * If we feed all the commits we want to verify to this command
13 *
14 * $ git rev-list --objects --stdin --not --all
15 *
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.
19 *
20 * Returns 0 if everything is connected, non-zero otherwise.
21 */
22 int check_connected(oid_iterate_fn fn, void *cb_data,
23 struct check_connected_options *opt)
24 {
25 struct child_process rev_list = CHILD_PROCESS_INIT;
26 FILE *rev_list_in;
27 struct check_connected_options defaults = CHECK_CONNECTED_INIT;
28 const struct object_id *oid;
29 int err = 0;
30 struct packed_git *new_pack = NULL;
31 struct transport *transport;
32 size_t base_len;
33
34 if (!opt)
35 opt = &defaults;
36 transport = opt->transport;
37
38 oid = fn(cb_data);
39 if (!oid) {
40 if (opt->err_fd)
41 close(opt->err_fd);
42 return err;
43 }
44
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,
52 base_len);
53 strbuf_addstr(&idx_file, ".idx");
54 new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
55 strbuf_release(&idx_file);
56 }
57
58 if (has_promisor_remote()) {
59 /*
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
66 * each wanted ref.
67 *
68 * Before checking for promisor packs, be sure we have the
69 * latest pack-files loaded into memory.
70 */
71 reprepare_packed_git(the_repository);
72 do {
73 struct packed_git *p;
74
75 for (p = get_all_packs(the_repository); p; p = p->next) {
76 if (!p->pack_promisor)
77 continue;
78 if (find_pack_entry_one(oid->hash, p))
79 goto promisor_pack_found;
80 }
81 /*
82 * Fallback to rev-list with oid and the rest of the
83 * object IDs provided by fn.
84 */
85 goto no_promisor_pack_found;
86 promisor_pack_found:
87 ;
88 } while ((oid = fn(cb_data)) != NULL);
89 free(new_pack);
90 return 0;
91 }
92
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);
97 }
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");
109 }
110 strvec_push(&rev_list.args, "--quiet");
111 strvec_push(&rev_list.args, "--alternate-refs");
112 if (opt->progress)
113 strvec_pushf(&rev_list.args, "--progress=%s",
114 _("Checking connectivity"));
115
116 rev_list.git_cmd = 1;
117 if (opt->env)
118 strvec_pushv(&rev_list.env, opt->env);
119 rev_list.in = -1;
120 rev_list.no_stdout = 1;
121 if (opt->err_fd)
122 rev_list.err = opt->err_fd;
123 else
124 rev_list.no_stderr = opt->quiet;
125
126 if (start_command(&rev_list)) {
127 free(new_pack);
128 return error(_("Could not run 'git rev-list'"));
129 }
130
131 sigchain_push(SIGPIPE, SIG_IGN);
132
133 rev_list_in = xfdopen(rev_list.in, "w");
134
135 do {
136 /*
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.
143 */
144 if (new_pack && find_pack_entry_one(oid->hash, new_pack))
145 continue;
146
147 if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0)
148 break;
149 } while ((oid = fn(cb_data)) != NULL);
150
151 if (ferror(rev_list_in) || fflush(rev_list_in)) {
152 if (errno != EPIPE && errno != EINVAL)
153 error_errno(_("failed write to rev-list"));
154 err = -1;
155 }
156
157 if (fclose(rev_list_in))
158 err = error_errno(_("failed to close rev-list's stdin"));
159
160 sigchain_pop(SIGPIPE);
161 free(new_pack);
162 return finish_command(&rev_list) || err;
163 }