diff mbox series

[v4,5/8] fetch: introduce `display_format` enum

Message ID 80ac00b0c4b0fde61a3edf48d996e3a60327a54d.1683636885.git.ps@pks.im (mailing list archive)
State Superseded
Headers show
Series fetch: introduce machine-parseable output | expand

Commit Message

Patrick Steinhardt May 9, 2023, 1:02 p.m. UTC
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>
---
 builtin/fetch.c | 101 ++++++++++++++++++++++++++++++------------------
 1 file changed, 64 insertions(+), 37 deletions(-)

Comments

Junio C Hamano May 9, 2023, 8:19 p.m. UTC | #1
Patrick Steinhardt <ps@pks.im> writes:

> 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.

Makes sense.

> +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;

OK.  Preparatory conversion without adding anything new.

> @@ -809,31 +814,42 @@ 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);

Naturally.

> -	display_state->refcol_width = 10;
> -	for (rm = ref_map; rm; rm = rm->next) {
> -		int width;
> +	switch (display_state->format) {
> +	case DISPLAY_FORMAT_FULL:
> +	case DISPLAY_FORMAT_COMPACT: {
> +		struct ref *rm;
>  
> -		if (rm->status == REF_STATUS_REJECT_SHALLOW ||
> -		    !rm->peer_ref ||
> -		    !strcmp(rm->name, "HEAD"))
> -			continue;
> +		display_state->refcol_width = 10;
> +		for (rm = ref_map; rm; rm = rm->next) {
> +			int width;
>  
> -		width = refcol_width(rm, display_state->compact_format);
> +			if (rm->status == REF_STATUS_REJECT_SHALLOW ||
> +			    !rm->peer_ref ||
> +			    !strcmp(rm->name, "HEAD"))
> +				continue;
>  
> -		/*
> -		 * Not precise calculation for compact mode because '*' can
> -		 * appear on the left hand side of '->' and shrink the column
> -		 * back.
> -		 */
> -		if (display_state->refcol_width < width)
> -			display_state->refcol_width = width;
> +			width = refcol_width(rm, display_state->format == DISPLAY_FORMAT_COMPACT);
> +
> +			/*
> +			 * Not precise calculation for compact mode because '*' can
> +			 * appear on the left hand side of '->' and shrink the column
> +			 * back.
> +			 */
> +			if (display_state->refcol_width < width)
> +				display_state->refcol_width = width;
> +		}
> +
> +		break;
> +	}
> +	default:
> +		BUG("unexpected display format %d", display_state->format);
>  	}

Due to reindentation, the patch is noisier than what it does (which
should be "nothing, other than allowing another value in the .format
member").

It makes me wonder if it would make it easier to read to move the
bulk of this code to a helper function.  If we are to give a name to
what is being done in the above hunk, what would it be?  It computes
display->refcol_width in which all records would fit, but presumably
if we are to add more things to be shown per ref and align them in a
simlar way, we would compute widths for these other things there as
well.  Perhaps compute_display_alignment() or somesuch?
Patrick Steinhardt May 10, 2023, 12:35 p.m. UTC | #2
On Tue, May 09, 2023 at 01:19:29PM -0700, Junio C Hamano wrote:
> Patrick Steinhardt <ps@pks.im> writes:
[snip]
> > -	display_state->refcol_width = 10;
> > -	for (rm = ref_map; rm; rm = rm->next) {
> > -		int width;
> > +	switch (display_state->format) {
> > +	case DISPLAY_FORMAT_FULL:
> > +	case DISPLAY_FORMAT_COMPACT: {
> > +		struct ref *rm;
> >  
> > -		if (rm->status == REF_STATUS_REJECT_SHALLOW ||
> > -		    !rm->peer_ref ||
> > -		    !strcmp(rm->name, "HEAD"))
> > -			continue;
> > +		display_state->refcol_width = 10;
> > +		for (rm = ref_map; rm; rm = rm->next) {
> > +			int width;
> >  
> > -		width = refcol_width(rm, display_state->compact_format);
> > +			if (rm->status == REF_STATUS_REJECT_SHALLOW ||
> > +			    !rm->peer_ref ||
> > +			    !strcmp(rm->name, "HEAD"))
> > +				continue;
> >  
> > -		/*
> > -		 * Not precise calculation for compact mode because '*' can
> > -		 * appear on the left hand side of '->' and shrink the column
> > -		 * back.
> > -		 */
> > -		if (display_state->refcol_width < width)
> > -			display_state->refcol_width = width;
> > +			width = refcol_width(rm, display_state->format == DISPLAY_FORMAT_COMPACT);
> > +
> > +			/*
> > +			 * Not precise calculation for compact mode because '*' can
> > +			 * appear on the left hand side of '->' and shrink the column
> > +			 * back.
> > +			 */
> > +			if (display_state->refcol_width < width)
> > +				display_state->refcol_width = width;
> > +		}
> > +
> > +		break;
> > +	}
> > +	default:
> > +		BUG("unexpected display format %d", display_state->format);
> >  	}
> 
> Due to reindentation, the patch is noisier than what it does (which
> should be "nothing, other than allowing another value in the .format
> member").
> 
> It makes me wonder if it would make it easier to read to move the
> bulk of this code to a helper function.  If we are to give a name to
> what is being done in the above hunk, what would it be?  It computes
> display->refcol_width in which all records would fit, but presumably
> if we are to add more things to be shown per ref and align them in a
> simlar way, we would compute widths for these other things there as
> well.  Perhaps compute_display_alignment() or somesuch?

The code already has such a function and calls it `refcol_width()`, but
it only computes the width for a single reference. The most natural
thing to do here would thus be to merge the loop over the references
into that function. This would also allow us to skip some weirdness,
like the fact that we skip some references inside `refcol_width()` while
we skip other references in the `ref_map` loop.

This refactoring is also quite noisy, but it makes the code simpler
overall and will make the patch introducing the enum less so.

Patrick
diff mbox series

Patch

diff --git a/builtin/fetch.c b/builtin/fetch.c
index 6aecf549e8..9e7e45344d 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -48,11 +48,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;
@@ -784,7 +790,6 @@  static int refcol_width(const struct ref *ref, int compact_format)
 static void display_state_init(struct display_state *display_state, struct ref *ref_map,
 			       const char *raw_url)
 {
-	struct ref *rm;
 	const char *format = "full";
 	int i;
 
@@ -809,31 +814,42 @@  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 = 10;
-	for (rm = ref_map; rm; rm = rm->next) {
-		int width;
+	switch (display_state->format) {
+	case DISPLAY_FORMAT_FULL:
+	case DISPLAY_FORMAT_COMPACT: {
+		struct ref *rm;
 
-		if (rm->status == REF_STATUS_REJECT_SHALLOW ||
-		    !rm->peer_ref ||
-		    !strcmp(rm->name, "HEAD"))
-			continue;
+		display_state->refcol_width = 10;
+		for (rm = ref_map; rm; rm = rm->next) {
+			int width;
 
-		width = refcol_width(rm, display_state->compact_format);
+			if (rm->status == REF_STATUS_REJECT_SHALLOW ||
+			    !rm->peer_ref ||
+			    !strcmp(rm->name, "HEAD"))
+				continue;
 
-		/*
-		 * Not precise calculation for compact mode because '*' can
-		 * appear on the left hand side of '->' and shrink the column
-		 * back.
-		 */
-		if (display_state->refcol_width < width)
-			display_state->refcol_width = width;
+			width = refcol_width(rm, display_state->format == DISPLAY_FORMAT_COMPACT);
+
+			/*
+			 * Not precise calculation for compact mode because '*' can
+			 * appear on the left hand side of '->' and shrink the column
+			 * back.
+			 */
+			if (display_state->refcol_width < width)
+				display_state->refcol_width = width;
+		}
+
+		break;
+	}
+	default:
+		BUG("unexpected display format %d", display_state->format);
 	}
 }
 
@@ -904,30 +920,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;
+
+		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;
+		}
+
+		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;
 	}
-
-	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->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);
+	default:
+		BUG("unexpected display format %d", display_state->format);
+	};
 	strbuf_addch(&display_state->buf, '\n');
 
 	fputs(display_state->buf.buf, stderr);