convert: add round trip check based on 'core.checkRoundtripEncoding'

UTF supports lossless conversion round tripping and conversions between UTF and other encodings are mostly round trip safe as Unicode aims to be a superset of all other character encodings. However, certain encodings (e.g. SHIFT-JIS) are known to have round trip issues [1]. Add 'core.checkRoundtripEncoding', which contains a comma separated list of encodings, to define for what encodings Git should check the conversion round trip if they are used in the 'working-tree-encoding' attribute. Set SHIFT-JIS as default value for 'core.checkRoundtripEncoding'. [1] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode Signed-off-by: Lars Schneider <larsxschneider@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lars Schneider committed Apr 15, 2018 at 20:16 UTC e92d6225361eba5ff34696122d1491dc7ace2a5a
7 files changed +137
Documentation/config.txt
+6
@@ -528,6 +528,12 @@ core.autocrlf::
528 This variable can be set to 'input',
529 in which case no output conversion is performed.
530
531 +core.checkRoundtripEncoding::
532 + A comma and/or whitespace separated list of encodings that Git
533 + performs UTF-8 round trip checks on if they are used in an
534 + `working-tree-encoding` attribute (see linkgit:gitattributes[5]).
535 + The default value is `SHIFT-JIS`.
536 +
537 core.symlinks::
538 If false, symbolic links are checked out as small plain files that
539 contain the link text. linkgit:git-update-index[1] and
Documentation/gitattributes.txt
+8
@@ -312,6 +312,14 @@ number of pitfalls:
312 internal contents as UTF-8 and try to convert it to UTF-16 on checkout.
313 That operation will fail and cause an error.
314
315 +- Reencoding content to non-UTF encodings can cause errors as the
316 + conversion might not be UTF-8 round trip safe. If you suspect your
317 + encoding to not be round trip safe, then add it to
318 + `core.checkRoundtripEncoding` to make Git check the round trip
319 + encoding (see linkgit:git-config[1]). SHIFT-JIS (Japanese character
320 + set) is known to have round trip issues with UTF-8 and is checked by
321 + default.
322 +
323 - Reencoding content requires resources that might slow down certain
324 Git operations (e.g 'git checkout' or 'git add').
325
config.c
+5
@@ -1172,6 +1172,11 @@ static int git_default_core_config(const char *var, const char *value)
1172 return 0;
1173 }
1174
1175 + if (!strcmp(var, "core.checkroundtripencoding")) {
1176 + check_roundtrip_encoding = xstrdup(value);
1177 + return 0;
1178 + }
1179 +
1180 if (!strcmp(var, "core.notesref")) {
1181 notes_ref_name = xstrdup(value);
1182 return 0;
convert.c
+77
@@ -347,6 +347,42 @@ static void trace_encoding(const char *context, const char *path,
347 strbuf_release(&trace);
348 }
349
350 +static int check_roundtrip(const char *enc_name)
351 +{
352 + /*
353 + * check_roundtrip_encoding contains a string of comma and/or
354 + * space separated encodings (eg. "UTF-16, ASCII, CP1125").
355 + * Search for the given encoding in that string.
356 + */
357 + const char *found = strcasestr(check_roundtrip_encoding, enc_name);
358 + const char *next;
359 + int len;
360 + if (!found)
361 + return 0;
362 + next = found + strlen(enc_name);
363 + len = strlen(check_roundtrip_encoding);
364 + return (found && (
365 + /*
366 + * check that the found encoding is at the
367 + * beginning of check_roundtrip_encoding or
368 + * that it is prefixed with a space or comma
369 + */
370 + found == check_roundtrip_encoding || (
371 + (isspace(found[-1]) || found[-1] == ',')
372 + )
373 + ) && (
374 + /*
375 + * check that the found encoding is at the
376 + * end of check_roundtrip_encoding or
377 + * that it is suffixed with a space or comma
378 + */
379 + next == check_roundtrip_encoding + len || (
380 + next < check_roundtrip_encoding + len &&
381 + (isspace(next[0]) || next[0] == ',')
382 + )
383 + ));
384 +}
385 +
386 static const char *default_encoding = "UTF-8";
387
388 static int encode_to_git(const char *path, const char *src, size_t src_len,
@@ -395,6 +431,47 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,
431 }
432 trace_encoding("destination", path, default_encoding, dst, dst_len);
433
434 + /*
435 + * UTF supports lossless conversion round tripping [1] and conversions
436 + * between UTF and other encodings are mostly round trip safe as
437 + * Unicode aims to be a superset of all other character encodings.
438 + * However, certain encodings (e.g. SHIFT-JIS) are known to have round
439 + * trip issues [2]. Check the round trip conversion for all encodings
440 + * listed in core.checkRoundtripEncoding.
441 + *
442 + * The round trip check is only performed if content is written to Git.
443 + * This ensures that no information is lost during conversion to/from
444 + * the internal UTF-8 representation.
445 + *
446 + * Please note, the code below is not tested because I was not able to
447 + * generate a faulty round trip without an iconv error. Iconv errors
448 + * are already caught above.
449 + *
450 + * [1] http://unicode.org/faq/utf_bom.html#gen2
451 + * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
452 + */
453 + if (die_on_error && check_roundtrip(enc)) {
454 + char *re_src;
455 + int re_src_len;
456 +
457 + re_src = reencode_string_len(dst, dst_len,
458 + enc, default_encoding,
459 + &re_src_len);
460 +
461 + trace_printf("Checking roundtrip encoding for %s...\n", enc);
462 + trace_encoding("reencoded source", path, enc,
463 + re_src, re_src_len);
464 +
465 + if (!re_src || src_len != re_src_len ||
466 + memcmp(src, re_src, src_len)) {
467 + const char* msg = _("encoding '%s' from %s to %s and "
468 + "back is not the same");
469 + die(msg, path, enc, default_encoding);
470 + }
471 +
472 + free(re_src);
473 + }
474 +
475 strbuf_attach(buf, dst, dst_len, dst_len + 1);
476 return 1;
477 }
convert.h
+1
@@ -56,6 +56,7 @@ struct delayed_checkout {
56 };
57
58 extern enum eol core_eol;
59 +extern char *check_roundtrip_encoding;
60 extern const char *get_cached_convert_stats_ascii(const struct index_state *istate,
61 const char *path);
62 extern const char *get_wt_convert_stats_ascii(const char *path);
environment.c
+1
@@ -50,6 +50,7 @@ int check_replace_refs = 1;
50 char *git_replace_ref_base;
51 enum eol core_eol = EOL_UNSET;
52 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
53 +char *check_roundtrip_encoding = "SHIFT-JIS";
54 unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
55 enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
56 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
t/t0028-working-tree-encoding.sh
+39
@@ -203,4 +203,43 @@ test_expect_success 'error if encoding garbage is already in Git' '
203 test_i18ngrep "error: BOM is required" err.out
204 '
205
206 +test_expect_success 'check roundtrip encoding' '
207 + test_when_finished "rm -f roundtrip.shift roundtrip.utf16" &&
208 + test_when_finished "git reset --hard HEAD" &&
209 +
210 + text="hallo there!\nroundtrip test here!" &&
211 + printf "$text" | iconv -f UTF-8 -t SHIFT-JIS >roundtrip.shift &&
212 + printf "$text" | iconv -f UTF-8 -t UTF-16 >roundtrip.utf16 &&
213 + echo "*.shift text working-tree-encoding=SHIFT-JIS" >>.gitattributes &&
214 +
215 + # SHIFT-JIS encoded files are round-trip checked by default...
216 + GIT_TRACE=1 git add .gitattributes roundtrip.shift 2>&1 |
217 + grep "Checking roundtrip encoding for SHIFT-JIS" &&
218 + git reset &&
219 +
220 + # ... unless we overwrite the Git config!
221 + ! GIT_TRACE=1 git -c core.checkRoundtripEncoding=garbage \
222 + add .gitattributes roundtrip.shift 2>&1 |
223 + grep "Checking roundtrip encoding for SHIFT-JIS" &&
224 + git reset &&
225 +
226 + # UTF-16 encoded files should not be round-trip checked by default...
227 + ! GIT_TRACE=1 git add roundtrip.utf16 2>&1 |
228 + grep "Checking roundtrip encoding for UTF-16" &&
229 + git reset &&
230 +
231 + # ... unless we tell Git to check it!
232 + GIT_TRACE=1 git -c core.checkRoundtripEncoding="UTF-16, UTF-32" \
233 + add roundtrip.utf16 2>&1 |
234 + grep "Checking roundtrip encoding for utf-16" &&
235 + git reset &&
236 +
237 + # ... unless we tell Git to check it!
238 + # (here we also check that the casing of the encoding is irrelevant)
239 + GIT_TRACE=1 git -c core.checkRoundtripEncoding="UTF-32, utf-16" \
240 + add roundtrip.utf16 2>&1 |
241 + grep "Checking roundtrip encoding for utf-16" &&
242 + git reset
243 +'
244 +
245 test_done