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>

Vasco Almeida committed Jul 28, 2016 at 13:14 UTC 1b8132d99d80ca98218c2685bbe5ba016a0a42f8
3 files changed +109 -22
cache.h
+10 -2
@@ -1566,10 +1566,18 @@ struct git_config_source {
1566 const char *blob;
1567 };
1568
1569 +enum config_origin_type {
1570 + CONFIG_ORIGIN_BLOB,
1571 + CONFIG_ORIGIN_FILE,
1572 + CONFIG_ORIGIN_STDIN,
1573 + CONFIG_ORIGIN_SUBMODULE_BLOB,
1574 + CONFIG_ORIGIN_CMDLINE
1575 +};
1576 +
1577 typedef int (*config_fn_t)(const char *, const char *, void *);
1578 extern int git_default_config(const char *, const char *, void *);
1579 extern int git_config_from_file(config_fn_t fn, const char *, void *);
1572 -extern int git_config_from_mem(config_fn_t fn, const char *origin_type,
1580 +extern int git_config_from_mem(config_fn_t fn, const enum config_origin_type,
1581 const char *name, const char *buf, size_t len, void *data);
1582 extern void git_config_push_parameter(const char *text);
1583 extern int git_config_from_parameters(config_fn_t fn, void *data);
@@ -1713,7 +1721,7 @@ extern int ignore_untracked_cache_config;
1721 struct key_value_info {
1722 const char *filename;
1723 int linenr;
1716 - const char *origin_type;
1724 + enum config_origin_type origin_type;
1725 enum config_scope scope;
1726 };
1727
config.c
+98 -19
@@ -24,7 +24,7 @@ struct config_source {
24 size_t pos;
25 } buf;
26 } u;
27 - const char *origin_type;
27 + enum config_origin_type origin_type;
28 const char *name;
29 const char *path;
30 int die_on_error;
@@ -245,6 +245,7 @@ int git_config_from_parameters(config_fn_t fn, void *data)
245
246 memset(&source, 0, sizeof(source));
247 source.prev = cf;
248 + source.origin_type = CONFIG_ORIGIN_CMDLINE;
249 cf = &source;
250
251 /* sq_dequote will write over it */
@@ -453,6 +454,8 @@ static int git_parse_source(config_fn_t fn, void *data)
454 int comment = 0;
455 int baselen = 0;
456 struct strbuf *var = &cf->var;
457 + int error_return = 0;
458 + char *error_msg = NULL;
459
460 /* U+FEFF Byte Order Mark in UTF8 */
461 const char *bomptr = utf8_bom;
@@ -507,10 +510,40 @@ static int git_parse_source(config_fn_t fn, void *data)
510 if (get_value(fn, data, var) < 0)
511 break;
512 }
513 +
514 + switch (cf->origin_type) {
515 + case CONFIG_ORIGIN_BLOB:
516 + error_msg = xstrfmt(_("bad config line %d in blob %s"),
517 + cf->linenr, cf->name);
518 + break;
519 + case CONFIG_ORIGIN_FILE:
520 + error_msg = xstrfmt(_("bad config line %d in file %s"),
521 + cf->linenr, cf->name);
522 + break;
523 + case CONFIG_ORIGIN_STDIN:
524 + error_msg = xstrfmt(_("bad config line %d in standard input"),
525 + cf->linenr);
526 + break;
527 + case CONFIG_ORIGIN_SUBMODULE_BLOB:
528 + error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"),
529 + cf->linenr, cf->name);
530 + break;
531 + case CONFIG_ORIGIN_CMDLINE:
532 + error_msg = xstrfmt(_("bad config line %d in command line %s"),
533 + cf->linenr, cf->name);
534 + break;
535 + default:
536 + error_msg = xstrfmt(_("bad config line %d in %s"),
537 + cf->linenr, cf->name);
538 + }
539 +
540 if (cf->die_on_error)
511 - die(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
541 + die("%s", error_msg);
542 else
513 - return error(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
543 + error_return = error("%s", error_msg);
544 +
545 + free(error_msg);
546 + return error_return;
547 }
548
549 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)
652 NORETURN
653 static void die_bad_number(const char *name, const char *value)
654 {
622 - const char *reason = errno == ERANGE ?
623 - "out of range" :
624 - "invalid unit";
655 if (!value)
656 value = "";
657
628 - if (cf && cf->origin_type && cf->name)
629 - die(_("bad numeric config value '%s' for '%s' in %s %s: %s"),
630 - value, name, cf->origin_type, cf->name, reason);
631 - die(_("bad numeric config value '%s' for '%s': %s"), value, name, reason);
658 + if (!(cf && cf->name))
659 + die(errno == ERANGE
660 + ? _("bad numeric config value '%s' for '%s': out of range")
661 + : _("bad numeric config value '%s' for '%s': invalid unit"),
662 + value, name);
663 +
664 + switch (cf->origin_type) {
665 + case CONFIG_ORIGIN_BLOB:
666 + die(errno == ERANGE
667 + ? _("bad numeric config value '%s' for '%s' in blob %s: out of range")
668 + : _("bad numeric config value '%s' for '%s' in blob %s: invalid unit"),
669 + value, name, cf->name);
670 + case CONFIG_ORIGIN_FILE:
671 + die(errno == ERANGE
672 + ? _("bad numeric config value '%s' for '%s' in file %s: out of range")
673 + : _("bad numeric config value '%s' for '%s' in file %s: invalid unit"),
674 + value, name, cf->name);
675 + case CONFIG_ORIGIN_STDIN:
676 + die(errno == ERANGE
677 + ? _("bad numeric config value '%s' for '%s' in standard input: out of range")
678 + : _("bad numeric config value '%s' for '%s' in standard input: invalid unit"),
679 + value, name);
680 + case CONFIG_ORIGIN_SUBMODULE_BLOB:
681 + die(errno == ERANGE
682 + ? _("bad numeric config value '%s' for '%s' in submodule-blob %s: out of range")
683 + : _("bad numeric config value '%s' for '%s' in submodule-blob %s: invalid unit"),
684 + value, name, cf->name);
685 + case CONFIG_ORIGIN_CMDLINE:
686 + die(errno == ERANGE
687 + ? _("bad numeric config value '%s' for '%s' in command line %s: out of range")
688 + : _("bad numeric config value '%s' for '%s' in command line %s: invalid unit"),
689 + value, name, cf->name);
690 + default:
691 + die(errno == ERANGE
692 + ? _("bad numeric config value '%s' for '%s' in %s: out of range")
693 + : _("bad numeric config value '%s' for '%s' in %s: invalid unit"),
694 + value, name, cf->name);
695 + }
696 }
697
698 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)
1169 }
1170
1171 static int do_config_from_file(config_fn_t fn,
1108 - const char *origin_type, const char *name, const char *path, FILE *f,
1172 + const enum config_origin_type origin_type,
1173 + const char *name, const char *path, FILE *f,
1174 void *data)
1175 {
1176 struct config_source top;
@@ -1124,7 +1189,7 @@ static int do_config_from_file(config_fn_t fn,
1189
1190 static int git_config_from_stdin(config_fn_t fn, void *data)
1191 {
1127 - return do_config_from_file(fn, "standard input", "", NULL, stdin, data);
1192 + return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin, data);
1193 }
1194
1195 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)
1200 f = fopen(filename, "r");
1201 if (f) {
1202 flockfile(f);
1138 - ret = do_config_from_file(fn, "file", filename, filename, f, data);
1203 + ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, filename, f, data);
1204 funlockfile(f);
1205 fclose(f);
1206 }
1207 return ret;
1208 }
1209
1145 -int git_config_from_mem(config_fn_t fn, const char *origin_type,
1210 +int git_config_from_mem(config_fn_t fn, const enum config_origin_type origin_type,
1211 const char *name, const char *buf, size_t len, void *data)
1212 {
1213 struct config_source top;
@@ -1179,7 +1244,7 @@ static int git_config_from_blob_sha1(config_fn_t fn,
1244 return error("reference '%s' does not point to a blob", name);
1245 }
1246
1182 - ret = git_config_from_mem(fn, "blob", name, buf, size, data);
1247 + ret = git_config_from_mem(fn, CONFIG_ORIGIN_BLOB, name, buf, size, data);
1248 free(buf);
1249
1250 return ret;
@@ -1390,12 +1455,12 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
1455 if (cf->name) {
1456 kv_info->filename = strintern(cf->name);
1457 kv_info->linenr = cf->linenr;
1393 - kv_info->origin_type = strintern(cf->origin_type);
1458 + kv_info->origin_type = cf->origin_type;
1459 } else {
1460 /* for values read from `git_config_from_parameters()` */
1461 kv_info->filename = NULL;
1462 kv_info->linenr = -1;
1398 - kv_info->origin_type = NULL;
1463 + kv_info->origin_type = CONFIG_ORIGIN_CMDLINE;
1464 }
1465 kv_info->scope = current_parsing_scope;
1466 si->util = kv_info;
@@ -2476,14 +2541,28 @@ int parse_config_key(const char *var,
2541
2542 const char *current_config_origin_type(void)
2543 {
2479 - const char *type;
2544 + int type;
2545 if (current_config_kvi)
2546 type = current_config_kvi->origin_type;
2547 else if(cf)
2548 type = cf->origin_type;
2549 else
2550 die("BUG: current_config_origin_type called outside config callback");
2486 - return type ? type : "command line";
2551 +
2552 + switch (type) {
2553 + case CONFIG_ORIGIN_BLOB:
2554 + return "blob";
2555 + case CONFIG_ORIGIN_FILE:
2556 + return "file";
2557 + case CONFIG_ORIGIN_STDIN:
2558 + return "standard input";
2559 + case CONFIG_ORIGIN_SUBMODULE_BLOB:
2560 + return "submodule-blob";
2561 + case CONFIG_ORIGIN_CMDLINE:
2562 + return "command line";
2563 + default:
2564 + die("BUG: unknown config origin type");
2565 + }
2566 }
2567
2568 const char *current_config_name(void)
submodule-config.c
+1 -1
@@ -448,7 +448,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
448 parameter.commit_sha1 = commit_sha1;
449 parameter.gitmodules_sha1 = sha1;
450 parameter.overwrite = 0;
451 - git_config_from_mem(parse_config, "submodule-blob", rev.buf,
451 + git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
452 config, config_size, &parameter);
453 free(config);
454