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