t5801: skip without hg
[git/dscho.git] / connected.c
blob1e89c1cd1d63f160bf7c95de41be61ff2d888e70
1 #include "cache.h"
2 #include "run-command.h"
3 #include "sigchain.h"
4 #include "connected.h"
6 /*
7  * If we feed all the commits we want to verify to this command
8  *
9  *  $ git rev-list --objects --stdin --not --all
10  *
11  * and if it does not error out, that means everything reachable from
12  * these commits locally exists and is connected to our existing refs.
13  * Note that this does _not_ validate the individual objects.
14  *
15  * Returns 0 if everything is connected, non-zero otherwise.
16  */
17 int check_everything_connected(sha1_iterate_fn fn, int quiet, void *cb_data)
19         struct child_process rev_list;
20         const char *argv[] = {"rev-list", "--objects",
21                               "--stdin", "--not", "--all", NULL, NULL};
22         char commit[41];
23         unsigned char sha1[20];
24         int err = 0;
26         if (fn(cb_data, sha1))
27                 return err;
29         if (quiet)
30                 argv[5] = "--quiet";
32         memset(&rev_list, 0, sizeof(rev_list));
33         rev_list.argv = argv;
34         rev_list.git_cmd = 1;
35         rev_list.in = -1;
36         rev_list.no_stdout = 1;
37         rev_list.no_stderr = quiet;
38         if (start_command(&rev_list))
39                 return error(_("Could not run 'git rev-list'"));
41         sigchain_push(SIGPIPE, SIG_IGN);
43         commit[40] = '\n';
44         do {
45                 memcpy(commit, sha1_to_hex(sha1), 40);
46                 if (write_in_full(rev_list.in, commit, 41) < 0) {
47                         if (errno != EPIPE && errno != EINVAL)
48                                 error(_("failed write to rev-list: %s"),
49                                       strerror(errno));
50                         err = -1;
51                         break;
52                 }
53         } while (!fn(cb_data, sha1));
55         if (close(rev_list.in)) {
56                 error(_("failed to close rev-list's stdin: %s"), strerror(errno));
57                 err = -1;
58         }
60         sigchain_pop(SIGPIPE);
61         return finish_command(&rev_list) || err;