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

[GSoC Patch 1/2] gettext: fall back to env-derived charset when unset

From
K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
Date
Aug 21, 2026, 13:53 UTC
Message-ID
<20260821135410.429698-2-jayatheerthkulkarni2005@gmail.com>
In-Reply-To
<20260821135410.429698-1-jayatheerthkulkarni2005@gmail.com>

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

Mentored-by: Justin Tobler <jltobler@gmail.com>
Mentored-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com>
Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
---
 gettext.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
diff --git a/gettext.c b/gettext.c
index 8d08a61f84..5376a0de0f 100644
--- a/gettext.c
+++ b/gettext.c
@@ -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);
 }
-- 
2.55.GIT
Previous: K JayatheerthNext: K Jayatheerth
Message 2 of 7 in “add unicode support to git repo structure”
  1. 0/2 add unicode support to git repo structureK Jayatheerth, Aug 21, 2026
  2. 1/2 gettext: fall back to env-derived charset when unsetK Jayatheerth, Aug 21, 2026
  3. 2/2 repo: add Unicode support for `repo structure` outputK Jayatheerth, Aug 21, 2026
  4. Junio C HamanoAug 21, 2026
  5. Oswald BuddenhagenAug 23, 2026
  6. Junio C HamanoAug 23, 2026
  7. Tuomas AholaAug 23, 2026

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.