Skip to content

Commit 58afbe8

Browse files
pks-tgitster
authored andcommitted
fetch: lift up parsing of "fetch.output" config variable
Parsing the display format happens inside of `display_state_init()`. As we only need to check for a simple config entry, this is a natural location to put this code as it means that display-state logic is neatly contained in a single location. We're about to introduce a new "porcelain" output format though that is intended to be parseable by machines, for example inside of a script. This format can be enabled by passing the `--porcelain` switch to git-fetch(1). As a consequence, we'll have to add a second callsite that influences the output format, which will become awkward to handle. Refactor the code such that callers are expected to pass the display format that is to be used into `display_state_init()`. This allows us to lift up the code into the main function, where we can then hook it into command line options parser in a follow-up commit. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5095793 commit 58afbe8

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

builtin/fetch.c

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,14 @@ static int fetch_write_commit_graph = -1;
106106
static int stdin_refspecs = 0;
107107
static int negotiate_only;
108108

109+
struct fetch_config {
110+
enum display_format display_format;
111+
};
112+
109113
static int git_fetch_config(const char *k, const char *v, void *cb)
110114
{
115+
struct fetch_config *fetch_config = cb;
116+
111117
if (!strcmp(k, "fetch.prune")) {
112118
fetch_prune_config = git_config_bool(k, v);
113119
return 0;
@@ -146,6 +152,18 @@ static int git_fetch_config(const char *k, const char *v, void *cb)
146152
return 0;
147153
}
148154

155+
if (!strcmp(k, "fetch.output")) {
156+
if (!v)
157+
return config_error_nonbool(k);
158+
else if (!strcasecmp(v, "full"))
159+
fetch_config->display_format = DISPLAY_FORMAT_FULL;
160+
else if (!strcasecmp(v, "compact"))
161+
fetch_config->display_format = DISPLAY_FORMAT_COMPACT;
162+
else
163+
die(_("invalid value for '%s': '%s'"),
164+
"fetch.output", v);
165+
}
166+
149167
return git_default_config(k, v, cb);
150168
}
151169

@@ -802,14 +820,13 @@ static int refcol_width(const struct ref *ref_map, int compact_format)
802820
}
803821

804822
static void display_state_init(struct display_state *display_state, struct ref *ref_map,
805-
const char *raw_url)
823+
const char *raw_url, enum display_format format)
806824
{
807-
const char *format = "full";
808825
int i;
809826

810827
memset(display_state, 0, sizeof(*display_state));
811-
812828
strbuf_init(&display_state->buf, 0);
829+
display_state->format = format;
813830

814831
if (raw_url)
815832
display_state->url = transport_anonymize_url(raw_url);
@@ -826,15 +843,6 @@ static void display_state_init(struct display_state *display_state, struct ref *
826843
if (verbosity < 0)
827844
return;
828845

829-
git_config_get_string_tmp("fetch.output", &format);
830-
if (!strcasecmp(format, "full"))
831-
display_state->format = DISPLAY_FORMAT_FULL;
832-
else if (!strcasecmp(format, "compact"))
833-
display_state->format = DISPLAY_FORMAT_COMPACT;
834-
else
835-
die(_("invalid value for '%s': '%s'"),
836-
"fetch.output", format);
837-
838846
switch (display_state->format) {
839847
case DISPLAY_FORMAT_FULL:
840848
case DISPLAY_FORMAT_COMPACT:
@@ -1608,7 +1616,8 @@ static int backfill_tags(struct display_state *display_state,
16081616
}
16091617

16101618
static int do_fetch(struct transport *transport,
1611-
struct refspec *rs)
1619+
struct refspec *rs,
1620+
enum display_format display_format)
16121621
{
16131622
struct ref_transaction *transaction = NULL;
16141623
struct ref *ref_map = NULL;
@@ -1694,7 +1703,7 @@ static int do_fetch(struct transport *transport,
16941703
if (retcode)
16951704
goto cleanup;
16961705

1697-
display_state_init(&display_state, ref_map, transport->url);
1706+
display_state_init(&display_state, ref_map, transport->url, display_format);
16981707

16991708
if (atomic_fetch) {
17001709
transaction = ref_transaction_begin(&err);
@@ -2077,7 +2086,8 @@ static inline void fetch_one_setup_partial(struct remote *remote)
20772086
}
20782087

20792088
static int fetch_one(struct remote *remote, int argc, const char **argv,
2080-
int prune_tags_ok, int use_stdin_refspecs)
2089+
int prune_tags_ok, int use_stdin_refspecs,
2090+
enum display_format display_format)
20812091
{
20822092
struct refspec rs = REFSPEC_INIT_FETCH;
20832093
int i;
@@ -2144,7 +2154,7 @@ static int fetch_one(struct remote *remote, int argc, const char **argv,
21442154
sigchain_push_common(unlock_pack_on_signal);
21452155
atexit(unlock_pack_atexit);
21462156
sigchain_push(SIGPIPE, SIG_IGN);
2147-
exit_code = do_fetch(gtransport, &rs);
2157+
exit_code = do_fetch(gtransport, &rs, display_format);
21482158
sigchain_pop(SIGPIPE);
21492159
refspec_clear(&rs);
21502160
transport_disconnect(gtransport);
@@ -2154,6 +2164,9 @@ static int fetch_one(struct remote *remote, int argc, const char **argv,
21542164

21552165
int cmd_fetch(int argc, const char **argv, const char *prefix)
21562166
{
2167+
struct fetch_config config = {
2168+
.display_format = DISPLAY_FORMAT_FULL,
2169+
};
21572170
int i;
21582171
const char *bundle_uri;
21592172
struct string_list list = STRING_LIST_INIT_DUP;
@@ -2173,7 +2186,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
21732186
free(anon);
21742187
}
21752188

2176-
git_config(git_fetch_config, NULL);
2189+
git_config(git_fetch_config, &config);
21772190
if (the_repository->gitdir) {
21782191
prepare_repo_settings(the_repository);
21792192
the_repository->settings.command_requires_full_index = 0;
@@ -2310,7 +2323,8 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
23102323
} else if (remote) {
23112324
if (filter_options.choice || repo_has_promisor_remote(the_repository))
23122325
fetch_one_setup_partial(remote);
2313-
result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs);
2326+
result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs,
2327+
config.display_format);
23142328
} else {
23152329
int max_children = max_jobs;
23162330

0 commit comments

Comments
 (0)