Skip to content

Commit 5095793

Browse files
pks-tgitster
authored andcommitted
fetch: introduce display_format enum
We currently have two different display formats in git-fetch(1) with the "full" and "compact" formats. This is tracked with a boolean value that simply denotes whether the display format is supposed to be compacted or not. This works reasonably well while there are only two formats, but we're about to introduce another format that will make this a bit more awkward to use. Introduce a `enum display_format` that is more readily extensible. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9539638 commit 5095793

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

builtin/fetch.c

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ enum {
5050
TAGS_SET = 2
5151
};
5252

53+
enum display_format {
54+
DISPLAY_FORMAT_UNKNOWN = 0,
55+
DISPLAY_FORMAT_FULL,
56+
DISPLAY_FORMAT_COMPACT,
57+
};
58+
5359
struct display_state {
5460
struct strbuf buf;
5561

5662
int refcol_width;
57-
int compact_format;
63+
enum display_format format;
5864

5965
char *url;
6066
int url_len, shown_url;
@@ -822,14 +828,22 @@ static void display_state_init(struct display_state *display_state, struct ref *
822828

823829
git_config_get_string_tmp("fetch.output", &format);
824830
if (!strcasecmp(format, "full"))
825-
display_state->compact_format = 0;
831+
display_state->format = DISPLAY_FORMAT_FULL;
826832
else if (!strcasecmp(format, "compact"))
827-
display_state->compact_format = 1;
833+
display_state->format = DISPLAY_FORMAT_COMPACT;
828834
else
829835
die(_("invalid value for '%s': '%s'"),
830836
"fetch.output", format);
831837

832-
display_state->refcol_width = refcol_width(ref_map, display_state->compact_format);
838+
switch (display_state->format) {
839+
case DISPLAY_FORMAT_FULL:
840+
case DISPLAY_FORMAT_COMPACT:
841+
display_state->refcol_width = refcol_width(ref_map,
842+
display_state->format == DISPLAY_FORMAT_COMPACT);
843+
break;
844+
default:
845+
BUG("unexpected display format %d", display_state->format);
846+
}
833847
}
834848

835849
static void display_state_release(struct display_state *display_state)
@@ -899,30 +913,41 @@ static void display_ref_update(struct display_state *display_state, char code,
899913
const char *remote, const char *local,
900914
int summary_width)
901915
{
902-
int width;
903-
904916
if (verbosity < 0)
905917
return;
906918

907919
strbuf_reset(&display_state->buf);
908920

909-
if (!display_state->shown_url) {
910-
strbuf_addf(&display_state->buf, _("From %.*s\n"),
911-
display_state->url_len, display_state->url);
912-
display_state->shown_url = 1;
913-
}
921+
switch (display_state->format) {
922+
case DISPLAY_FORMAT_FULL:
923+
case DISPLAY_FORMAT_COMPACT: {
924+
int width;
914925

915-
width = (summary_width + strlen(summary) - gettext_width(summary));
916-
remote = prettify_refname(remote);
917-
local = prettify_refname(local);
926+
if (!display_state->shown_url) {
927+
strbuf_addf(&display_state->buf, _("From %.*s\n"),
928+
display_state->url_len, display_state->url);
929+
display_state->shown_url = 1;
930+
}
918931

919-
strbuf_addf(&display_state->buf, " %c %-*s ", code, width, summary);
920-
if (!display_state->compact_format)
921-
print_remote_to_local(display_state, remote, local);
922-
else
923-
print_compact(display_state, remote, local);
924-
if (error)
925-
strbuf_addf(&display_state->buf, " (%s)", error);
932+
width = (summary_width + strlen(summary) - gettext_width(summary));
933+
remote = prettify_refname(remote);
934+
local = prettify_refname(local);
935+
936+
strbuf_addf(&display_state->buf, " %c %-*s ", code, width, summary);
937+
938+
if (display_state->format != DISPLAY_FORMAT_COMPACT)
939+
print_remote_to_local(display_state, remote, local);
940+
else
941+
print_compact(display_state, remote, local);
942+
943+
if (error)
944+
strbuf_addf(&display_state->buf, " (%s)", error);
945+
946+
break;
947+
}
948+
default:
949+
BUG("unexpected display format %d", display_state->format);
950+
};
926951
strbuf_addch(&display_state->buf, '\n');
927952

928953
fputs(display_state->buf.buf, stderr);

0 commit comments

Comments
 (0)