Skip to content

Commit 7043c70

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3be89f9 commit 7043c70

File tree

5 files changed

+47
-45
lines changed

5 files changed

+47
-45
lines changed

builtin/clone.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,13 @@ static void update_remote_refs(const struct ref *refs,
624624
const struct ref *rm = mapped_refs;
625625

626626
if (check_connectivity) {
627+
struct check_connected_options opt = CHECK_CONNECTED_INIT;
628+
629+
opt.transport = transport;
630+
627631
if (transport->progress)
628632
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))
631634
die(_("remote did not send all necessary objects"));
632635
if (transport->progress)
633636
fprintf(stderr, _("done.\n"));

builtin/fetch.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
729729
url = xstrdup("foreign");
730730

731731
rm = ref_map;
732-
if (check_everything_connected(iterate_ref_map, 0, &rm)) {
732+
if (check_connected(iterate_ref_map, &rm, NULL)) {
733733
rc = error(_("%s did not send all necessary objects\n"), url);
734734
goto abort;
735735
}
@@ -866,6 +866,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
866866
static int quickfetch(struct ref *ref_map)
867867
{
868868
struct ref *rm = ref_map;
869+
struct check_connected_options opt = CHECK_CONNECTED_INIT;
869870

870871
/*
871872
* If we are deepening a shallow clone we already have these
@@ -876,7 +877,8 @@ static int quickfetch(struct ref *ref_map)
876877
*/
877878
if (depth)
878879
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);
880882
}
881883

882884
static int fetch_refs(struct transport *transport, struct ref *ref_map)

builtin/receive-pack.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
737737
{
738738
static struct lock_file shallow_lock;
739739
struct sha1_array extra = SHA1_ARRAY_INIT;
740-
const char *alt_file;
740+
struct check_connected_options opt = CHECK_CONNECTED_INIT;
741741
uint32_t mask = 1 << (cmd->index % 32);
742742
int i;
743743

@@ -749,9 +749,8 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
749749
!delayed_reachability_test(si, i))
750750
sha1_array_append(&extra, si->shallow->sha1[i]);
751751

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)) {
755754
rollback_lock_file(&shallow_lock);
756755
sha1_array_clear(&extra);
757756
return -1;
@@ -1160,8 +1159,8 @@ static void set_connectivity_errors(struct command *commands,
11601159
if (shallow_update && si->shallow_ref[cmd->index])
11611160
/* to be checked in update_shallow_ref() */
11621161
continue;
1163-
if (!check_everything_connected(command_singleton_iterator,
1164-
0, &singleton))
1162+
if (!check_connected(command_singleton_iterator, &singleton,
1163+
NULL))
11651164
continue;
11661165
cmd->error_string = "missing necessary objects";
11671166
}
@@ -1330,7 +1329,7 @@ static void execute_commands(struct command *commands,
13301329

13311330
data.cmds = commands;
13321331
data.si = si;
1333-
if (check_everything_connected(iterate_receive_command_list, 0, &data))
1332+
if (check_connected(iterate_receive_command_list, &data, NULL))
13341333
set_connectivity_errors(commands, si);
13351334

13361335
reject_updates_to_hidden(commands);

connected.c

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
#include "connected.h"
55
#include "transport.h"
66

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-
}
117
/*
128
* If we feed all the commits we want to verify to this command
139
*
@@ -19,19 +15,22 @@ int check_everything_connected(sha1_iterate_fn fn, int quiet, void *cb_data)
1915
*
2016
* Returns 0 if everything is connected, non-zero otherwise.
2117
*/
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)
2720
{
2821
struct child_process rev_list = CHILD_PROCESS_INIT;
22+
struct check_connected_options defaults = CHECK_CONNECTED_INIT;
2923
char commit[41];
3024
unsigned char sha1[20];
3125
int err = 0;
3226
struct packed_git *new_pack = NULL;
27+
struct transport *transport;
3328
size_t base_len;
3429

30+
if (!opt)
31+
opt = &defaults;
32+
transport = opt->transport;
33+
3534
if (fn(cb_data, sha1))
3635
return err;
3736

@@ -46,9 +45,9 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
4645
strbuf_release(&idx_file);
4746
}
4847

49-
if (shallow_file) {
48+
if (opt->shallow_file) {
5049
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);
5251
}
5352
argv_array_push(&rev_list.args,"rev-list");
5453
argv_array_push(&rev_list.args, "--objects");
@@ -60,7 +59,7 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
6059
rev_list.git_cmd = 1;
6160
rev_list.in = -1;
6261
rev_list.no_stdout = 1;
63-
rev_list.no_stderr = quiet;
62+
rev_list.no_stderr = opt->quiet;
6463
if (start_command(&rev_list))
6564
return error(_("Could not run 'git rev-list'"));
6665

@@ -94,19 +93,3 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
9493
sigchain_pop(SIGPIPE);
9594
return finish_command(&rev_list) || err;
9695
}
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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,33 @@ struct transport;
1010
*/
1111
typedef int (*sha1_iterate_fn)(void *, unsigned char [20]);
1212

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+
1330
/*
1431
* Make sure that our object store has all the commits necessary to
1532
* connect the ancestry chain to some of our existing refs, and all
1633
* the trees and blobs that these commits use.
1734
*
1835
* 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.
1938
*/
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);
2641

2742
#endif /* CONNECTED_H */

0 commit comments

Comments
 (0)