Commit | Line | Data |
---|---|---|
f96400cb JH |
1 | #include "cache.h" |
2 | #include "run-command.h" | |
3 | #include "sigchain.h" | |
4 | #include "connected.h" | |
c6807a40 | 5 | #include "transport.h" |
f96400cb | 6 | |
c6807a40 NTND |
7 | int check_everything_connected(sha1_iterate_fn fn, int quiet, void *cb_data) |
8 | { | |
9 | return check_everything_connected_with_transport(fn, quiet, cb_data, NULL); | |
10 | } | |
f96400cb JH |
11 | /* |
12 | * If we feed all the commits we want to verify to this command | |
13 | * | |
d21c463d | 14 | * $ git rev-list --objects --stdin --not --all |
f96400cb JH |
15 | * |
16 | * and if it does not error out, that means everything reachable from | |
d21c463d JH |
17 | * these commits locally exists and is connected to our existing refs. |
18 | * Note that this does _not_ validate the individual objects. | |
f96400cb JH |
19 | * |
20 | * Returns 0 if everything is connected, non-zero otherwise. | |
21 | */ | |
614db3e2 NTND |
22 | static int check_everything_connected_real(sha1_iterate_fn fn, |
23 | int quiet, | |
24 | void *cb_data, | |
25 | struct transport *transport, | |
26 | const char *shallow_file) | |
f96400cb | 27 | { |
d3180279 | 28 | struct child_process rev_list = CHILD_PROCESS_INIT; |
f96400cb JH |
29 | char commit[41]; |
30 | unsigned char sha1[20]; | |
3be89f9b | 31 | int err = 0; |
c6807a40 | 32 | struct packed_git *new_pack = NULL; |
26936bfd | 33 | size_t base_len; |
f96400cb JH |
34 | |
35 | if (fn(cb_data, sha1)) | |
36 | return err; | |
37 | ||
c6807a40 NTND |
38 | if (transport && transport->smart_options && |
39 | transport->smart_options->self_contained_and_connected && | |
40 | transport->pack_lockfile && | |
26936bfd | 41 | strip_suffix(transport->pack_lockfile, ".keep", &base_len)) { |
c6807a40 | 42 | struct strbuf idx_file = STRBUF_INIT; |
26936bfd | 43 | strbuf_add(&idx_file, transport->pack_lockfile, base_len); |
c6807a40 NTND |
44 | strbuf_addstr(&idx_file, ".idx"); |
45 | new_pack = add_packed_git(idx_file.buf, idx_file.len, 1); | |
46 | strbuf_release(&idx_file); | |
47 | } | |
48 | ||
614db3e2 | 49 | if (shallow_file) { |
3be89f9b JK |
50 | argv_array_push(&rev_list.args, "--shallow-file"); |
51 | argv_array_push(&rev_list.args, shallow_file); | |
614db3e2 | 52 | } |
3be89f9b JK |
53 | argv_array_push(&rev_list.args,"rev-list"); |
54 | argv_array_push(&rev_list.args, "--objects"); | |
55 | argv_array_push(&rev_list.args, "--stdin"); | |
56 | argv_array_push(&rev_list.args, "--not"); | |
57 | argv_array_push(&rev_list.args, "--all"); | |
58 | argv_array_push(&rev_list.args, "--quiet"); | |
f96400cb | 59 | |
f96400cb JH |
60 | rev_list.git_cmd = 1; |
61 | rev_list.in = -1; | |
62 | rev_list.no_stdout = 1; | |
63 | rev_list.no_stderr = quiet; | |
64 | if (start_command(&rev_list)) | |
65 | return error(_("Could not run 'git rev-list'")); | |
66 | ||
67 | sigchain_push(SIGPIPE, SIG_IGN); | |
68 | ||
69 | commit[40] = '\n'; | |
70 | do { | |
c6807a40 NTND |
71 | /* |
72 | * If index-pack already checked that: | |
73 | * - there are no dangling pointers in the new pack | |
74 | * - the pack is self contained | |
75 | * Then if the updated ref is in the new pack, then we | |
76 | * are sure the ref is good and not sending it to | |
77 | * rev-list for verification. | |
78 | */ | |
79 | if (new_pack && find_pack_entry_one(sha1, new_pack)) | |
80 | continue; | |
81 | ||
f96400cb JH |
82 | memcpy(commit, sha1_to_hex(sha1), 40); |
83 | if (write_in_full(rev_list.in, commit, 41) < 0) { | |
84 | if (errno != EPIPE && errno != EINVAL) | |
5cc026e2 | 85 | error_errno(_("failed write to rev-list")); |
f96400cb JH |
86 | err = -1; |
87 | break; | |
88 | } | |
89 | } while (!fn(cb_data, sha1)); | |
90 | ||
5cc026e2 NTND |
91 | if (close(rev_list.in)) |
92 | err = error_errno(_("failed to close rev-list's stdin")); | |
f96400cb JH |
93 | |
94 | sigchain_pop(SIGPIPE); | |
95 | return finish_command(&rev_list) || err; | |
96 | } | |
614db3e2 NTND |
97 | |
98 | int check_everything_connected_with_transport(sha1_iterate_fn fn, | |
99 | int quiet, | |
100 | void *cb_data, | |
101 | struct transport *transport) | |
102 | { | |
103 | return check_everything_connected_real(fn, quiet, cb_data, | |
104 | transport, NULL); | |
105 | } | |
106 | ||
107 | int check_shallow_connected(sha1_iterate_fn fn, int quiet, void *cb_data, | |
108 | const char *shallow_file) | |
109 | { | |
110 | return check_everything_connected_real(fn, quiet, cb_data, | |
111 | NULL, shallow_file); | |
112 | } |