Merge branch 'js/xread-in-full' into maint
[git] / connected.c
1 #include "cache.h"
2 #include "run-command.h"
3 #include "sigchain.h"
4 #include "connected.h"
5 #include "transport.h"
6
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 }
11 /*
12  * If we feed all the commits we want to verify to this command
13  *
14  *  $ git rev-list --objects --stdin --not --all
15  *
16  * and if it does not error out, that means everything reachable from
17  * these commits locally exists and is connected to our existing refs.
18  * Note that this does _not_ validate the individual objects.
19  *
20  * Returns 0 if everything is connected, non-zero otherwise.
21  */
22 int check_everything_connected_with_transport(sha1_iterate_fn fn,
23                                               int quiet,
24                                               void *cb_data,
25                                               struct transport *transport)
26 {
27         struct child_process rev_list;
28         const char *argv[] = {"rev-list", "--objects",
29                               "--stdin", "--not", "--all", NULL, NULL};
30         char commit[41];
31         unsigned char sha1[20];
32         int err = 0;
33         struct packed_git *new_pack = NULL;
34
35         if (fn(cb_data, sha1))
36                 return err;
37
38         if (transport && transport->smart_options &&
39             transport->smart_options->self_contained_and_connected &&
40             transport->pack_lockfile &&
41             !suffixcmp(transport->pack_lockfile, ".keep")) {
42                 struct strbuf idx_file = STRBUF_INIT;
43                 strbuf_addstr(&idx_file, transport->pack_lockfile);
44                 strbuf_setlen(&idx_file, idx_file.len - 5); /* ".keep" */
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
50         if (quiet)
51                 argv[5] = "--quiet";
52
53         memset(&rev_list, 0, sizeof(rev_list));
54         rev_list.argv = argv;
55         rev_list.git_cmd = 1;
56         rev_list.in = -1;
57         rev_list.no_stdout = 1;
58         rev_list.no_stderr = quiet;
59         if (start_command(&rev_list))
60                 return error(_("Could not run 'git rev-list'"));
61
62         sigchain_push(SIGPIPE, SIG_IGN);
63
64         commit[40] = '\n';
65         do {
66                 /*
67                  * If index-pack already checked that:
68                  * - there are no dangling pointers in the new pack
69                  * - the pack is self contained
70                  * Then if the updated ref is in the new pack, then we
71                  * are sure the ref is good and not sending it to
72                  * rev-list for verification.
73                  */
74                 if (new_pack && find_pack_entry_one(sha1, new_pack))
75                         continue;
76
77                 memcpy(commit, sha1_to_hex(sha1), 40);
78                 if (write_in_full(rev_list.in, commit, 41) < 0) {
79                         if (errno != EPIPE && errno != EINVAL)
80                                 error(_("failed write to rev-list: %s"),
81                                       strerror(errno));
82                         err = -1;
83                         break;
84                 }
85         } while (!fn(cb_data, sha1));
86
87         if (close(rev_list.in)) {
88                 error(_("failed to close rev-list's stdin: %s"), strerror(errno));
89                 err = -1;
90         }
91
92         sigchain_pop(SIGPIPE);
93         return finish_command(&rev_list) || err;
94 }