check_everything_connected: use a struct with named options

The number of variants of check_everything_connected has grown over the years, so that the "real" function takes several possibly-zero, possibly-NULL arguments. We hid the complexity behind some wrapper functions, but this doesn't scale well when we want to add new options. If we add more wrapper variants to handle the new options, then we can get a combinatorial explosion when those options might be used together (right now nobody wants to use both "shallow" and "transport" together, so we get by with just a few wrappers). If instead we add new parameters to each function, each of which can have a default value, then callers who want the defaults end up with confusing invocations like: check_everything_connected(fn, 0, data, -1, 0, NULL); where it is unclear which parameter is which (and every caller needs updated when we add new options). Instead, let's add a struct to hold all of the optional parameters. This is a little more verbose for the callers (who have to declare the struct and fill it in), but it makes their code much easier to follow, because every option is named as it is set (and unused options do not have to be mentioned at all). Note that we could also stick the iteration function and its callback data into the option struct, too. But since those are required for each call, by avoiding doing so, we can let very simple callers just pass "NULL" for the options and not worry about the struct at all. While we're touching each site, let's also rename the function to check_connected(). The existing name was quite long, and not all of the wrappers even used the full name. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 15, 2016 at 06:30 UTC 7043c7071cdbdef60ce8413f425da622b8e6dc85
5 files changed +47 -45
builtin/clone.c
+5 -2
@@ -624,10 +624,13 @@ static void update_remote_refs(const struct ref *refs,
624 const struct ref *rm = mapped_refs;
625
626 if (check_connectivity) {
627 + struct check_connected_options opt = CHECK_CONNECTED_INIT;
628 +
629 + opt.transport = transport;
630 +
631 if (transport->progress)
632 fprintf(stderr, _("Checking connectivity... "));
629 - if (check_everything_connected_with_transport(iterate_ref_map,
630 - 0, &rm, transport))
633 + if (check_connected(iterate_ref_map, &rm, &opt))
634 die(_("remote did not send all necessary objects"));
635 if (transport->progress)
636 fprintf(stderr, _("done.\n"));
builtin/fetch.c
+4 -2
@@ -729,7 +729,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
729 url = xstrdup("foreign");
730
731 rm = ref_map;
732 - if (check_everything_connected(iterate_ref_map, 0, &rm)) {
732 + if (check_connected(iterate_ref_map, &rm, NULL)) {
733 rc = error(_("%s did not send all necessary objects\n"), url);
734 goto abort;
735 }
@@ -866,6 +866,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
866 static int quickfetch(struct ref *ref_map)
867 {
868 struct ref *rm = ref_map;
869 + struct check_connected_options opt = CHECK_CONNECTED_INIT;
870
871 /*
872 * If we are deepening a shallow clone we already have these
@@ -876,7 +877,8 @@ static int quickfetch(struct ref *ref_map)
877 */
878 if (depth)
879 return -1;
879 - return check_everything_connected(iterate_ref_map, 1, &rm);
880 + opt.quiet = 1;
881 + return check_connected(iterate_ref_map, &rm, &opt);
882 }
883
884 static int fetch_refs(struct transport *transport, struct ref *ref_map)
builtin/receive-pack.c
+6 -7
@@ -737,7 +737,7 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
737 {
738 static struct lock_file shallow_lock;
739 struct sha1_array extra = SHA1_ARRAY_INIT;
740 - const char *alt_file;
740 + struct check_connected_options opt = CHECK_CONNECTED_INIT;
741 uint32_t mask = 1 << (cmd->index % 32);
742 int i;
743
@@ -749,9 +749,8 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
749 !delayed_reachability_test(si, i))
750 sha1_array_append(&extra, si->shallow->sha1[i]);
751
752 - setup_alternate_shallow(&shallow_lock, &alt_file, &extra);
753 - if (check_shallow_connected(command_singleton_iterator,
754 - 0, cmd, alt_file)) {
752 + setup_alternate_shallow(&shallow_lock, &opt.shallow_file, &extra);
753 + if (check_connected(command_singleton_iterator, cmd, &opt)) {
754 rollback_lock_file(&shallow_lock);
755 sha1_array_clear(&extra);
756 return -1;
@@ -1160,8 +1159,8 @@ static void set_connectivity_errors(struct command *commands,
1159 if (shallow_update && si->shallow_ref[cmd->index])
1160 /* to be checked in update_shallow_ref() */
1161 continue;
1163 - if (!check_everything_connected(command_singleton_iterator,
1164 - 0, &singleton))
1162 + if (!check_connected(command_singleton_iterator, &singleton,
1163 + NULL))
1164 continue;
1165 cmd->error_string = "missing necessary objects";
1166 }
@@ -1330,7 +1329,7 @@ static void execute_commands(struct command *commands,
1329
1330 data.cmds = commands;
1331 data.si = si;
1333 - if (check_everything_connected(iterate_receive_command_list, 0, &data))
1332 + if (check_connected(iterate_receive_command_list, &data, NULL))
1333 set_connectivity_errors(commands, si);
1334
1335 reject_updates_to_hidden(commands);
connected.c
+11 -28
@@ -4,10 +4,6 @@
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 -}
7 /*
8 * If we feed all the commits we want to verify to this command
9 *
@@ -19,19 +15,22 @@ int check_everything_connected(sha1_iterate_fn fn, int quiet, void *cb_data)
15 *
16 * Returns 0 if everything is connected, non-zero otherwise.
17 */
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)
18 +int check_connected(sha1_iterate_fn fn, void *cb_data,
19 + struct check_connected_options *opt)
20 {
21 struct child_process rev_list = CHILD_PROCESS_INIT;
22 + struct check_connected_options defaults = CHECK_CONNECTED_INIT;
23 char commit[41];
24 unsigned char sha1[20];
25 int err = 0;
26 struct packed_git *new_pack = NULL;
27 + struct transport *transport;
28 size_t base_len;
29
30 + if (!opt)
31 + opt = &defaults;
32 + transport = opt->transport;
33 +
34 if (fn(cb_data, sha1))
35 return err;
36
@@ -46,9 +45,9 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
45 strbuf_release(&idx_file);
46 }
47
49 - if (shallow_file) {
48 + if (opt->shallow_file) {
49 argv_array_push(&rev_list.args, "--shallow-file");
51 - argv_array_push(&rev_list.args, shallow_file);
50 + argv_array_push(&rev_list.args, opt->shallow_file);
51 }
52 argv_array_push(&rev_list.args,"rev-list");
53 argv_array_push(&rev_list.args, "--objects");
@@ -60,7 +59,7 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
59 rev_list.git_cmd = 1;
60 rev_list.in = -1;
61 rev_list.no_stdout = 1;
63 - rev_list.no_stderr = quiet;
62 + rev_list.no_stderr = opt->quiet;
63 if (start_command(&rev_list))
64 return error(_("Could not run 'git rev-list'"));
65
@@ -94,19 +93,3 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
93 sigchain_pop(SIGPIPE);
94 return finish_command(&rev_list) || err;
95 }
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 -}
connected.h
+21 -6
@@ -10,18 +10,33 @@ struct transport;
10 */
11 typedef int (*sha1_iterate_fn)(void *, unsigned char [20]);
12
13 +/*
14 + * Named-arguments struct for check_connected. All arguments are
15 + * optional, and can be left to defaults as set by CHECK_CONNECTED_INIT.
16 + */
17 +struct check_connected_options {
18 + /* Avoid printing any errors to stderr. */
19 + int quiet;
20 +
21 + /* --shallow-file to pass to rev-list sub-process */
22 + const char *shallow_file;
23 +
24 + /* Transport whose objects we are checking, if available. */
25 + struct transport *transport;
26 +};
27 +
28 +#define CHECK_CONNECTED_INIT { 0 }
29 +
30 /*
31 * Make sure that our object store has all the commits necessary to
32 * connect the ancestry chain to some of our existing refs, and all
33 * the trees and blobs that these commits use.
34 *
35 * Return 0 if Ok, non zero otherwise (i.e. some missing objects)
36 + *
37 + * If "opt" is NULL, behaves as if CHECK_CONNECTED_INIT was passed.
38 */
20 -extern int check_everything_connected(sha1_iterate_fn, int quiet, void *cb_data);
21 -extern int check_shallow_connected(sha1_iterate_fn, int quiet, void *cb_data,
22 - const char *shallow_file);
23 -extern int check_everything_connected_with_transport(sha1_iterate_fn, int quiet,
24 - void *cb_data,
25 - struct transport *transport);
39 +int check_connected(sha1_iterate_fn fn, void *cb_data,
40 + struct check_connected_options *opt);
41
42 #endif /* CONNECTED_H */