Skip to content

Commit e92d622

Browse files
larsxschneidergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 541d059 commit e92d622

File tree

7 files changed

+137
-0
lines changed

7 files changed

+137
-0
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,12 @@ core.autocrlf::
528528
This variable can be set to 'input',
529529
in which case no output conversion is performed.
530530

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+
531537
core.symlinks::
532538
If false, symbolic links are checked out as small plain files that
533539
contain the link text. linkgit:git-update-index[1] and

Documentation/gitattributes.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ number of pitfalls:
312312
internal contents as UTF-8 and try to convert it to UTF-16 on checkout.
313313
That operation will fail and cause an error.
314314

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+
315323
- Reencoding content requires resources that might slow down certain
316324
Git operations (e.g 'git checkout' or 'git add').
317325

config.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,11 @@ static int git_default_core_config(const char *var, const char *value)
11721172
return 0;
11731173
}
11741174

1175+
if (!strcmp(var, "core.checkroundtripencoding")) {
1176+
check_roundtrip_encoding = xstrdup(value);
1177+
return 0;
1178+
}
1179+
11751180
if (!strcmp(var, "core.notesref")) {
11761181
notes_ref_name = xstrdup(value);
11771182
return 0;

convert.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,42 @@ static void trace_encoding(const char *context, const char *path,
347347
strbuf_release(&trace);
348348
}
349349

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+
350386
static const char *default_encoding = "UTF-8";
351387

352388
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,
395431
}
396432
trace_encoding("destination", path, default_encoding, dst, dst_len);
397433

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+
398475
strbuf_attach(buf, dst, dst_len, dst_len + 1);
399476
return 1;
400477
}

convert.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct delayed_checkout {
5656
};
5757

5858
extern enum eol core_eol;
59+
extern char *check_roundtrip_encoding;
5960
extern const char *get_cached_convert_stats_ascii(const struct index_state *istate,
6061
const char *path);
6162
extern const char *get_wt_convert_stats_ascii(const char *path);

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ int check_replace_refs = 1;
5050
char *git_replace_ref_base;
5151
enum eol core_eol = EOL_UNSET;
5252
int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
53+
char *check_roundtrip_encoding = "SHIFT-JIS";
5354
unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
5455
enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
5556
enum rebase_setup_type autorebase = AUTOREBASE_NEVER;

t/t0028-working-tree-encoding.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,43 @@ test_expect_success 'error if encoding garbage is already in Git' '
203203
test_i18ngrep "error: BOM is required" err.out
204204
'
205205

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+
206245
test_done

0 commit comments

Comments
 (0)