Skip to content

Commit 2b94bec

Browse files
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>
1 parent 3b77c2b commit 2b94bec

File tree

7 files changed

+136
-0
lines changed

7 files changed

+136
-0
lines changed

Documentation/config.txt

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

533+
core.checkRoundtripEncoding::
534+
A comma separated list of encodings that Git performs UTF-8 round
535+
trip checks on if they are used in an `working-tree-encoding`
536+
attribute (see linkgit:gitattributes[5]). The default value is
537+
`SHIFT-JIS`.
538+
533539
core.symlinks::
534540
If false, symbolic links are checked out as small plain files that
535541
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
@@ -298,6 +298,14 @@ number of pitfalls:
298298
in particular the case for older Git versions and alternative Git
299299
implementations such as JGit or libgit2 (as of February 2018).
300300

301+
- Reencoding content to non-UTF encodings can cause errors as the
302+
conversion might not be UTF-8 round trip safe. If you suspect your
303+
encoding to not be round trip safe, then add it to
304+
`core.checkRoundtripEncoding` to make Git check the round trip
305+
encoding (see linkgit:git-config[1]). SHIFT-JIS (Japanese character
306+
set) is known to have round trip issues with UTF-8 and is checked by
307+
default.
308+
301309
- Reencoding content requires resources that might slow down certain
302310
Git operations (e.g 'git checkout' or 'git add').
303311

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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,39 @@ static void trace_encoding(const char *context, const char *path,
289289
strbuf_release(&trace);
290290
}
291291

292+
static int check_roundtrip(const char* enc_name)
293+
{
294+
/*
295+
* check_roundtrip_encoding contains a string of space and/or
296+
* comma separated encodings (eg. "UTF-16, ASCII, CP1125").
297+
* Search for the given encoding in that string.
298+
*/
299+
const char *found = strcasestr(check_roundtrip_encoding, enc_name);
300+
const char *next = found + strlen(enc_name);
301+
int len = strlen(check_roundtrip_encoding);
302+
return (found && (
303+
/*
304+
* check that the found encoding is at the
305+
* beginning of check_roundtrip_encoding or
306+
* that it is prefixed with a space or comma
307+
*/
308+
found == check_roundtrip_encoding || (
309+
found > check_roundtrip_encoding &&
310+
(*(found-1) == ' ' || *(found-1) == ',')
311+
)
312+
) && (
313+
/*
314+
* check that the found encoding is at the
315+
* end of check_roundtrip_encoding or
316+
* that it is suffixed with a space or comma
317+
*/
318+
next == check_roundtrip_encoding + len || (
319+
next < check_roundtrip_encoding + len &&
320+
(*next == ' ' || *next == ',')
321+
)
322+
));
323+
}
324+
292325
static struct encoding {
293326
const char *name;
294327
struct encoding *next;
@@ -366,6 +399,47 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,
366399
}
367400
trace_encoding("destination", path, default_encoding, dst, dst_len);
368401

402+
/*
403+
* UTF supports lossless conversion round tripping [1] and conversions
404+
* between UTF and other encodings are mostly round trip safe as
405+
* Unicode aims to be a superset of all other character encodings.
406+
* However, certain encodings (e.g. SHIFT-JIS) are known to have round
407+
* trip issues [2]. Check the round trip conversion for all encodings
408+
* listed in core.checkRoundtripEncoding.
409+
*
410+
* The round trip check is only performed if content is written to Git.
411+
* This ensures that no information is lost during conversion to/from
412+
* the internal UTF-8 representation.
413+
*
414+
* Please note, the code below is not tested because I was not able to
415+
* generate a faulty round trip without an iconv error. Iconv errors
416+
* are already caught above.
417+
*
418+
* [1] http://unicode.org/faq/utf_bom.html#gen2
419+
* [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
420+
*/
421+
if ((conv_flags & CONV_WRITE_OBJECT) && check_roundtrip(enc->name)) {
422+
char *re_src;
423+
int re_src_len;
424+
425+
re_src = reencode_string_len(dst, dst_len,
426+
enc->name, default_encoding,
427+
&re_src_len);
428+
429+
trace_printf("Checking roundtrip encoding for %s...\n", enc->name);
430+
trace_encoding("reencoded source", path, enc->name,
431+
re_src, re_src_len);
432+
433+
if (!re_src || src_len != re_src_len ||
434+
memcmp(src, re_src, src_len)) {
435+
const char* msg = _("encoding '%s' from %s to %s and "
436+
"back is not the same");
437+
die(msg, path, enc->name, default_encoding);
438+
}
439+
440+
free(re_src);
441+
}
442+
369443
strbuf_attach(buf, dst, dst_len, dst_len + 1);
370444
return 1;
371445
}

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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,45 @@ test_expect_success 'error if encoding garbage is already in Git' '
209209
git reset --hard $BEFORE_STATE
210210
'
211211

212+
test_expect_success 'check roundtrip encoding' '
213+
text="hallo there!\nroundtrip test here!" &&
214+
printf "$text" | iconv -f UTF-8 -t SHIFT-JIS >roundtrip.shift &&
215+
printf "$text" | iconv -f UTF-8 -t UTF-16 >roundtrip.utf16 &&
216+
echo "*.shift text working-tree-encoding=SHIFT-JIS" >>.gitattributes &&
217+
218+
# SHIFT-JIS encoded files are round-trip checked by default...
219+
GIT_TRACE=1 git add .gitattributes roundtrip.shift 2>&1 >/dev/null |
220+
grep "Checking roundtrip encoding for SHIFT-JIS" &&
221+
git reset &&
222+
223+
# ... unless we overwrite the Git config!
224+
test_config core.checkRoundtripEncoding "garbage" &&
225+
! GIT_TRACE=1 git add .gitattributes roundtrip.shift 2>&1 >/dev/null |
226+
grep "Checking roundtrip encoding for SHIFT-JIS" &&
227+
test_unconfig core.checkRoundtripEncoding &&
228+
git reset &&
229+
230+
# UTF-16 encoded files should not be round-trip checked by default...
231+
! GIT_TRACE=1 git add roundtrip.utf16 2>&1 >/dev/null |
232+
grep "Checking roundtrip encoding for UTF-16" &&
233+
git reset &&
234+
235+
# ... unless we tell Git to check it!
236+
test_config_global core.checkRoundtripEncoding "UTF-16, UTF-32" &&
237+
GIT_TRACE=1 git add roundtrip.utf16 2>&1 >/dev/null |
238+
grep "Checking roundtrip encoding for UTF-16" &&
239+
git reset &&
240+
241+
# ... unless we tell Git to check it!
242+
# (here we also check that the casing of the encoding is irrelevant)
243+
test_config_global core.checkRoundtripEncoding "UTF-32, utf-16" &&
244+
GIT_TRACE=1 git add roundtrip.utf16 2>&1 >/dev/null |
245+
grep "Checking roundtrip encoding for UTF-16" &&
246+
git reset &&
247+
248+
# cleanup
249+
rm roundtrip.shift roundtrip.utf16 &&
250+
git reset --hard HEAD
251+
'
252+
212253
test_done

0 commit comments

Comments
 (0)