]>
Commit | Line | Data |
---|---|---|
f96400cb | 1 | #include "cache.h" |
dfa33a29 | 2 | #include "object-store.h" |
f96400cb JH |
3 | #include "run-command.h" |
4 | #include "sigchain.h" | |
5 | #include "connected.h" | |
c6807a40 | 6 | #include "transport.h" |
0abe14f6 | 7 | #include "packfile.h" |
b14ed5ad | 8 | #include "promisor-remote.h" |
f96400cb JH |
9 | |
10 | /* | |
11 | * If we feed all the commits we want to verify to this command | |
12 | * | |
d21c463d | 13 | * $ git rev-list --objects --stdin --not --all |
f96400cb JH |
14 | * |
15 | * and if it does not error out, that means everything reachable from | |
d21c463d JH |
16 | * these commits locally exists and is connected to our existing refs. |
17 | * Note that this does _not_ validate the individual objects. | |
f96400cb JH |
18 | * |
19 | * Returns 0 if everything is connected, non-zero otherwise. | |
20 | */ | |
6ccac9ee | 21 | int check_connected(oid_iterate_fn fn, void *cb_data, |
7043c707 | 22 | struct check_connected_options *opt) |
f96400cb | 23 | { |
d3180279 | 24 | struct child_process rev_list = CHILD_PROCESS_INIT; |
7043c707 | 25 | struct check_connected_options defaults = CHECK_CONNECTED_INIT; |
6ccac9ee | 26 | char commit[GIT_MAX_HEXSZ + 1]; |
27 | struct object_id oid; | |
3be89f9b | 28 | int err = 0; |
c6807a40 | 29 | struct packed_git *new_pack = NULL; |
7043c707 | 30 | struct transport *transport; |
26936bfd | 31 | size_t base_len; |
9d958cc0 | 32 | const unsigned hexsz = the_hash_algo->hexsz; |
f96400cb | 33 | |
7043c707 JK |
34 | if (!opt) |
35 | opt = &defaults; | |
36 | transport = opt->transport; | |
37 | ||
6ccac9ee | 38 | if (fn(cb_data, &oid)) { |
e0331849 JK |
39 | if (opt->err_fd) |
40 | close(opt->err_fd); | |
f96400cb | 41 | return err; |
e0331849 | 42 | } |
f96400cb | 43 | |
c6807a40 NTND |
44 | if (transport && transport->smart_options && |
45 | transport->smart_options->self_contained_and_connected && | |
46 | transport->pack_lockfile && | |
26936bfd | 47 | strip_suffix(transport->pack_lockfile, ".keep", &base_len)) { |
c6807a40 | 48 | struct strbuf idx_file = STRBUF_INIT; |
26936bfd | 49 | strbuf_add(&idx_file, transport->pack_lockfile, base_len); |
c6807a40 NTND |
50 | strbuf_addstr(&idx_file, ".idx"); |
51 | new_pack = add_packed_git(idx_file.buf, idx_file.len, 1); | |
52 | strbuf_release(&idx_file); | |
53 | } | |
54 | ||
2b98478c | 55 | if (has_promisor_remote()) { |
dfa33a29 JS |
56 | /* |
57 | * For partial clones, we don't want to have to do a regular | |
58 | * connectivity check because we have to enumerate and exclude | |
59 | * all promisor objects (slow), and then the connectivity check | |
60 | * itself becomes a no-op because in a partial clone every | |
61 | * object is a promisor object. Instead, just make sure we | |
50033772 JT |
62 | * received, in a promisor packfile, the objects pointed to by |
63 | * each wanted ref. | |
b739d971 DS |
64 | * |
65 | * Before checking for promisor packs, be sure we have the | |
66 | * latest pack-files loaded into memory. | |
dfa33a29 | 67 | */ |
b739d971 | 68 | reprepare_packed_git(the_repository); |
dfa33a29 | 69 | do { |
50033772 JT |
70 | struct packed_git *p; |
71 | ||
72 | for (p = get_all_packs(the_repository); p; p = p->next) { | |
73 | if (!p->pack_promisor) | |
74 | continue; | |
75 | if (find_pack_entry_one(oid.hash, p)) | |
76 | goto promisor_pack_found; | |
77 | } | |
2b98478c JT |
78 | /* |
79 | * Fallback to rev-list with oid and the rest of the | |
80 | * object IDs provided by fn. | |
81 | */ | |
82 | goto no_promisor_pack_found; | |
50033772 JT |
83 | promisor_pack_found: |
84 | ; | |
dfa33a29 JS |
85 | } while (!fn(cb_data, &oid)); |
86 | return 0; | |
87 | } | |
88 | ||
2b98478c | 89 | no_promisor_pack_found: |
7043c707 | 90 | if (opt->shallow_file) { |
3be89f9b | 91 | argv_array_push(&rev_list.args, "--shallow-file"); |
7043c707 | 92 | argv_array_push(&rev_list.args, opt->shallow_file); |
614db3e2 | 93 | } |
3be89f9b JK |
94 | argv_array_push(&rev_list.args,"rev-list"); |
95 | argv_array_push(&rev_list.args, "--objects"); | |
96 | argv_array_push(&rev_list.args, "--stdin"); | |
b14ed5ad | 97 | if (has_promisor_remote()) |
acb0c572 | 98 | argv_array_push(&rev_list.args, "--exclude-promisor-objects"); |
cf1e7c07 JT |
99 | if (!opt->is_deepening_fetch) { |
100 | argv_array_push(&rev_list.args, "--not"); | |
101 | argv_array_push(&rev_list.args, "--all"); | |
102 | } | |
3be89f9b | 103 | argv_array_push(&rev_list.args, "--quiet"); |
39b44ba7 | 104 | argv_array_push(&rev_list.args, "--alternate-refs"); |
70d5e2d7 JK |
105 | if (opt->progress) |
106 | argv_array_pushf(&rev_list.args, "--progress=%s", | |
107 | _("Checking connectivity")); | |
f96400cb | 108 | |
f96400cb | 109 | rev_list.git_cmd = 1; |
526f108a | 110 | rev_list.env = opt->env; |
f96400cb JH |
111 | rev_list.in = -1; |
112 | rev_list.no_stdout = 1; | |
e0331849 JK |
113 | if (opt->err_fd) |
114 | rev_list.err = opt->err_fd; | |
115 | else | |
116 | rev_list.no_stderr = opt->quiet; | |
117 | ||
f96400cb JH |
118 | if (start_command(&rev_list)) |
119 | return error(_("Could not run 'git rev-list'")); | |
120 | ||
121 | sigchain_push(SIGPIPE, SIG_IGN); | |
122 | ||
9d958cc0 | 123 | commit[hexsz] = '\n'; |
f96400cb | 124 | do { |
c6807a40 NTND |
125 | /* |
126 | * If index-pack already checked that: | |
127 | * - there are no dangling pointers in the new pack | |
128 | * - the pack is self contained | |
129 | * Then if the updated ref is in the new pack, then we | |
130 | * are sure the ref is good and not sending it to | |
131 | * rev-list for verification. | |
132 | */ | |
6ccac9ee | 133 | if (new_pack && find_pack_entry_one(oid.hash, new_pack)) |
c6807a40 NTND |
134 | continue; |
135 | ||
9d958cc0 | 136 | memcpy(commit, oid_to_hex(&oid), hexsz); |
137 | if (write_in_full(rev_list.in, commit, hexsz + 1) < 0) { | |
f96400cb | 138 | if (errno != EPIPE && errno != EINVAL) |
5cc026e2 | 139 | error_errno(_("failed write to rev-list")); |
f96400cb JH |
140 | err = -1; |
141 | break; | |
142 | } | |
6ccac9ee | 143 | } while (!fn(cb_data, &oid)); |
f96400cb | 144 | |
5cc026e2 NTND |
145 | if (close(rev_list.in)) |
146 | err = error_errno(_("failed to close rev-list's stdin")); | |
f96400cb JH |
147 | |
148 | sigchain_pop(SIGPIPE); | |
149 | return finish_command(&rev_list) || err; | |
150 | } |