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