]> git.ipfire.org Git - thirdparty/git.git/blame - connected.c
builtin/apply: make parse_ignorewhitespace_option() return -1 instead of die()ing
[thirdparty/git.git] / connected.c
CommitLineData
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
7int 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
22static 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;
614db3e2 29 const char *argv[9];
f96400cb
JH
30 char commit[41];
31 unsigned char sha1[20];
614db3e2 32 int err = 0, ac = 0;
c6807a40 33 struct packed_git *new_pack = NULL;
26936bfd 34 size_t base_len;
f96400cb
JH
35
36 if (fn(cb_data, sha1))
37 return err;
38
c6807a40
NTND
39 if (transport && transport->smart_options &&
40 transport->smart_options->self_contained_and_connected &&
41 transport->pack_lockfile &&
26936bfd 42 strip_suffix(transport->pack_lockfile, ".keep", &base_len)) {
c6807a40 43 struct strbuf idx_file = STRBUF_INIT;
26936bfd 44 strbuf_add(&idx_file, transport->pack_lockfile, base_len);
c6807a40
NTND
45 strbuf_addstr(&idx_file, ".idx");
46 new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
47 strbuf_release(&idx_file);
48 }
49
614db3e2
NTND
50 if (shallow_file) {
51 argv[ac++] = "--shallow-file";
52 argv[ac++] = shallow_file;
53 }
54 argv[ac++] = "rev-list";
55 argv[ac++] = "--objects";
56 argv[ac++] = "--stdin";
57 argv[ac++] = "--not";
58 argv[ac++] = "--all";
f96400cb 59 if (quiet)
614db3e2
NTND
60 argv[ac++] = "--quiet";
61 argv[ac] = NULL;
f96400cb 62
f96400cb
JH
63 rev_list.argv = argv;
64 rev_list.git_cmd = 1;
65 rev_list.in = -1;
66 rev_list.no_stdout = 1;
67 rev_list.no_stderr = quiet;
68 if (start_command(&rev_list))
69 return error(_("Could not run 'git rev-list'"));
70
71 sigchain_push(SIGPIPE, SIG_IGN);
72
73 commit[40] = '\n';
74 do {
c6807a40
NTND
75 /*
76 * If index-pack already checked that:
77 * - there are no dangling pointers in the new pack
78 * - the pack is self contained
79 * Then if the updated ref is in the new pack, then we
80 * are sure the ref is good and not sending it to
81 * rev-list for verification.
82 */
83 if (new_pack && find_pack_entry_one(sha1, new_pack))
84 continue;
85
f96400cb
JH
86 memcpy(commit, sha1_to_hex(sha1), 40);
87 if (write_in_full(rev_list.in, commit, 41) < 0) {
88 if (errno != EPIPE && errno != EINVAL)
5cc026e2 89 error_errno(_("failed write to rev-list"));
f96400cb
JH
90 err = -1;
91 break;
92 }
93 } while (!fn(cb_data, sha1));
94
5cc026e2
NTND
95 if (close(rev_list.in))
96 err = error_errno(_("failed to close rev-list's stdin"));
f96400cb
JH
97
98 sigchain_pop(SIGPIPE);
99 return finish_command(&rev_list) || err;
100}
614db3e2
NTND
101
102int check_everything_connected_with_transport(sha1_iterate_fn fn,
103 int quiet,
104 void *cb_data,
105 struct transport *transport)
106{
107 return check_everything_connected_real(fn, quiet, cb_data,
108 transport, NULL);
109}
110
111int check_shallow_connected(sha1_iterate_fn fn, int quiet, void *cb_data,
112 const char *shallow_file)
113{
114 return check_everything_connected_real(fn, quiet, cb_data,
115 NULL, shallow_file);
116}