]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fetch: centralize handling of per-reference format
authorPatrick Steinhardt <ps@pks.im>
Mon, 20 Mar 2023 12:35:32 +0000 (13:35 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 20 Mar 2023 18:02:43 +0000 (11:02 -0700)
The function `format_display()` is used to print a single reference
update to a buffer which will then ultimately be printed by the caller.
This architecture causes us to duplicate some logic across the different
callsites of this function. This makes it hard to follow the code as
some parts of the logic are located in one place, while other parts of
the logic are located in a different place. Furthermore, by having the
logic scattered around it becomes quite hard to implement a new output
format for the reference updates.

We can make the logic a whole lot easier to understand by making the
`format_display()` function self-contained so that it handles formatting
and printing of the references. This will eventually allow us to easily
implement a completely different output format, but also opens the door
to conditionally print to either stdout or stderr depending on the
output format.

As a first step towards that goal we move the formatting directive used
by both callers to print a single reference update into this function.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/fetch.c

index 81ba3900cb44bbb1b3bdb6fa4183c01be56c8798..a66428dfd83ce2b119c911d4bbe9cf5b08114b81 100644 (file)
@@ -885,13 +885,14 @@ static void format_display(struct display_state *display_state,
 
        width = (summary_width + strlen(summary) - gettext_width(summary));
 
-       strbuf_addf(display_buffer, "%c %-*s ", code, width, summary);
+       strbuf_addf(display_buffer, " %c %-*s ", code, width, summary);
        if (!display_state->compact_format)
                print_remote_to_local(display_state, display_buffer, remote, prettify_refname(local));
        else
                print_compact(display_state, display_buffer, remote, prettify_refname(local));
        if (error)
                strbuf_addf(display_buffer, "  (%s)", error);
+       strbuf_addch(display_buffer, '\n');
 }
 
 static int update_local_ref(struct ref *ref,
@@ -1271,7 +1272,7 @@ static int store_updated_refs(struct display_state *display_state,
                                                        url_len, url);
                                        shown_url = 1;
                                }
-                               fprintf(stderr, " %s\n", note.buf);
+                               fputs(note.buf, stderr);
                        }
                }
        }
@@ -1432,7 +1433,7 @@ static int prune_refs(struct display_state *display_state,
                        format_display(display_state, &sb, '-', _("[deleted]"), NULL,
                                       _("(none)"), ref->name,
                                       summary_width);
-                       fprintf(stderr, " %s\n",sb.buf);
+                       fputs(sb.buf, stderr);
                        strbuf_release(&sb);
                        warn_dangling_symref(stderr, dangling_msg, ref->name);
                }