git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 2/6] fetch: refactor ref update status formatting code

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jun 5, 2016, 03:11 UTC
Message-ID
<20160605031141.23513-3-pclouds@gmail.com>
In-Reply-To
<20160605031141.23513-1-pclouds@gmail.com>

This makes it easier to change the formatting later. And it makes sure translators cannot mess up format specifiers and break Git.

There are a couple call sites where the length of the second column is TRANSPORT_SUMMARY_WIDTH instead of calculated by TRANSPORT_SUMMARY(), which is enforced now. The result should be the same because these call sites do not contain characters outside ASCII range.

The two strbuf_addf() calls instead of one is mostly to reduce diff-noise in a future patch where "ref -> ref" is reformatted differently.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/fetch.c | 77 ++++++++++++++++++++++++++++-----------------------------
 1 file changed, 38 insertions(+), 39 deletions(-)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 1582ca7..a7f152a 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -451,6 +451,16 @@ fail:
 
 #define REFCOL_WIDTH  10
 
+static void format_display(struct strbuf *display, char code,
+			   const char *summary, const char *error,
+			   const char *remote, const char *local)
+{
+	strbuf_addf(display, "%c %-*s ", code, TRANSPORT_SUMMARY(summary));
+	strbuf_addf(display, "%-*s -> %s", REFCOL_WIDTH, remote, local);
+	if (error)
+		strbuf_addf(display, "  (%s)", error);
+}
+
 static int update_local_ref(struct ref *ref,
 			    const char *remote,
 			    const struct ref *remote_ref,
@@ -467,9 +477,8 @@ static int update_local_ref(struct ref *ref,
 
 	if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
 		if (verbosity > 0)
-			strbuf_addf(display, "= %-*s %-*s -> %s",
-				    TRANSPORT_SUMMARY(_("[up to date]")),
-				    REFCOL_WIDTH, remote, pretty_ref);
+			format_display(display, '=', _("[up to date]"), NULL,
+				       remote, pretty_ref);
 		return 0;
 	}
 
@@ -481,10 +490,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...
 		 */
-		strbuf_addf(display,
-			    _("! %-*s %-*s -> %s  (can't fetch in current branch)"),
-			    TRANSPORT_SUMMARY(_("[rejected]")),
-			    REFCOL_WIDTH, remote, pretty_ref);
+		format_display(display, '!', _("[rejected]"),
+			       _("can't fetch in current branch"),
+			       remote, pretty_ref);
 		return 1;
 	}
 
@@ -492,11 +500,9 @@ static int update_local_ref(struct ref *ref,
 	    starts_with(ref->name, "refs/tags/")) {
 		int r;
 		r = s_update_ref("updating tag", ref, 0);
-		strbuf_addf(display, "%c %-*s %-*s -> %s%s",
-			    r ? '!' : '-',
-			    TRANSPORT_SUMMARY(_("[tag update]")),
-			    REFCOL_WIDTH, remote, pretty_ref,
-			    r ? _("  (unable to update local ref)") : "");
+		format_display(display, r ? '!' : '-', _("[tag update]"),
+			       r ? _("unable to update local ref") : NULL,
+			       remote, pretty_ref);
 		return r;
 	}
 
@@ -527,11 +533,9 @@ static int update_local_ref(struct ref *ref,
 		    (recurse_submodules != RECURSE_SUBMODULES_ON))
 			check_for_new_submodule_commits(ref->new_oid.hash);
 		r = s_update_ref(msg, ref, 0);
-		strbuf_addf(display, "%c %-*s %-*s -> %s%s",
-			    r ? '!' : '*',
-			    TRANSPORT_SUMMARY(what),
-			    REFCOL_WIDTH, remote, pretty_ref,
-			    r ? _("  (unable to update local ref)") : "");
+		format_display(display, r ? '!' : '*', what,
+			       r ? _("unable to update local ref") : NULL,
+			       remote, pretty_ref);
 		return r;
 	}
 
@@ -545,11 +549,9 @@ static int update_local_ref(struct ref *ref,
 		    (recurse_submodules != RECURSE_SUBMODULES_ON))
 			check_for_new_submodule_commits(ref->new_oid.hash);
 		r = s_update_ref("fast-forward", ref, 1);
-		strbuf_addf(display, "%c %-*s %-*s -> %s%s",
-			    r ? '!' : ' ',
-			    TRANSPORT_SUMMARY_WIDTH, quickref.buf,
-			    REFCOL_WIDTH, remote, pretty_ref,
-			    r ? _("  (unable to update local ref)") : "");
+		format_display(display, r ? '!' : ' ', quickref.buf,
+			       r ? _("unable to update local ref") : NULL,
+			       remote, pretty_ref);
 		strbuf_release(&quickref);
 		return r;
 	} else if (force || ref->force) {
@@ -562,18 +564,14 @@ static int update_local_ref(struct ref *ref,
 		    (recurse_submodules != RECURSE_SUBMODULES_ON))
 			check_for_new_submodule_commits(ref->new_oid.hash);
 		r = s_update_ref("forced-update", ref, 1);
-		strbuf_addf(display, "%c %-*s %-*s -> %s  (%s)",
-			    r ? '!' : '+',
-			    TRANSPORT_SUMMARY_WIDTH, quickref.buf,
-			    REFCOL_WIDTH, remote, pretty_ref,
-			    r ? _("unable to update local ref") : _("forced update"));
+		format_display(display, r ? '!' : '+', quickref.buf,
+			       r ? _("unable to update local ref") : _("forced update"),
+			       remote, pretty_ref);
 		strbuf_release(&quickref);
 		return r;
 	} else {
-		strbuf_addf(display, "! %-*s %-*s -> %s  %s",
-			    TRANSPORT_SUMMARY(_("[rejected]")),
-			    REFCOL_WIDTH, remote, pretty_ref,
-			    _("(non-fast-forward)"));
+		format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
+			       remote, pretty_ref);
 		return 1;
 	}
 }
@@ -714,11 +712,10 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
 				rc |= update_local_ref(ref, what, rm, &note);
 				free(ref);
 			} else
-				strbuf_addf(&note, "* %-*s %-*s -> FETCH_HEAD",
-					    TRANSPORT_SUMMARY_WIDTH,
-					    *kind ? kind : "branch",
-					    REFCOL_WIDTH,
-					    *what ? what : "HEAD");
+				format_display(&note, '*',
+					       *kind ? kind : "branch", NULL,
+					       *what ? what : "HEAD",
+					       "FETCH_HEAD");
 			if (note.len) {
 				if (verbosity >= 0 && !shown_url) {
 					fprintf(stderr, _("From %.*s\n"),
@@ -812,13 +809,15 @@ static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
 
 	if (verbosity >= 0) {
 		for (ref = stale_refs; ref; ref = ref->next) {
+			struct strbuf sb = STRBUF_INIT;
 			if (!shown_url) {
 				fprintf(stderr, _("From %.*s\n"), url_len, url);
 				shown_url = 1;
 			}
-			fprintf(stderr, " x %-*s %-*s -> %s\n",
-				TRANSPORT_SUMMARY(_("[deleted]")),
-				REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
+			format_display(&sb, 'x', _("[deleted]"), NULL,
+				       _("(none)"), prettify_refname(ref->name));
+			fprintf(stderr, " %s\n",sb.buf);
+			strbuf_release(&sb);
 			warn_dangling_symref(stderr, dangling_msg, ref->name);
 		}
 	}
-- 
2.8.2.524.g6ff3d78
Previous: Marc BranchaudNext: Nguyễn Thái Ngọc Duy
Message 40 of 71 in “Better ref summary alignment in "git fetch"”
  1. 0/2 Better ref summary alignment in "git fetch"Nguyễn Thái Ngọc Duy, May 22, 2016
  2. 1/2 fetch: better alignment in ref summaryNguyễn Thái Ngọc Duy, May 22, 2016
  3. Junio C HamanoMay 23, 2016
  4. Duy NguyenMay 23, 2016
  5. Marc BranchaudMay 26, 2016
  6. Jeff KingMay 26, 2016
  7. Junio C HamanoMay 26, 2016
  8. Marc BranchaudMay 26, 2016
  9. Junio C HamanoMay 26, 2016
  10. Marc BranchaudMay 26, 2016
  11. Jeff KingMay 26, 2016
  12. Duy NguyenJun 2, 2016
  13. Junio C HamanoJun 2, 2016
  14. 2/2 fetch: reduce ref column size when there are enough short ref namesNguyễn Thái Ngọc Duy, May 22, 2016
  15. 0/3 Better ref summary alignment in "git fetch"Nguyễn Thái Ngọc Duy, Jun 3, 2016
  16. 1/3 git-fetch.txt: document fetch outputNguyễn Thái Ngọc Duy, Jun 3, 2016
  17. Marc BranchaudJun 3, 2016
  18. Jeff KingJun 3, 2016
  19. 2/3 fetch: refactor ref update status formatting codeNguyễn Thái Ngọc Duy, Jun 3, 2016
  20. Junio C HamanoJun 3, 2016
  21. 3/3 fetch: reduce duplicate in ref update status linesNguyễn Thái Ngọc Duy, Jun 3, 2016
  22. Marc BranchaudJun 3, 2016
  23. Junio C HamanoJun 3, 2016
  24. Marc BranchaudJun 3, 2016
  25. Junio C HamanoJun 3, 2016
  26. Duy NguyenJun 4, 2016
  27. Duy NguyenJun 4, 2016
  28. Junio C HamanoJun 4, 2016
  29. Duy NguyenJun 5, 2016
  30. Junio C HamanoJun 3, 2016
  31. Duy NguyenJun 3, 2016
  32. Jeff KingJun 3, 2016
  33. Duy NguyenJun 3, 2016
  34. Junio C HamanoJun 4, 2016
  35. Jeff KingJun 3, 2016
  36. Junio C HamanoJun 3, 2016
  37. 0/6 Better ref summary alignment in "git fetch"Nguyễn Thái Ngọc Duy, Jun 5, 2016
  38. 1/6 git-fetch.txt: document fetch outputNguyễn Thái Ngọc Duy, Jun 5, 2016
  39. Marc BranchaudJun 6, 2016
  40. 2/6 fetch: refactor ref update status formatting codeNguyễn Thái Ngọc Duy, Jun 5, 2016
  41. 3/6 fetch: change flag code for displaying tag update and deleted refNguyễn Thái Ngọc Duy, Jun 5, 2016
  42. 4/6 fetch: align all "remote -> local" outputNguyễn Thái Ngọc Duy, Jun 5, 2016
  43. 5/6 fetch: reduce duplicate in ref update status lines with { -> }Nguyễn Thái Ngọc Duy, Jun 5, 2016
  44. 6/6 fetch: reduce duplicate in ref update status lines with placeholderNguyễn Thái Ngọc Duy, Jun 5, 2016
  45. 0/5 Better ref summary alignment in "git fetch"Nguyễn Thái Ngọc Duy, Jun 26, 2016
  46. 1/5 git-fetch.txt: document fetch outputNguyễn Thái Ngọc Duy, Jun 26, 2016
  47. Jakub NarębskiJul 4, 2016
  48. Duy NguyenJul 4, 2016
  49. Jakub NarębskiJul 4, 2016
  50. Duy NguyenJul 4, 2016
  51. 2/5 fetch: refactor ref update status formatting codeNguyễn Thái Ngọc Duy, Jun 26, 2016
  52. 3/5 fetch: change flag code for displaying tag update and deleted refNguyễn Thái Ngọc Duy, Jun 26, 2016
  53. 4/5 fetch: align all "remote -> local" outputNguyễn Thái Ngọc Duy, Jun 26, 2016
  54. 5/5 fetch: reduce duplicate in ref update status lines with placeholderNguyễn Thái Ngọc Duy, Jun 26, 2016
  55. Eric SunshineJun 27, 2016
  56. Duy NguyenJun 27, 2016
  57. Junio C HamanoJun 27, 2016
  58. Jeff KingJun 27, 2016
  59. Duy NguyenJun 27, 2016
  60. Duy NguyenJun 30, 2016
  61. Jeff KingJul 1, 2016
  62. 0/5 Better ref summary alignment in "git fetch"Nguyễn Thái Ngọc Duy, Jul 1, 2016
  63. 1/5 git-fetch.txt: document fetch outputNguyễn Thái Ngọc Duy, Jul 1, 2016
  64. 2/5 fetch: refactor ref update status formatting codeNguyễn Thái Ngọc Duy, Jul 1, 2016
  65. 5/5 fetch: reduce duplicate in ref update status lines with placeholderNguyễn Thái Ngọc Duy, Jul 1, 2016
  66. 3/5 fetch: change flag code for displaying tag update and deleted refNguyễn Thái Ngọc Duy, Jul 1, 2016
  67. 4/5 fetch: align all "remote -> local" outputNguyễn Thái Ngọc Duy, Jul 1, 2016
  68. Junio C HamanoJul 1, 2016
  69. Duy NguyenJul 2, 2016
  70. Marc BranchaudJul 4, 2016
  71. Duy NguyenJul 4, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.