summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2023-05-10 14:34:24 +0200
committerJunio C Hamano <gitster@pobox.com>2023-05-10 10:35:25 -0700
commit50957937f92691215492f4c62dd0a18e39f17a2e (patch)
treeb7a19392a285de2b0f67e67587215712a1b97bcd
parent9539638a2bbc88717547585ae660bba3bbfaef8b (diff)
downloadgit-50957937f92691215492f4c62dd0a18e39f17a2e.tar.gz
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>
-rw-r--r--builtin/fetch.c67
1 files changed, 46 insertions, 21 deletions
diff --git a/builtin/fetch.c b/builtin/fetch.c
index b5bf1f8d14..2d1d4913be 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -50,11 +50,17 @@ enum {
TAGS_SET = 2
};
+enum display_format {
+ DISPLAY_FORMAT_UNKNOWN = 0,
+ DISPLAY_FORMAT_FULL,
+ DISPLAY_FORMAT_COMPACT,
+};
+
struct display_state {
struct strbuf buf;
int refcol_width;
- int compact_format;
+ enum display_format format;
char *url;
int url_len, shown_url;
@@ -822,14 +828,22 @@ static void display_state_init(struct display_state *display_state, struct ref *
git_config_get_string_tmp("fetch.output", &format);
if (!strcasecmp(format, "full"))
- display_state->compact_format = 0;
+ display_state->format = DISPLAY_FORMAT_FULL;
else if (!strcasecmp(format, "compact"))
- display_state->compact_format = 1;
+ display_state->format = DISPLAY_FORMAT_COMPACT;
else
die(_("invalid value for '%s': '%s'"),
"fetch.output", format);
- display_state->refcol_width = refcol_width(ref_map, display_state->compact_format);
+ switch (display_state->format) {
+ case DISPLAY_FORMAT_FULL:
+ case DISPLAY_FORMAT_COMPACT:
+ display_state->refcol_width = refcol_width(ref_map,
+ display_state->format == DISPLAY_FORMAT_COMPACT);
+ break;
+ default:
+ BUG("unexpected display format %d", display_state->format);
+ }
}
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,
const char *remote, const char *local,
int summary_width)
{
- int width;
-
if (verbosity < 0)
return;
strbuf_reset(&display_state->buf);
- if (!display_state->shown_url) {
- strbuf_addf(&display_state->buf, _("From %.*s\n"),
- display_state->url_len, display_state->url);
- display_state->shown_url = 1;
- }
+ switch (display_state->format) {
+ case DISPLAY_FORMAT_FULL:
+ case DISPLAY_FORMAT_COMPACT: {
+ int width;
- width = (summary_width + strlen(summary) - gettext_width(summary));
- remote = prettify_refname(remote);
- local = prettify_refname(local);
+ if (!display_state->shown_url) {
+ strbuf_addf(&display_state->buf, _("From %.*s\n"),
+ display_state->url_len, display_state->url);
+ display_state->shown_url = 1;
+ }
- strbuf_addf(&display_state->buf, " %c %-*s ", code, width, summary);
- if (!display_state->compact_format)
- print_remote_to_local(display_state, remote, local);
- else
- print_compact(display_state, remote, local);
- if (error)
- strbuf_addf(&display_state->buf, " (%s)", error);
+ width = (summary_width + strlen(summary) - gettext_width(summary));
+ remote = prettify_refname(remote);
+ local = prettify_refname(local);
+
+ strbuf_addf(&display_state->buf, " %c %-*s ", code, width, summary);
+
+ if (display_state->format != DISPLAY_FORMAT_COMPACT)
+ print_remote_to_local(display_state, remote, local);
+ else
+ print_compact(display_state, remote, local);
+
+ if (error)
+ strbuf_addf(&display_state->buf, " (%s)", error);
+
+ break;
+ }
+ default:
+ BUG("unexpected display format %d", display_state->format);
+ };
strbuf_addch(&display_state->buf, '\n');
fputs(display_state->buf.buf, stderr);