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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed May 10, 2023 at 14:34 UTC 50957937f92691215492f4c62dd0a18e39f17a2e
1 file changed +46 -21
builtin/fetch.c
+46 -21
@@ -50,11 +50,17 @@ enum {
50 TAGS_SET = 2
51 };
52
53 +enum display_format {
54 + DISPLAY_FORMAT_UNKNOWN = 0,
55 + DISPLAY_FORMAT_FULL,
56 + DISPLAY_FORMAT_COMPACT,
57 +};
58 +
59 struct display_state {
60 struct strbuf buf;
61
62 int refcol_width;
57 - int compact_format;
63 + enum display_format format;
64
65 char *url;
66 int url_len, shown_url;
@@ -822,14 +828,22 @@ static void display_state_init(struct display_state *display_state, struct ref *
828
829 git_config_get_string_tmp("fetch.output", &format);
830 if (!strcasecmp(format, "full"))
825 - display_state->compact_format = 0;
831 + display_state->format = DISPLAY_FORMAT_FULL;
832 else if (!strcasecmp(format, "compact"))
827 - display_state->compact_format = 1;
833 + display_state->format = DISPLAY_FORMAT_COMPACT;
834 else
835 die(_("invalid value for '%s': '%s'"),
836 "fetch.output", format);
837
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 + }
847 }
848
849 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,
913 const char *remote, const char *local,
914 int summary_width)
915 {
902 - int width;
903 -
916 if (verbosity < 0)
917 return;
918
919 strbuf_reset(&display_state->buf);
920
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;
925
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 + }
931
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 + };
951 strbuf_addch(&display_state->buf, '\n');
952
953 fputs(display_state->buf.buf, stderr);