[PATCH 8/8] fetch: centralize printing of reference updates

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In order to print updated references during a fetch, the two different
call sites that do this will first call `format_display()` followed by a
call to `fputs()`. This is needlessly roundabout now that we have the
`display_state` structure that encapsulates all of the printing logic
for references.

Move displaying the reference updates into `format_display()` and rename
it to `display_ref_update()` to better match its new purpose. We have
now centralized the logic to print reference updates into a single
function, which enables us to more readily introduce new formats that
may even be printing to a different file descriptor than `stderr`.

Signed-off-by: Patrick Steinhardt <ps@xxxxxx>
---
 builtin/fetch.c | 105 ++++++++++++++++++++++++------------------------
 1 file changed, 52 insertions(+), 53 deletions(-)

diff --git a/builtin/fetch.c b/builtin/fetch.c
index 31724e9aaf..6e864f8457 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -48,6 +48,8 @@ enum {
 };
 
 struct display_state {
+	struct strbuf buf;
+
 	int refcol_width;
 	int summary_width;
 	int compact_format;
@@ -788,6 +790,8 @@ static void display_state_init(struct display_state *display, struct ref *ref_ma
 
 	memset(display, 0, sizeof(*display));
 
+	strbuf_init(&display->buf, 0);
+
 	if (raw_url)
 		display->url = transport_anonymize_url(raw_url);
 	else
@@ -837,14 +841,14 @@ static void display_state_init(struct display_state *display, struct ref *ref_ma
 
 static void display_state_release(struct display_state *display)
 {
+	strbuf_release(&display->buf);
 	free(display->url);
 }
 
 static void print_remote_to_local(struct display_state *display,
-				  struct strbuf *display_buffer,
 				  const char *remote, const char *local)
 {
-	strbuf_addf(display_buffer, "%-*s -> %s", display->refcol_width, remote, local);
+	strbuf_addf(&display->buf, "%-*s -> %s", display->refcol_width, remote, local);
 }
 
 static int find_and_replace(struct strbuf *haystack,
@@ -874,14 +878,14 @@ static int find_and_replace(struct strbuf *haystack,
 	return 1;
 }
 
-static void print_compact(struct display_state *display, struct strbuf *display_buffer,
+static void print_compact(struct display_state *display,
 			  const char *remote, const char *local)
 {
 	struct strbuf r = STRBUF_INIT;
 	struct strbuf l = STRBUF_INIT;
 
 	if (!strcmp(remote, local)) {
-		strbuf_addf(display_buffer, "%-*s -> *", display->refcol_width, remote);
+		strbuf_addf(&display->buf, "%-*s -> *", display->refcol_width, remote);
 		return;
 	}
 
@@ -890,44 +894,46 @@ static void print_compact(struct display_state *display, struct strbuf *display_
 
 	if (!find_and_replace(&r, local, "*"))
 		find_and_replace(&l, remote, "*");
-	print_remote_to_local(display, display_buffer, r.buf, l.buf);
+	print_remote_to_local(display, r.buf, l.buf);
 
 	strbuf_release(&r);
 	strbuf_release(&l);
 }
 
-static void format_display(struct display_state *display,
-			   struct strbuf *display_buffer, char code,
-			   const char *summary, const char *error,
-			   const char *remote, const char *local)
+static void display_ref_update(struct display_state *display, char code,
+			       const char *summary, const char *error,
+			       const char *remote, const char *local)
 {
 	int width;
 
 	if (verbosity < 0)
 		return;
 
+	strbuf_reset(&display->buf);
+
 	if (!display->shown_url) {
-		strbuf_addf(display_buffer, _("From %.*s\n"), display->url_len, display->url);
+		strbuf_addf(&display->buf, _("From %.*s\n"), display->url_len, display->url);
 		display->shown_url = 1;
 	}
 
 	width = (display->summary_width + strlen(summary) - gettext_width(summary));
 
-	strbuf_addf(display_buffer, " %c %-*s ", code, width, summary);
+	strbuf_addf(&display->buf, " %c %-*s ", code, width, summary);
 	if (!display->compact_format)
-		print_remote_to_local(display, display_buffer, remote, prettify_refname(local));
+		print_remote_to_local(display, remote, prettify_refname(local));
 	else
-		print_compact(display, display_buffer, remote, prettify_refname(local));
+		print_compact(display, remote, prettify_refname(local));
 	if (error)
-		strbuf_addf(display_buffer, "  (%s)", error);
-	strbuf_addch(display_buffer, '\n');
+		strbuf_addf(&display->buf, "  (%s)", error);
+	strbuf_addch(&display->buf, '\n');
+
+	fputs(display->buf.buf, stderr);
 }
 
 static int update_local_ref(struct ref *ref,
 			    struct ref_transaction *transaction,
 			    struct display_state *display,
-			    const char *remote, const struct ref *remote_ref,
-			    struct strbuf *display_buffer)
+			    const char *remote, const struct ref *remote_ref)
 {
 	struct commit *current = NULL, *updated;
 	int fast_forward = 0;
@@ -937,8 +943,8 @@ static int update_local_ref(struct ref *ref,
 
 	if (oideq(&ref->old_oid, &ref->new_oid)) {
 		if (verbosity > 0)
-			format_display(display, display_buffer, '=', _("[up to date]"), NULL,
-				       remote, ref->name);
+			display_ref_update(display, '=', _("[up to date]"), NULL,
+					   remote, ref->name);
 		return 0;
 	}
 
@@ -949,9 +955,9 @@ static int update_local_ref(struct ref *ref,
 		 * If this is the head, and it's not okay to update
 		 * the head, and the old value of the head isn't empty...
 		 */
-		format_display(display, display_buffer, '!', _("[rejected]"),
-			       _("can't fetch into checked-out branch"),
-			       remote, ref->name);
+		display_ref_update(display, '!', _("[rejected]"),
+				   _("can't fetch into checked-out branch"),
+				   remote, ref->name);
 		return 1;
 	}
 
@@ -960,14 +966,14 @@ static int update_local_ref(struct ref *ref,
 		if (force || ref->force) {
 			int r;
 			r = s_update_ref("updating tag", ref, transaction, 0);
-			format_display(display, display_buffer, r ? '!' : 't', _("[tag update]"),
-				       r ? _("unable to update local ref") : NULL,
-				       remote, ref->name);
+			display_ref_update(display, r ? '!' : 't', _("[tag update]"),
+					   r ? _("unable to update local ref") : NULL,
+					   remote, ref->name);
 			return r;
 		} else {
-			format_display(display, display_buffer, '!', _("[rejected]"),
-				       _("would clobber existing tag"),
-				       remote, ref->name);
+			display_ref_update(display, '!', _("[rejected]"),
+					   _("would clobber existing tag"),
+					   remote, ref->name);
 			return 1;
 		}
 	}
@@ -998,9 +1004,9 @@ static int update_local_ref(struct ref *ref,
 		}
 
 		r = s_update_ref(msg, ref, transaction, 0);
-		format_display(display, display_buffer, r ? '!' : '*', what,
-			       r ? _("unable to update local ref") : NULL,
-			       remote, ref->name);
+		display_ref_update(display, r ? '!' : '*', what,
+				   r ? _("unable to update local ref") : NULL,
+				   remote, ref->name);
 		return r;
 	}
 
@@ -1020,9 +1026,9 @@ static int update_local_ref(struct ref *ref,
 		strbuf_addstr(&quickref, "..");
 		strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
 		r = s_update_ref("fast-forward", ref, transaction, 1);
-		format_display(display, display_buffer, r ? '!' : ' ', quickref.buf,
-			       r ? _("unable to update local ref") : NULL,
-			       remote, ref->name);
+		display_ref_update(display, r ? '!' : ' ', quickref.buf,
+				   r ? _("unable to update local ref") : NULL,
+				   remote, ref->name);
 		strbuf_release(&quickref);
 		return r;
 	} else if (force || ref->force) {
@@ -1032,14 +1038,14 @@ static int update_local_ref(struct ref *ref,
 		strbuf_addstr(&quickref, "...");
 		strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
 		r = s_update_ref("forced-update", ref, transaction, 1);
-		format_display(display, display_buffer, r ? '!' : '+', quickref.buf,
-			       r ? _("unable to update local ref") : _("forced update"),
-			       remote, ref->name);
+		display_ref_update(display, r ? '!' : '+', quickref.buf,
+				   r ? _("unable to update local ref") : _("forced update"),
+				   remote, ref->name);
 		strbuf_release(&quickref);
 		return r;
 	} else {
-		format_display(display, display_buffer, '!', _("[rejected]"), _("non-fast-forward"),
-			       remote, ref->name);
+		display_ref_update(display, '!', _("[rejected]"), _("non-fast-forward"),
+				   remote, ref->name);
 		return 1;
 	}
 }
@@ -1261,10 +1267,8 @@ static int store_updated_refs(struct display_state *display,
 					  rm->fetch_head_status,
 					  note.buf, display->url, display->url_len);
 
-			strbuf_reset(&note);
 			if (ref) {
-				rc |= update_local_ref(ref, transaction, display, what,
-						       rm, &note);
+				rc |= update_local_ref(ref, transaction, display, what, rm);
 				free(ref);
 			} else if (write_fetch_head || dry_run) {
 				/*
@@ -1272,13 +1276,11 @@ static int store_updated_refs(struct display_state *display,
 				 * would be written to FETCH_HEAD, if --dry-run
 				 * is set).
 				 */
-				format_display(display, &note, '*',
-					       *kind ? kind : "branch", NULL,
-					       *what ? what : "HEAD",
-					       "FETCH_HEAD");
+				display_ref_update(display, '*',
+						   *kind ? kind : "branch", NULL,
+						   *what ? what : "HEAD",
+						   "FETCH_HEAD");
 			}
-			if (note.len)
-				fputs(note.buf, stderr);
 		}
 	}
 
@@ -1412,11 +1414,8 @@ static int prune_refs(struct display_state *display,
 
 	if (verbosity >= 0) {
 		for (ref = stale_refs; ref; ref = ref->next) {
-			struct strbuf sb = STRBUF_INIT;
-			format_display(display, &sb, '-', _("[deleted]"), NULL,
-				       _("(none)"), ref->name);
-			fputs(sb.buf, stderr);
-			strbuf_release(&sb);
+			display_ref_update(display, '-', _("[deleted]"), NULL,
+					   _("(none)"), ref->name);
 			warn_dangling_symref(stderr, dangling_msg, ref->name);
 		}
 	}
-- 
2.40.0

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux