Skip to contentSkip to conversation

add unicode support to git repo structure

PATCH In review K Jayatheerth 4 participants 7 messages Aug 21 – Aug 23

lore
Share & tools
lore
#0 lore raw

cover add unicode support to git repo structure

Currently, git repo structure always renders its table output using plain ASCII characters (|, -, *), regardless of the user's locale. This series introduces Unicode box-drawing characters (│, ├, ─, ┼, ┤) and bullet points (•) when a UTF-8 locale is detected, providing a cleaner and more visually distinct hierarchical output on modern terminals while gracefully falling back to the existing ASCII formatting otherwise.

Summary of changes:

- Patch 1 (gettext: fall back to env-derived charset when unset):
Fixes an issue where is_utf8_locale() would leave charset unset (NULL) when git_setup_gettext() returns early upon missing the locale directory (such as in uninstalled development builds). Because is_encoding_utf8(NULL) defaults to 1, is_utf8_locale() would mistakenly report a UTF-8 locale even under non-UTF-8 environments like LC_ALL=C. This patch enables the environment-derived fallback (LC_ALL, LC_CTYPE, LANG) whenever charset is unset, regardless of NO_GETTEXT.

- Patch 2 (repo: add Unicode support for repo structure output):
Updates builtin/repo.c (stats_table_setup_structure() and stats_table_print_structure()) to query is_utf8_locale() and emit Unicode box-drawing borders and bullets when running under a UTF-8 locale, falling back to ASCII when not in UTF-8. In t/t1901-repo-structure.sh, tests follow the established t/lib-git-svn.sh convention for discovering GIT_TEST_UTF8_LOCALE to define a UTF8_LOCALE test prerequisite, existing tests are pinned to LC_ALL=C to reliably test the ASCII fallback path, and new tests are added to verify the UTF-8 output rendering (including when running with a missing locale directory).

K Jayatheerth (2):
gettext: fall back to env-derived charset when unset
repo: add Unicode support for repo structure output

 builtin/repo.c            | 110 ++++++++++++++++-------------
 gettext.c                 |  32 +++++----
 t/t1901-repo-structure.sh | 141 +++++++++++++++++++++++++++++++++++++-
 3 files changed, 221 insertions(+), 62 deletions(-)
Signature · 1 line
2.55.GIT
#1 lore raw

PATCH 1/2 gettext: fall back to env-derived charset when unset

is_utf8_locale() relies on the static charset variable, which is normally initialized by init_gettext_charset(). That initialization only happens when git_setup_gettext() successfully locates the locale directory.

When running directly from the source tree without make install, or in other environments where the locale directory is unavailable, git_setup_gettext() returns early, leaving charset unset (NULL). Because is_encoding_utf8(NULL) defaults to 1, is_utf8_locale() would mistakenly report a UTF-8 locale even in non-UTF-8 environments (e.g. under LC_ALL=C).

The fallback that derives the charset from LC_ALL, LC_CTYPE, or LANG was previously compiled only under NO_GETTEXT. That left gettext-enabled builds without a fallback when charset remains uninitialized.

Make the fallback conditional on charset being unset rather than on NO_GETTEXT. This ensures is_utf8_locale() accurately inspects the environment-derived charset regardless of whether gettext support is enabled.

---
 gettext.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
gettext.c +18 -14
index 8d08a61f84..5376a0de0f 100644
@@ -141,19 +141,23 @@ int gettext_width(const char *s)
 
 int is_utf8_locale(void)
 {
-#ifdef NO_GETTEXT
-	if (!charset) {
-		const char *env = getenv("LC_ALL");
-		if (!env || !*env)
-			env = getenv("LC_CTYPE");
-		if (!env || !*env)
-			env = getenv("LANG");
-		if (!env)
-			env = "";
-		if (strchr(env, '.'))
-			env = strchr(env, '.') + 1;
-		charset = xstrdup(env);
+	const char *c = charset;
+
+	if (!c) {
+		static char fallback_charset[64];
+		if (!*fallback_charset) {
+			const char *env = getenv("LC_ALL");
+			if (!env || !*env)
+				env = getenv("LC_CTYPE");
+			if (!env || !*env)
+				env = getenv("LANG");
+			if (!env)
+				env = "";
+			if (strchr(env, '.'))
+				env = strchr(env, '.') + 1;
+			strlcpy(fallback_charset, env, sizeof(fallback_charset));
+		}
+		c = fallback_charset;
 	}
-#endif
-	return is_encoding_utf8(charset);
+	return is_encoding_utf8(c);
 }
Signature · 1 line
2.55.GIT
#2 lore raw

PATCH 2/2 repo: add Unicode support for `repo structure` output

In reply to K Jayatheerth

ASCII output ignores locale support for UTF-8. Use box-drawing characters and bullets when the locale supports UTF-8, since modern terminals render tables cleanly this way, and fall back to ASCII otherwise.

Tests now discover an available UTF-8 locale to set a UTF8_LOCALE prerequisite, and existing ASCII table tests are explicitly pinned to LC_ALL=C so their behavior remains deterministic regardless of the runner's environment.

---
 builtin/repo.c            | 110 ++++++++++++++++-------------
 t/t1901-repo-structure.sh | 141 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 203 insertions(+), 48 deletions(-)
builtin/repo.c +64 -46
index 84e012f83f..2c1cca6f2e 100644
@@ -498,82 +498,86 @@ static void stats_table_setup_structure(struct stats_table *table,
 	size_t object_count_total;
 	size_t disk_object_total;
 	size_t ref_total;
+	const int utf8 = is_utf8_locale();
+	const char *bullet_l0 = utf8 ? "•" : "*";
+	const char *bullet_l1 = utf8 ? "  •" : "  *";
+	const char *bullet_l2 = utf8 ? "    •" : "    *";
 
 	ref_total = get_total_reference_count(refs);
-	stats_table_addf(table, "* %s", _("References"));
-	stats_table_count_addf(table, ref_total, "  * %s", _("Count"));
-	stats_table_count_addf(table, refs->branches, "    * %s", _("Branches"));
-	stats_table_count_addf(table, refs->tags, "    * %s", _("Tags"));
-	stats_table_count_addf(table, refs->remotes, "    * %s", _("Remotes"));
-	stats_table_count_addf(table, refs->others, "    * %s", _("Others"));
+	stats_table_addf(table, "%s %s", bullet_l0, _("References"));
+	stats_table_count_addf(table, ref_total, "%s %s", bullet_l1, _("Count"));
+	stats_table_count_addf(table, refs->branches, "%s %s", bullet_l2, _("Branches"));
+	stats_table_count_addf(table, refs->tags, "%s %s", bullet_l2, _("Tags"));
+	stats_table_count_addf(table, refs->remotes, "%s %s", bullet_l2, _("Remotes"));
+	stats_table_count_addf(table, refs->others, "%s %s", bullet_l2, _("Others"));
 
 	object_count_total = get_total_object_values(&objects->type_counts);
 	stats_table_addf(table, "");
-	stats_table_addf(table, "* %s", _("Reachable objects"));
-	stats_table_count_addf(table, object_count_total, "  * %s", _("Count"));
+	stats_table_addf(table, "%s %s", bullet_l0, _("Reachable objects"));
+	stats_table_count_addf(table, object_count_total, "%s %s", bullet_l1, _("Count"));
 	stats_table_count_addf(table, objects->type_counts.commits,
-			       "    * %s", _("Commits"));
+			       "%s %s", bullet_l2, _("Commits"));
 	stats_table_count_addf(table, objects->type_counts.trees,
-			       "    * %s", _("Trees"));
+			       "%s %s", bullet_l2, _("Trees"));
 	stats_table_count_addf(table, objects->type_counts.blobs,
-			       "    * %s", _("Blobs"));
+			       "%s %s", bullet_l2, _("Blobs"));
 	stats_table_count_addf(table, objects->type_counts.tags,
-			       "    * %s", _("Tags"));
+			       "%s %s", bullet_l2, _("Tags"));
 
 	inflated_object_total = get_total_object_values(&objects->inflated_sizes);
 	stats_table_size_addf(table, inflated_object_total,
-			      "  * %s", _("Inflated size"));
+			      "%s %s", bullet_l1, _("Inflated size"));
 	stats_table_size_addf(table, objects->inflated_sizes.commits,
-			      "    * %s", _("Commits"));
+			      "%s %s", bullet_l2, _("Commits"));
 	stats_table_size_addf(table, objects->inflated_sizes.trees,
-			      "    * %s", _("Trees"));
+			      "%s %s", bullet_l2, _("Trees"));
 	stats_table_size_addf(table, objects->inflated_sizes.blobs,
-			      "    * %s", _("Blobs"));
+			      "%s %s", bullet_l2, _("Blobs"));
 	stats_table_size_addf(table, objects->inflated_sizes.tags,
-			      "    * %s", _("Tags"));
+			      "%s %s", bullet_l2, _("Tags"));
 
 	disk_object_total = get_total_object_values(&objects->disk_sizes);
 	stats_table_size_addf(table, disk_object_total,
-			      "  * %s", _("Disk size"));
+			      "%s %s", bullet_l1, _("Disk size"));
 	stats_table_size_addf(table, objects->disk_sizes.commits,
-			      "    * %s", _("Commits"));
+			      "%s %s", bullet_l2, _("Commits"));
 	stats_table_size_addf(table, objects->disk_sizes.trees,
-			      "    * %s", _("Trees"));
+			      "%s %s", bullet_l2, _("Trees"));
 	stats_table_size_addf(table, objects->disk_sizes.blobs,
-			      "    * %s", _("Blobs"));
+			      "%s %s", bullet_l2, _("Blobs"));
 	stats_table_size_addf(table, objects->disk_sizes.tags,
-			      "    * %s", _("Tags"));
+			      "%s %s", bullet_l2, _("Tags"));
 
 	stats_table_addf(table, "");
-	stats_table_addf(table, "* %s", _("Largest objects"));
-	stats_table_addf(table, "  * %s", _("Commits"));
+	stats_table_addf(table, "%s %s", bullet_l0, _("Largest objects"));
+	stats_table_addf(table, "%s %s", bullet_l1, _("Commits"));
 	stats_table_object_size_addf(table,
 				     &objects->largest.commit_size.oid,
 				     objects->largest.commit_size.value,
-				     "    * %s", _("Maximum size"));
+				     "%s %s", bullet_l2, _("Maximum size"));
 	stats_table_object_count_addf(table,
 				      &objects->largest.parent_count.oid,
 				      objects->largest.parent_count.value,
-				      "    * %s", _("Maximum parents"));
-	stats_table_addf(table, "  * %s", _("Trees"));
+				      "%s %s", bullet_l2, _("Maximum parents"));
+	stats_table_addf(table, "%s %s", bullet_l1, _("Trees"));
 	stats_table_object_size_addf(table,
 				     &objects->largest.tree_size.oid,
 				     objects->largest.tree_size.value,
-				     "    * %s", _("Maximum size"));
+				     "%s %s", bullet_l2, _("Maximum size"));
 	stats_table_object_count_addf(table,
 				      &objects->largest.tree_entries.oid,
 				      objects->largest.tree_entries.value,
-				      "    * %s", _("Maximum entries"));
-	stats_table_addf(table, "  * %s", _("Blobs"));
+				      "%s %s", bullet_l2, _("Maximum entries"));
+	stats_table_addf(table, "%s %s", bullet_l1, _("Blobs"));
 	stats_table_object_size_addf(table,
 				     &objects->largest.blob_size.oid,
 				     objects->largest.blob_size.value,
-				     "    * %s", _("Maximum size"));
-	stats_table_addf(table, "  * %s", _("Tags"));
+				     "%s %s", bullet_l2, _("Maximum size"));
+	stats_table_addf(table, "%s %s", bullet_l1, _("Tags"));
 	stats_table_object_size_addf(table,
 				     &objects->largest.tag_size.oid,
 				     objects->largest.tag_size.value,
-				     "    * %s", _("Maximum size"));
+				     "%s %s", bullet_l2, _("Maximum size"));
 }
 
 #define INDEX_WIDTH 4
@@ -589,28 +593,42 @@ static void stats_table_print_structure(const struct stats_table *table)
 	int unit_col_width = table->unit_col_width;
 	struct string_list_item *item;
 	struct strbuf buf = STRBUF_INIT;
+	const int utf8 = is_utf8_locale();
+	const char *border_left = utf8 ? "│ " : "| ";
+	const char *border_mid = utf8 ? " │ " : " | ";
+	const char *border_right = utf8 ? " │" : " |";
 
 	if (title_name_width > name_col_width)
 		name_col_width = title_name_width;
 	if (title_value_width > value_col_width + unit_col_width + 1)
 		value_col_width = title_value_width - unit_col_width;
 
-	strbuf_addstr(&buf, "| ");
+	strbuf_addstr(&buf, border_left);
 	strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width + INDEX_WIDTH,
 			  name_col_title);
-	strbuf_addstr(&buf, " | ");
+	strbuf_addstr(&buf, border_mid);
 	strbuf_utf8_align(&buf, ALIGN_LEFT,
 			  value_col_width + unit_col_width + 1, value_col_title);
-	strbuf_addstr(&buf, " |");
+	strbuf_addstr(&buf, border_right);
 	printf("%s\n", buf.buf);
 
-	printf("| ");
-	for (int i = 0; i < name_col_width + INDEX_WIDTH; i++)
-		putchar('-');
-	printf(" | ");
-	for (int i = 0; i < value_col_width + unit_col_width + 1; i++)
-		putchar('-');
-	printf(" |\n");
+	if (utf8) {
+		printf("├─");
+		for (int i = 0; i < name_col_width + INDEX_WIDTH; i++)
+			printf("─");
+		printf("─┼─");
+		for (int i = 0; i < value_col_width + unit_col_width + 1; i++)
+			printf("─");
+		printf("─┤\n");
+	} else {
+		printf("| ");
+		for (int i = 0; i < name_col_width + INDEX_WIDTH; i++)
+			putchar('-');
+		printf(" | ");
+		for (int i = 0; i < value_col_width + unit_col_width + 1; i++)
+			putchar('-');
+		printf(" |\n");
+	}
 
 	for_each_string_list_item(item, &table->rows) {
 		struct stats_table_entry *entry = item->util;
@@ -624,7 +642,7 @@ static void stats_table_print_structure(const struct stats_table *table)
 		}
 
 		strbuf_reset(&buf);
-		strbuf_addstr(&buf, "| ");
+		strbuf_addstr(&buf, border_left);
 		strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width, item->string);
 
 		if (entry && entry->oid)
@@ -633,11 +651,11 @@ static void stats_table_print_structure(const struct stats_table *table)
 		else
 			strbuf_addchars(&buf, ' ', INDEX_WIDTH);
 
-		strbuf_addstr(&buf, " | ");
+		strbuf_addstr(&buf, border_mid);
 		strbuf_utf8_align(&buf, ALIGN_RIGHT, value_col_width, value);
 		strbuf_addch(&buf, ' ');
 		strbuf_utf8_align(&buf, ALIGN_LEFT, unit_col_width, unit);
-		strbuf_addstr(&buf, " |");
+		strbuf_addstr(&buf, border_right);
 		printf("%s\n", buf.buf);
 	}
 
t/t1901-repo-structure.sh +139 -2

163 lines not on this page · raw message

Signature · 1 line
2.55.GIT
#3 lore raw
In reply to K Jayatheerth
Quoted 6 lines · K Jayatheerth

Currently, git repo structure always renders its table output using plain ASCII characters (|, -, *), regardless of the user's locale. This series introduces Unicode box-drawing characters (│, ├, ─, ┼, ┤) and bullet points (•) when a UTF-8 locale is detected, providing a cleaner and more visually distinct hierarchical output on modern terminals while gracefully falling back to the existing ASCII formatting otherwise.

Generally speaking, Unicode box-drawing characters do not work as ASCII art components as well as they should, because terminals often do not agree on how wide they should be rendered.

So I am not very enthusiastic about this change.

#4 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · K Jayatheerth

Currently, git repo structure always renders its table output using plain ASCII characters (|, -, *), regardless of the user's locale. This series introduces Unicode box-drawing characters (│, ├, ─, ┼, ┤) and bullet points (•) when a UTF-8 locale is detected, providing a cleaner and more visually distinct hierarchical output on modern terminals while gracefully falling back to the existing ASCII formatting otherwise.

Quoted 4 lines

Generally speaking, Unicode box-drawing characters do not work as ASCII art components as well as they should, because terminals often do not agree on how wide they should be rendered.

i can instantly think of two popular applications i'm using every day which use box drawing chars: mutt (thread and attachment tree displays) and midnight commander (panel/dialog frames). if "often" above were accurate, these projects would be getting a lot of complaints about that, which they don't.

if compatibility with broken terminals/fonts is considered important, an option can be introduced to opt out from the modern experience.

(any takers for giving git log --graph the same treatment?)

#5 lore raw
Quoted 2 lines · Oswald Buddenhagen

if compatibility with broken terminals/fonts is considered important, an option can be introduced to opt out from the modern experience.

Absolutely. My response was mostly a reaction to the implementation that only paid attention to the locale settings, leaving no way for people to opt out without discarding UTF-8 altogether.

#6 lore raw
In reply to Oswald Buddenhagen
Quoted 6 lines · Oswald Buddenhagen

i can instantly think of two popular applications i'm using every day which use box drawing chars: mutt (thread and attachment tree displays) and midnight commander (panel/dialog frames). if "often" above were accurate, these projects would be getting a lot of complaints about that, which they don't.

Hmm, mutt seems to use ACS (alternative character set) as a fallback option:

```
            else if (Charset_is_utf8)
              addstr("\342\224\224"); /* WACS_LLCORNER */
            else
              addch(ACS_LLCORNER);
```

How relevant that is to us, I don't know, but perhaps something to think about.

Signature · 1 line
Tuomas