{"thread":{"id":"48014","subject":"[PATCH v11 00/10] convert: add support for different encodings","startedAt":"2018-03-09T17:37:00Z","lastAt":"2018-04-15T16:54:19Z","messageCount":25,"participants":["lars.schneider@autodesk.com","Junio C Hamano","Lars Schneider","Eric Sunshine","Torsten Bögershausen"],"isPatch":true,"patchVersion":11,"patchTotal":10},"messages":[{"id":"341350","messageId":"20180309173536.62012-1-lars.schneider@autodesk.com","threadId":"48014","inReplyTo":null,"subject":"[PATCH v11 00/10] convert: add support for different encodings","fromName":"","fromEmail":"lars.schneider@autodesk.com","sentAt":"2018-03-09T17:35:26Z","receivedAt":"2018-03-09T17:37:00Z","isPatch":true,"sender":{"key":"lars.schneider@autodesk.com","avatar":"https://gravatar.com/avatar/524188df3a38b02692619777bb90d9cc735db1ad75f5304e0bf09f8efd299775?d=mp&s=160"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nHi,\n\nPatches 1-5,9 are preparation and helper functions.\nPatch 6-8,10 are the actual change. Patch 8 is new.\n\nThis series depends on Torsten's 8462ff43e4 (convert_to_git():\nsafe_crlf/checksafe becomes int conv_flags, 2018-01-13) which is\nalready in master.\n\nChanges since v10:\n\n* rename startscase_with() to istarts_with() (Duy)\n* validate_encoding() advises the canonical form of the UTF\n  encoding name to the user (Junio)\n  --> I added it this as a separate commit that you could be dropped\n      if desired by the reviewers.\n* fix documentation for roundtrip check (Junio)\n* use isspace() to check whitespace/tab delimiter\n  in core.checkRoundtripEncoding (Junio)\n* remove dead code in roundtrip check (Junio)\n* fix invalid # in comment (Eric)\n* detect UTF8 and UTF-8 as default encoding (Eric)\n* make asterisk stick to the variable, not type (Junio)\n* print an error if \"w-t-e\" does not have a proper value (Junio)\n  --> BTW: I noticed that the attribute is not set to \"git_attr__false\"\n      even if I define \"-working-tree-encoding\". I haven't investigated\n      further yet. Might that be a bug? If yes, then this should be\n      addresses in a separate patch series.\n\nThanks,\nLars\n\n\n  RFC: https://public-inbox.org/git/BDB9B884-6D17-4BE3-A83C-F67E2AFA2B46@gmail.com/\n   v1: https://public-inbox.org/git/20171211155023.1405-1-lars.schneider@autodesk.com/\n   v2: https://public-inbox.org/git/20171229152222.39680-1-lars.schneider@autodesk.com/\n   v3: https://public-inbox.org/git/20180106004808.77513-1-lars.schneider@autodesk.com/\n   v4: https://public-inbox.org/git/20180120152418.52859-1-lars.schneider@autodesk.com/\n   v5: https://public-inbox.org/git/20180129201855.9182-1-tboegi@web.de/\n   v6: https://public-inbox.org/git/20180209132830.55385-1-lars.schneider@autodesk.com/\n   v7: https://public-inbox.org/git/20180215152711.158-1-lars.schneider@autodesk.com/\n   v8: https://public-inbox.org/git/20180224162801.98860-1-lars.schneider@autodesk.com/\n   v9: https://public-inbox.org/git/20180304201418.60958-1-lars.schneider@autodesk.com/\n  v10: https://public-inbox.org/git/20180307173026.30058-1-lars.schneider@autodesk.com/\n\nBase Ref:\nWeb-Diff: https://github.com/larsxschneider/git/commit/afc02ce2e0\nCheckout: git fetch https://github.com/larsxschneider/git encoding-v11 && git checkout afc02ce2e0\n\n\n### Interdiff (v10..v11):\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex d7a56054a5..7dcac9b540 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -531,10 +531,10 @@ core.autocrlf::\n \tin which case no output conversion is performed.\n\n core.checkRoundtripEncoding::\n-\tA comma separated list of encodings that Git performs UTF-8 round\n-\ttrip checks on if they are used in an `working-tree-encoding`\n-\tattribute (see linkgit:gitattributes[5]). The default value is\n-\t`SHIFT-JIS`.\n+\tA comma and/or whitespace separated list of encodings that Git\n+\tperforms UTF-8 round trip checks on if they are used in an\n+\t`working-tree-encoding` attribute (see linkgit:gitattributes[5]).\n+\tThe default value is `SHIFT-JIS`.\n\n core.symlinks::\n \tIf false, symbolic links are checked out as small plain files that\ndiff --git a/convert.c b/convert.c\nindex e861f1abbc..c2d24882c1 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -270,7 +270,7 @@ static int validate_encoding(const char *path, const char *enc,\n \t\t      const char *data, size_t len, int die_on_error)\n {\n \t/* We only check for UTF here as UTF?? can be an alias for UTF-?? */\n-\tif (startscase_with(enc, \"UTF\")) {\n+\tif (istarts_with(enc, \"UTF\")) {\n \t\t/*\n \t\t * Check for detectable errors in UTF encodings\n \t\t */\n@@ -284,12 +284,15 @@ static int validate_encoding(const char *path, const char *enc,\n \t\t\t */\n \t\t\tconst char *advise_msg = _(\n \t\t\t\t\"The file '%s' contains a byte order \"\n-\t\t\t\t\"mark (BOM). Please use %s as \"\n+\t\t\t\t\"mark (BOM). Please use UTF-%s as \"\n \t\t\t\t\"working-tree-encoding.\");\n-\t\t\tchar *upper_enc = xstrdup_toupper(enc);\n-\t\t\tupper_enc[strlen(upper_enc)-2] = '\\0';\n-\t\t\tadvise(advise_msg, path, upper_enc);\n-\t\t\tfree(upper_enc);\n+\t\t\tconst char *stripped = \"\";\n+\t\t\tchar *upper = xstrdup_toupper(enc);\n+\t\t\tupper[strlen(upper)-2] = '\\0';\n+\t\t\tif (!skip_prefix(upper, \"UTF-\", &stripped))\n+\t\t\t\tskip_prefix(stripped, \"UTF\", &stripped);\n+\t\t\tadvise(advise_msg, path, stripped);\n+\t\t\tfree(upper);\n \t\t\tif (die_on_error)\n \t\t\t\tdie(error_msg, path, enc);\n \t\t\telse {\n@@ -301,12 +304,15 @@ static int validate_encoding(const char *path, const char *enc,\n \t\t\t\t\"BOM is required in '%s' if encoded as %s\");\n \t\t\tconst char *advise_msg = _(\n \t\t\t\t\"The file '%s' is missing a byte order \"\n-\t\t\t\t\"mark (BOM). Please use %sBE or %sLE \"\n+\t\t\t\t\"mark (BOM). Please use UTF-%sBE or UTF-%sLE \"\n \t\t\t\t\"(depending on the byte order) as \"\n \t\t\t\t\"working-tree-encoding.\");\n-\t\t\tchar *upper_enc = xstrdup_toupper(enc);\n-\t\t\tadvise(advise_msg, path, upper_enc, upper_enc);\n-\t\t\tfree(upper_enc);\n+\t\t\tconst char *stripped = \"\";\n+\t\t\tchar *upper = xstrdup_toupper(enc);\n+\t\t\tif (!skip_prefix(upper, \"UTF-\", &stripped))\n+\t\t\t\tskip_prefix(stripped, \"UTF\", &stripped);\n+\t\t\tadvise(advise_msg, path, stripped, stripped);\n+\t\t\tfree(upper);\n \t\t\tif (die_on_error)\n \t\t\t\tdie(error_msg, path, enc);\n \t\t\telse {\n@@ -344,8 +350,8 @@ static void trace_encoding(const char *context, const char *path,\n static int check_roundtrip(const char *enc_name)\n {\n \t/*\n-\t * check_roundtrip_encoding contains a string of space and/or\n-\t * comma separated encodings (eg. \"UTF-16, ASCII, CP1125\").\n+\t * check_roundtrip_encoding contains a string of comma and/or\n+\t * space separated encodings (eg. \"UTF-16, ASCII, CP1125\").\n \t * Search for the given encoding in that string.\n \t */\n \tconst char *found = strcasestr(check_roundtrip_encoding, enc_name);\n@@ -362,8 +368,7 @@ static int check_roundtrip(const char* enc_name)\n \t\t\t * that it is prefixed with a space or comma\n \t\t\t */\n \t\t\tfound == check_roundtrip_encoding || (\n-\t\t\t\tfound > check_roundtrip_encoding &&\n-\t\t\t\t(*(found-1) == ' ' || *(found-1) == ',')\n+\t\t\t\t(isspace(found[-1]) || found[-1] == ',')\n \t\t\t)\n \t\t) && (\n \t\t\t/*\n@@ -373,7 +378,7 @@ static int check_roundtrip(const char* enc_name)\n \t\t\t */\n \t\t\tnext == check_roundtrip_encoding + len || (\n \t\t\t\tnext < check_roundtrip_encoding + len &&\n-\t\t\t\t(*next == ' ' || *next == ',')\n+\t\t\t\t(isspace(next[0]) || next[0] == ',')\n \t\t\t)\n \t\t));\n }\n@@ -1213,12 +1218,16 @@ static const char *git_path_check_encoding(struct attr_check_item *check)\n {\n \tconst char *value = check->value;\n\n-\tif (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value) ||\n-\t    !strlen(value))\n+\tif (ATTR_UNSET(value) || !strlen(value))\n \t\treturn NULL;\n\n+\tif (ATTR_TRUE(value) || ATTR_FALSE(value)) {\n+\t\terror(_(\"working-tree-encoding attribute requires a value\"));\n+\t\treturn NULL;\n+\t}\n+\n \t/* Don't encode to the default encoding */\n-\tif (!strcasecmp(value, default_encoding))\n+\tif (is_encoding_utf8(value) && is_encoding_utf8(default_encoding))\n \t\treturn NULL;\n\n \treturn value;\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex f648da0c11..95c9b34832 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -455,7 +455,7 @@ extern void (*get_warn_routine(void))(const char *warn, va_list params);\n extern void set_die_is_recursing_routine(int (*routine)(void));\n\n extern int starts_with(const char *str, const char *prefix);\n-extern int startscase_with(const char *str, const char *prefix);\n+extern int istarts_with(const char *str, const char *prefix);\n\n /*\n  * If the string \"str\" begins with the string found in \"prefix\", return 1.\ndiff --git a/strbuf.c b/strbuf.c\nindex 5779a2d591..99812b8488 100644\n--- a/strbuf.c\n+++ b/strbuf.c\n@@ -11,7 +11,7 @@ int starts_with(const char *str, const char *prefix)\n \t\t\treturn 0;\n }\n\n-int startscase_with(const char *str, const char *prefix)\n+int istarts_with(const char *str, const char *prefix)\n {\n \tfor (; ; str++, prefix++)\n \t\tif (!*prefix)\ndiff --git a/t/t0028-working-tree-encoding.sh b/t/t0028-working-tree-encoding.sh\nindex 7cff41a350..07089bba2e 100755\n--- a/t/t0028-working-tree-encoding.sh\n+++ b/t/t0028-working-tree-encoding.sh\n@@ -68,7 +68,7 @@ do\n \t\ttest_when_finished \"git reset --hard HEAD\" &&\n\n \t\techo \"*.utf${i}be text working-tree-encoding=utf-${i}be\" >>.gitattributes &&\n-\t\techo \"*.utf${i}le text working-tree-encoding=utf-${i}le\" >>.gitattributes &&\n+\t\techo \"*.utf${i}le text working-tree-encoding=utf-${i}LE\" >>.gitattributes &&\n\n \t\t# Here we add a UTF-16 (resp. UTF-32) files with BOM (big/little-endian)\n \t\t# but we tell Git to treat it as UTF-16BE/UTF-16LE (resp. UTF-32).\n@@ -76,18 +76,22 @@ do\n \t\tcp bebom.utf${i}be.raw bebom.utf${i}be &&\n \t\ttest_must_fail git add bebom.utf${i}be 2>err.out &&\n \t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}be\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i} as working-tree-encoding\" err.out &&\n\n \t\tcp lebom.utf${i}le.raw lebom.utf${i}be &&\n \t\ttest_must_fail git add lebom.utf${i}be 2>err.out &&\n \t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}be\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i} as working-tree-encoding\" err.out &&\n\n \t\tcp bebom.utf${i}be.raw bebom.utf${i}le &&\n \t\ttest_must_fail git add bebom.utf${i}le 2>err.out &&\n-\t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}le\" err.out &&\n+\t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}LE\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i} as working-tree-encoding\" err.out &&\n\n \t\tcp lebom.utf${i}le.raw lebom.utf${i}le &&\n \t\ttest_must_fail git add lebom.utf${i}le 2>err.out &&\n-\t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}le\" err.out\n+\t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}LE\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i} as working-tree-encoding\" err.out\n \t'\n\n \ttest_expect_success \"check required UTF-${i} BOM\" '\n@@ -98,10 +102,12 @@ do\n \t\tcp nobom.utf${i}be.raw nobom.utf${i} &&\n \t\ttest_must_fail git add nobom.utf${i} 2>err.out &&\n \t\ttest_i18ngrep \"fatal: BOM is required .* utf-${i}\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i}BE or UTF-${i}LE\" err.out &&\n\n \t\tcp nobom.utf${i}le.raw nobom.utf${i} &&\n \t\ttest_must_fail git add nobom.utf${i} 2>err.out &&\n-\t\ttest_i18ngrep \"fatal: BOM is required .* utf-${i}\" err.out\n+\t\ttest_i18ngrep \"fatal: BOM is required .* utf-${i}\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i}BE or UTF-${i}LE\" err.out\n \t'\n\n \ttest_expect_success \"eol conversion for UTF-${i} encoded files on checkout\" '\n@@ -143,9 +149,20 @@ done\n test_expect_success 'check unsupported encodings' '\n \ttest_when_finished \"git reset --hard HEAD\" &&\n\n-\techo \"*.nothing text working-tree-encoding=\" >>.gitattributes &&\n-\tprintf \"nothing\" >t.nothing &&\n-\tgit add t.nothing &&\n+\techo \"*.set text working-tree-encoding\" >>.gitattributes &&\n+\tprintf \"set\" >t.set &&\n+\tgit add t.set 2>err.out &&\n+\ttest_i18ngrep \"error: working-tree-encoding attribute requires a value\" err.out &&\n+\n+\techo \"*.unset text -working-tree-encoding\" >>.gitattributes &&\n+\tprintf \"unset\" >t.unset &&\n+\tgit add t.unset 2>err.out &&\n+\ttest_i18ngrep \"error: working-tree-encoding attribute requires a value\" err.out &&\n+\n+\techo \"*.empty text working-tree-encoding=\" >>.gitattributes &&\n+\tprintf \"empty\" >t.empty &&\n+\tgit add t.empty 2>err.out &&\n+\ttest_i18ngrep \"error: working-tree-encoding attribute requires a value\" err.out &&\n\n \techo \"*.garbage text working-tree-encoding=garbage\" >>.gitattributes &&\n \tprintf \"garbage\" >t.garbage &&\n\n\n### Patches\n\nLars Schneider (10):\n  strbuf: remove unnecessary NUL assignment in xstrdup_tolower()\n  strbuf: add xstrdup_toupper()\n  strbuf: add a case insensitive starts_with()\n  utf8: add function to detect prohibited UTF-16/32 BOM\n  utf8: add function to detect a missing UTF-16/32 BOM\n  convert: add 'working-tree-encoding' attribute\n  convert: check for detectable errors in UTF encodings\n  convert: advise canonical UTF encoding names\n  convert: add tracing for 'working-tree-encoding' attribute\n  convert: add round trip check based on 'core.checkRoundtripEncoding'\n\n Documentation/config.txt         |   6 +\n Documentation/gitattributes.txt  |  88 +++++++++++++\n config.c                         |   5 +\n convert.c                        | 277 ++++++++++++++++++++++++++++++++++++++-\n convert.h                        |   2 +\n environment.c                    |   1 +\n git-compat-util.h                |   1 +\n sha1_file.c                      |   2 +-\n strbuf.c                         |  22 +++-\n strbuf.h                         |   1 +\n t/t0028-working-tree-encoding.sh | 247 ++++++++++++++++++++++++++++++++++\n utf8.c                           |  39 ++++++\n utf8.h                           |  28 ++++\n 13 files changed, 716 insertions(+), 3 deletions(-)\n create mode 100755 t/t0028-working-tree-encoding.sh\n\n\nbase-commit: 8a2f0888555ce46ac87452b194dec5cb66fb1417\n--\n2.16.2\n\n"},{"id":"341351","messageId":"20180309173536.62012-3-lars.schneider@autodesk.com","threadId":"48014","inReplyTo":"20180309173536.62012-1-lars.schneider@autodesk.com","subject":"[PATCH v11 02/10] strbuf: add xstrdup_toupper()","fromName":"","fromEmail":"lars.schneider@autodesk.com","sentAt":"2018-03-09T17:35:28Z","receivedAt":"2018-03-09T17:37:04Z","isPatch":true,"sender":{"key":"lars.schneider@autodesk.com","avatar":"https://gravatar.com/avatar/524188df3a38b02692619777bb90d9cc735db1ad75f5304e0bf09f8efd299775?d=mp&s=160"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nCreate a copy of an existing string and make all characters upper case.\nSimilar xstrdup_tolower().\n\nThis function is used in a subsequent commit.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n strbuf.c | 12 ++++++++++++\n strbuf.h |  1 +\n 2 files changed, 13 insertions(+)\n\ndiff --git a/strbuf.c b/strbuf.c\nindex 55b7daeb35..b635f0bdc4 100644\n--- a/strbuf.c\n+++ b/strbuf.c\n@@ -784,6 +784,18 @@ char *xstrdup_tolower(const char *string)\n \treturn result;\n }\n \n+char *xstrdup_toupper(const char *string)\n+{\n+\tchar *result;\n+\tsize_t len, i;\n+\n+\tlen = strlen(string);\n+\tresult = xmallocz(len);\n+\tfor (i = 0; i < len; i++)\n+\t\tresult[i] = toupper(string[i]);\n+\treturn result;\n+}\n+\n char *xstrvfmt(const char *fmt, va_list ap)\n {\n \tstruct strbuf buf = STRBUF_INIT;\ndiff --git a/strbuf.h b/strbuf.h\nindex 14c8c10d66..df7ced53ed 100644\n--- a/strbuf.h\n+++ b/strbuf.h\n@@ -607,6 +607,7 @@ __attribute__((format (printf,2,3)))\n extern int fprintf_ln(FILE *fp, const char *fmt, ...);\n \n char *xstrdup_tolower(const char *);\n+char *xstrdup_toupper(const char *);\n \n /**\n  * Create a newly allocated string using printf format. You can do this easily\n-- \n2.16.2\n\n"},{"id":"341352","messageId":"20180309173536.62012-9-lars.schneider@autodesk.com","threadId":"48014","inReplyTo":"20180309173536.62012-1-lars.schneider@autodesk.com","subject":"[PATCH v11 08/10] convert: advise canonical UTF encoding names","fromName":"","fromEmail":"lars.schneider@autodesk.com","sentAt":"2018-03-09T17:35:34Z","receivedAt":"2018-03-09T17:37:06Z","isPatch":true,"sender":{"key":"lars.schneider@autodesk.com","avatar":"https://gravatar.com/avatar/524188df3a38b02692619777bb90d9cc735db1ad75f5304e0bf09f8efd299775?d=mp&s=160"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nThe canonical name of an UTF encoding has the format UTF, dash, number,\nand an optionally byte order in upper case (e.g. UTF-8 or UTF-16BE).\nSome iconv versions support alternative names without a dash or with\nlower case characters.\n\nTo avoid problems between different iconv version always suggest the\ncanonical UTF names in advise messages.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n convert.c                        | 21 +++++++++++++++++----\n t/t0028-working-tree-encoding.sh | 10 ++++++++--\n 2 files changed, 25 insertions(+), 6 deletions(-)\n\ndiff --git a/convert.c b/convert.c\nindex b80d666a6b..9a3ae7cce1 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -279,12 +279,20 @@ static int validate_encoding(const char *path, const char *enc,\n \t\t\t\t\"BOM is prohibited in '%s' if encoded as %s\");\n \t\t\t/*\n \t\t\t * This advice is shown for UTF-??BE and UTF-??LE encodings.\n+\t\t\t * We cut off the last two characters of the encoding name\n+\t\t\t # to generate the encoding name suitable for BOMs.\n \t\t\t */\n \t\t\tconst char *advise_msg = _(\n \t\t\t\t\"The file '%s' contains a byte order \"\n-\t\t\t\t\"mark (BOM). Please use %.6s as \"\n+\t\t\t\t\"mark (BOM). Please use UTF-%s as \"\n \t\t\t\t\"working-tree-encoding.\");\n-\t\t\tadvise(advise_msg, path, enc);\n+\t\t\tconst char *stripped = \"\";\n+\t\t\tchar *upper = xstrdup_toupper(enc);\n+\t\t\tupper[strlen(upper)-2] = '\\0';\n+\t\t\tif (!skip_prefix(upper, \"UTF-\", &stripped))\n+\t\t\t\tskip_prefix(stripped, \"UTF\", &stripped);\n+\t\t\tadvise(advise_msg, path, stripped);\n+\t\t\tfree(upper);\n \t\t\tif (die_on_error)\n \t\t\t\tdie(error_msg, path, enc);\n \t\t\telse {\n@@ -296,10 +304,15 @@ static int validate_encoding(const char *path, const char *enc,\n \t\t\t\t\"BOM is required in '%s' if encoded as %s\");\n \t\t\tconst char *advise_msg = _(\n \t\t\t\t\"The file '%s' is missing a byte order \"\n-\t\t\t\t\"mark (BOM). Please use %sBE or %sLE \"\n+\t\t\t\t\"mark (BOM). Please use UTF-%sBE or UTF-%sLE \"\n \t\t\t\t\"(depending on the byte order) as \"\n \t\t\t\t\"working-tree-encoding.\");\n-\t\t\tadvise(advise_msg, path, enc, enc);\n+\t\t\tconst char *stripped = \"\";\n+\t\t\tchar *upper = xstrdup_toupper(enc);\n+\t\t\tif (!skip_prefix(upper, \"UTF-\", &stripped))\n+\t\t\t\tskip_prefix(stripped, \"UTF\", &stripped);\n+\t\t\tadvise(advise_msg, path, stripped, stripped);\n+\t\t\tfree(upper);\n \t\t\tif (die_on_error)\n \t\t\t\tdie(error_msg, path, enc);\n \t\t\telse {\ndiff --git a/t/t0028-working-tree-encoding.sh b/t/t0028-working-tree-encoding.sh\nindex e8408dfe5c..1bee7b9f71 100755\n--- a/t/t0028-working-tree-encoding.sh\n+++ b/t/t0028-working-tree-encoding.sh\n@@ -74,18 +74,22 @@ do\n \t\tcp bebom.utf${i}be.raw bebom.utf${i}be &&\n \t\ttest_must_fail git add bebom.utf${i}be 2>err.out &&\n \t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}be\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i} as working-tree-encoding\" err.out &&\n \n \t\tcp lebom.utf${i}le.raw lebom.utf${i}be &&\n \t\ttest_must_fail git add lebom.utf${i}be 2>err.out &&\n \t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}be\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i} as working-tree-encoding\" err.out &&\n \n \t\tcp bebom.utf${i}be.raw bebom.utf${i}le &&\n \t\ttest_must_fail git add bebom.utf${i}le 2>err.out &&\n \t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}LE\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i} as working-tree-encoding\" err.out &&\n \n \t\tcp lebom.utf${i}le.raw lebom.utf${i}le &&\n \t\ttest_must_fail git add lebom.utf${i}le 2>err.out &&\n-\t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}LE\" err.out\n+\t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}LE\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i} as working-tree-encoding\" err.out\n \t'\n \n \ttest_expect_success \"check required UTF-${i} BOM\" '\n@@ -96,10 +100,12 @@ do\n \t\tcp nobom.utf${i}be.raw nobom.utf${i} &&\n \t\ttest_must_fail git add nobom.utf${i} 2>err.out &&\n \t\ttest_i18ngrep \"fatal: BOM is required .* utf-${i}\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i}BE or UTF-${i}LE\" err.out &&\n \n \t\tcp nobom.utf${i}le.raw nobom.utf${i} &&\n \t\ttest_must_fail git add nobom.utf${i} 2>err.out &&\n-\t\ttest_i18ngrep \"fatal: BOM is required .* utf-${i}\" err.out\n+\t\ttest_i18ngrep \"fatal: BOM is required .* utf-${i}\" err.out &&\n+\t\ttest_i18ngrep \"use UTF-${i}BE or UTF-${i}LE\" err.out\n \t'\n \n \ttest_expect_success \"eol conversion for UTF-${i} encoded files on checkout\" '\n-- \n2.16.2\n\n"},{"id":"341353","messageId":"20180309173536.62012-4-lars.schneider@autodesk.com","threadId":"48014","inReplyTo":"20180309173536.62012-1-lars.schneider@autodesk.com","subject":"[PATCH v11 03/10] strbuf: add a case insensitive starts_with()","fromName":"","fromEmail":"lars.schneider@autodesk.com","sentAt":"2018-03-09T17:35:29Z","receivedAt":"2018-03-09T17:37:08Z","isPatch":true,"sender":{"key":"lars.schneider@autodesk.com","avatar":"https://gravatar.com/avatar/524188df3a38b02692619777bb90d9cc735db1ad75f5304e0bf09f8efd299775?d=mp&s=160"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nCheck in a case insensitive manner if one string is a prefix of another\nstring.\n\nThis function is used in a subsequent commit.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n git-compat-util.h | 1 +\n strbuf.c          | 9 +++++++++\n 2 files changed, 10 insertions(+)\n\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex 68b2ad531e..95c9b34832 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -455,6 +455,7 @@ extern void (*get_warn_routine(void))(const char *warn, va_list params);\n extern void set_die_is_recursing_routine(int (*routine)(void));\n \n extern int starts_with(const char *str, const char *prefix);\n+extern int istarts_with(const char *str, const char *prefix);\n \n /*\n  * If the string \"str\" begins with the string found in \"prefix\", return 1.\ndiff --git a/strbuf.c b/strbuf.c\nindex b635f0bdc4..99812b8488 100644\n--- a/strbuf.c\n+++ b/strbuf.c\n@@ -11,6 +11,15 @@ int starts_with(const char *str, const char *prefix)\n \t\t\treturn 0;\n }\n \n+int istarts_with(const char *str, const char *prefix)\n+{\n+\tfor (; ; str++, prefix++)\n+\t\tif (!*prefix)\n+\t\t\treturn 1;\n+\t\telse if (tolower(*str) != tolower(*prefix))\n+\t\t\treturn 0;\n+}\n+\n int skip_to_optional_arg_default(const char *str, const char *prefix,\n \t\t\t\t const char **arg, const char *def)\n {\n-- \n2.16.2\n\n"},{"id":"341354","messageId":"20180309173536.62012-10-lars.schneider@autodesk.com","threadId":"48014","inReplyTo":"20180309173536.62012-1-lars.schneider@autodesk.com","subject":"[PATCH v11 09/10] convert: add tracing for 'working-tree-encoding' attribute","fromName":"","fromEmail":"lars.schneider@autodesk.com","sentAt":"2018-03-09T17:35:35Z","receivedAt":"2018-03-09T17:37:13Z","isPatch":true,"sender":{"key":"lars.schneider@autodesk.com","avatar":"https://gravatar.com/avatar/524188df3a38b02692619777bb90d9cc735db1ad75f5304e0bf09f8efd299775?d=mp&s=160"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nAdd the GIT_TRACE_WORKING_TREE_ENCODING environment variable to enable\ntracing for content that is reencoded with the 'working-tree-encoding'\nattribute. This is useful to debug encoding issues.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n convert.c                        | 25 +++++++++++++++++++++++++\n t/t0028-working-tree-encoding.sh |  2 ++\n 2 files changed, 27 insertions(+)\n\ndiff --git a/convert.c b/convert.c\nindex 9a3ae7cce1..d739078016 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -324,6 +324,29 @@ static int validate_encoding(const char *path, const char *enc,\n \treturn 0;\n }\n \n+static void trace_encoding(const char *context, const char *path,\n+\t\t\t   const char *encoding, const char *buf, size_t len)\n+{\n+\tstatic struct trace_key coe = TRACE_KEY_INIT(WORKING_TREE_ENCODING);\n+\tstruct strbuf trace = STRBUF_INIT;\n+\tint i;\n+\n+\tstrbuf_addf(&trace, \"%s (%s, considered %s):\\n\", context, path, encoding);\n+\tfor (i = 0; i < len && buf; ++i) {\n+\t\tstrbuf_addf(\n+\t\t\t&trace,\"| \\e[2m%2i:\\e[0m %2x \\e[2m%c\\e[0m%c\",\n+\t\t\ti,\n+\t\t\t(unsigned char) buf[i],\n+\t\t\t(buf[i] > 32 && buf[i] < 127 ? buf[i] : ' '),\n+\t\t\t((i+1) % 8 && (i+1) < len ? ' ' : '\\n')\n+\t\t);\n+\t}\n+\tstrbuf_addchars(&trace, '\\n', 1);\n+\n+\ttrace_strbuf(&coe, &trace);\n+\tstrbuf_release(&trace);\n+}\n+\n static const char *default_encoding = \"UTF-8\";\n \n static int encode_to_git(const char *path, const char *src, size_t src_len,\n@@ -352,6 +375,7 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,\n \tif (validate_encoding(path, enc, src, src_len, die_on_error))\n \t\treturn 0;\n \n+\ttrace_encoding(\"source\", path, enc, src, src_len);\n \tdst = reencode_string_len(src, src_len, default_encoding, enc,\n \t\t\t\t  &dst_len);\n \tif (!dst) {\n@@ -369,6 +393,7 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,\n \t\t\treturn 0;\n \t\t}\n \t}\n+\ttrace_encoding(\"destination\", path, default_encoding, dst, dst_len);\n \n \tstrbuf_attach(buf, dst, dst_len, dst_len + 1);\n \treturn 1;\ndiff --git a/t/t0028-working-tree-encoding.sh b/t/t0028-working-tree-encoding.sh\nindex 1bee7b9f71..f68e282c5e 100755\n--- a/t/t0028-working-tree-encoding.sh\n+++ b/t/t0028-working-tree-encoding.sh\n@@ -4,6 +4,8 @@ test_description='working-tree-encoding conversion via gitattributes'\n \n . ./test-lib.sh\n \n+GIT_TRACE_WORKING_TREE_ENCODING=1 && export GIT_TRACE_WORKING_TREE_ENCODING\n+\n test_expect_success 'setup test files' '\n \tgit config core.eol lf &&\n \n-- \n2.16.2\n\n"},{"id":"341355","messageId":"20180309173536.62012-8-lars.schneider@autodesk.com","threadId":"48014","inReplyTo":"20180309173536.62012-1-lars.schneider@autodesk.com","subject":"[PATCH v11 07/10] convert: check for detectable errors in UTF encodings","fromName":"","fromEmail":"lars.schneider@autodesk.com","sentAt":"2018-03-09T17:35:33Z","receivedAt":"2018-03-09T17:37:15Z","isPatch":true,"sender":{"key":"lars.schneider@autodesk.com","avatar":"https://gravatar.com/avatar/524188df3a38b02692619777bb90d9cc735db1ad75f5304e0bf09f8efd299775?d=mp&s=160"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nCheck that new content is valid with respect to the user defined\n'working-tree-encoding' attribute.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n convert.c                        | 48 ++++++++++++++++++++++++++++++++++\n t/t0028-working-tree-encoding.sh | 56 ++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 104 insertions(+)\n\ndiff --git a/convert.c b/convert.c\nindex aa59ecfe49..b80d666a6b 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -266,6 +266,51 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,\n \n }\n \n+static int validate_encoding(const char *path, const char *enc,\n+\t\t      const char *data, size_t len, int die_on_error)\n+{\n+\t/* We only check for UTF here as UTF?? can be an alias for UTF-?? */\n+\tif (istarts_with(enc, \"UTF\")) {\n+\t\t/*\n+\t\t * Check for detectable errors in UTF encodings\n+\t\t */\n+\t\tif (has_prohibited_utf_bom(enc, data, len)) {\n+\t\t\tconst char *error_msg = _(\n+\t\t\t\t\"BOM is prohibited in '%s' if encoded as %s\");\n+\t\t\t/*\n+\t\t\t * This advice is shown for UTF-??BE and UTF-??LE encodings.\n+\t\t\t */\n+\t\t\tconst char *advise_msg = _(\n+\t\t\t\t\"The file '%s' contains a byte order \"\n+\t\t\t\t\"mark (BOM). Please use %.6s as \"\n+\t\t\t\t\"working-tree-encoding.\");\n+\t\t\tadvise(advise_msg, path, enc);\n+\t\t\tif (die_on_error)\n+\t\t\t\tdie(error_msg, path, enc);\n+\t\t\telse {\n+\t\t\t\treturn error(error_msg, path, enc);\n+\t\t\t}\n+\n+\t\t} else if (is_missing_required_utf_bom(enc, data, len)) {\n+\t\t\tconst char *error_msg = _(\n+\t\t\t\t\"BOM is required in '%s' if encoded as %s\");\n+\t\t\tconst char *advise_msg = _(\n+\t\t\t\t\"The file '%s' is missing a byte order \"\n+\t\t\t\t\"mark (BOM). Please use %sBE or %sLE \"\n+\t\t\t\t\"(depending on the byte order) as \"\n+\t\t\t\t\"working-tree-encoding.\");\n+\t\t\tadvise(advise_msg, path, enc, enc);\n+\t\t\tif (die_on_error)\n+\t\t\t\tdie(error_msg, path, enc);\n+\t\t\telse {\n+\t\t\t\treturn error(error_msg, path, enc);\n+\t\t\t}\n+\t\t}\n+\n+\t}\n+\treturn 0;\n+}\n+\n static const char *default_encoding = \"UTF-8\";\n \n static int encode_to_git(const char *path, const char *src, size_t src_len,\n@@ -291,6 +336,9 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,\n \tif (!buf && !src)\n \t\treturn 1;\n \n+\tif (validate_encoding(path, enc, src, src_len, die_on_error))\n+\t\treturn 0;\n+\n \tdst = reencode_string_len(src, src_len, default_encoding, enc,\n \t\t\t\t  &dst_len);\n \tif (!dst) {\ndiff --git a/t/t0028-working-tree-encoding.sh b/t/t0028-working-tree-encoding.sh\nindex e492945a01..e8408dfe5c 100755\n--- a/t/t0028-working-tree-encoding.sh\n+++ b/t/t0028-working-tree-encoding.sh\n@@ -62,6 +62,46 @@ test_expect_success 'check $GIT_DIR/info/attributes support' '\n \n for i in 16 32\n do\n+\ttest_expect_success \"check prohibited UTF-${i} BOM\" '\n+\t\ttest_when_finished \"git reset --hard HEAD\" &&\n+\n+\t\techo \"*.utf${i}be text working-tree-encoding=utf-${i}be\" >>.gitattributes &&\n+\t\techo \"*.utf${i}le text working-tree-encoding=utf-${i}LE\" >>.gitattributes &&\n+\n+\t\t# Here we add a UTF-16 (resp. UTF-32) files with BOM (big/little-endian)\n+\t\t# but we tell Git to treat it as UTF-16BE/UTF-16LE (resp. UTF-32).\n+\t\t# In these cases the BOM is prohibited.\n+\t\tcp bebom.utf${i}be.raw bebom.utf${i}be &&\n+\t\ttest_must_fail git add bebom.utf${i}be 2>err.out &&\n+\t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}be\" err.out &&\n+\n+\t\tcp lebom.utf${i}le.raw lebom.utf${i}be &&\n+\t\ttest_must_fail git add lebom.utf${i}be 2>err.out &&\n+\t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}be\" err.out &&\n+\n+\t\tcp bebom.utf${i}be.raw bebom.utf${i}le &&\n+\t\ttest_must_fail git add bebom.utf${i}le 2>err.out &&\n+\t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}LE\" err.out &&\n+\n+\t\tcp lebom.utf${i}le.raw lebom.utf${i}le &&\n+\t\ttest_must_fail git add lebom.utf${i}le 2>err.out &&\n+\t\ttest_i18ngrep \"fatal: BOM is prohibited .* utf-${i}LE\" err.out\n+\t'\n+\n+\ttest_expect_success \"check required UTF-${i} BOM\" '\n+\t\ttest_when_finished \"git reset --hard HEAD\" &&\n+\n+\t\techo \"*.utf${i} text working-tree-encoding=utf-${i}\" >>.gitattributes &&\n+\n+\t\tcp nobom.utf${i}be.raw nobom.utf${i} &&\n+\t\ttest_must_fail git add nobom.utf${i} 2>err.out &&\n+\t\ttest_i18ngrep \"fatal: BOM is required .* utf-${i}\" err.out &&\n+\n+\t\tcp nobom.utf${i}le.raw nobom.utf${i} &&\n+\t\ttest_must_fail git add nobom.utf${i} 2>err.out &&\n+\t\ttest_i18ngrep \"fatal: BOM is required .* utf-${i}\" err.out\n+\t'\n+\n \ttest_expect_success \"eol conversion for UTF-${i} encoded files on checkout\" '\n \t\ttest_when_finished \"rm -f crlf.utf${i}.raw lf.utf${i}.raw\" &&\n \t\ttest_when_finished \"git reset --hard HEAD^\" &&\n@@ -141,4 +181,20 @@ test_expect_success 'error if encoding round trip is not the same during refresh\n \ttest_i18ngrep \"error: .* overwritten by checkout:\" err.out\n '\n \n+test_expect_success 'error if encoding garbage is already in Git' '\n+\tBEFORE_STATE=$(git rev-parse HEAD) &&\n+\ttest_when_finished \"git reset --hard $BEFORE_STATE\" &&\n+\n+\t# Skip the UTF-16 filter for the added file\n+\t# This simulates a Git version that has no checkoutEncoding support\n+\tcp nobom.utf16be.raw nonsense.utf16 &&\n+\tTEST_HASH=$(git hash-object --no-filters -w nonsense.utf16) &&\n+\tgit update-index --add --cacheinfo 100644 $TEST_HASH nonsense.utf16 &&\n+\tCOMMIT=$(git commit-tree -p $(git rev-parse HEAD) -m \"plain commit\" $(git write-tree)) &&\n+\tgit update-ref refs/heads/master $COMMIT &&\n+\n+\tgit diff 2>err.out &&\n+\ttest_i18ngrep \"error: BOM is required\" err.out\n+'\n+\n test_done\n-- \n2.16.2\n\n"},{"id":"341356","messageId":"20180309173536.62012-7-lars.schneider@autodesk.com","threadId":"48014","inReplyTo":"20180309173536.62012-1-lars.schneider@autodesk.com","subject":"[PATCH v11 06/10] convert: add 'working-tree-encoding' attribute","fromName":"","fromEmail":"lars.schneider@autodesk.com","sentAt":"2018-03-09T17:35:32Z","receivedAt":"2018-03-09T17:37:18Z","isPatch":true,"sender":{"key":"lars.schneider@autodesk.com","avatar":"https://gravatar.com/avatar/524188df3a38b02692619777bb90d9cc735db1ad75f5304e0bf09f8efd299775?d=mp&s=160"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nGit recognizes files encoded with ASCII or one of its supersets (e.g.\nUTF-8 or ISO-8859-1) as text files. All other encodings are usually\ninterpreted as binary and consequently built-in Git text processing\ntools (e.g. 'git diff') as well as most Git web front ends do not\nvisualize the content.\n\nAdd an attribute to tell Git what encoding the user has defined for a\ngiven file. If the content is added to the index, then Git converts the\ncontent to a canonical UTF-8 representation. On checkout Git will\nreverse the conversion.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n Documentation/gitattributes.txt  |  80 ++++++++++++++++++++++\n convert.c                        | 114 ++++++++++++++++++++++++++++++-\n convert.h                        |   1 +\n sha1_file.c                      |   2 +-\n t/t0028-working-tree-encoding.sh | 144 +++++++++++++++++++++++++++++++++++++++\n 5 files changed, 339 insertions(+), 2 deletions(-)\n create mode 100755 t/t0028-working-tree-encoding.sh\n\ndiff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt\nindex 30687de81a..31a4f92840 100644\n--- a/Documentation/gitattributes.txt\n+++ b/Documentation/gitattributes.txt\n@@ -272,6 +272,86 @@ few exceptions.  Even though...\n   catch potential problems early, safety triggers.\n \n \n+`working-tree-encoding`\n+^^^^^^^^^^^^^^^^^^^^^^^\n+\n+Git recognizes files encoded in ASCII or one of its supersets (e.g.\n+UTF-8, ISO-8859-1, ...) as text files. Files encoded in certain other\n+encodings (e.g. UTF-16) are interpreted as binary and consequently\n+built-in Git text processing tools (e.g. 'git diff') as well as most Git\n+web front ends do not visualize the contents of these files by default.\n+\n+In these cases you can tell Git the encoding of a file in the working\n+directory with the `working-tree-encoding` attribute. If a file with this\n+attribute is added to Git, then Git reencodes the content from the\n+specified encoding to UTF-8. Finally, Git stores the UTF-8 encoded\n+content in its internal data structure (called \"the index\"). On checkout\n+the content is reencoded back to the specified encoding.\n+\n+Please note that using the `working-tree-encoding` attribute may have a\n+number of pitfalls:\n+\n+- Alternative Git implementations (e.g. JGit or libgit2) and older Git\n+  versions (as of March 2018) do not support the `working-tree-encoding`\n+  attribute. If you decide to use the `working-tree-encoding` attribute\n+  in your repository, then it is strongly recommended to ensure that all\n+  clients working with the repository support it.\n+\n+  For example, Microsoft Visual Studio resources files (`*.rc`) or\n+  PowerShell script files (`*.ps1`) are sometimes encoded in UTF-16.\n+  If you declare `*.ps1` as files as UTF-16 and you add `foo.ps1` with\n+  a `working-tree-encoding` enabled Git client, then `foo.ps1` will be\n+  stored as UTF-8 internally. A client without `working-tree-encoding`\n+  support will checkout `foo.ps1` as UTF-8 encoded file. This will\n+  typically cause trouble for the users of this file.\n+\n+  If a Git client, that does not support the `working-tree-encoding`\n+  attribute, adds a new file `bar.ps1`, then `bar.ps1` will be\n+  stored \"as-is\" internally (in this example probably as UTF-16).\n+  A client with `working-tree-encoding` support will interpret the\n+  internal contents as UTF-8 and try to convert it to UTF-16 on checkout.\n+  That operation will fail and cause an error.\n+\n+- Reencoding content requires resources that might slow down certain\n+  Git operations (e.g 'git checkout' or 'git add').\n+\n+Use the `working-tree-encoding` attribute only if you cannot store a file\n+in UTF-8 encoding and if you want Git to be able to process the content\n+as text.\n+\n+As an example, use the following attributes if your '*.ps1' files are\n+UTF-16 encoded with byte order mark (BOM) and you want Git to perform\n+automatic line ending conversion based on your platform.\n+\n+------------------------\n+*.ps1\t\ttext working-tree-encoding=UTF-16\n+------------------------\n+\n+Use the following attributes if your '*.ps1' files are UTF-16 little\n+endian encoded without BOM and you want Git to use Windows line endings\n+in the working directory. Please note, it is highly recommended to\n+explicitly define the line endings with `eol` if the `working-tree-encoding`\n+attribute is used to avoid ambiguity.\n+\n+------------------------\n+*.ps1\t\ttext working-tree-encoding=UTF-16LE eol=CRLF\n+------------------------\n+\n+You can get a list of all available encodings on your platform with the\n+following command:\n+\n+------------------------\n+iconv --list\n+------------------------\n+\n+If you do not know the encoding of a file, then you can use the `file`\n+command to guess the encoding:\n+\n+------------------------\n+file foo.ps1\n+------------------------\n+\n+\n `ident`\n ^^^^^^^\n \ndiff --git a/convert.c b/convert.c\nindex b976eb968c..aa59ecfe49 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -7,6 +7,7 @@\n #include \"sigchain.h\"\n #include \"pkt-line.h\"\n #include \"sub-process.h\"\n+#include \"utf8.h\"\n \n /*\n  * convert.c - convert a file when checking it out and checking it in.\n@@ -265,6 +266,78 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,\n \n }\n \n+static const char *default_encoding = \"UTF-8\";\n+\n+static int encode_to_git(const char *path, const char *src, size_t src_len,\n+\t\t\t struct strbuf *buf, const char *enc, int conv_flags)\n+{\n+\tchar *dst;\n+\tint dst_len;\n+\tint die_on_error = conv_flags & CONV_WRITE_OBJECT;\n+\n+\t/*\n+\t * No encoding is specified or there is nothing to encode.\n+\t * Tell the caller that the content was not modified.\n+\t */\n+\tif (!enc || (src && !src_len))\n+\t\treturn 0;\n+\n+\t/*\n+\t * Looks like we got called from \"would_convert_to_git()\".\n+\t * This means Git wants to know if it would encode (= modify!)\n+\t * the content. Let's answer with \"yes\", since an encoding was\n+\t * specified.\n+\t */\n+\tif (!buf && !src)\n+\t\treturn 1;\n+\n+\tdst = reencode_string_len(src, src_len, default_encoding, enc,\n+\t\t\t\t  &dst_len);\n+\tif (!dst) {\n+\t\t/*\n+\t\t * We could add the blob \"as-is\" to Git. However, on checkout\n+\t\t * we would try to reencode to the original encoding. This\n+\t\t * would fail and we would leave the user with a messed-up\n+\t\t * working tree. Let's try to avoid this by screaming loud.\n+\t\t */\n+\t\tconst char* msg = _(\"failed to encode '%s' from %s to %s\");\n+\t\tif (die_on_error)\n+\t\t\tdie(msg, path, enc, default_encoding);\n+\t\telse {\n+\t\t\terror(msg, path, enc, default_encoding);\n+\t\t\treturn 0;\n+\t\t}\n+\t}\n+\n+\tstrbuf_attach(buf, dst, dst_len, dst_len + 1);\n+\treturn 1;\n+}\n+\n+static int encode_to_worktree(const char *path, const char *src, size_t src_len,\n+\t\t\t      struct strbuf *buf, const char *enc)\n+{\n+\tchar *dst;\n+\tint dst_len;\n+\n+\t/*\n+\t * No encoding is specified or there is nothing to encode.\n+\t * Tell the caller that the content was not modified.\n+\t */\n+\tif (!enc || (src && !src_len))\n+\t\treturn 0;\n+\n+\tdst = reencode_string_len(src, src_len, enc, default_encoding,\n+\t\t\t\t  &dst_len);\n+\tif (!dst) {\n+\t\terror(\"failed to encode '%s' from %s to %s\",\n+\t\t\tpath, default_encoding, enc);\n+\t\treturn 0;\n+\t}\n+\n+\tstrbuf_attach(buf, dst, dst_len, dst_len + 1);\n+\treturn 1;\n+}\n+\n static int crlf_to_git(const struct index_state *istate,\n \t\t       const char *path, const char *src, size_t len,\n \t\t       struct strbuf *buf,\n@@ -978,6 +1051,25 @@ static int ident_to_worktree(const char *path, const char *src, size_t len,\n \treturn 1;\n }\n \n+static const char *git_path_check_encoding(struct attr_check_item *check)\n+{\n+\tconst char *value = check->value;\n+\n+\tif (ATTR_UNSET(value) || !strlen(value))\n+\t\treturn NULL;\n+\n+\tif (ATTR_TRUE(value) || ATTR_FALSE(value)) {\n+\t\terror(_(\"working-tree-encoding attribute requires a value\"));\n+\t\treturn NULL;\n+\t}\n+\n+\t/* Don't encode to the default encoding */\n+\tif (!strcasecmp(value, default_encoding))\n+\t\treturn NULL;\n+\n+\treturn value;\n+}\n+\n static enum crlf_action git_path_check_crlf(struct attr_check_item *check)\n {\n \tconst char *value = check->value;\n@@ -1033,6 +1125,7 @@ struct conv_attrs {\n \tenum crlf_action attr_action; /* What attr says */\n \tenum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */\n \tint ident;\n+\tconst char *working_tree_encoding; /* Supported encoding or default encoding if NULL */\n };\n \n static void convert_attrs(struct conv_attrs *ca, const char *path)\n@@ -1041,7 +1134,8 @@ static void convert_attrs(struct conv_attrs *ca, const char *path)\n \n \tif (!check) {\n \t\tcheck = attr_check_initl(\"crlf\", \"ident\", \"filter\",\n-\t\t\t\t\t \"eol\", \"text\", NULL);\n+\t\t\t\t\t \"eol\", \"text\", \"working-tree-encoding\",\n+\t\t\t\t\t NULL);\n \t\tuser_convert_tail = &user_convert;\n \t\tgit_config(read_convert_config, NULL);\n \t}\n@@ -1064,6 +1158,7 @@ static void convert_attrs(struct conv_attrs *ca, const char *path)\n \t\t\telse if (eol_attr == EOL_CRLF)\n \t\t\t\tca->crlf_action = CRLF_TEXT_CRLF;\n \t\t}\n+\t\tca->working_tree_encoding = git_path_check_encoding(ccheck + 5);\n \t} else {\n \t\tca->drv = NULL;\n \t\tca->crlf_action = CRLF_UNDEFINED;\n@@ -1144,6 +1239,13 @@ int convert_to_git(const struct index_state *istate,\n \t\tsrc = dst->buf;\n \t\tlen = dst->len;\n \t}\n+\n+\tret |= encode_to_git(path, src, len, dst, ca.working_tree_encoding, conv_flags);\n+\tif (ret && dst) {\n+\t\tsrc = dst->buf;\n+\t\tlen = dst->len;\n+\t}\n+\n \tif (!(conv_flags & CONV_EOL_KEEP_CRLF)) {\n \t\tret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, conv_flags);\n \t\tif (ret && dst) {\n@@ -1167,6 +1269,7 @@ void convert_to_git_filter_fd(const struct index_state *istate,\n \tif (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL))\n \t\tdie(\"%s: clean filter '%s' failed\", path, ca.drv->name);\n \n+\tencode_to_git(path, dst->buf, dst->len, dst, ca.working_tree_encoding, conv_flags);\n \tcrlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, conv_flags);\n \tident_to_git(path, dst->buf, dst->len, dst, ca.ident);\n }\n@@ -1198,6 +1301,12 @@ static int convert_to_working_tree_internal(const char *path, const char *src,\n \t\t}\n \t}\n \n+\tret |= encode_to_worktree(path, src, len, dst, ca.working_tree_encoding);\n+\tif (ret) {\n+\t\tsrc = dst->buf;\n+\t\tlen = dst->len;\n+\t}\n+\n \tret_filter = apply_filter(\n \t\tpath, src, len, -1, dst, ca.drv, CAP_SMUDGE, dco);\n \tif (!ret_filter && ca.drv && ca.drv->required)\n@@ -1664,6 +1773,9 @@ struct stream_filter *get_stream_filter(const char *path, const unsigned char *s\n \tif (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean))\n \t\treturn NULL;\n \n+\tif (ca.working_tree_encoding)\n+\t\treturn NULL;\n+\n \tif (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)\n \t\treturn NULL;\n \ndiff --git a/convert.h b/convert.h\nindex 65ab3e5167..1d9539ed0b 100644\n--- a/convert.h\n+++ b/convert.h\n@@ -12,6 +12,7 @@ struct index_state;\n #define CONV_EOL_RNDTRP_WARN  (1<<1) /* Warn if CRLF to LF to CRLF is different */\n #define CONV_EOL_RENORMALIZE  (1<<2) /* Convert CRLF to LF */\n #define CONV_EOL_KEEP_CRLF    (1<<3) /* Keep CRLF line endings as is */\n+#define CONV_WRITE_OBJECT     (1<<4) /* Content is written to the index */\n \n extern int global_conv_flags_eol;\n \ndiff --git a/sha1_file.c b/sha1_file.c\nindex 6bc7c6ada9..e2f319d677 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -138,7 +138,7 @@ static int get_conv_flags(unsigned flags)\n \tif (flags & HASH_RENORMALIZE)\n \t\treturn CONV_EOL_RENORMALIZE;\n \telse if (flags & HASH_WRITE_OBJECT)\n-\t  return global_conv_flags_eol;\n+\t\treturn global_conv_flags_eol | CONV_WRITE_OBJECT;\n \telse\n \t\treturn 0;\n }\ndiff --git a/t/t0028-working-tree-encoding.sh b/t/t0028-working-tree-encoding.sh\nnew file mode 100755\nindex 0000000000..e492945a01\n--- /dev/null\n+++ b/t/t0028-working-tree-encoding.sh\n@@ -0,0 +1,144 @@\n+#!/bin/sh\n+\n+test_description='working-tree-encoding conversion via gitattributes'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup test files' '\n+\tgit config core.eol lf &&\n+\n+\ttext=\"hallo there!\\ncan you read me?\" &&\n+\techo \"*.utf16 text working-tree-encoding=utf-16\" >.gitattributes &&\n+\tprintf \"$text\" >test.utf8.raw &&\n+\tprintf \"$text\" | iconv -f UTF-8 -t UTF-16 >test.utf16.raw &&\n+\tprintf \"$text\" | iconv -f UTF-8 -t UTF-32 >test.utf32.raw &&\n+\n+\t# Line ending tests\n+\tprintf \"one\\ntwo\\nthree\\n\" >lf.utf8.raw &&\n+\tprintf \"one\\r\\ntwo\\r\\nthree\\r\\n\" >crlf.utf8.raw &&\n+\n+\t# BOM tests\n+\tprintf \"\\0a\\0b\\0c\"                         >nobom.utf16be.raw &&\n+\tprintf \"a\\0b\\0c\\0\"                         >nobom.utf16le.raw &&\n+\tprintf \"\\376\\777\\0a\\0b\\0c\"                 >bebom.utf16be.raw &&\n+\tprintf \"\\777\\376a\\0b\\0c\\0\"                 >lebom.utf16le.raw &&\n+\tprintf \"\\0\\0\\0a\\0\\0\\0b\\0\\0\\0c\"             >nobom.utf32be.raw &&\n+\tprintf \"a\\0\\0\\0b\\0\\0\\0c\\0\\0\\0\"             >nobom.utf32le.raw &&\n+\tprintf \"\\0\\0\\376\\777\\0\\0\\0a\\0\\0\\0b\\0\\0\\0c\" >bebom.utf32be.raw &&\n+\tprintf \"\\777\\376\\0\\0a\\0\\0\\0b\\0\\0\\0c\\0\\0\\0\" >lebom.utf32le.raw &&\n+\n+\t# Add only UTF-16 file, we will add the UTF-32 file later\n+\tcp test.utf16.raw test.utf16 &&\n+\tcp test.utf32.raw test.utf32 &&\n+\tgit add .gitattributes test.utf16 &&\n+\tgit commit -m initial\n+'\n+\n+test_expect_success 'ensure UTF-8 is stored in Git' '\n+\ttest_when_finished \"rm -f test.utf16.git\" &&\n+\n+\tgit cat-file -p :test.utf16 >test.utf16.git &&\n+\ttest_cmp_bin test.utf8.raw test.utf16.git\n+'\n+\n+test_expect_success 're-encode to UTF-16 on checkout' '\n+\ttest_when_finished \"rm -f test.utf16.raw\" &&\n+\n+\trm test.utf16 &&\n+\tgit checkout test.utf16 &&\n+\ttest_cmp_bin test.utf16.raw test.utf16\n+'\n+\n+test_expect_success 'check $GIT_DIR/info/attributes support' '\n+\ttest_when_finished \"rm -f test.utf32.git\" &&\n+\ttest_when_finished \"git reset --hard HEAD\" &&\n+\n+\techo \"*.utf32 text working-tree-encoding=utf-32\" >.git/info/attributes &&\n+\tgit add test.utf32 &&\n+\n+\tgit cat-file -p :test.utf32 >test.utf32.git &&\n+\ttest_cmp_bin test.utf8.raw test.utf32.git\n+'\n+\n+for i in 16 32\n+do\n+\ttest_expect_success \"eol conversion for UTF-${i} encoded files on checkout\" '\n+\t\ttest_when_finished \"rm -f crlf.utf${i}.raw lf.utf${i}.raw\" &&\n+\t\ttest_when_finished \"git reset --hard HEAD^\" &&\n+\n+\t\tcat lf.utf8.raw | iconv -f UTF-8 -t UTF-${i} >lf.utf${i}.raw &&\n+\t\tcat crlf.utf8.raw | iconv -f UTF-8 -t UTF-${i} >crlf.utf${i}.raw &&\n+\t\tcp crlf.utf${i}.raw eol.utf${i} &&\n+\n+\t\tcat >expectIndexLF <<-EOF &&\n+\t\t\ti/lf    w/-text attr/text             \teol.utf${i}\n+\t\tEOF\n+\n+\t\tgit add eol.utf${i} &&\n+\t\tgit commit -m eol &&\n+\n+\t\t# UTF-${i} with CRLF (Windows line endings)\n+\t\trm eol.utf${i} &&\n+\t\tgit -c core.eol=crlf checkout eol.utf${i} &&\n+\t\ttest_cmp_bin crlf.utf${i}.raw eol.utf${i} &&\n+\n+\t\t# Although the file has CRLF in the working tree,\n+\t\t# ensure LF in the index\n+\t\tgit ls-files --eol eol.utf${i} >actual &&\n+\t\ttest_cmp expectIndexLF actual &&\n+\n+\t\t# UTF-${i} with LF (Unix line endings)\n+\t\trm eol.utf${i} &&\n+\t\tgit -c core.eol=lf checkout eol.utf${i} &&\n+\t\ttest_cmp_bin lf.utf${i}.raw eol.utf${i} &&\n+\n+\t\t# The file LF in the working tree, ensure LF in the index\n+\t\tgit ls-files --eol eol.utf${i} >actual &&\n+\t\ttest_cmp expectIndexLF actual\n+\t'\n+done\n+\n+test_expect_success 'check unsupported encodings' '\n+\ttest_when_finished \"git reset --hard HEAD\" &&\n+\n+\techo \"*.set text working-tree-encoding\" >>.gitattributes &&\n+\tprintf \"set\" >t.set &&\n+\tgit add t.set 2>err.out &&\n+\ttest_i18ngrep \"error: working-tree-encoding attribute requires a value\" err.out &&\n+\n+\techo \"*.unset text -working-tree-encoding\" >>.gitattributes &&\n+\tprintf \"unset\" >t.unset &&\n+\tgit add t.unset 2>err.out &&\n+\ttest_i18ngrep \"error: working-tree-encoding attribute requires a value\" err.out &&\n+\n+\techo \"*.empty text working-tree-encoding=\" >>.gitattributes &&\n+\tprintf \"empty\" >t.empty &&\n+\tgit add t.empty 2>err.out &&\n+\ttest_i18ngrep \"error: working-tree-encoding attribute requires a value\" err.out &&\n+\n+\techo \"*.garbage text working-tree-encoding=garbage\" >>.gitattributes &&\n+\tprintf \"garbage\" >t.garbage &&\n+\ttest_must_fail git add t.garbage 2>err.out &&\n+\ttest_i18ngrep \"fatal: failed to encode\" err.out\n+'\n+\n+test_expect_success 'error if encoding round trip is not the same during refresh' '\n+\tBEFORE_STATE=$(git rev-parse HEAD) &&\n+\ttest_when_finished \"git reset --hard $BEFORE_STATE\" &&\n+\n+\t# Add and commit a UTF-16 file but skip the \"working-tree-encoding\"\n+\t# filter. Consequently, the in-repo representation is UTF-16 and not\n+\t# UTF-8. This simulates a Git version that has no working tree encoding\n+\t# support.\n+\techo \"*.utf16le text working-tree-encoding=utf-16le\" >.gitattributes &&\n+\techo \"hallo\" >nonsense.utf16le &&\n+\tTEST_HASH=$(git hash-object --no-filters -w nonsense.utf16le) &&\n+\tgit update-index --add --cacheinfo 100644 $TEST_HASH nonsense.utf16le &&\n+\tCOMMIT=$(git commit-tree -p $(git rev-parse HEAD) -m \"plain commit\" $(git write-tree)) &&\n+\tgit update-ref refs/heads/master $COMMIT &&\n+\n+\ttest_must_fail git checkout HEAD^ 2>err.out &&\n+\ttest_i18ngrep \"error: .* overwritten by checkout:\" err.out\n+'\n+\n+test_done\n-- \n2.16.2\n\n"},{"id":"341357","messageId":"20180309173536.62012-11-lars.schneider@autodesk.com","threadId":"48014","inReplyTo":"20180309173536.62012-1-lars.schneider@autodesk.com","subject":"[PATCH v11 10/10] convert: add round trip check based on 'core.checkRoundtripEncoding'","fromName":"","fromEmail":"lars.schneider@autodesk.com","sentAt":"2018-03-09T17:35:36Z","receivedAt":"2018-03-09T17:37:21Z","isPatch":true,"sender":{"key":"lars.schneider@autodesk.com","avatar":"https://gravatar.com/avatar/524188df3a38b02692619777bb90d9cc735db1ad75f5304e0bf09f8efd299775?d=mp&s=160"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nUTF supports lossless conversion round tripping and conversions between\nUTF and other encodings are mostly round trip safe as Unicode aims to be\na superset of all other character encodings. However, certain encodings\n(e.g. SHIFT-JIS) are known to have round trip issues [1].\n\nAdd 'core.checkRoundtripEncoding', which contains a comma separated\nlist of encodings, to define for what encodings Git should check the\nconversion round trip if they are used in the 'working-tree-encoding'\nattribute.\n\nSet SHIFT-JIS as default value for 'core.checkRoundtripEncoding'.\n\n[1] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n Documentation/config.txt         |  6 +++\n Documentation/gitattributes.txt  |  8 ++++\n config.c                         |  5 +++\n convert.c                        | 79 +++++++++++++++++++++++++++++++++++++++-\n convert.h                        |  1 +\n environment.c                    |  1 +\n t/t0028-working-tree-encoding.sh | 39 ++++++++++++++++++++\n 7 files changed, 138 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 0e25b2c92b..7dcac9b540 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -530,6 +530,12 @@ core.autocrlf::\n \tThis variable can be set to 'input',\n \tin which case no output conversion is performed.\n \n+core.checkRoundtripEncoding::\n+\tA comma and/or whitespace separated list of encodings that Git\n+\tperforms UTF-8 round trip checks on if they are used in an\n+\t`working-tree-encoding` attribute (see linkgit:gitattributes[5]).\n+\tThe default value is `SHIFT-JIS`.\n+\n core.symlinks::\n \tIf false, symbolic links are checked out as small plain files that\n \tcontain the link text. linkgit:git-update-index[1] and\ndiff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt\nindex 31a4f92840..aa3deae392 100644\n--- a/Documentation/gitattributes.txt\n+++ b/Documentation/gitattributes.txt\n@@ -312,6 +312,14 @@ number of pitfalls:\n   internal contents as UTF-8 and try to convert it to UTF-16 on checkout.\n   That operation will fail and cause an error.\n \n+- Reencoding content to non-UTF encodings can cause errors as the\n+  conversion might not be UTF-8 round trip safe. If you suspect your\n+  encoding to not be round trip safe, then add it to\n+  `core.checkRoundtripEncoding` to make Git check the round trip\n+  encoding (see linkgit:git-config[1]). SHIFT-JIS (Japanese character\n+  set) is known to have round trip issues with UTF-8 and is checked by\n+  default.\n+\n - Reencoding content requires resources that might slow down certain\n   Git operations (e.g 'git checkout' or 'git add').\n \ndiff --git a/config.c b/config.c\nindex 1f003fbb90..d0ada9fcd4 100644\n--- a/config.c\n+++ b/config.c\n@@ -1172,6 +1172,11 @@ static int git_default_core_config(const char *var, const char *value)\n \t\treturn 0;\n \t}\n \n+\tif (!strcmp(var, \"core.checkroundtripencoding\")) {\n+\t\tcheck_roundtrip_encoding = xstrdup(value);\n+\t\treturn 0;\n+\t}\n+\n \tif (!strcmp(var, \"core.notesref\")) {\n \t\tnotes_ref_name = xstrdup(value);\n \t\treturn 0;\ndiff --git a/convert.c b/convert.c\nindex d739078016..c2d24882c1 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -347,6 +347,42 @@ static void trace_encoding(const char *context, const char *path,\n \tstrbuf_release(&trace);\n }\n \n+static int check_roundtrip(const char *enc_name)\n+{\n+\t/*\n+\t * check_roundtrip_encoding contains a string of comma and/or\n+\t * space separated encodings (eg. \"UTF-16, ASCII, CP1125\").\n+\t * Search for the given encoding in that string.\n+\t */\n+\tconst char *found = strcasestr(check_roundtrip_encoding, enc_name);\n+\tconst char *next;\n+\tint len;\n+\tif (!found)\n+\t\treturn 0;\n+\tnext = found + strlen(enc_name);\n+\tlen = strlen(check_roundtrip_encoding);\n+\treturn (found && (\n+\t\t\t/*\n+\t\t\t * check that the found encoding is at the\n+\t\t\t * beginning of check_roundtrip_encoding or\n+\t\t\t * that it is prefixed with a space or comma\n+\t\t\t */\n+\t\t\tfound == check_roundtrip_encoding || (\n+\t\t\t\t(isspace(found[-1]) || found[-1] == ',')\n+\t\t\t)\n+\t\t) && (\n+\t\t\t/*\n+\t\t\t * check that the found encoding is at the\n+\t\t\t * end of check_roundtrip_encoding or\n+\t\t\t * that it is suffixed with a space or comma\n+\t\t\t */\n+\t\t\tnext == check_roundtrip_encoding + len || (\n+\t\t\t\tnext < check_roundtrip_encoding + len &&\n+\t\t\t\t(isspace(next[0]) || next[0] == ',')\n+\t\t\t)\n+\t\t));\n+}\n+\n static const char *default_encoding = \"UTF-8\";\n \n static int encode_to_git(const char *path, const char *src, size_t src_len,\n@@ -395,6 +431,47 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,\n \t}\n \ttrace_encoding(\"destination\", path, default_encoding, dst, dst_len);\n \n+\t/*\n+\t * UTF supports lossless conversion round tripping [1] and conversions\n+\t * between UTF and other encodings are mostly round trip safe as\n+\t * Unicode aims to be a superset of all other character encodings.\n+\t * However, certain encodings (e.g. SHIFT-JIS) are known to have round\n+\t * trip issues [2]. Check the round trip conversion for all encodings\n+\t * listed in core.checkRoundtripEncoding.\n+\t *\n+\t * The round trip check is only performed if content is written to Git.\n+\t * This ensures that no information is lost during conversion to/from\n+\t * the internal UTF-8 representation.\n+\t *\n+\t * Please note, the code below is not tested because I was not able to\n+\t * generate a faulty round trip without an iconv error. Iconv errors\n+\t * are already caught above.\n+\t *\n+\t * [1] http://unicode.org/faq/utf_bom.html#gen2\n+\t * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode\n+\t */\n+\tif (die_on_error && check_roundtrip(enc)) {\n+\t\tchar *re_src;\n+\t\tint re_src_len;\n+\n+\t\tre_src = reencode_string_len(dst, dst_len,\n+\t\t\t\t\t     enc, default_encoding,\n+\t\t\t\t\t     &re_src_len);\n+\n+\t\ttrace_printf(\"Checking roundtrip encoding for %s...\\n\", enc);\n+\t\ttrace_encoding(\"reencoded source\", path, enc,\n+\t\t\t       re_src, re_src_len);\n+\n+\t\tif (!re_src || src_len != re_src_len ||\n+\t\t    memcmp(src, re_src, src_len)) {\n+\t\t\tconst char* msg = _(\"encoding '%s' from %s to %s and \"\n+\t\t\t\t\t    \"back is not the same\");\n+\t\t\tdie(msg, path, enc, default_encoding);\n+\t\t}\n+\n+\t\tfree(re_src);\n+\t}\n+\n \tstrbuf_attach(buf, dst, dst_len, dst_len + 1);\n \treturn 1;\n }\n@@ -1150,7 +1227,7 @@ static const char *git_path_check_encoding(struct attr_check_item *check)\n \t}\n \n \t/* Don't encode to the default encoding */\n-\tif (!strcasecmp(value, default_encoding))\n+\tif (is_encoding_utf8(value) && is_encoding_utf8(default_encoding))\n \t\treturn NULL;\n \n \treturn value;\ndiff --git a/convert.h b/convert.h\nindex 1d9539ed0b..765abfbd60 100644\n--- a/convert.h\n+++ b/convert.h\n@@ -56,6 +56,7 @@ struct delayed_checkout {\n };\n \n extern enum eol core_eol;\n+extern char *check_roundtrip_encoding;\n extern const char *get_cached_convert_stats_ascii(const struct index_state *istate,\n \t\t\t\t\t\t  const char *path);\n extern const char *get_wt_convert_stats_ascii(const char *path);\ndiff --git a/environment.c b/environment.c\nindex 10a32c20ac..5bae9131ad 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -50,6 +50,7 @@ int check_replace_refs = 1;\n char *git_replace_ref_base;\n enum eol core_eol = EOL_UNSET;\n int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;\n+char *check_roundtrip_encoding = \"SHIFT-JIS\";\n unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;\n enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;\n enum rebase_setup_type autorebase = AUTOREBASE_NEVER;\ndiff --git a/t/t0028-working-tree-encoding.sh b/t/t0028-working-tree-encoding.sh\nindex f68e282c5e..07089bba2e 100755\n--- a/t/t0028-working-tree-encoding.sh\n+++ b/t/t0028-working-tree-encoding.sh\n@@ -205,4 +205,43 @@ test_expect_success 'error if encoding garbage is already in Git' '\n \ttest_i18ngrep \"error: BOM is required\" err.out\n '\n \n+test_expect_success 'check roundtrip encoding' '\n+\ttest_when_finished \"rm -f roundtrip.shift roundtrip.utf16\" &&\n+\ttest_when_finished \"git reset --hard HEAD\" &&\n+\n+\ttext=\"hallo there!\\nroundtrip test here!\" &&\n+\tprintf \"$text\" | iconv -f UTF-8 -t SHIFT-JIS >roundtrip.shift &&\n+\tprintf \"$text\" | iconv -f UTF-8 -t UTF-16 >roundtrip.utf16 &&\n+\techo \"*.shift text working-tree-encoding=SHIFT-JIS\" >>.gitattributes &&\n+\n+\t# SHIFT-JIS encoded files are round-trip checked by default...\n+\tGIT_TRACE=1 git add .gitattributes roundtrip.shift 2>&1 |\n+\t\tgrep \"Checking roundtrip encoding for SHIFT-JIS\" &&\n+\tgit reset &&\n+\n+\t# ... unless we overwrite the Git config!\n+\t! GIT_TRACE=1 git -c core.checkRoundtripEncoding=garbage \\\n+\t\tadd .gitattributes roundtrip.shift 2>&1 |\n+\t\tgrep \"Checking roundtrip encoding for SHIFT-JIS\" &&\n+\tgit reset &&\n+\n+\t# UTF-16 encoded files should not be round-trip checked by default...\n+\t! GIT_TRACE=1 git add roundtrip.utf16 2>&1 |\n+\t\tgrep \"Checking roundtrip encoding for UTF-16\" &&\n+\tgit reset &&\n+\n+\t# ... unless we tell Git to check it!\n+\tGIT_TRACE=1 git -c core.checkRoundtripEncoding=\"UTF-16, UTF-32\" \\\n+\t\tadd roundtrip.utf16 2>&1 |\n+\t\tgrep \"Checking roundtrip encoding for utf-16\" &&\n+\tgit reset &&\n+\n+\t# ... unless we tell Git to check it!\n+\t# (here we also check that the casing of the encoding is irrelevant)\n+\tGIT_TRACE=1 git -c core.checkRoundtripEncoding=\"UTF-32, utf-16\" \\\n+\t\tadd roundtrip.utf16 2>&1 |\n+\t\tgrep \"Checking roundtrip encoding for utf-16\" &&\n+\tgit reset\n+'\n+\n test_done\n-- \n2.16.2\n\n"},{"id":"341358","messageId":"20180309173536.62012-2-lars.schneider@autodesk.com","threadId":"48014","inReplyTo":"20180309173536.62012-1-lars.schneider@autodesk.com","subject":"[PATCH v11 01/10] strbuf: remove unnecessary NUL assignment in xstrdup_tolower()","fromName":"","fromEmail":"lars.schneider@autodesk.com","sentAt":"2018-03-09T17:35:27Z","receivedAt":"2018-03-09T17:37:24Z","isPatch":true,"sender":{"key":"lars.schneider@autodesk.com","avatar":"https://gravatar.com/avatar/524188df3a38b02692619777bb90d9cc735db1ad75f5304e0bf09f8efd299775?d=mp&s=160"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nSince 3733e69464 (use xmallocz to avoid size arithmetic, 2016-02-22) we\nallocate the buffer for the lower case string with xmallocz(). This\nalready ensures a NUL at the end of the allocated buffer.\n\nRemove the unnecessary assignment.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n strbuf.c | 1 -\n 1 file changed, 1 deletion(-)\n\ndiff --git a/strbuf.c b/strbuf.c\nindex 1df674e919..55b7daeb35 100644\n--- a/strbuf.c\n+++ b/strbuf.c\n@@ -781,7 +781,6 @@ char *xstrdup_tolower(const char *string)\n \tresult = xmallocz(len);\n \tfor (i = 0; i < len; i++)\n \t\tresult[i] = tolower(string[i]);\n-\tresult[i] = '\\0';\n \treturn result;\n }\n \n-- \n2.16.2\n\n"},{"id":"341359","messageId":"20180309173536.62012-6-lars.schneider@autodesk.com","threadId":"48014","inReplyTo":"20180309173536.62012-1-lars.schneider@autodesk.com","subject":"[PATCH v11 05/10] utf8: add function to detect a missing UTF-16/32 BOM","fromName":"","fromEmail":"lars.schneider@autodesk.com","sentAt":"2018-03-09T17:35:31Z","receivedAt":"2018-03-09T17:37:27Z","isPatch":true,"sender":{"key":"lars.schneider@autodesk.com","avatar":"https://gravatar.com/avatar/524188df3a38b02692619777bb90d9cc735db1ad75f5304e0bf09f8efd299775?d=mp&s=160"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nIf the endianness is not defined in the encoding name, then let's\nbe strict and require a BOM to avoid any encoding confusion. The\nis_missing_required_utf_bom() function returns true if a required BOM\nis missing.\n\nThe Unicode standard instructs to assume big-endian if there in no BOM\nfor UTF-16/32 [1][2]. However, the W3C/WHATWG encoding standard used\nin HTML5 recommends to assume little-endian to \"deal with deployed\ncontent\" [3]. Strictly requiring a BOM seems to be the safest option\nfor content in Git.\n\nThis function is used in a subsequent commit.\n\n[1] http://unicode.org/faq/utf_bom.html#gen6\n[2] http://www.unicode.org/versions/Unicode10.0.0/ch03.pdf\n     Section 3.10, D98, page 132\n[3] https://encoding.spec.whatwg.org/#utf-16le\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n utf8.c | 13 +++++++++++++\n utf8.h | 19 +++++++++++++++++++\n 2 files changed, 32 insertions(+)\n\ndiff --git a/utf8.c b/utf8.c\nindex e4b99580f0..81c6678df1 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -564,6 +564,19 @@ int has_prohibited_utf_bom(const char *enc, const char *data, size_t len)\n \t);\n }\n \n+int is_missing_required_utf_bom(const char *enc, const char *data, size_t len)\n+{\n+\treturn (\n+\t   (!strcasecmp(enc, \"UTF-16\") || !strcasecmp(enc, \"UTF16\")) &&\n+\t   !(has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) ||\n+\t     has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom)))\n+\t) || (\n+\t   (!strcasecmp(enc, \"UTF-32\") || !strcasecmp(enc, \"UTF32\")) &&\n+\t   !(has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) ||\n+\t     has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom)))\n+\t);\n+}\n+\n /*\n  * Returns first character length in bytes for multi-byte `text` according to\n  * `encoding`.\ndiff --git a/utf8.h b/utf8.h\nindex 0db1db4519..cce654a64a 100644\n--- a/utf8.h\n+++ b/utf8.h\n@@ -79,4 +79,23 @@ void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int wid\n  */\n int has_prohibited_utf_bom(const char *enc, const char *data, size_t len);\n \n+/*\n+ * If the endianness is not defined in the encoding name, then we\n+ * require a BOM. The function returns true if a required BOM is missing.\n+ *\n+ * The Unicode standard instructs to assume big-endian if there in no\n+ * BOM for UTF-16/32 [1][2]. However, the W3C/WHATWG encoding standard\n+ * used in HTML5 recommends to assume little-endian to \"deal with\n+ * deployed content\" [3].\n+ *\n+ * Therefore, strictly requiring a BOM seems to be the safest option for\n+ * content in Git.\n+ *\n+ * [1] http://unicode.org/faq/utf_bom.html#gen6\n+ * [2] http://www.unicode.org/versions/Unicode10.0.0/ch03.pdf\n+ *     Section 3.10, D98, page 132\n+ * [3] https://encoding.spec.whatwg.org/#utf-16le\n+ */\n+int is_missing_required_utf_bom(const char *enc, const char *data, size_t len);\n+\n #endif\n-- \n2.16.2\n\n"},{"id":"341360","messageId":"20180309173536.62012-5-lars.schneider@autodesk.com","threadId":"48014","inReplyTo":"20180309173536.62012-1-lars.schneider@autodesk.com","subject":"[PATCH v11 04/10] utf8: add function to detect prohibited UTF-16/32 BOM","fromName":"","fromEmail":"lars.schneider@autodesk.com","sentAt":"2018-03-09T17:35:30Z","receivedAt":"2018-03-09T17:37:31Z","isPatch":true,"sender":{"key":"lars.schneider@autodesk.com","avatar":"https://gravatar.com/avatar/524188df3a38b02692619777bb90d9cc735db1ad75f5304e0bf09f8efd299775?d=mp&s=160"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nWhenever a data stream is declared to be UTF-16BE, UTF-16LE, UTF-32BE\nor UTF-32LE a BOM must not be used [1]. The function returns true if\nthis is the case.\n\nThis function is used in a subsequent commit.\n\n[1] http://unicode.org/faq/utf_bom.html#bom10\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n utf8.c | 26 ++++++++++++++++++++++++++\n utf8.h |  9 +++++++++\n 2 files changed, 35 insertions(+)\n\ndiff --git a/utf8.c b/utf8.c\nindex 2c27ce0137..e4b99580f0 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -538,6 +538,32 @@ char *reencode_string_len(const char *in, int insz,\n }\n #endif\n \n+static int has_bom_prefix(const char *data, size_t len,\n+\t\t\t  const char *bom, size_t bom_len)\n+{\n+\treturn (len >= bom_len) && !memcmp(data, bom, bom_len);\n+}\n+\n+static const char utf16_be_bom[] = {0xFE, 0xFF};\n+static const char utf16_le_bom[] = {0xFF, 0xFE};\n+static const char utf32_be_bom[] = {0x00, 0x00, 0xFE, 0xFF};\n+static const char utf32_le_bom[] = {0xFF, 0xFE, 0x00, 0x00};\n+\n+int has_prohibited_utf_bom(const char *enc, const char *data, size_t len)\n+{\n+\treturn (\n+\t  (!strcasecmp(enc, \"UTF-16BE\") || !strcasecmp(enc, \"UTF-16LE\") ||\n+\t   !strcasecmp(enc, \"UTF16BE\") || !strcasecmp(enc, \"UTF16LE\")) &&\n+\t  (has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) ||\n+\t   has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom)))\n+\t) || (\n+\t  (!strcasecmp(enc, \"UTF-32BE\") || !strcasecmp(enc, \"UTF-32LE\") ||\n+\t   !strcasecmp(enc, \"UTF32BE\") || !strcasecmp(enc, \"UTF32LE\")) &&\n+\t  (has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) ||\n+\t   has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom)))\n+\t);\n+}\n+\n /*\n  * Returns first character length in bytes for multi-byte `text` according to\n  * `encoding`.\ndiff --git a/utf8.h b/utf8.h\nindex 6bbcf31a83..0db1db4519 100644\n--- a/utf8.h\n+++ b/utf8.h\n@@ -70,4 +70,13 @@ typedef enum {\n void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,\n \t\t       const char *s);\n \n+/*\n+ * If a data stream is declared as UTF-16BE or UTF-16LE, then a UTF-16\n+ * BOM must not be used [1]. The same applies for the UTF-32 equivalents.\n+ * The function returns true if this rule is violated.\n+ *\n+ * [1] http://unicode.org/faq/utf_bom.html#bom10\n+ */\n+int has_prohibited_utf_bom(const char *enc, const char *data, size_t len);\n+\n #endif\n-- \n2.16.2\n\n"},{"id":"341363","messageId":"xmqqy3j15b2h.fsf@gitster-ct.c.googlers.com","threadId":"48014","inReplyTo":"20180309173536.62012-8-lars.schneider@autodesk.com","subject":"Re: [PATCH v11 07/10] convert: check for detectable errors in UTF encodings","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-03-09T19:00:06Z","receivedAt":"2018-03-09T19:00:19Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"lars.schneider@autodesk.com writes:\n\n> +\t\t\tconst char *advise_msg = _(\n> +\t\t\t\t\"The file '%s' contains a byte order \"\n> +\t\t\t\t\"mark (BOM). Please use %.6s as \"\n> +\t\t\t\t\"working-tree-encoding.\");\n\nI know that this will go away in a later step, but why \".6\"?\n\n> +\t\t\tadvise(advise_msg, path, enc);\n"},{"id":"341364","messageId":"AB377212-0551-4DFF-A953-734DC847934B@gmail.com","threadId":"48014","inReplyTo":"xmqqy3j15b2h.fsf@gitster-ct.c.googlers.com","subject":"Re: [PATCH v11 07/10] convert: check for detectable errors in UTF encodings","fromName":"Lars Schneider","fromEmail":"larsxschneider@gmail.com","sentAt":"2018-03-09T19:04:34Z","receivedAt":"2018-03-09T19:04:44Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"\n> On 09 Mar 2018, at 20:00, Junio C Hamano <gitster@pobox.com> wrote:\n> \n> lars.schneider@autodesk.com writes:\n> \n>> +\t\t\tconst char *advise_msg = _(\n>> +\t\t\t\t\"The file '%s' contains a byte order \"\n>> +\t\t\t\t\"mark (BOM). Please use %.6s as \"\n>> +\t\t\t\t\"working-tree-encoding.\");\n> \n> I know that this will go away in a later step, but why \".6\"?\n\nI deleted the original comment in the rebase, sorry:\n\n    /*\n     * This advice is shown for UTF-??BE and UTF-??LE\n     * encodings. We truncate the encoding name to 6\n     * chars with %.6s to cut off the last two \"byte\n     * order\" characters.\n     */\n\n- Lars\n"},{"id":"341366","messageId":"xmqqmuzh5alb.fsf@gitster-ct.c.googlers.com","threadId":"48014","inReplyTo":"20180309173536.62012-7-lars.schneider@autodesk.com","subject":"Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-03-09T19:10:24Z","receivedAt":"2018-03-09T19:10:31Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"lars.schneider@autodesk.com writes:\n\n> +static const char *default_encoding = \"UTF-8\";\n> +\n> ...\n> +static const char *git_path_check_encoding(struct attr_check_item *check)\n> +{\n> +\tconst char *value = check->value;\n> +\n> +\tif (ATTR_UNSET(value) || !strlen(value))\n> +\t\treturn NULL;\n> +\n> +\tif (ATTR_TRUE(value) || ATTR_FALSE(value)) {\n> +\t\terror(_(\"working-tree-encoding attribute requires a value\"));\n> +\t\treturn NULL;\n> +\t}\n\nHmph, so we decide to be loud but otherwise ignore an undefined\nconfiguration?  Shouldn't we rather die instead to avoid touching\nthe user data in unexpected ways?\n\n> +\n> +\t/* Don't encode to the default encoding */\n> +\tif (!strcasecmp(value, default_encoding))\n> +\t\treturn NULL;\n\nIs this an optimization to avoid \"recode one encoding to the same\nencoding\" no-op overhead?  We already have the optimization in the\nsame spirit in may existing codepaths that has nothing to do with\nw-t-e, and I think we should share the code.  Two pieces of thought\ncomes to mind.\n\nOne is a lot smaller in scale: Is same_encoding() sufficient for\nthis callsite instead of strcasecmp()?\n\nThe other one is a lot bigger: Looking at all the existing callers\nof same_encoding() that call reencode_string() when it returns false,\nwould it make sense to drop same_encoding() and move the optimization\nto reencode_string() instead?\n\nI suspect that the answer to the smaller one is \"yes, and even if\nnot, it should be easy to enhance/extend same_encoding() to make it\ndo what we want it to, and such a change will benefit even existing\ncallers.\"  The answer to the larger one is likely \"the optimization\nis not about skipping only reencode_string() call but other things\nare subtly different among callers of same_encoding(), so such a\nrefactoring would not be all that useful.\"\n\nThe above still holds for the code after 10/10 touches this part.\n"},{"id":"341367","messageId":"xmqqlgf15akp.fsf@gitster-ct.c.googlers.com","threadId":"48014","inReplyTo":"20180309173536.62012-8-lars.schneider@autodesk.com","subject":"Re: [PATCH v11 07/10] convert: check for detectable errors in UTF encodings","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-03-09T19:10:46Z","receivedAt":"2018-03-09T19:10:56Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"lars.schneider@autodesk.com writes:\n\n> +\t\t\tconst char *advise_msg = _(\n> +\t\t\t\t\"The file '%s' contains a byte order \"\n> +\t\t\t\t\"mark (BOM). Please use %.6s as \"\n> +\t\t\t\t\"working-tree-encoding.\");\n\nI know that this will go away in a later step, but why \".6\"?\n\n> +\t\t\tadvise(advise_msg, path, enc);\n"},{"id":"341368","messageId":"xmqqefkt5ak0.fsf@gitster-ct.c.googlers.com","threadId":"48014","inReplyTo":"20180309173536.62012-9-lars.schneider@autodesk.com","subject":"Re: [PATCH v11 08/10] convert: advise canonical UTF encoding names","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-03-09T19:11:11Z","receivedAt":"2018-03-09T19:11:21Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"lars.schneider@autodesk.com writes:\n\n> From: Lars Schneider <larsxschneider@gmail.com>\n>\n> The canonical name of an UTF encoding has the format UTF, dash, number,\n> and an optionally byte order in upper case (e.g. UTF-8 or UTF-16BE).\n> Some iconv versions support alternative names without a dash or with\n> lower case characters.\n>\n> To avoid problems between different iconv version always suggest the\n> canonical UTF names in advise messages.\n>\n> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>\n> ---\n\nI think it is probably better to squash this to earlier step,\ni.e. jumping straight to the endgame solution.\n\n> diff --git a/convert.c b/convert.c\n> index b80d666a6b..9a3ae7cce1 100644\n> --- a/convert.c\n> +++ b/convert.c\n> @@ -279,12 +279,20 @@ static int validate_encoding(const char *path, const char *enc,\n>  \t\t\t\t\"BOM is prohibited in '%s' if encoded as %s\");\n>  \t\t\t/*\n>  \t\t\t * This advice is shown for UTF-??BE and UTF-??LE encodings.\n> +\t\t\t * We cut off the last two characters of the encoding name\n> +\t\t\t # to generate the encoding name suitable for BOMs.\n>  \t\t\t */\n\nI somehow thought that I saw \"s/#/*/\" in somebody's response during\nthe previous round?\n\n>  \t\t\tconst char *advise_msg = _(\n>  \t\t\t\t\"The file '%s' contains a byte order \"\n> -\t\t\t\t\"mark (BOM). Please use %.6s as \"\n> +\t\t\t\t\"mark (BOM). Please use UTF-%s as \"\n>  \t\t\t\t\"working-tree-encoding.\");\n> -\t\t\tadvise(advise_msg, path, enc);\n> +\t\t\tconst char *stripped = \"\";\n> +\t\t\tchar *upper = xstrdup_toupper(enc);\n> +\t\t\tupper[strlen(upper)-2] = '\\0';\n> +\t\t\tif (!skip_prefix(upper, \"UTF-\", &stripped))\n> +\t\t\t\tskip_prefix(stripped, \"UTF\", &stripped);\n> +\t\t\tadvise(advise_msg, path, stripped);\n> +\t\t\tfree(upper);\n\nIf this codepath is ever entered with \"enc\" that does not begin with\n\"UTF\" (e.g. \"Shift_JIS\", which is impossible in the current code,\nbut I'll talk about future-proofing here), then neither of these\nskip_prefix will trigger, and then you'd end up suggesting to use\n\"UTF-\" that is nonsense.  Perhaps initialize stripped to NULL and\nforce advise to segv to catch such a programmer error?\n"},{"id":"341371","messageId":"CAPig+cTH7wmrBwiyBxr=D1g6dTw65ZRfGPX_ok2PYaMoGJk0Dg@mail.gmail.com","threadId":"48014","inReplyTo":"20180309173536.62012-11-lars.schneider@autodesk.com","subject":"Re: [PATCH v11 10/10] convert: add round trip check based on 'core.checkRoundtripEncoding'","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2018-03-09T20:18:08Z","receivedAt":"2018-03-09T20:18:14Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Mar 9, 2018 at 12:35 PM,  <lars.schneider@autodesk.com> wrote:\n> [...]\n> Add 'core.checkRoundtripEncoding', which contains a comma separated\n> list of encodings, to define for what encodings Git should check the\n> conversion round trip if they are used in the 'working-tree-encoding'\n> attribute.\n> [...]\n> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>\n> ---\n> diff --git a/convert.c b/convert.c\n> @@ -1150,7 +1227,7 @@ static const char *git_path_check_encoding(struct attr_check_item *check)\n>         /* Don't encode to the default encoding */\n> -       if (!strcasecmp(value, default_encoding))\n> +       if (is_encoding_utf8(value) && is_encoding_utf8(default_encoding))\n>                 return NULL;\n\nThis change belongs in 6/10, not 10/10, methinks.\n"},{"id":"341372","messageId":"xmqq1sgt578g.fsf@gitster-ct.c.googlers.com","threadId":"48014","inReplyTo":"CAPig+cTH7wmrBwiyBxr=D1g6dTw65ZRfGPX_ok2PYaMoGJk0Dg@mail.gmail.com","subject":"Re: [PATCH v11 10/10] convert: add round trip check based on 'core.checkRoundtripEncoding'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-03-09T20:22:55Z","receivedAt":"2018-03-09T20:23:07Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eric Sunshine <sunshine@sunshineco.com> writes:\n\n> On Fri, Mar 9, 2018 at 12:35 PM,  <lars.schneider@autodesk.com> wrote:\n>> [...]\n>> Add 'core.checkRoundtripEncoding', which contains a comma separated\n>> list of encodings, to define for what encodings Git should check the\n>> conversion round trip if they are used in the 'working-tree-encoding'\n>> attribute.\n>> [...]\n>> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>\n>> ---\n>> diff --git a/convert.c b/convert.c\n>> @@ -1150,7 +1227,7 @@ static const char *git_path_check_encoding(struct attr_check_item *check)\n>>         /* Don't encode to the default encoding */\n>> -       if (!strcasecmp(value, default_encoding))\n>> +       if (is_encoding_utf8(value) && is_encoding_utf8(default_encoding))\n>>                 return NULL;\n>\n> This change belongs in 6/10, not 10/10, methinks.\n\nIt is actually worse than that, no?  When default_encoding is\n(somehow) configured not to be UTF-8, e.g. \"Shift_JIS\", we used to\navoid converting from Shift_JIS to Shift_JIS, but the optimization\nno longer happens with this code.\n\nIn any case, I think same_encoding() is probably a good thing to use\nhere at step 6/10, so the point is moot, I guess.\n"},{"id":"341373","messageId":"CAPig+cQwv_Nmy2P_7xQgzMSMdTUmgp455Tx=jZkj9=VzjJ1XZw@mail.gmail.com","threadId":"48014","inReplyTo":"xmqq1sgt578g.fsf@gitster-ct.c.googlers.com","subject":"Re: [PATCH v11 10/10] convert: add round trip check based on 'core.checkRoundtripEncoding'","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2018-03-09T20:27:29Z","receivedAt":"2018-03-09T20:27:35Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Mar 9, 2018 at 3:22 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Eric Sunshine <sunshine@sunshineco.com> writes:\n>> On Fri, Mar 9, 2018 at 12:35 PM,  <lars.schneider@autodesk.com> wrote:\n>>>         /* Don't encode to the default encoding */\n>>> -       if (!strcasecmp(value, default_encoding))\n>>> +       if (is_encoding_utf8(value) && is_encoding_utf8(default_encoding))\n>>>                 return NULL;\n>>\n>> This change belongs in 6/10, not 10/10, methinks.\n>\n> It is actually worse than that, no?  When default_encoding is\n> (somehow) configured not to be UTF-8, e.g. \"Shift_JIS\", we used to\n> avoid converting from Shift_JIS to Shift_JIS, but the optimization\n> no longer happens with this code.\n>\n> In any case, I think same_encoding() is probably a good thing to use\n> here at step 6/10, so the point is moot, I guess.\n\nAgreed, same_encoding() would be superior.\n"},{"id":"341834","messageId":"BA576CCC-CF0C-4D50-AFC8-5C8FC7F59697@gmail.com","threadId":"48014","inReplyTo":"xmqqmuzh5alb.fsf@gitster-ct.c.googlers.com","subject":"Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute","fromName":"Lars Schneider","fromEmail":"larsxschneider@gmail.com","sentAt":"2018-03-15T21:23:03Z","receivedAt":"2018-03-15T21:23:14Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"\n> On 09 Mar 2018, at 20:10, Junio C Hamano <gitster@pobox.com> wrote:\n> \n> lars.schneider@autodesk.com writes:\n> \n>> +static const char *default_encoding = \"UTF-8\";\n>> +\n>> ...\n>> +static const char *git_path_check_encoding(struct attr_check_item *check)\n>> +{\n>> +\tconst char *value = check->value;\n>> +\n>> +\tif (ATTR_UNSET(value) || !strlen(value))\n>> +\t\treturn NULL;\n>> +\n>> +\tif (ATTR_TRUE(value) || ATTR_FALSE(value)) {\n>> +\t\terror(_(\"working-tree-encoding attribute requires a value\"));\n>> +\t\treturn NULL;\n>> +\t}\n> \n> Hmph, so we decide to be loud but otherwise ignore an undefined\n> configuration?  Shouldn't we rather die instead to avoid touching\n> the user data in unexpected ways?\n\nOK.\n\n\n>> +\n>> +\t/* Don't encode to the default encoding */\n>> +\tif (!strcasecmp(value, default_encoding))\n>> +\t\treturn NULL;\n> \n> Is this an optimization to avoid \"recode one encoding to the same\n> encoding\" no-op overhead?\n\nCorrect.\n\n>  We already have the optimization in the\n> same spirit in may existing codepaths that has nothing to do with\n> w-t-e, and I think we should share the code.  Two pieces of thought\n> comes to mind.\n> \n> One is a lot smaller in scale: Is same_encoding() sufficient for\n> this callsite instead of strcasecmp()?\n\nYes!\n\n\n> The other one is a lot bigger: Looking at all the existing callers\n> of same_encoding() that call reencode_string() when it returns false,\n> would it make sense to drop same_encoding() and move the optimization\n> to reencode_string() instead?\n> \n> I suspect that the answer to the smaller one is \"yes, and even if\n> not, it should be easy to enhance/extend same_encoding() to make it\n> do what we want it to, and such a change will benefit even existing\n> callers.\"  The answer to the larger one is likely \"the optimization\n> is not about skipping only reencode_string() call but other things\n> are subtly different among callers of same_encoding(), so such a\n> refactoring would not be all that useful.\"\n\nI agree. reencode_string() would need to signal 3 cases:\n1. reencode performed\n2. reencode not necessary\n3. reencode failed\n\nWe could model \"reencode not necessary\" as \"char *in == char *return\".\nHowever, I think this should be tackled in a separate series.\n\nThanks\nLars\n"},{"id":"341837","messageId":"D1598F51-5D9E-42FA-A9B7-C1462526B9CB@gmail.com","threadId":"48014","inReplyTo":"xmqqefkt5ak0.fsf@gitster-ct.c.googlers.com","subject":"Re: [PATCH v11 08/10] convert: advise canonical UTF encoding names","fromName":"Lars Schneider","fromEmail":"larsxschneider@gmail.com","sentAt":"2018-03-15T22:42:26Z","receivedAt":"2018-03-15T22:42:52Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"\n> On 09 Mar 2018, at 20:11, Junio C Hamano <gitster@pobox.com> wrote:\n> \n> lars.schneider@autodesk.com writes:\n> \n>> From: Lars Schneider <larsxschneider@gmail.com>\n>> \n>> The canonical name of an UTF encoding has the format UTF, dash, number,\n>> and an optionally byte order in upper case (e.g. UTF-8 or UTF-16BE).\n>> Some iconv versions support alternative names without a dash or with\n>> lower case characters.\n>> \n>> To avoid problems between different iconv version always suggest the\n>> canonical UTF names in advise messages.\n>> \n>> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>\n>> ---\n> \n> I think it is probably better to squash this to earlier step,\n> i.e. jumping straight to the endgame solution.\n\nok!\n\n\n>> diff --git a/convert.c b/convert.c\n>> index b80d666a6b..9a3ae7cce1 100644\n>> --- a/convert.c\n>> +++ b/convert.c\n>> @@ -279,12 +279,20 @@ static int validate_encoding(const char *path, const char *enc,\n>> \t\t\t\t\"BOM is prohibited in '%s' if encoded as %s\");\n>> \t\t\t/*\n>> \t\t\t * This advice is shown for UTF-??BE and UTF-??LE encodings.\n>> +\t\t\t * We cut off the last two characters of the encoding name\n>> +\t\t\t # to generate the encoding name suitable for BOMs.\n>> \t\t\t */\n> \n> I somehow thought that I saw \"s/#/*/\" in somebody's response during\n> the previous round?\n\nOops. Will fix!\n\n\n>> \t\t\tconst char *advise_msg = _(\n>> \t\t\t\t\"The file '%s' contains a byte order \"\n>> -\t\t\t\t\"mark (BOM). Please use %.6s as \"\n>> +\t\t\t\t\"mark (BOM). Please use UTF-%s as \"\n>> \t\t\t\t\"working-tree-encoding.\");\n>> -\t\t\tadvise(advise_msg, path, enc);\n>> +\t\t\tconst char *stripped = \"\";\n>> +\t\t\tchar *upper = xstrdup_toupper(enc);\n>> +\t\t\tupper[strlen(upper)-2] = '\\0';\n>> +\t\t\tif (!skip_prefix(upper, \"UTF-\", &stripped))\n>> +\t\t\t\tskip_prefix(stripped, \"UTF\", &stripped);\n>> +\t\t\tadvise(advise_msg, path, stripped);\n>> +\t\t\tfree(upper);\n> \n> If this codepath is ever entered with \"enc\" that does not begin with\n> \"UTF\" (e.g. \"Shift_JIS\", which is impossible in the current code,\n> but I'll talk about future-proofing here), then neither of these\n> skip_prefix will trigger, and then you'd end up suggesting to use\n> \"UTF-\" that is nonsense.  Perhaps initialize stripped to NULL and\n> force advise to segv to catch such a programmer error?\n\nAgreed!\n\n\nThanks,\nLars\n\n"},{"id":"342077","messageId":"20180318072435.GA24190@tor.lan","threadId":"48014","inReplyTo":"20180309173536.62012-7-lars.schneider@autodesk.com","subject":"Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute","fromName":"Torsten Bögershausen","fromEmail":"tboegi@web.de","sentAt":"2018-03-18T07:24:35Z","receivedAt":"2018-03-18T07:25:03Z","isPatch":true,"sender":{"key":"tboegi@web.de","avatar":"https://avatars.githubusercontent.com/u/7138363?v=4"},"body":"Some comments inline\n\nOn Fri, Mar 09, 2018 at 06:35:32PM +0100, lars.schneider@autodesk.com wrote:\n> From: Lars Schneider <larsxschneider@gmail.com>\n> \n> Git recognizes files encoded with ASCII or one of its supersets (e.g.\n> UTF-8 or ISO-8859-1) as text files. All other encodings are usually\n> interpreted as binary and consequently built-in Git text processing\n> tools (e.g. 'git diff') as well as most Git web front ends do not\n> visualize the content.\n> \n> Add an attribute to tell Git what encoding the user has defined for a\n> given file. If the content is added to the index, then Git converts the\n\nMinor comment:\n\"Git converts the content\"\nEverywhere else (?) \"encodes or reencodes\" is used.\n\"Git reencodes the content\" may be more consistent.\n\n\n[No comments on the .gitattributes]\n\n>  \n> diff --git a/convert.c b/convert.c\n> index b976eb968c..aa59ecfe49 100644\n> --- a/convert.c\n> +++ b/convert.c\n> @@ -7,6 +7,7 @@\n>  #include \"sigchain.h\"\n>  #include \"pkt-line.h\"\n>  #include \"sub-process.h\"\n> +#include \"utf8.h\"\n>  \n>  /*\n>   * convert.c - convert a file when checking it out and checking it in.\n> @@ -265,6 +266,78 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,\n>  \n>  }\n>  \n> +static const char *default_encoding = \"UTF-8\";\n> +\n> +static int encode_to_git(const char *path, const char *src, size_t src_len,\n> +\t\t\t struct strbuf *buf, const char *enc, int conv_flags)\n> +{\n> +\tchar *dst;\n> +\tint dst_len;\n> +\tint die_on_error = conv_flags & CONV_WRITE_OBJECT;\n> +\n> +\t/*\n> +\t * No encoding is specified or there is nothing to encode.\n> +\t * Tell the caller that the content was not modified.\n> +\t */\n> +\tif (!enc || (src && !src_len))\n> +\t\treturn 0;\n\n(This may have been discussed before.\n As we checked (enc != NULL) I think we can add here:)\n\tif (is_encoding_utf8(enc))\n\t\treturn 0;\n\n> +\n> +\t/*\n> +\t * Looks like we got called from \"would_convert_to_git()\".\n> +\t * This means Git wants to know if it would encode (= modify!)\n> +\t * the content. Let's answer with \"yes\", since an encoding was\n> +\t * specified.\n> +\t */\n> +\tif (!buf && !src)\n> +\t\treturn 1;\n> +\n> +\tdst = reencode_string_len(src, src_len, default_encoding, enc,\n> +\t\t\t\t  &dst_len);\n> +\tif (!dst) {\n> +\t\t/*\n> +\t\t * We could add the blob \"as-is\" to Git. However, on checkout\n> +\t\t * we would try to reencode to the original encoding. This\n> +\t\t * would fail and we would leave the user with a messed-up\n> +\t\t * working tree. Let's try to avoid this by screaming loud.\n> +\t\t */\n> +\t\tconst char* msg = _(\"failed to encode '%s' from %s to %s\");\n> +\t\tif (die_on_error)\n> +\t\t\tdie(msg, path, enc, default_encoding);\n> +\t\telse {\n> +\t\t\terror(msg, path, enc, default_encoding);\n> +\t\t\treturn 0;\n> +\t\t}\n> +\t}\n> +\n> +\tstrbuf_attach(buf, dst, dst_len, dst_len + 1);\n> +\treturn 1;\n> +}\n> +\n> +static int encode_to_worktree(const char *path, const char *src, size_t src_len,\n> +\t\t\t      struct strbuf *buf, const char *enc)\n> +{\n> +\tchar *dst;\n> +\tint dst_len;\n> +\n> +\t/*\n> +\t * No encoding is specified or there is nothing to encode.\n> +\t * Tell the caller that the content was not modified.\n> +\t */\n> +\tif (!enc || (src && !src_len))\n> +\t\treturn 0;\n\n Same as above:\n\tif (is_encoding_utf8(enc))\n\t\treturn 0;\n\n> +\n> +\tdst = reencode_string_len(src, src_len, enc, default_encoding,\n> +\t\t\t\t  &dst_len);\n> +\tif (!dst) {\n> +\t\terror(\"failed to encode '%s' from %s to %s\",\n> +\t\t\tpath, default_encoding, enc);\n> +\t\treturn 0;\n> +\t}\n> +\n> +\tstrbuf_attach(buf, dst, dst_len, dst_len + 1);\n> +\treturn 1;\n> +}\n> +\n>  static int crlf_to_git(const struct index_state *istate,\n>  \t\t       const char *path, const char *src, size_t len,\n>  \t\t       struct strbuf *buf,\n> @@ -978,6 +1051,25 @@ static int ident_to_worktree(const char *path, const char *src, size_t len,\n>  \treturn 1;\n>  }\n>  \n> +static const char *git_path_check_encoding(struct attr_check_item *check)\n> +{\n> +\tconst char *value = check->value;\n> +\n> +\tif (ATTR_UNSET(value) || !strlen(value))\n> +\t\treturn NULL;\n> +\n\n\n> +\tif (ATTR_TRUE(value) || ATTR_FALSE(value)) {\n> +\t\terror(_(\"working-tree-encoding attribute requires a value\"));\n> +\t\treturn NULL;\n> +\t}\n\nTRUE or false are values, but just wrong ones.\nIf this test is removed, the user will see \"failed to encode \"TRUE\" to \"UTF-8\",\nwhich should give enough information to fix it.\n\n> +\n> +\t/* Don't encode to the default encoding */\n> +\tif (!strcasecmp(value, default_encoding))\n> +\t\treturn NULL;\n Same as above ?:\n\tif (is_encoding_utf8(value))\n\t\treturn 0;\n\n"},{"id":"343553","messageId":"0FEBEFB2-46D6-4688-AF07-654B56FFF9D8@gmail.com","threadId":"48014","inReplyTo":"20180318072435.GA24190@tor.lan","subject":"Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute","fromName":"Lars Schneider","fromEmail":"larsxschneider@gmail.com","sentAt":"2018-04-01T13:24:54Z","receivedAt":"2018-04-01T13:25:13Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"\n> On 18 Mar 2018, at 08:24, Torsten Bögershausen <tboegi@web.de> wrote:\n> \n> Some comments inline\n> \n> On Fri, Mar 09, 2018 at 06:35:32PM +0100, lars.schneider@autodesk.com wrote:\n>> From: Lars Schneider <larsxschneider@gmail.com>\n>> \n>> Git recognizes files encoded with ASCII or one of its supersets (e.g.\n>> UTF-8 or ISO-8859-1) as text files. All other encodings are usually\n>> interpreted as binary and consequently built-in Git text processing\n>> tools (e.g. 'git diff') as well as most Git web front ends do not\n>> visualize the content.\n>> \n>> Add an attribute to tell Git what encoding the user has defined for a\n>> given file. If the content is added to the index, then Git converts the\n> \n> Minor comment:\n> \"Git converts the content\"\n> Everywhere else (?) \"encodes or reencodes\" is used.\n> \"Git reencodes the content\" may be more consistent.\n\nOK, will change.\n\n\n>> \n>> +static const char *default_encoding = \"UTF-8\";\n>> +\n>> +static int encode_to_git(const char *path, const char *src, size_t src_len,\n>> +\t\t\t struct strbuf *buf, const char *enc, int conv_flags)\n>> +{\n>> +\tchar *dst;\n>> +\tint dst_len;\n>> +\tint die_on_error = conv_flags & CONV_WRITE_OBJECT;\n>> +\n>> +\t/*\n>> +\t * No encoding is specified or there is nothing to encode.\n>> +\t * Tell the caller that the content was not modified.\n>> +\t */\n>> +\tif (!enc || (src && !src_len))\n>> +\t\treturn 0;\n> \n> (This may have been discussed before.\n> As we checked (enc != NULL) I think we can add here:)\n> \tif (is_encoding_utf8(enc))\n> \t\treturn 0;\n\nThis should be covered in git_path_check_encoding(),\nintroduced in v12:\n\n        /* Don't encode to the default encoding */\n\tif (same_encoding(value, default_encoding))\n\t\treturn NULL;\n\nIn that function the encoding of a certain file is read from\nthe .gitattributes. If the encoding matches the compile-time\ndefined default encoding (= UTF-8), then the encoding is set\nto NULL.\n\n\n>> \n>> +\n>> +static int encode_to_worktree(const char *path, const char *src, size_t src_len,\n>> +\t\t\t      struct strbuf *buf, const char *enc)\n>> +{\n>> +\tchar *dst;\n>> +\tint dst_len;\n>> +\n>> +\t/*\n>> +\t * No encoding is specified or there is nothing to encode.\n>> +\t * Tell the caller that the content was not modified.\n>> +\t */\n>> +\tif (!enc || (src && !src_len))\n>> +\t\treturn 0;\n> \n> Same as above:\n> \tif (is_encoding_utf8(enc))\n> \t\treturn 0;\n> \n>> +\n>> +\tdst = reencode_string_len(src, src_len, enc, default_encoding,\n>> +\t\t\t\t  &dst_len);\n>> +\tif (!dst) {\n>> +\t\terror(\"failed to encode '%s' from %s to %s\",\n>> +\t\t\tpath, default_encoding, enc);\n>> +\t\treturn 0;\n>> +\t}\n>> +\n>> +\tstrbuf_attach(buf, dst, dst_len, dst_len + 1);\n>> +\treturn 1;\n>> +}\n>> +\n>> static int crlf_to_git(const struct index_state *istate,\n>> \t\t       const char *path, const char *src, size_t len,\n>> \t\t       struct strbuf *buf,\n>> @@ -978,6 +1051,25 @@ static int ident_to_worktree(const char *path, const char *src, size_t len,\n>> \treturn 1;\n>> }\n>> \n>> +static const char *git_path_check_encoding(struct attr_check_item *check)\n>> +{\n>> +\tconst char *value = check->value;\n>> +\n>> +\tif (ATTR_UNSET(value) || !strlen(value))\n>> +\t\treturn NULL;\n>> +\n> \n> \n>> +\tif (ATTR_TRUE(value) || ATTR_FALSE(value)) {\n>> +\t\terror(_(\"working-tree-encoding attribute requires a value\"));\n>> +\t\treturn NULL;\n>> +\t}\n> \n> TRUE or false are values, but just wrong ones.\n> If this test is removed, the user will see \"failed to encode \"TRUE\" to \"UTF-8\",\n> which should give enough information to fix it.\n\nI see your point. However, I would like to stop the processing right\nthere for these invalid values. How about \n\n  error(_(\"true/false are no valid working-tree-encodings\"));\n\nI think that is the most straight forward/helpful error message\nfor the enduser (I consider the term \"boolean\" but dismissed it\nas potentially confusing to folks not familiar with the term).\n\nOK with you?\n\n> \n>> +\n>> +\t/* Don't encode to the default encoding */\n>> +\tif (!strcasecmp(value, default_encoding))\n>> +\t\treturn NULL;\n> Same as above ?:\n> \tif (is_encoding_utf8(value))\n> \t\treturn 0;\n\nYes, that was fixed in v12 as mentioned above :-)\n\n- Lars"},{"id":"343870","messageId":"583f9ec3-3aef-d823-9fd6-3cc126ac47f6@web.de","threadId":"48014","inReplyTo":"0FEBEFB2-46D6-4688-AF07-654B56FFF9D8@gmail.com","subject":"Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute","fromName":"Torsten Bögershausen","fromEmail":"tboegi@web.de","sentAt":"2018-04-05T16:41:55Z","receivedAt":"2018-04-05T16:42:22Z","isPatch":true,"sender":{"key":"tboegi@web.de","avatar":"https://avatars.githubusercontent.com/u/7138363?v=4"},"body":"On 01.04.18 15:24, Lars Schneider wrote:\n>> TRUE or false are values, but just wrong ones.\n>> If this test is removed, the user will see \"failed to encode \"TRUE\" to \"UTF-8\",\n>> which should give enough information to fix it.\n> \n> I see your point. However, I would like to stop the processing right\n> there for these invalid values. How about \n> \n>   error(_(\"true/false are no valid working-tree-encodings\"));\n> \n> I think that is the most straight forward/helpful error message\n> for the enduser (I consider the term \"boolean\" but dismissed it\n> as potentially confusing to folks not familiar with the term).\n> \n> OK with you?\n\nYes.\n\nAnother thing that came up recently, independent of your series:\n\nWhat should happen if a user specifies \"UTF-8\" and the file\nhas an UTF-8 encoded BOM ?\nI ask because I stumbled over such a file coming from a Windows\nwhich the java compiler under Linux didn't accept.\n\nAnd because some tools love to put an UTF-8 encoded BOM\ninto text files.\n\nThe clearest thing would be to extend the BOM check in 5/9\nto cover UTF-32, UTF-16 and UTF-8.\n\nAre there any plans to do so?\n\nAnd thanks for the work.\n"},{"id":"344733","messageId":"8EBD3571-D5FA-471B-BD5F-D8401043D503@gmail.com","threadId":"48014","inReplyTo":"583f9ec3-3aef-d823-9fd6-3cc126ac47f6@web.de","subject":"Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute","fromName":"Lars Schneider","fromEmail":"larsxschneider@gmail.com","sentAt":"2018-04-15T16:54:02Z","receivedAt":"2018-04-15T16:54:19Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"\n> On 05 Apr 2018, at 18:41, Torsten Bögershausen <tboegi@web.de> wrote:\n> \n> On 01.04.18 15:24, Lars Schneider wrote:\n>>> TRUE or false are values, but just wrong ones.\n>>> If this test is removed, the user will see \"failed to encode \"TRUE\" to \"UTF-8\",\n>>> which should give enough information to fix it.\n>> \n>> I see your point. However, I would like to stop the processing right\n>> there for these invalid values. How about \n>> \n>>  error(_(\"true/false are no valid working-tree-encodings\"));\n>> \n>> I think that is the most straight forward/helpful error message\n>> for the enduser (I consider the term \"boolean\" but dismissed it\n>> as potentially confusing to folks not familiar with the term).\n>> \n>> OK with you?\n> \n> Yes.\n\nGreat!\n\n\n> Another thing that came up recently, independent of your series:\n> \n> What should happen if a user specifies \"UTF-8\" and the file\n> has an UTF-8 encoded BOM ?\n> I ask because I stumbled over such a file coming from a Windows\n> which the java compiler under Linux didn't accept.\n> \n> And because some tools love to put an UTF-8 encoded BOM\n> into text files.\n> \n> The clearest thing would be to extend the BOM check in 5/9\n> to cover UTF-32, UTF-16 and UTF-8.\n> \n> Are there any plans to do so?\n\nIf `working-tree-encoding` is not defined or defined as UTF-8,\nthen we would return from encode_to_git() early. That means we\nwould never run validate_encoding() which would check the BOM.\n\nHowever, adding the UTF-8 BOM would still make sense. This way\nGit could scream if a user set `working-tree-encoding` to UTF-16\nbut the file is really UTF-8 encoded.\n\n\n> And thanks for the work.\n\nThanks :-)\n\n\n- Lars"}]}