i18n: config: unfold error messages marked for translation

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 config_oringin_type to ease management of the various
configuration origin types (blob, file, etc).  Previously origin type
was considered from command line if cf->origin_type == NULL, i.e.,
uninitialized. Now we set origin_type to CONFIG_ORIGIN_CMDLINE in
git_config_from_parameters() and configset_add_value().

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

Signed-off-by: Vasco Almeida <vascomalmeida@sapo.pt>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Vasco Almeida 2016-07-28 13:14:03 +00:00 committed by Junio C Hamano
parent 996ee6d27a
commit 1b8132d99d
3 changed files with 109 additions and 22 deletions

12
cache.h
View File

@ -1566,10 +1566,18 @@ struct git_config_source {
const char *blob; const char *blob;
}; };


enum config_origin_type {
CONFIG_ORIGIN_BLOB,
CONFIG_ORIGIN_FILE,
CONFIG_ORIGIN_STDIN,
CONFIG_ORIGIN_SUBMODULE_BLOB,
CONFIG_ORIGIN_CMDLINE
};

typedef int (*config_fn_t)(const char *, const char *, void *); typedef int (*config_fn_t)(const char *, const char *, void *);
extern int git_default_config(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_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 config_origin_type,
const char *name, const char *buf, size_t len, void *data); const char *name, const char *buf, size_t len, void *data);
extern void git_config_push_parameter(const char *text); extern void git_config_push_parameter(const char *text);
extern int git_config_from_parameters(config_fn_t fn, void *data); extern int git_config_from_parameters(config_fn_t fn, void *data);
@ -1713,7 +1721,7 @@ extern int ignore_untracked_cache_config;
struct key_value_info { struct key_value_info {
const char *filename; const char *filename;
int linenr; int linenr;
const char *origin_type; enum config_origin_type origin_type;
enum config_scope scope; enum config_scope scope;
}; };



117
config.c
View File

@ -24,7 +24,7 @@ struct config_source {
size_t pos; size_t pos;
} buf; } buf;
} u; } u;
const char *origin_type; enum config_origin_type origin_type;
const char *name; const char *name;
const char *path; const char *path;
int die_on_error; int die_on_error;
@ -245,6 +245,7 @@ int git_config_from_parameters(config_fn_t fn, void *data)


memset(&source, 0, sizeof(source)); memset(&source, 0, sizeof(source));
source.prev = cf; source.prev = cf;
source.origin_type = CONFIG_ORIGIN_CMDLINE;
cf = &source; cf = &source;


/* sq_dequote will write over it */ /* sq_dequote will write over it */
@ -453,6 +454,8 @@ static int git_parse_source(config_fn_t fn, void *data)
int comment = 0; int comment = 0;
int baselen = 0; int baselen = 0;
struct strbuf *var = &cf->var; struct strbuf *var = &cf->var;
int error_return = 0;
char *error_msg = NULL;


/* U+FEFF Byte Order Mark in UTF8 */ /* U+FEFF Byte Order Mark in UTF8 */
const char *bomptr = utf8_bom; const char *bomptr = utf8_bom;
@ -507,10 +510,40 @@ static int git_parse_source(config_fn_t fn, void *data)
if (get_value(fn, data, var) < 0) if (get_value(fn, data, var) < 0)
break; break;
} }

switch (cf->origin_type) {
case CONFIG_ORIGIN_BLOB:
error_msg = xstrfmt(_("bad config line %d in blob %s"),
cf->linenr, cf->name);
break;
case CONFIG_ORIGIN_FILE:
error_msg = xstrfmt(_("bad config line %d in file %s"),
cf->linenr, cf->name);
break;
case CONFIG_ORIGIN_STDIN:
error_msg = xstrfmt(_("bad config line %d in standard input"),
cf->linenr);
break;
case CONFIG_ORIGIN_SUBMODULE_BLOB:
error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"),
cf->linenr, cf->name);
break;
case CONFIG_ORIGIN_CMDLINE:
error_msg = xstrfmt(_("bad config line %d in command line %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) if (cf->die_on_error)
die(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name); die("%s", error_msg);
else else
return error(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name); error_return = error("%s", error_msg);

free(error_msg);
return error_return;
} }


static int parse_unit_factor(const char *end, uintmax_t *val) static int parse_unit_factor(const char *end, uintmax_t *val)
@ -619,16 +652,47 @@ int git_parse_ulong(const char *value, unsigned long *ret)
NORETURN NORETURN
static void die_bad_number(const char *name, const char *value) static void die_bad_number(const char *name, const char *value)
{ {
const char *reason = errno == ERANGE ?
"out of range" :
"invalid unit";
if (!value) if (!value)
value = ""; value = "";


if (cf && cf->origin_type && cf->name) if (!(cf && cf->name))
die(_("bad numeric config value '%s' for '%s' in %s %s: %s"), die(errno == ERANGE
value, name, cf->origin_type, cf->name, reason); ? _("bad numeric config value '%s' for '%s': out of range")
die(_("bad numeric config value '%s' for '%s': %s"), value, name, reason); : _("bad numeric config value '%s' for '%s': invalid unit"),
value, name);

switch (cf->origin_type) {
case CONFIG_ORIGIN_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 CONFIG_ORIGIN_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 CONFIG_ORIGIN_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 CONFIG_ORIGIN_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);
case CONFIG_ORIGIN_CMDLINE:
die(errno == ERANGE
? _("bad numeric config value '%s' for '%s' in command line %s: out of range")
: _("bad numeric config value '%s' for '%s' in command line %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) int git_config_int(const char *name, const char *value)
@ -1105,7 +1169,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, static int do_config_from_file(config_fn_t fn,
const char *origin_type, const char *name, const char *path, FILE *f, const enum config_origin_type origin_type,
const char *name, const char *path, FILE *f,
void *data) void *data)
{ {
struct config_source top; struct config_source top;
@ -1124,7 +1189,7 @@ static int do_config_from_file(config_fn_t fn,


static int git_config_from_stdin(config_fn_t fn, void *data) 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, CONFIG_ORIGIN_STDIN, "", NULL, stdin, data);
} }


int git_config_from_file(config_fn_t fn, const char *filename, void *data) int git_config_from_file(config_fn_t fn, const char *filename, void *data)
@ -1135,14 +1200,14 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
f = fopen(filename, "r"); f = fopen(filename, "r");
if (f) { if (f) {
flockfile(f); flockfile(f);
ret = do_config_from_file(fn, "file", filename, filename, f, data); ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, filename, f, data);
funlockfile(f); funlockfile(f);
fclose(f); fclose(f);
} }
return ret; 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 config_origin_type origin_type,
const char *name, const char *buf, size_t len, void *data) const char *name, const char *buf, size_t len, void *data)
{ {
struct config_source top; struct config_source top;
@ -1179,7 +1244,7 @@ static int git_config_from_blob_sha1(config_fn_t fn,
return error("reference '%s' does not point to a blob", name); 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, CONFIG_ORIGIN_BLOB, name, buf, size, data);
free(buf); free(buf);


return ret; return ret;
@ -1390,12 +1455,12 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
if (cf->name) { if (cf->name) {
kv_info->filename = strintern(cf->name); kv_info->filename = strintern(cf->name);
kv_info->linenr = cf->linenr; kv_info->linenr = cf->linenr;
kv_info->origin_type = strintern(cf->origin_type); kv_info->origin_type = cf->origin_type;
} else { } else {
/* for values read from `git_config_from_parameters()` */ /* for values read from `git_config_from_parameters()` */
kv_info->filename = NULL; kv_info->filename = NULL;
kv_info->linenr = -1; kv_info->linenr = -1;
kv_info->origin_type = NULL; kv_info->origin_type = CONFIG_ORIGIN_CMDLINE;
} }
kv_info->scope = current_parsing_scope; kv_info->scope = current_parsing_scope;
si->util = kv_info; si->util = kv_info;
@ -2476,14 +2541,28 @@ int parse_config_key(const char *var,


const char *current_config_origin_type(void) const char *current_config_origin_type(void)
{ {
const char *type; int type;
if (current_config_kvi) if (current_config_kvi)
type = current_config_kvi->origin_type; type = current_config_kvi->origin_type;
else if(cf) else if(cf)
type = cf->origin_type; type = cf->origin_type;
else else
die("BUG: current_config_origin_type called outside config callback"); die("BUG: current_config_origin_type called outside config callback");
return type ? type : "command line";
switch (type) {
case CONFIG_ORIGIN_BLOB:
return "blob";
case CONFIG_ORIGIN_FILE:
return "file";
case CONFIG_ORIGIN_STDIN:
return "standard input";
case CONFIG_ORIGIN_SUBMODULE_BLOB:
return "submodule-blob";
case CONFIG_ORIGIN_CMDLINE:
return "command line";
default:
die("BUG: unknown config origin type");
}
} }


const char *current_config_name(void) const char *current_config_name(void)

View File

@ -448,7 +448,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
parameter.commit_sha1 = commit_sha1; parameter.commit_sha1 = commit_sha1;
parameter.gitmodules_sha1 = sha1; parameter.gitmodules_sha1 = sha1;
parameter.overwrite = 0; parameter.overwrite = 0;
git_config_from_mem(parse_config, "submodule-blob", rev.buf, git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
config, config_size, &parameter); config, config_size, &parameter);
free(config); free(config);