git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Vasco Almeida <vascomalmeida@sapo.pt>
To: git@vger.kernel.org
Cc: "Vasco Almeida" <vascomalmeida@sapo.pt>,
	"Jiang Xin" <worldhello.net@gmail.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	Sunshine <sunshine@sunshineco.com>,
	"Junio C Hamano" <gitster@pobox.com>
Subject: [PATCH v4 27/38] i18n: config: unfold error messages marked for translation
Date: Tue,  7 Jun 2016 11:52:26 +0000	[thread overview]
Message-ID: <1465300357-7557-28-git-send-email-vascomalmeida@sapo.pt> (raw)
In-Reply-To: <1465300357-7557-1-git-send-email-vascomalmeida@sapo.pt>

Introduced in 473166b ("config: add 'origin_type' to config_source
struct", 2016-02-19), Git can inform the user about the origin of a
config error, but the implementation does not allow translators to
translate the keywords 'file', 'blob, 'standard input', and
'submodule-blob'. Moreover, for the second message, a reason for the
error is appended to the message, not allowing translators to translate
that reason either.

Unfold the message into several templates for each known origin_type.
That would result in better translation at the expense of code
verbosity.

Add enum git_config_oringin_type to ease management of the various
configuration origin types (blob, file, etc).

For the first instance, use xstrfmt() function to prepare the message
string, instead of doing something like it's done for the second one,
because intelligibility and code conciseness are improved for that
instance.

Signed-off-by: Vasco Almeida <vascomalmeida@sapo.pt>
---
 cache.h            |   9 ++++-
 config.c           | 101 ++++++++++++++++++++++++++++++++++++++++++++---------
 submodule-config.c |   2 +-
 3 files changed, 94 insertions(+), 18 deletions(-)

diff --git a/cache.h b/cache.h
index 6049f86..4bded9a 100644
--- a/cache.h
+++ b/cache.h
@@ -1559,10 +1559,17 @@ struct git_config_source {
 	const char *blob;
 };
 
+enum git_config_origin_type {
+	CFG_BLOB,
+	CFG_FILE,
+	CFG_STDIN,
+	CFG_SUBMODULE_BLOB
+};
+
 typedef int (*config_fn_t)(const char *, const char *, void *);
 extern int git_default_config(const char *, const char *, void *);
 extern int git_config_from_file(config_fn_t fn, const char *, void *);
-extern int git_config_from_mem(config_fn_t fn, const char *origin_type,
+extern int git_config_from_mem(config_fn_t fn, const enum git_config_origin_type,
 					const char *name, const char *buf, size_t len, void *data);
 extern void git_config_push_parameter(const char *text);
 extern int git_config_from_parameters(config_fn_t fn, void *data);
diff --git a/config.c b/config.c
index f51c56b..e86f0aa 100644
--- a/config.c
+++ b/config.c
@@ -24,7 +24,7 @@ struct config_source {
 			size_t pos;
 		} buf;
 	} u;
-	const char *origin_type;
+	enum git_config_origin_type origin_type;
 	const char *name;
 	const char *path;
 	int die_on_error;
@@ -417,6 +417,8 @@ static int git_parse_source(config_fn_t fn, void *data)
 	int comment = 0;
 	int baselen = 0;
 	struct strbuf *var = &cf->var;
+	int error_return = 0;
+	char *error_msg = NULL;
 
 	/* U+FEFF Byte Order Mark in UTF8 */
 	const char *bomptr = utf8_bom;
@@ -471,10 +473,36 @@ static int git_parse_source(config_fn_t fn, void *data)
 		if (get_value(fn, data, var) < 0)
 			break;
 	}
+
+	switch (cf->origin_type) {
+	case CFG_BLOB:
+		error_msg = xstrfmt(_("bad config line %d in blob %s"),
+				      cf->linenr, cf->name);
+		break;
+	case CFG_FILE:
+		error_msg = xstrfmt(_("bad config line %d in file %s"),
+				      cf->linenr, cf->name);
+		break;
+	case CFG_STDIN:
+		error_msg = xstrfmt(_("bad config line %d in standard input"),
+				      cf->linenr);
+		break;
+	case CFG_SUBMODULE_BLOB:
+		error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"),
+				       cf->linenr, cf->name);
+		break;
+	default:
+		error_msg = xstrfmt(_("bad config line %d in %s"),
+				      cf->linenr, cf->name);
+	}
+
 	if (cf->die_on_error)
-		die(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
+		die(error_msg);
 	else
-		return error(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
+		error_return =  error(error_msg);
+
+	free(error_msg);
+	return error_return;
 }
 
 static int parse_unit_factor(const char *end, uintmax_t *val)
@@ -583,16 +611,42 @@ int git_parse_ulong(const char *value, unsigned long *ret)
 NORETURN
 static void die_bad_number(const char *name, const char *value)
 {
-	const char *reason = errno == ERANGE ?
-			     "out of range" :
-			     "invalid unit";
 	if (!value)
 		value = "";
 
-	if (cf && cf->origin_type && cf->name)
-		die(_("bad numeric config value '%s' for '%s' in %s %s: %s"),
-		    value, name, cf->origin_type, cf->name, reason);
-	die(_("bad numeric config value '%s' for '%s': %s"), value, name, reason);
+	if (!(cf && cf->name))
+		die(errno == ERANGE
+		    ? _("bad numeric config value '%s' for '%s': out of range")
+		    : _("bad numeric config value '%s' for '%s': invalid unit"),
+		    value, name);
+
+	switch (cf->origin_type) {
+	case CFG_BLOB:
+		die(errno == ERANGE
+		    ? _("bad numeric config value '%s' for '%s' in blob %s: out of range")
+		    : _("bad numeric config value '%s' for '%s' in blob %s: invalid unit"),
+		    value, name, cf->name);
+	case CFG_FILE:
+		die(errno == ERANGE
+		    ? _("bad numeric config value '%s' for '%s' in file %s: out of range")
+		    : _("bad numeric config value '%s' for '%s' in file %s: invalid unit"),
+		    value, name, cf->name);
+	case CFG_STDIN:
+		die(errno == ERANGE
+		    ? _("bad numeric config value '%s' for '%s' in standard input: out of range")
+		    : _("bad numeric config value '%s' for '%s' in standard input: invalid unit"),
+		    value, name);
+	case CFG_SUBMODULE_BLOB:
+		die(errno == ERANGE
+		    ? _("bad numeric config value '%s' for '%s' in submodule-blob %s: out of range")
+		    : _("bad numeric config value '%s' for '%s' in submodule-blob %s: invalid unit"),
+		    value, name, cf->name);
+	default:
+		die(errno == ERANGE
+		    ? _("bad numeric config value '%s' for '%s' in %s: out of range")
+		    : _("bad numeric config value '%s' for '%s' in %s: invalid unit"),
+		    value, name, cf->name);
+	}
 }
 
 int git_config_int(const char *name, const char *value)
@@ -1069,7 +1123,8 @@ static int do_config_from(struct config_source *top, config_fn_t fn, void *data)
 }
 
 static int do_config_from_file(config_fn_t fn,
-		const char *origin_type, const char *name, const char *path, FILE *f,
+		const enum git_config_origin_type origin_type,
+		const char *name, const char *path, FILE *f,
 		void *data)
 {
 	struct config_source top;
@@ -1088,7 +1143,7 @@ static int do_config_from_file(config_fn_t fn,
 
 static int git_config_from_stdin(config_fn_t fn, void *data)
 {
-	return do_config_from_file(fn, "standard input", "", NULL, stdin, data);
+	return do_config_from_file(fn, CFG_STDIN, "", NULL, stdin, data);
 }
 
 int git_config_from_file(config_fn_t fn, const char *filename, void *data)
@@ -1099,14 +1154,14 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
 	f = fopen(filename, "r");
 	if (f) {
 		flockfile(f);
-		ret = do_config_from_file(fn, "file", filename, filename, f, data);
+		ret = do_config_from_file(fn, CFG_FILE, filename, filename, f, data);
 		funlockfile(f);
 		fclose(f);
 	}
 	return ret;
 }
 
-int git_config_from_mem(config_fn_t fn, const char *origin_type,
+int git_config_from_mem(config_fn_t fn, const enum git_config_origin_type origin_type,
 			const char *name, const char *buf, size_t len, void *data)
 {
 	struct config_source top;
@@ -1143,7 +1198,7 @@ static int git_config_from_blob_sha1(config_fn_t fn,
 		return error("reference '%s' does not point to a blob", name);
 	}
 
-	ret = git_config_from_mem(fn, "blob", name, buf, size, data);
+	ret = git_config_from_mem(fn, CFG_BLOB, name, buf, size, data);
 	free(buf);
 
 	return ret;
@@ -2442,7 +2497,21 @@ int parse_config_key(const char *var,
 
 const char *current_config_origin_type(void)
 {
-	return cf && cf->origin_type ? cf->origin_type : "command line";
+	if (!cf)
+		return "command line";
+
+	switch (cf->origin_type) {
+	case CFG_BLOB:
+		return "blob";
+	case CFG_FILE:
+		return "file";
+	case CFG_STDIN:
+		return "standard input";
+	case CFG_SUBMODULE_BLOB:
+		return "submodule-blob";
+	default:
+		return "command line";
+	}
 }
 
 const char *current_config_name(void)
diff --git a/submodule-config.c b/submodule-config.c
index debab29..f53f6bd 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -439,7 +439,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
 	parameter.commit_sha1 = commit_sha1;
 	parameter.gitmodules_sha1 = sha1;
 	parameter.overwrite = 0;
-	git_config_from_mem(parse_config, "submodule-blob", rev.buf,
+	git_config_from_mem(parse_config, CFG_SUBMODULE_BLOB, rev.buf,
 			config, config_size, &parameter);
 	free(config);
 
-- 
2.7.3

  parent reply	other threads:[~2016-06-07 11:55 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-07 11:51 [PATCH v4 00/38] i18n and test updates Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 01/38] i18n: builtin/remote.c: fix mark for translation Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 02/38] i18n: advice: mark string about detached head " Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 03/38] i18n: advice: internationalize message for conflicts Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 04/38] i18n: transport: mark strings for translation Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 05/38] i18n: sequencer: mark entire sentences " Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 06/38] i18n: sequencer: mark string " Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 07/38] i18n: merge-octopus: mark messages " Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 08/38] merge-octupus: use die shell function from git-sh-setup.sh Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 09/38] i18n: rebase: fix marked string to use eval_gettext variant Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 10/38] i18n: rebase: mark placeholder for translation Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 11/38] i18n: bisect: simplify error message for i18n Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 12/38] t6030: update to use test_i18ncmp Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 13/38] i18n: git-sh-setup.sh: mark strings for translation Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 14/38] i18n: rebase-interactive: " Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 15/38] i18n: rebase-interactive: mark here-doc " Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 16/38] i18n: rebase-interactive: mark comments of squash " Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 17/38] i18n: setup: mark strings " Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 18/38] tests: use test_i18n* functions to suppress false positives Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 19/38] tests: unpack-trees: update to use test_i18n* functions Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 20/38] t9003: become resilient to GETTEXT_POISON Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 21/38] t4153: fix negated test_i18ngrep call Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 22/38] t5523: use test_i18ngrep for negation Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 23/38] i18n: bisect: mark strings for translation Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 24/38] i18n: transport-helper.c: change N_() call to _() Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 25/38] i18n: notes: mark strings for translation Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 26/38] i18n: notes: mark options " Vasco Almeida
2016-06-07 11:52 ` Vasco Almeida [this message]
2016-06-07 22:25   ` [PATCH v4 27/38] i18n: config: unfold error messages marked " Junio C Hamano
2016-06-07 11:52 ` [PATCH v4 28/38] i18n: merge: mark messages " Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 29/38] i18n: merge: change command option help to lowercase Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 30/38] i18n: sequencer: add period to error message Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 31/38] i18n: standardise messages Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 32/38] i18n: remote: mark URL fallback text for translation Vasco Almeida
2016-06-07 11:52 ` [PATCH v4 33/38] i18n: remote: allow translations to reorder message Vasco Almeida
2016-06-07 12:55 ` [PATCH v4 34/38] i18n: init-db: join message pieces Vasco Almeida
2016-06-17 17:41   ` Vasco Almeida
2016-06-07 12:55 ` [PATCH v4 35/38] i18n: submodule: join strings marked for translation Vasco Almeida
2016-06-07 12:55 ` [PATCH v4 36/38] i18n: submodule: escape shell variables inside eval_gettext Vasco Almeida
2016-06-07 12:55 ` [PATCH v4 37/38] i18n: unmark die messages for translation Vasco Almeida
2016-06-07 22:07   ` Junio C Hamano
2016-06-08 11:51     ` Pranit Bauva
2016-06-07 12:55 ` [PATCH v4 38/38] i18n: branch: mark comment when editing branch description " Vasco Almeida

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1465300357-7557-28-git-send-email-vascomalmeida@sapo.pt \
    --to=vascomalmeida@sapo.pt \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=sunshine@sunshineco.com \
    --cc=worldhello.net@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).