convert: add support for different encodings
v8 cover convert: add support for different encodings
From: Lars Schneider <[email protected]>
Hi,
Patches 1-4, 6 are preparation and helper functions. Patch 5,7 are the actual change.
This series depends on Torsten's 8462ff43e4 (convert_to_git():
safe_crlf/checksafe becomes int conv_flags, 2018-01-13) which is already in master.
Changes since v7:
* make it clearer in the documentation that Git stores content "as-is" by default. Content is only stored in UTF-8 if w-t-e is used (Junio)
* add test case for $GIT_DIR/info/attributes support (Junio)
Thanks,
Lars
RFC: https://public-inbox.org/git/[email protected]/
v1: https://public-inbox.org/git/[email protected]/
v2: https://public-inbox.org/git/[email protected]/
v3: https://public-inbox.org/git/[email protected]/
v4: https://public-inbox.org/git/[email protected]/
v5: https://public-inbox.org/git/[email protected]/
v6: https://public-inbox.org/git/[email protected]/
v7: https://public-inbox.org/git/[email protected]/
Base Ref:
Web-Diff: https://github.com/larsxschneider/git/commit/2758a2da29
Checkout: git fetch https://github.com/larsxschneider/git encoding-v8 && git checkout 2758a2da29
### Interdiff (v7..v8):
Documentation/gitattributes.txt +23 -11
@@ -275,11 +275,11 @@ few exceptions. Even though... `working-tree-encoding` ^^^^^^^^^^^^^^^^^^^^^^^ -Git recognizes files encoded with ASCII or one of its supersets (e.g. -UTF-8 or ISO-8859-1) as text files. All other encodings are usually -interpreted as binary and consequently built-in Git text processing -tools (e.g. 'git diff') as well as most Git web front ends do not -visualize the content. +Git recognizes files encoded in ASCII or one of its supersets (e.g. +UTF-8, ISO-8859-1, ...) as text files. Files encoded in certain other +encodings (e.g. UTF-16) are interpreted as binary and consequently +built-in Git text processing tools (e.g. 'git diff') as well as most Git +web front ends do not visualize the contents of these files by default. In these cases you can tell Git the encoding of a file in the working directory with the `working-tree-encoding` attribute. If a file with this
@@ -291,12 +291,24 @@ the content is reencoded back to the specified encoding. Please note that using the `working-tree-encoding` attribute may have a number of pitfalls: -- Third party Git implementations that do not support the - `working-tree-encoding` attribute will checkout the respective files - UTF-8 encoded and not in the expected encoding. Consequently, these - files will appear different which typically causes trouble. This is - in particular the case for older Git versions and alternative Git - implementations such as JGit or libgit2 (as of February 2018). +- Alternative Git implementations (e.g. JGit or libgit2) and older Git + versions (as of March 2018) do not support the `working-tree-encoding` + attribute. If you decide to use the `working-tree-encoding` attribute + in your repository, then it is strongly recommended to ensure that all + clients working with the repository support it. + + If you declare `*.proj` files as UTF-16 and you add `foo.proj` with an + `working-tree-encoding` enabled Git client, then `foo.proj` will be + stored as UTF-8 internally. A client without `working-tree-encoding` + support will checkout `foo.proj` as UTF-8 encoded file. This will + typically cause trouble for the users of this file. + + If a Git client, that does not support the `working-tree-encoding` + attribute, adds a new file `bar.proj`, then `bar.proj` will be + stored "as-is" internally (in this example probably as UTF-16). + A client with `working-tree-encoding` support will interpret the + internal contents as UTF-8 and try to convert it to UTF-16 on checkout. + That operation will fail and cause an error. - Reencoding content to non-UTF encodings can cause errors as the conversion might not be UTF-8 round trip safe. If you suspect your
t/t0028-working-tree-encoding.sh +17 -1
@@ -13,8 +13,11 @@ test_expect_success 'setup test repo' ' echo "*.utf16 text working-tree-encoding=utf-16" >.gitattributes && printf "$text" >test.utf8.raw && printf "$text" | iconv -f UTF-8 -t UTF-16 >test.utf16.raw && + printf "$text" | iconv -f UTF-8 -t UTF-32 >test.utf32.raw && cp test.utf16.raw test.utf16 && + cp test.utf32.raw test.utf32 && + # Add only UTF-16 file, we will add the UTF-32 file later git add .gitattributes test.utf16 && git commit -m initial '
@@ -24,7 +27,7 @@ test_expect_success 'ensure UTF-8 is stored in Git' ' test_cmp_bin test.utf8.raw test.utf16.git && # cleanup - rm test.utf8.raw test.utf16.git + rm test.utf16.git ' test_expect_success 're-encode to UTF-16 on checkout' '
@@ -36,6 +39,19 @@ test_expect_success 're-encode to UTF-16 on checkout' ' rm test.utf16.raw ' +test_expect_success 'check $GIT_DIR/info/attributes support' ' + echo "*.utf32 text working-tree-encoding=utf-32" >.git/info/attributes && + + git add test.utf32 && + + git cat-file -p :test.utf32 >test.utf32.git && + test_cmp_bin test.utf8.raw test.utf32.git && + + # cleanup + git reset --hard HEAD && + rm test.utf8.raw test.utf32.raw test.utf32.git +' + test_expect_success 'check prohibited UTF BOM' ' printf "\0a\0b\0c" >nobom.utf16be.raw && printf "a\0b\0c\0" >nobom.utf16le.raw &&
### Patches
Lars Schneider (7):
strbuf: remove unnecessary NUL assignment in xstrdup_tolower()
strbuf: add xstrdup_toupper()
utf8: add function to detect prohibited UTF-16/32 BOM
utf8: add function to detect a missing UTF-16/32 BOM
convert: add 'working-tree-encoding' attribute
convert: add tracing for 'working-tree-encoding' attribute
convert: add round trip check based on 'core.checkRoundtripEncoding'
Documentation/config.txt | 6 + Documentation/gitattributes.txt | 86 +++++++++++++ config.c | 5 + convert.c | 256 ++++++++++++++++++++++++++++++++++++- convert.h | 2 + environment.c | 1 + sha1_file.c | 2 +- strbuf.c | 13 +- strbuf.h | 1 + t/t0028-working-tree-encoding.sh | 269 +++++++++++++++++++++++++++++++++++++++ utf8.c | 37 ++++++ utf8.h | 25 ++++ 12 files changed, 700 insertions(+), 3 deletions(-) create mode 100755 t/t0028-working-tree-encoding.sh
base-commit: 8a2f0888555ce46ac87452b194dec5cb66fb1417 --
2.16.1
PATCH v8 1/7 strbuf: remove unnecessary NUL assignment in xstrdup_tolower()
From: Lars Schneider <[email protected]>
Since 3733e69464 (use xmallocz to avoid size arithmetic, 2016-02-22) we allocate the buffer for the lower case string with xmallocz(). This already ensures a NUL at the end of the allocated buffer.
Remove the unnecessary assignment.
--- strbuf.c | 1 - 1 file changed, 1 deletion(-)
strbuf.c +0 -1
@@ -781,7 +781,6 @@ char *xstrdup_tolower(const char *string) result =xmallocz (len);for (i =0 ; i < len; i++) result[i] =tolower (string[i]); - result[i] ='\0' ;return result; }
Signature · 1 line
2.16.1
PATCH v8 2/7 strbuf: add xstrdup_toupper()
In reply to [email protected]From: Lars Schneider <[email protected]>
Create a copy of an existing string and make all characters upper case. Similar xstrdup_tolower().
This function is used in a subsequent commit.
--- strbuf.c | 12 ++++++++++++ strbuf.h | 1 + 2 files changed, 13 insertions(+)
strbuf.c +12 -0
@@ -784,6 +784,18 @@ char *xstrdup_tolower(const char *string)return result; } +char *xstrdup_toupper (const char *string) +{ +char *result; +size_t len, i; + + len =strlen (string); + result =xmallocz (len); +for (i =0 ; i < len; i++) + result[i] =toupper (string[i]); +return result; +} +char *xstrvfmt (const char *fmt,va_list ap) {struct strbuf buf =STRBUF_INIT ;
strbuf.h +1 -0
@@ -607,6 +607,7 @@ __attribute__((format (printf,2,3)))extern int fprintf_ln (FILE *fp,const char *fmt, ...);char *xstrdup_tolower (const char *); +char *xstrdup_toupper (const char *);/** * Create a newly allocated string using printf format. You can do this easily
Signature · 1 line
2.16.1
PATCH v8 3/7 utf8: add function to detect prohibited UTF-16/32 BOM
In reply to [email protected]From: Lars Schneider <[email protected]>
Whenever a data stream is declared to be UTF-16BE, UTF-16LE, UTF-32BE or UTF-32LE a BOM must not be used [1]. The function returns true if this is the case.
This function is used in a subsequent commit.
[1] http://unicode.org/faq/utf_bom.html#bom10
--- utf8.c | 24 ++++++++++++++++++++++++ utf8.h | 9 +++++++++ 2 files changed, 33 insertions(+)
utf8.c +24 -0
@@ -538,6 +538,30 @@ char *reencode_string_len(const char *in, int insz, }#endif +static int has_bom_prefix (const char *data,size_t len, +const char *bom,size_t bom_len) +{ +return (len >= bom_len) && !memcmp (data, bom, bom_len); +} + +static const char utf16_be_bom[] = {0xFE ,0xFF }; +static const char utf16_le_bom[] = {0xFF ,0xFE }; +static const char utf32_be_bom[] = {0x00 ,0x00 ,0xFE ,0xFF }; +static const char utf32_le_bom[] = {0xFF ,0xFE ,0x00 ,0x00 }; + +int has_prohibited_utf_bom (const char *enc,const char *data,size_t len) +{ +return ( + (!strcmp (enc,"UTF-16BE" ) || !strcmp (enc,"UTF-16LE" )) && + (has_bom_prefix (data, len, utf16_be_bom,sizeof (utf16_be_bom)) || +has_bom_prefix (data, len, utf16_le_bom,sizeof (utf16_le_bom))) + ) || ( + (!strcmp (enc,"UTF-32BE" ) || !strcmp (enc,"UTF-32LE" )) && + (has_bom_prefix (data, len, utf32_be_bom,sizeof (utf32_be_bom)) || +has_bom_prefix (data, len, utf32_le_bom,sizeof (utf32_le_bom))) + ); +} +/* * Returns first character length in bytes for multi-byte `text` according to * `encoding`.
utf8.h +9 -0
@@ -70,4 +70,13 @@ typedef enum {void strbuf_utf8_align (struct strbuf *buf,align_type position,unsigned int width,const char *s); +/* +* Whenever a data stream is declared to be UTF-16BE, UTF-16LE, UTF-32BE +* or UTF-32LE a BOM must not be used [1]. The function returns true if +* this is the case. +* +* [1] http://unicode.org/faq/utf_bom.html#bom10 +*/ +int has_prohibited_utf_bom (const char *enc,const char *data,size_t len); +#endif
Signature · 1 line
2.16.1
Re: [PATCH v8 3/7] utf8: add function to detect prohibited UTF-16/32 BOM
Quoted 12 lines · On Sat, Feb 24, 2018 at 11:27 AM, <[email protected]> wrote:
Whenever a data stream is declared to be UTF-16BE, UTF-16LE, UTF-32BE or UTF-32LE a BOM must not be used [1]. The function returns true if this is the case.
[1] http://unicode.org/faq/utf_bom.html#bom10
Signed-off-by Lars Schneider <[email protected]>---
utf8.c +2
-0@@ -538,6 +538,30 @@ char *reencode_string_len(const char *in, int insz, +int has_prohibited_utf_bom (const char *enc,const char *data,size_t len) +{
utf8.c char *reencode_string_len(const char *in, int insz, · line 549
Quoted 10 lines
utf8.c +10
-0+return ( + (!strcmp (enc,"UTF-16BE" ) || !strcmp (enc,"UTF-16LE" )) && + (has_bom_prefix (data, len, utf16_be_bom,sizeof (utf16_be_bom)) || +has_bom_prefix (data, len, utf16_le_bom,sizeof (utf16_le_bom))) + ) || ( + (!strcmp (enc,"UTF-32BE" ) || !strcmp (enc,"UTF-32LE" )) && + (has_bom_prefix (data, len, utf32_be_bom,sizeof (utf32_be_bom)) || +has_bom_prefix (data, len, utf32_le_bom,sizeof (utf32_le_bom))) + ); +}
Is this interpretation correct? When I read [1], I interpret it as saying that no BOM _of any sort_ should be present when the encoding is declared as one of UTF-16BE, UTF-16LE, UTF-32BE, or UTF-32LE. This code, on the other hand, only checks for BOMs corresponding to the declared size (16 or 32 bits).
I suppose the intention of [1] is to detect a mismatch between the declared encoding and how the stream is actually encoded. The check implemented here will fail to detect a mismatch between, say, declared encoding UTF-16BE and actual encoding UTF-32BE.
Re: [PATCH v8 3/7] utf8: add function to detect prohibited UTF-16/32 BOM
Quoted 22 lines · Eric Sunshine
Quoted 18 lines · On Sat, Feb 24, 2018 at 11:27 AM, <[email protected]> wrote:
Whenever a data stream is declared to be UTF-16BE, UTF-16LE, UTF-32BE or UTF-32LE a BOM must not be used [1]. The function returns true if this is the case.
[1] http://unicode.org/faq/utf_bom.html#bom10
Signed-off-by Lars Schneider <[email protected]>---
utf8.c +8
-0@@ -538,6 +538,30 @@ char *reencode_string_len(const char *in, int insz, +int has_prohibited_utf_bom (const char *enc,const char *data,size_t len) +{ +return ( + (!strcmp (enc,"UTF-16BE" ) || !strcmp (enc,"UTF-16LE" )) && + (has_bom_prefix (data, len, utf16_be_bom,sizeof (utf16_be_bom)) || +has_bom_prefix (data, len, utf16_le_bom,sizeof (utf16_le_bom))) + ) || ( + (!strcmp(enc, "UTF-32BE") || !strcmp(enc, "UTF-32LE")) &&
Quoted 8 lines
utf8.c char *reencode_string_len(const char *in, int insz, · line 563
Quoted 4 lines
utf8.c +4
-0+ (has_bom_prefix (data, len, utf32_be_bom,sizeof (utf32_be_bom)) || +has_bom_prefix (data, len, utf32_le_bom,sizeof (utf32_le_bom))) + ); +}Is this interpretation correct? When I read [1], I interpret it as saying that no BOM
_ofanysort_should be present when the encoding is declared as one of UTF-16BE, UTF-16LE, UTF-32BE, or UTF-32LE.
Correct!
Quoted 3 lines
This
code, on the other hand, only checks for BOMs corresponding to the declared size (16 or 32 bits).
Hmm. Interesting thought. You are saying that my code won't complain if a document declared as UTF-16LE has a UTF32-LE BOM, correct? I would say this is correct behavior in context of this function. This function assumes that the document is proper UTF-16/UTF-16BE/UTF-16LE but it is wrongly declared with respect to its BOM in the .gitattributes. Would this comment make it more clear to you?
/*
* If a data stream is declared as UTF-16BE or UTF-16LE, then a UTF-16
* BOM must not be used [1]. The same applies for the UTF-32 equivalents.
* The function returns true if this rule is violated.
*
* [1] http://unicode.org/faq/utf_bom.html#bom10
*/
I think what you are referring to is a different class of error and would therefore warrant its own checker function. Would you agree?
Quoted 4 lines
I suppose the intention of [1] is to detect a mismatch between the declared encoding and how the stream is actually encoded. The check implemented here will fail to detect a mismatch between, say, declared encoding UTF-16BE and actual encoding UTF-32BE.
As stated above the intention is to detect wrong BOMs! I think we cannot detect the "declared as UTF-16BE but actually UTF-32BE" error.
Consider this:
printf "test" | iconv -f UTF-8 -t UTF-32BE | iconv -f UTF-16BE -t UTF-8 | od -c
0000000 \0 t \0 e \0 s \0 t
0000010
In the first step we "encode" the string to UTF-32BE and then we "decode" it as UTF-16BE. The result is valid although not correct. Does this make sense?
Thanks,
Lars
Re: [PATCH v8 3/7] utf8: add function to detect prohibited UTF-16/32 BOM
Quoted 5 lines · Lars Schneider
Quoted 4 lines · Eric Sunshine
Is this interpretation correct? When I read [1], I interpret it as saying that no BOM
_ofanysort_should be present when the encoding is declared as one of UTF-16BE, UTF-16LE, UTF-32BE, or UTF-32LE.
Quoted 7 lines
Correct!
Quoted 2 lines
This code, on the other hand, only checks for BOMs corresponding to the declared size (16 or 32 bits).
Hmm. Interesting thought. You are saying that my code won't complain if a document declared as UTF-16LE has a UTF32-LE BOM, correct?
Well, not specifically that case since UTF-16LE BOM is a subset of UTF32-LE BOM.
My observation was more general in that [1] seems to say that there should be _no_ BOM whatsoever if one of UTF-16BE, UTF-16LE, UTF-32BE, or UTF-32LE is declared.
Quoted 6 lines
I would say
this is correct behavior in context of this function. This function assumes that the document is proper UTF-16/UTF-16BE/UTF-16LE but it is wrongly declared with respect to its BOM in the .gitattributes. Would this comment make it more clear to you?
/*
Quoted 8 lines
* If a data stream is declared as UTF-16BE or UTF-16LE, then a UTF-16
* BOM must not be used [1]. The same applies for the UTF-32 equivalents.
* The function returns true if this rule is violated.
*
* [1] http://unicode.org/faq/utf_bom.html#bom10
*/
I think what you are referring to is a different class of error and would therefore warrant its own checker function. Would you agree?
I don't understand to what different class of error you refer. The FAQ[1] seems pretty clear to me in that if one of those declarations is used explicitly, then there should be _no_ BOM, period. It doesn't say anything about allowing a BOM for a differently-sized encoding (16 vs 32).
If I squint very hard, I _guess_ I can see how you interpret [1] with the more narrow meaning of the restriction applying only to a BOM of the same size as the declared encoding, though reading it that way doesn't come easily to me.
Quoted 8 lines
Quoted 4 lines
I suppose the intention of [1] is to detect a mismatch between the declared encoding and how the stream is actually encoded. The check implemented here will fail to detect a mismatch between, say, declared encoding UTF-16BE and actual encoding UTF-32BE.
As stated above the intention is to detect wrong BOMs! I think we cannot detect the "declared as UTF-16BE but actually UTF-32BE" error.
Quoted 8 lines
Consider this:
printf "test" | iconv -f UTF-8 -t UTF-32BE | iconv -f UTF-16BE -t UTF-8 | od -c
0000000 \0 t \0 e \0 s \0 t
0000010In the first step we "encode" the string to UTF-32BE and then we "decode" it as UTF-16BE. The result is valid although not correct. Does this make sense?
I'm probably being dense, but I don't understand what this is trying to illustrate in relation to has_prohibited_utf_bom().
Re: [PATCH v8 3/7] utf8: add function to detect prohibited UTF-16/32 BOM
Quoted 12 lines · Eric Sunshine
Quoted 7 lines · Lars Schneider
Quoted 4 lines · Eric Sunshine
Is this interpretation correct? When I read [1], I interpret it as saying that no BOM
_ofanysort_should be present when the encoding is declared as one of UTF-16BE, UTF-16LE, UTF-32BE, or UTF-32LE.Correct!
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
This code, on the other hand, only checks for BOMs corresponding to the declared size (16 or 32 bits).
Hmm. Interesting thought. You are saying that my code won't complain if a document declared as UTF-16LE has a UTF32-LE BOM, correct?
Well, not specifically that case since UTF-16LE BOM is a subset of UTF32-LE BOM.
Correct - bad example on my part!
Quoted 3 lines
My observation was more general in that [1] seems to say that there should be _no_ BOM whatsoever if one of UTF-16BE, UTF-16LE, UTF-32BE, or UTF-32LE is declared.
You are saying that a document declared as UTF-16LE must not start with 0000feff (UTF-32BE BOM)? I interpreted that situation as a "feff" in the middle of a file and therefore the BOM should be treated as ZWNBSP as explained here: http://unicode.org/faq/utf_bom.html#bom6
Plus, if "_no_ BOM whatsoever" is allowed then wouldn't we need to check for UTF-1, UTF-7, and UTF-8 BOM's too?
I dunno.
Quoted 21 lines
Quoted 6 lines
I would say
this is correct behavior in context of this function. This function assumes that the document is proper UTF-16/UTF-16BE/UTF-16LE but it is wrongly declared with respect to its BOM in the .gitattributes. Would this comment make it more clear to you?
/*Quoted 8 lines
* If a data stream is declared as UTF-16BE or UTF-16LE, then a UTF-16
* BOM must not be used [1]. The same applies for the UTF-32 equivalents.
* The function returns true if this rule is violated.
*
* [1] http://unicode.org/faq/utf_bom.html#bom10
*/
I think what you are referring to is a different class of error and would therefore warrant its own checker function. Would you agree?I don't understand to what different class of error you refer. The FAQ[1] seems pretty clear to me in that if one of those declarations is used explicitly, then there should be _no_ BOM, period. It doesn't say anything about allowing a BOM for a differently-sized encoding (16 vs 32).
Quoted 4 lines
If I squint very hard, I _guess_ I can see how you interpret [1] with the more narrow meaning of the restriction applying only to a BOM of the same size as the declared encoding, though reading it that way doesn't come easily to me.
For me it is somewhat the other way around :-)
Since I am not sure what is right, I decided to ask the Internet:
https://stackoverflow.com/questions/49038872/is-a-utf-32be-bom-valid-in-an-utf-16le-declared-data-stream
Let's see if someone has a good answer.
- Lars
PATCH v8 4/7 utf8: add function to detect a missing UTF-16/32 BOM
In reply to [email protected]From: Lars Schneider <[email protected]>
If the endianness is not defined in the encoding name, then let's be strict and require a BOM to avoid any encoding confusion. The is_missing_required_utf_bom() function returns true if a required BOM is missing.
The Unicode standard instructs to assume big-endian if there in no BOM for UTF-16/32 [1][2]. However, the W3C/WHATWG encoding standard used in HTML5 recommends to assume little-endian to "deal with deployed content" [3]. Strictly requiring a BOM seems to be the safest option for content in Git.
This function is used in a subsequent commit.
[1] http://unicode.org/faq/utf_bom.html#gen6
[2] http://www.unicode.org/versions/Unicode10.0.0/ch03.pdf
Section 3.10, D98, page 132
[3] https://encoding.spec.whatwg.org/#utf-16le
--- utf8.c | 13 +++++++++++++ utf8.h | 16 ++++++++++++++++ 2 files changed, 29 insertions(+)
utf8.c +13 -0
@@ -562,6 +562,19 @@ int has_prohibited_utf_bom(const char *enc, const char *data, size_t len) ); } +int is_missing_required_utf_bom (const char *enc,const char *data,size_t len) +{ +return ( + !strcmp (enc,"UTF-16" ) && + !(has_bom_prefix (data, len, utf16_be_bom,sizeof (utf16_be_bom)) || +has_bom_prefix (data, len, utf16_le_bom,sizeof (utf16_le_bom))) + ) || ( + !strcmp (enc,"UTF-32" ) && + !(has_bom_prefix (data, len, utf32_be_bom,sizeof (utf32_be_bom)) || +has_bom_prefix (data, len, utf32_le_bom,sizeof (utf32_le_bom))) + ); +} +/* * Returns first character length in bytes for multi-byte `text` according to * `encoding`.
utf8.h +16 -0
@@ -79,4 +79,20 @@ void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int wid*/ int has_prohibited_utf_bom (const char *enc,const char *data,size_t len); +/* +* If the endianness is not defined in the encoding name, then we +* require a BOM. The function returns true if a required BOM is missing. +* +* The Unicode standard instructs to assume big-endian if there +* in no BOM for UTF-16/32 [1][2]. However, the W3C/WHATWG +* encoding standard used in HTML5 recommends to assume +* little-endian to "deal with deployed content" [3]. +* +* [1] http://unicode.org/faq/utf_bom.html#gen6 +* [2] http://www.unicode.org/versions/Unicode10.0.0/ch03.pdf +* Section 3.10, D98, page 132 +* [3] https://encoding.spec.whatwg.org/#utf-16le +*/ +int is_missing_required_utf_bom (const char *enc,const char *data,size_t len); +#endif
Signature · 1 line
2.16.1
Re: [PATCH v8 4/7] utf8: add function to detect a missing UTF-16/32 BOM
Quoted 15 lines · On Sat, Feb 24, 2018 at 11:27 AM, <[email protected]> wrote:
If the endianness is not defined in the encoding name, then let's be strict and require a BOM to avoid any encoding confusion. The
is_missing_required_utf_bom()function returns true if a required BOM is missing.The Unicode standard instructs to assume big-endian if there in no BOM for UTF-16/32 [1][2]. However, the W3C/WHATWG encoding standard used in HTML5 recommends to assume little-endian to "deal with deployed content" [3]. Strictly requiring a BOM seems to be the safest option for content in Git.
Signed-off-by Lars Schneider <[email protected]>---
utf8.h void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int wid · line 86
Quoted 8 lines
utf8.h +8
-0@@ -79,4 +79,20 @@ void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int wid +/* +* If the endianness is not defined in the encoding name, then we +* require a BOM. The function returns true if a required BOM is missing. +* +* The Unicode standard instructs to assume big-endian if there +* in no BOM for UTF-16/32 [1][2]. However, the W3C/WHATWG +* encoding standard used in HTML5 recommends to assume +* little-endian to "deal with deployed content" [3].
Perhaps you could tack on to the comment here the final bit of explanation from the commit message which ties these conflicting recommendations together. In particular:
Therefore, strictly requiring a BOM seems to be the safest option for content in Git.
Quoted 2 lines
utf8.h +2
-0+*/ +int is_missing_required_utf_bom (const char *enc,const char *data,size_t len);
Re: [PATCH v8 4/7] utf8: add function to detect a missing UTF-16/32 BOM
Quoted 32 lines · Eric Sunshine
Quoted 15 lines · On Sat, Feb 24, 2018 at 11:27 AM, <[email protected]> wrote:
If the endianness is not defined in the encoding name, then let's be strict and require a BOM to avoid any encoding confusion. The
is_missing_required_utf_bom()function returns true if a required BOM is missing.The Unicode standard instructs to assume big-endian if there in no BOM for UTF-16/32 [1][2]. However, the W3C/WHATWG encoding standard used in HTML5 recommends to assume little-endian to "deal with deployed content" [3]. Strictly requiring a BOM seems to be the safest option for content in Git.
Signed-off-by Lars Schneider <[email protected]>---
utf8.h void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int wid · line 86
Quoted 8 lines
utf8.h +8
-0@@ -79,4 +79,20 @@ void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int wid +/* +* If the endianness is not defined in the encoding name, then we +* require a BOM. The function returns true if a required BOM is missing. +* +* The Unicode standard instructs to assume big-endian if there +* in no BOM for UTF-16/32 [1][2]. However, the W3C/WHATWG +* encoding standard used in HTML5 recommends to assume +* little-endian to "deal with deployed content" [3]. Perhaps you could tack on to the comment here the final bit of explanation from the commit message which ties these conflicting recommendations together. In particular:
Quoted 2 lines
Therefore, strictly requiring a BOM seems to be the safest option for content in Git.
Agreed. I'll change it.
Thanks,
Lars
PATCH v8 5/7 convert: add 'working-tree-encoding' attribute
In reply to [email protected]From: Lars Schneider <[email protected]>
Git recognizes files encoded with ASCII or one of its supersets (e.g. UTF-8 or ISO-8859-1) as text files. All other encodings are usually interpreted as binary and consequently built-in Git text processing tools (e.g. 'git diff') as well as most Git web front ends do not visualize the content.
Add an attribute to tell Git what encoding the user has defined for a given file. If the content is added to the index, then Git converts the content to a canonical UTF-8 representation. On checkout Git will reverse the conversion.
--- Documentation/gitattributes.txt | 78 ++++++++++++++ convert.c | 157 ++++++++++++++++++++++++++- convert.h | 1 + sha1_file.c | 2 +- t/t0028-working-tree-encoding.sh | 226 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 462 insertions(+), 2 deletions(-) create mode 100755 t/t0028-working-tree-encoding.sh
Documentation/gitattributes.txt +78 -0
@@ -272,6 +272,84 @@ few exceptions. Even though... catch potential problems early, safety triggers. +`working-tree-encoding` +^^^^^^^^^^^^^^^^^^^^^^^ + +Git recognizes files encoded in ASCII or one of its supersets (e.g. +UTF-8, ISO-8859-1, ...) as text files. Files encoded in certain other +encodings (e.g. UTF-16) are interpreted as binary and consequently +built-in Git text processing tools (e.g. 'git diff') as well as most Git +web front ends do not visualize the contents of these files by default. + +In these cases you can tell Git the encoding of a file in the working +directory with the `working-tree-encoding` attribute. If a file with this +attribute is added to Git, then Git reencodes the content from the +specified encoding to UTF-8. Finally, Git stores the UTF-8 encoded +content in its internal data structure (called "the index"). On checkout +the content is reencoded back to the specified encoding. + +Please note that using the `working-tree-encoding` attribute may have a +number of pitfalls: + +- Alternative Git implementations (e.g. JGit or libgit2) and older Git + versions (as of March 2018) do not support the `working-tree-encoding` + attribute. If you decide to use the `working-tree-encoding` attribute + in your repository, then it is strongly recommended to ensure that all + clients working with the repository support it. + + If you declare `*.proj` files as UTF-16 and you add `foo.proj` with an + `working-tree-encoding` enabled Git client, then `foo.proj` will be + stored as UTF-8 internally. A client without `working-tree-encoding` + support will checkout `foo.proj` as UTF-8 encoded file. This will + typically cause trouble for the users of this file. + + If a Git client, that does not support the `working-tree-encoding` + attribute, adds a new file `bar.proj`, then `bar.proj` will be + stored "as-is" internally (in this example probably as UTF-16). + A client with `working-tree-encoding` support will interpret the + internal contents as UTF-8 and try to convert it to UTF-16 on checkout. + That operation will fail and cause an error. + +- Reencoding content requires resources that might slow down certain + Git operations (e.g 'git checkout' or 'git add'). + +Use the `working-tree-encoding` attribute only if you cannot store a file +in UTF-8 encoding and if you want Git to be able to process the content +as text. + +As an example, use the following attributes if your '*.proj' files are +UTF-16 encoded with byte order mark (BOM) and you want Git to perform +automatic line ending conversion based on your platform. + +------------------------ +*.proj text working-tree-encoding=UTF-16 +------------------------ + +Use the following attributes if your '*.proj' files are UTF-16 little +endian encoded without BOM and you want Git to use Windows line endings +in the working directory. Please note, it is highly recommended to +explicitly define the line endings with `eol` if the `working-tree-encoding` +attribute is used to avoid ambiguity. + +------------------------ +*.proj text working-tree-encoding=UTF-16LE eol=CRLF +------------------------ + +You can get a list of all available encodings on your platform with the +following command: + +------------------------ +iconv --list +------------------------ + +If you do not know the encoding of a file, then you can use the `file` +command to guess the encoding: + +------------------------ +file foo.proj +------------------------ + + `ident` ^^^^^^^
convert.c +156 -1
218 lines not on this page · raw message
convert.h +1 -0
@@ -12,6 +12,7 @@ struct index_state;#define CONV_EOL_RNDTRP_WARN (1<<1)/* Warn if CRLF to LF to CRLF is different */ #define CONV_EOL_RENORMALIZE (1<<2)/* Convert CRLF to LF */ #define CONV_EOL_KEEP_CRLF (1<<3)/* Keep CRLF line endings as is */ +#define CONV_WRITE_OBJECT (1<<4)/* Content is written to the index */ extern int global_conv_flags_eol;
sha1_file.c +1 -1
@@ -138,7 +138,7 @@ static int get_conv_flags(unsigned flags)if (flags &HASH_RENORMALIZE )return CONV_EOL_RENORMALIZE ;else if (flags &HASH_WRITE_OBJECT ) -return global_conv_flags_eol; +return global_conv_flags_eol |CONV_WRITE_OBJECT ;else return 0 ; }
t/t0028-working-tree-encoding.sh new file +226 -0
226 lines not on this page · raw message
Signature · 1 line
2.16.1
Re: [PATCH v8 5/7] convert: add 'working-tree-encoding' attribute
Quoted 19 lines · On Sat, Feb 24, 2018 at 11:27 AM, <[email protected]> wrote:
Git recognizes files encoded with ASCII or one of its supersets (e.g. UTF-8 or ISO-8859-1) as text files. All other encodings are usually interpreted as binary and consequently built-in Git text processing tools (e.g. 'git diff') as well as most Git web front ends do not visualize the content.
Add an attribute to tell Git what encoding the user has defined for a given file. If the content is added to the index, then Git converts the content to a canonical UTF-8 representation. On checkout Git will reverse the conversion.
Signed-off-by Lars Schneider <[email protected]>---
Documentation/gitattributes.txt +4
-0@@ -272,6 +272,84 @@ few exceptions. Even though... +Please note that using the `working-tree-encoding` attribute may have a +number of pitfalls: + +- Alternative Git implementations (e.g. JGit or libgit2) and older Git
Documentation/gitattributes.txt few exceptions. Even though... · line 285
Quoted 10 lines
Documentation/gitattributes.txt +10
-0+ versions (as of March 2018) do not support the `working-tree-encoding` + attribute. If you decide to use the `working-tree-encoding` attribute + in your repository, then it is strongly recommended to ensure that all + clients working with the repository support it. + + If you declare `*.proj` files as UTF-16 and you add `foo.proj` with an + `working-tree-encoding` enabled Git client, then `foo.proj` will be + stored as UTF-8 internally. A client without `working-tree-encoding` + support will checkout `foo.proj` as UTF-8 encoded file. This will + typically cause trouble for the users of this file.
The above paragraph is giving an example of the scenario described in the paragraph above it. It might, therefore, be clearer to start this paragraph with "For example,...". Also, as an aid to non-Windows developers, it might be helpful to introduce ".proj" files as "Microsoft Visual Studio *.proj files...".
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 269
Quoted 6 lines
convert.c +4
-0@@ -265,6 +266,110 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, +static struct encoding { +const char *name; +struct encoding *next; +} *encoding, **encoding_tail;
Why does this struct need to be a linked-list since it contains only a name? In fact, why do the struct and linked list exist at all? Back in v1 when the struct held more members, it made sense to place them in a collection so they could be re-used, but today it just seems an unnecessary artifact which buys you nothing.
Is the plan that some future patch series will add members to the struct? If not, then it seems that it should be retired altogether.
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 301
Quoted 11 lines
convert.c +11
-0+static const char *default_encoding ="UTF-8" ; + +static int encode_to_git (const char *path,const char *src,size_t src_len, +struct strbuf *buf,struct encoding *enc,int conv_flags) +{ + [...] +if (has_prohibited_utf_bom (enc->name, src, src_len)) { +const char *error_msg =_ ( +"BOM is prohibited in '%s' if encoded as %s" ); +/* +* This advise is shown for UTF-??BE and UTF-??LE encodings.
s/advise/advice/
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 312
Quoted 11 lines
convert.c +11
-0+* We truncate the encoding name to 6 chars with %.6s to cut +* off the last two "byte order" characters. +*/ +const char *advise_msg =_ ( +"The file '%s' contains a byte order mark (BOM). " +"Please use %.6s as working-tree-encoding." ); +advise (advise_msg, path, enc->name); +if (conv_flags &CONV_WRITE_OBJECT ) +die (error_msg, path, enc->name); +else +error (error_msg, path, enc->name);
What is the intended behavior in the non-die() case? As implemented, this is still going to attempt the conversion even though it threw an error. Should it be returning 0 here instead? Same question for the couple additional cases below.
Quoted 4 lines
convert.c +4
-0+ + }else if (is_missing_required_utf_bom (enc->name, src, src_len)) { +const char *error_msg =_ ( +"BOM is required in '%s' if encoded as %s" );
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 326
Quoted 10 lines
convert.c +10
-0+const char *advise_msg =_ ( +"The file '%s' is missing a byte order mark (BOM). " +"Please use %sBE or %sLE (depending on the byte order) " +"as working-tree-encoding." ); +advise (advise_msg, path, enc->name, enc->name); +if (conv_flags &CONV_WRITE_OBJECT ) +die (error_msg, path, enc->name); +else +error (error_msg, path, enc->name); + }
For a function which presumably is agnostic about the working-tree encoding (supporting whatever iconv does), it nevertheless has unusually intimate knowledge about UTF BOMs, in general, and the internals of has_prohibited_utf_bom() and is_missing_required_utf_bom(), in particular. It might be cleaner, and would simplify this function, if all this UTF BOM-specific gunk was moved elsewhere and generalized into a "validate conversion" function.
As this series is already at v8, perhaps such cleanup could be done as a follow-on series (by someone) rather than re-rolling.
Quoted 19 lines
convert.c +19
-0+ dst =reencode_string_len (src, src_len, default_encoding, enc->name, + &dst_len); +if (!dst) { +/* +* We could add the blob "as-is" to Git. However, on checkout +* we would try to reencode to the original encoding. This +* would fail and we would leave the user with a messed-up +* working tree. Let's try to avoid this by screaming loud. +*/ +const char * msg =_ ("failed to encode '%s' from %s to %s" ); +if (conv_flags &CONV_WRITE_OBJECT ) +die (msg, path, enc->name, default_encoding); +else +error (msg, path, enc->name, default_encoding); + } + +strbuf_attach (buf, dst, dst_len, dst_len +1 ); +return 1 ; +}
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 365
Quoted 10 lines
convert.c +10
-0+ +static int encode_to_worktree (const char *path,const char *src,size_t src_len, +struct strbuf *buf,struct encoding *enc) +{ + [...] + dst =reencode_string_len (src, src_len, enc->name, default_encoding, + &dst_len); +if (!dst) { +error ("failed to encode '%s' from %s to %s" , + path, enc->name, default_encoding);
The last two arguments need to be swapped.
Quoted 10 lines
convert.c +9
-0+return 0 ; + } + +strbuf_attach (buf, dst, dst_len, dst_len +1 ); +return 1 ; +}@@ -978,6 +1083,35 @@ static int ident_to_worktree(const char *path, const char *src, size_t len, +static struct encoding *git_path_check_encoding (struct attr_check_item *check) +{ + if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value) ||
convert.c static int ident_to_worktree(const char *path, const char *src, size_t len, · line 1095
Quoted 10 lines
convert.c +10
-0+ !strlen(value)) +return NULL ; + +for (enc = encoding; enc; enc = enc->next) +if (!strcasecmp (value, enc->name)) +return enc; + +/* Don't encode to the default encoding */ +if (!strcasecmp (value, default_encoding)) +return NULL ;
You could handle this easy-to-detect case before attempting the more expensive loop just above. (Not necessarily worth a re-roll.)
Quoted 13 lines
convert.c +11
-0+ enc =xcalloc (1 ,sizeof (*enc)); +/* +* Ensure encoding names are always upper case (e.g. UTF-8) to +* simplify subsequent string comparisons. +*/ + enc->name =xstrdup_toupper (value); + *encoding_tail = enc; + encoding_tail = &(enc->next); + +return enc; +}
t/t0028-working-tree-encoding.sh line 7
Quoted 7 lines
t/t0028-working-tree-encoding.sh +7
-0@@ -0,0 +1,226 @@ +test_expect_success 'ensure UTF-8 is stored in Git' ' + git cat-file -p :test.utf16 >test.utf16.git && + test_cmp_bin test.utf8.raw test.utf16.git && + + # cleanup + rm test.utf16.git +'
This cleanup won't take place if test_cmp_bin() fails. Instead, cleanups are typically handled by test_when_finished() to ensure that the cleanup action occurs even if the test fails.
test_expect_success 'ensure UTF-8 is stored in Git' '
test_when_finished "rm -f test.utf16.git" &&
...Same comment for remaining tests.
t/t0028-working-tree-encoding.sh line 62
Quoted 10 lines
t/t0028-working-tree-encoding.sh +10
-0+test_expect_success 'check prohibited UTF BOM' ' + printf "\0a\0b\0c" >nobom.utf16be.raw && + printf "a\0b\0c\0" >nobom.utf16le.raw && + printf "\376\777\0a\0b\0c" >bebom.utf16be.raw && + printf "\777\376a\0b\0c\0" >lebom.utf16le.raw && + + printf "\0\0\0a\0\0\0b\0\0\0c" >nobom.utf32be.raw && + printf "a\0\0\0b\0\0\0c\0\0\0" >nobom.utf32le.raw && + printf "\0\0\376\777\0\0\0a\0\0\0b\0\0\0c" >bebom.utf32be.raw && + printf "\777\376\0\0a\0\0\0b\0\0\0c\0\0\0" >lebom.utf32le.raw &&
These resources seem to be used by multiple tests. Perhaps create them in a distinct "setup" test instead of bundling them in this test?
Quoted 34 lines
t/t0028-working-tree-encoding.sh +34
-0+ echo "*.utf16be text working-tree-encoding=utf-16be" >>.gitattributes && + echo "*.utf16le text working-tree-encoding=utf-16le" >>.gitattributes && + echo "*.utf32be text working-tree-encoding=utf-32be" >>.gitattributes && + echo "*.utf32le text working-tree-encoding=utf-32le" >>.gitattributes && + + # Here we add a UTF-16 files with BOM (big-endian and little-endian) + # but we tell Git to treat it as UTF-16BE/UTF-16LE. In these cases + # the BOM is prohibited. + cp bebom.utf16be.raw bebom.utf16be && + test_must_fail git add bebom.utf16be 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-16BE" err.out && + + cp lebom.utf16le.raw lebom.utf16be && + test_must_fail git add lebom.utf16be 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-16BE" err.out && + + cp bebom.utf16be.raw bebom.utf16le && + test_must_fail git add bebom.utf16le 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-16LE" err.out && + + cp lebom.utf16le.raw lebom.utf16le && + test_must_fail git add lebom.utf16le 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-16LE" err.out && + + # ... and the same for UTF-32 + cp bebom.utf32be.raw bebom.utf32be && + test_must_fail git add bebom.utf32be 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-32BE" err.out && + + cp lebom.utf32le.raw lebom.utf32be && + test_must_fail git add lebom.utf32be 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-32BE" err.out && + + cp bebom.utf32be.raw bebom.utf32le &&
t/t0028-working-tree-encoding.sh line 107
Quoted 10 lines
t/t0028-working-tree-encoding.sh +10
-0+ test_must_fail git add bebom.utf32le 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-32LE" err.out && + + cp lebom.utf32le.raw lebom.utf32le && + test_must_fail git add lebom.utf32le 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-32LE" err.out && + + # cleanup + git reset --hard HEAD +'
Clumping all these checks into the same test makes it difficult to determine which one failed (if one does fail). Also, the amount of copy/paste code is easy to get wrong and a bit onerous to review. Automating via for-loops would address these concerns. For instance, to check all combinations (not just 8 combinations tested above):
for i in 16be 16le 32be 32le
do
for j in 16be 16le 32be 32le
do
test_expect_success "check prohibited UTF BOM utf$i/utf$j" '
test_when_finished "git reset --hard HEAD" &&
cp bebom.utf$i.raw bebom.utf$j &&
test_must_fail git add bebom.utf$j 2>err.out &&
J=$(echo $j | tr bel BEL) &&
test_i18ngrep "fatal: BOM is prohibited .* UTF-$J" err.out
'
done
doneAlternately, the test could be moved to a function and called for each combination:
check_prohibited_bom () {
i=$1
j=$2
test_expect_success "check prohibited UTF BOM utf$i/utf$j" '
...
'
}
check_prohibited_bom 16be 16be
check_prohibited_bom 16be 16le
...Same comment regarding copy/paste in tests below.
Probably not worth a re-roll.
Quoted 23 lines
t/t0028-working-tree-encoding.sh +23
-0+test_expect_success 'check required UTF BOM' ' + echo "*.utf32 text working-tree-encoding=utf-32" >>.gitattributes && + + cp nobom.utf16be.raw nobom.utf16 && + test_must_fail git add nobom.utf16 2>err.out && + test_i18ngrep "fatal: BOM is required .* UTF-16" err.out && + + cp nobom.utf16le.raw nobom.utf16 && + test_must_fail git add nobom.utf16 2>err.out && + test_i18ngrep "fatal: BOM is required .* UTF-16" err.out && + + cp nobom.utf32be.raw nobom.utf32 && + test_must_fail git add nobom.utf32 2>err.out && + test_i18ngrep "fatal: BOM is required .* UTF-32" err.out && + + cp nobom.utf32le.raw nobom.utf32 && + test_must_fail git add nobom.utf32 2>err.out && + test_i18ngrep "fatal: BOM is required .* UTF-32" err.out && + + # cleanup + rm nobom.utf16 nobom.utf32 && + git reset --hard HEAD +'
Re: [PATCH v8 5/7] convert: add 'working-tree-encoding' attribute
Quoted 31 lines · Eric Sunshine
Quoted 27 lines · On Sat, Feb 24, 2018 at 11:27 AM, <[email protected]> wrote:
Git recognizes files encoded with ASCII or one of its supersets (e.g. UTF-8 or ISO-8859-1) as text files. All other encodings are usually interpreted as binary and consequently built-in Git text processing tools (e.g. 'git diff') as well as most Git web front ends do not visualize the content.
Add an attribute to tell Git what encoding the user has defined for a given file. If the content is added to the index, then Git converts the content to a canonical UTF-8 representation. On checkout Git will reverse the conversion.
Signed-off-by Lars Schneider <[email protected]>---
Documentation/gitattributes.txt +12
-0@@ -272,6 +272,84 @@ few exceptions. Even though... +Please note that using the `working-tree-encoding` attribute may have a +number of pitfalls: + +- Alternative Git implementations (e.g. JGit or libgit2) and older Git + versions (as of March 2018) do not support the `working-tree-encoding` + attribute. If you decide to use the `working-tree-encoding` attribute + in your repository, then it is strongly recommended to ensure that all + clients working with the repository support it. + + If you declare `*.proj` files as UTF-16 and you add `foo.proj` with an + `working-tree-encoding` enabled Git client, then `foo.proj` will be + stored as UTF-8 internally. A client without `working-tree-encoding`
Quoted 8 lines
Documentation/gitattributes.txt few exceptions. Even though... · line 304
Quoted 2 lines
Documentation/gitattributes.txt +2
-0+ support will checkout `foo.proj` as UTF-8 encoded file. This will + typically cause trouble for the users of this file.The above paragraph is giving an example of the scenario described in the paragraph above it. It might, therefore, be clearer to start this paragraph with "For example,...". Also, as an aid to non-Windows developers, it might be helpful to introduce ".proj" files as "Microsoft Visual Studio
*.projfiles...".
Agreed. How about this?
For example, Microsoft Visual Studio resources files (*.rc) or PowerShell script files (*.ps1) are sometimes encoded in UTF-16. If you declare *.ps1 as files as UTF-16 and you add foo.ps1 with a working-tree-encoding enabled Git client, then foo.ps1 will be stored as UTF-8 internally. A client without working-tree-encoding support will checkout foo.ps1 as UTF-8 encoded file. This will typically cause trouble for the users of this file.
Quoted 13 lines
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 269
Quoted 6 lines
convert.c +4
-0@@ -265,6 +266,110 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, +static struct encoding { +const char *name; +struct encoding *next; +} *encoding, **encoding_tail;Why does this struct need to be a linked-list since it contains only a name? In fact, why do the struct and linked list exist at all? Back in v1 when the struct held more members, it made sense to place them in a collection so they could be re-used, but today it just seems an unnecessary artifact which buys you nothing.
Quoted 2 lines
Is the plan that some future patch series will add members to the struct? If not, then it seems that it should be retired altogether.
Absolutely! Thank you!
Quoted 5 lines
Quoted 5 lines
convert.c +5
-0+static const char *default_encoding ="UTF-8" ; + +static int encode_to_git (const char *path,const char *src,size_t src_len, +struct strbuf *buf,struct encoding *enc,int conv_flags) +{
Quoted 8 lines
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 301
Quoted 6 lines
convert.c +6
-0+ [...] +if (has_prohibited_utf_bom (enc->name, src, src_len)) { +const char *error_msg =_ ( +"BOM is prohibited in '%s' if encoded as %s" ); +/* +* This advise is shown for UTF-??BE and UTF-??LE encodings. s/advise/advice/
Agreed!
Quoted 12 lines
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 312
Quoted 11 lines
convert.c +11
-0+* We truncate the encoding name to 6 chars with %.6s to cut +* off the last two "byte order" characters. +*/ +const char *advise_msg =_ ( +"The file '%s' contains a byte order mark (BOM). " +"Please use %.6s as working-tree-encoding." ); +advise (advise_msg, path, enc->name); +if (conv_flags &CONV_WRITE_OBJECT ) +die (error_msg, path, enc->name); +else +error (error_msg, path, enc->name);
Quoted 4 lines
What is the intended behavior in the non-die() case? As implemented, this is still going to attempt the conversion even though it threw an error. Should it be returning 0 here instead? Same question for the couple additional cases below.
Good question. My intention was to print an error and then let iconv try the conversion anyways. I agree that this is bogus. Let's return in case of an error right away.
Quoted 15 lines
Quoted 4 lines
convert.c +4
-0+ + }else if (is_missing_required_utf_bom (enc->name, src, src_len)) { +const char *error_msg =_ ( +"BOM is required in '%s' if encoded as %s" );convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 326
Quoted 10 lines
convert.c +10
-0+const char *advise_msg =_ ( +"The file '%s' is missing a byte order mark (BOM). " +"Please use %sBE or %sLE (depending on the byte order) " +"as working-tree-encoding." ); +advise (advise_msg, path, enc->name, enc->name); +if (conv_flags &CONV_WRITE_OBJECT ) +die (error_msg, path, enc->name); +else +error (error_msg, path, enc->name); + }
Quoted 7 lines
For a function which presumably is agnostic about the working-tree encoding (supporting whatever iconv does), it nevertheless has unusually intimate knowledge about UTF BOMs, in general, and the internals of
has_prohibited_utf_bom()andis_missing_required_utf_bom(), in particular. It might be cleaner, and would simplify this function, if all this UTF BOM-specific gunk was moved elsewhere and generalized into a "validate conversion" function.
Agreed! How about this?
/*
* If we convert to an UTF encoding, then check for common BOM errors.
*/
if (!memcmp("UTF-", enc, 4))
if (has_utf_bom_error(path, enc, src, src_len, die_on_error))
return 0;Quoted 23 lines
Quoted 23 lines
convert.c +23
-0+ dst =reencode_string_len (src, src_len, default_encoding, enc->name, + &dst_len); +if (!dst) { +/* +* We could add the blob "as-is" to Git. However, on checkout +* we would try to reencode to the original encoding. This +* would fail and we would leave the user with a messed-up +* working tree. Let's try to avoid this by screaming loud. +*/ +const char * msg =_ ("failed to encode '%s' from %s to %s" ); +if (conv_flags &CONV_WRITE_OBJECT ) +die (msg, path, enc->name, default_encoding); +else +error (msg, path, enc->name, default_encoding); + } + +strbuf_attach (buf, dst, dst_len, dst_len +1 ); +return 1 ; +} + +static int encode_to_worktree (const char *path,const char *src,size_t src_len, +struct strbuf *buf,struct encoding *enc) +{
Quoted 8 lines
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 365
Quoted 6 lines
convert.c +6
-0+ [...] + dst =reencode_string_len (src, src_len, enc->name, default_encoding, + &dst_len); +if (!dst) { +error ("failed to encode '%s' from %s to %s" , + path, enc->name, default_encoding);The last two arguments need to be swapped.
Nice catch!
Quoted 15 lines
Quoted 15 lines
convert.c +14
-0+return 0 ; + } + +strbuf_attach (buf, dst, dst_len, dst_len +1 ); +return 1 ; +}@@ -978,6 +1083,35 @@ static int ident_to_worktree(const char *path, const char *src, size_t len, +static struct encoding *git_path_check_encoding (struct attr_check_item *check) +{ +if (ATTR_TRUE (value) ||ATTR_FALSE (value) ||ATTR_UNSET (value) || + !strlen (value)) +return NULL ; + +for (enc = encoding; enc; enc = enc->next) +if (!strcasecmp (value, enc->name))
Quoted 8 lines
convert.c static int ident_to_worktree(const char *path, const char *src, size_t len, · line 1101
Quoted 5 lines
convert.c +5
-0+return enc; + +/* Don't encode to the default encoding */ +if (!strcasecmp (value, default_encoding)) +return NULL ;You could handle this easy-to-detect case before attempting the more expensive loop just above. (Not necessarily worth a re-roll.)
True, but I delete the loop anyways when I removed the "encoding linked list" as suggested by your comment above.
Quoted 25 lines
Quoted 13 lines
convert.c +11
-0+ enc =xcalloc (1 ,sizeof (*enc)); +/* +* Ensure encoding names are always upper case (e.g. UTF-8) to +* simplify subsequent string comparisons. +*/ + enc->name =xstrdup_toupper (value); + *encoding_tail = enc; + encoding_tail = &(enc->next); + +return enc; +}t/t0028-working-tree-encoding.sh line 7
Quoted 7 lines
t/t0028-working-tree-encoding.sh +7
-0@@ -0,0 +1,226 @@ +test_expect_success 'ensure UTF-8 is stored in Git' ' + git cat-file -p :test.utf16 >test.utf16.git && + test_cmp_bin test.utf8.raw test.utf16.git && + + # cleanup + rm test.utf16.git +'This cleanup won't take place if
test_cmp_bin()fails. Instead, cleanups are typically handled bytest_when_finished()to ensure that the cleanup action occurs even if the test fails.
Quoted 5 lines
test_expect_success 'ensure UTF-8 is stored in Git' ' test_when_finished "rm -f test.utf16.git" && ...Same comment for remaining tests.
Agreed! That's the proper way to cleanup the tests. I'll fix my tests.
Quoted 5 lines
Quoted 5 lines
t/t0028-working-tree-encoding.sh +5
-0+test_expect_success 'check prohibited UTF BOM' ' + printf "\0a\0b\0c" >nobom.utf16be.raw && + printf "a\0b\0c\0" >nobom.utf16le.raw && + printf "\376\777\0a\0b\0c" >bebom.utf16be.raw && + printf "\777\376a\0b\0c\0" >lebom.utf16le.raw &&
Quoted 8 lines
t/t0028-working-tree-encoding.sh line 62
Quoted 5 lines
t/t0028-working-tree-encoding.sh +5
-0+ + printf "\0\0\0a\0\0\0b\0\0\0c" >nobom.utf32be.raw && + printf "a\0\0\0b\0\0\0c\0\0\0" >nobom.utf32le.raw && + printf "\0\0\376\777\0\0\0a\0\0\0b\0\0\0c" >bebom.utf32be.raw && + printf "\777\376\0\0a\0\0\0b\0\0\0c\0\0\0" >lebom.utf32le.raw &&These resources seem to be used by multiple tests. Perhaps create them in a distinct "setup" test instead of bundling them in this test?
Good idea!
Quoted 79 lines
Quoted 34 lines
t/t0028-working-tree-encoding.sh +34
-0+ echo "*.utf16be text working-tree-encoding=utf-16be" >>.gitattributes && + echo "*.utf16le text working-tree-encoding=utf-16le" >>.gitattributes && + echo "*.utf32be text working-tree-encoding=utf-32be" >>.gitattributes && + echo "*.utf32le text working-tree-encoding=utf-32le" >>.gitattributes && + + # Here we add a UTF-16 files with BOM (big-endian and little-endian) + # but we tell Git to treat it as UTF-16BE/UTF-16LE. In these cases + # the BOM is prohibited. + cp bebom.utf16be.raw bebom.utf16be && + test_must_fail git add bebom.utf16be 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-16BE" err.out && + + cp lebom.utf16le.raw lebom.utf16be && + test_must_fail git add lebom.utf16be 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-16BE" err.out && + + cp bebom.utf16be.raw bebom.utf16le && + test_must_fail git add bebom.utf16le 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-16LE" err.out && + + cp lebom.utf16le.raw lebom.utf16le && + test_must_fail git add lebom.utf16le 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-16LE" err.out && + + # ... and the same for UTF-32 + cp bebom.utf32be.raw bebom.utf32be && + test_must_fail git add bebom.utf32be 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-32BE" err.out && + + cp lebom.utf32le.raw lebom.utf32be && + test_must_fail git add lebom.utf32be 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-32BE" err.out && + + cp bebom.utf32be.raw bebom.utf32le &&t/t0028-working-tree-encoding.sh line 107
Quoted 10 lines
t/t0028-working-tree-encoding.sh +10
-0+ test_must_fail git add bebom.utf32le 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-32LE" err.out && + + cp lebom.utf32le.raw lebom.utf32le && + test_must_fail git add lebom.utf32le 2>err.out && + test_i18ngrep "fatal: BOM is prohibited .* UTF-32LE" err.out && + + # cleanup + git reset --hard HEAD +'Clumping all these checks into the same test makes it difficult to determine which one failed (if one does fail). Also, the amount of copy/paste code is easy to get wrong and a bit onerous to review. Automating via for-loops would address these concerns. For instance, to check all combinations (not just 8 combinations tested above):
for i in 16be 16le 32be 32le do for j in 16be 16le 32be 32le do test_expect_success "check prohibited UTF BOM utf$i/utf$j" ' test_when_finished "git reset --hard HEAD" && cp bebom.utf$i.raw bebom.utf$j && test_must_fail git add bebom.utf$j 2>err.out && J=$(echo $j | tr bel BEL) && test_i18ngrep "fatal: BOM is prohibited .* UTF-$J" err.out ' done doneAlternately, the test could be moved to a function and called for each combination:
check_prohibited_bom () { i=$1 j=$2 test_expect_success "check prohibited UTF BOM utf$i/utf$j" ' ... ' } check_prohibited_bom 16be 16be check_prohibited_bom 16be 16le ...
Quoted 1 line
Same comment regarding copy/paste in tests below.
In general I share your concern about code duplication. However, I don't want be "too clever" in testing code as it becomes hard to read it. Therefore, I implemented a loop over "16 32" which is a good compromise I think.
Thanks you very much for this thorough review,
Lars
Re: [PATCH v8 5/7] convert: add 'working-tree-encoding' attribute
Quoted 10 lines · Lars Schneider
Quoted 7 lines · Eric Sunshine
On Sat, Feb 24, 2018 at 11:27 AM, <[email protected]> wrote:
The above paragraph is giving an example of the scenario described in the paragraph above it. It might, therefore, be clearer to start this paragraph with "For example,...". Also, as an aid to non-Windows developers, it might be helpful to introduce ".proj" files as "Microsoft Visual Studio*.projfiles...".Agreed. How about this?
Quoted 7 lines
For example, Microsoft Visual Studio resources files (
*.rc) or PowerShell script files (*.ps1) are sometimes encoded in UTF-16. If you declare*.ps1as files as UTF-16 and you addfoo.ps1with aworking-tree-encodingenabled Git client, thenfoo.ps1will be stored as UTF-8 internally. A client withoutworking-tree-encodingsupport will checkoutfoo.ps1as UTF-8 encoded file. This will typically cause trouble for the users of this file.
Since "*.proj" is mentioned a number of times in paragraphs below this one, perhaps just stick with it instead of introducing "*.rc" and "*.ps1", which don't necessarily add value. Alternately, if you use .rc and .ps1, then change the remaining .proj references to follow suit.
Quoted 23 lines
Quoted 15 lines
Quoted 4 lines
convert.c +3
-0+ }else if (is_missing_required_utf_bom (enc->name, src, src_len)) { +const char *error_msg =_ ( +"BOM is required in '%s' if encoded as %s" );convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 326
Quoted 10 lines
convert.c +10
-0+const char *advise_msg =_ ( +"The file '%s' is missing a byte order mark (BOM). " +"Please use %sBE or %sLE (depending on the byte order) " +"as working-tree-encoding." ); +advise (advise_msg, path, enc->name, enc->name); +if (conv_flags &CONV_WRITE_OBJECT ) +die (error_msg, path, enc->name); +else +error (error_msg, path, enc->name); + }Quoted 7 lines
For a function which presumably is agnostic about the working-tree encoding (supporting whatever iconv does), it nevertheless has unusually intimate knowledge about UTF BOMs, in general, and the internals of
has_prohibited_utf_bom()andis_missing_required_utf_bom(), in particular. It might be cleaner, and would simplify this function, if all this UTF BOM-specific gunk was moved elsewhere and generalized into a "validate conversion" function.
Quoted 8 lines
Agreed! How about this?
/* * If we convert to an UTF encoding, then check for common BOM errors. */ if (!memcmp("UTF-", enc, 4)) if (has_utf_bom_error(path, enc, src, src_len, die_on_error)) return 0;
Better. The comment merely repeats the code, which is clear in its intent, thus doesn't really add value (but that's a minor point).
I'd probably generalize it further to a "validate conversion" function (which itself would have this UTF-specific knowledge), but that's a matter of taste and can be done later when/if other types of validation requirements arise.
PATCH v8 6/7 convert: add tracing for 'working-tree-encoding' attribute
In reply to [email protected]From: Lars Schneider <[email protected]>
Add the GIT_TRACE_WORKING_TREE_ENCODING environment variable to enable tracing for content that is reencoded with the 'working-tree-encoding' attribute. This is useful to debug encoding issues.
--- convert.c | 25 +++++++++++++++++++++++++ t/t0028-working-tree-encoding.sh | 2 ++ 2 files changed, 27 insertions(+)
convert.c +25 -0
@@ -266,6 +266,29 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, } +static void trace_encoding (const char *context,const char *path, +const char *encoding,const char *buf,size_t len) +{ +static struct trace_key coe =TRACE_KEY_INIT (WORKING_TREE_ENCODING ); +struct strbuf trace =STRBUF_INIT ; +int i; + +strbuf_addf (&trace,"%s (%s, considered %s):\n" , context, path, encoding); +for (i =0 ; i < len && buf; ++i) { +strbuf_addf ( + &trace,"| \e[2m%2i:\e[0m %2x \e[2m%c\e[0m%c" , + i, + (unsigned char ) buf[i], + (buf[i] >32 && buf[i] <127 ? buf[i] :' ' ), + ((i+1 ) %8 && (i+1 ) < len ?' ' :'\n' ) + ); + } +strbuf_addchars (&trace,'\n' ,1 ); + +trace_strbuf (&coe, &trace); +strbuf_release (&trace); +} + static struct encoding { const char *name; struct encoding *next;
@@ -325,6 +348,7 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,error (error_msg, path, enc->name); } +trace_encoding ("source" , path, enc->name, src, src_len); dst =reencode_string_len (src, src_len, default_encoding, enc->name, &dst_len);if (!dst) {
@@ -340,6 +364,7 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,else error (msg, path, enc->name, default_encoding); } +trace_encoding ("destination" , path, default_encoding, dst, dst_len);strbuf_attach (buf, dst, dst_len, dst_len +1 );return 1 ;
t/t0028-working-tree-encoding.sh +2 -0
@@ -4,6 +4,8 @@ test_description='working-tree-encoding conversion via gitattributes' . ./test-lib.sh +GIT_TRACE_WORKING_TREE_ENCODING=1 && export GIT_TRACE_WORKING_TREE_ENCODING + test_expect_success 'setup test repo' ' git config core.eol lf &&
Signature · 1 line
2.16.1
PATCH v8 7/7 convert: add round trip check based on 'core.checkRoundtripEncoding'
In reply to [email protected]From: Lars Schneider <[email protected]>
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
--- Documentation/config.txt | 6 ++++ Documentation/gitattributes.txt | 8 +++++ config.c | 5 +++ convert.c | 74 ++++++++++++++++++++++++++++++++++++++++ convert.h | 1 + environment.c | 1 + t/t0028-working-tree-encoding.sh | 41 ++++++++++++++++++++++ 7 files changed, 136 insertions(+)
Documentation/config.txt +6 -0
@@ -530,6 +530,12 @@ core.autocrlf:: This variable can be set to 'input', in which case no output conversion is performed. +core.checkRoundtripEncoding:: + A comma separated list of encodings that Git performs UTF-8 round + trip checks on if they are used in an `working-tree-encoding` + attribute (see linkgit:gitattributes[5]). The default value is + `SHIFT-JIS`. + core.symlinks:: If false, symbolic links are checked out as small plain files that contain the link text. linkgit:git-update-index[1] and
Documentation/gitattributes.txt +8 -0
@@ -310,6 +310,14 @@ number of pitfalls: internal contents as UTF-8 and try to convert it to UTF-16 on checkout. That operation will fail and cause an error. +- Reencoding content to non-UTF encodings can cause errors as the + conversion might not be UTF-8 round trip safe. If you suspect your + encoding to not be round trip safe, then add it to + `core.checkRoundtripEncoding` to make Git check the round trip + encoding (see linkgit:git-config[1]). SHIFT-JIS (Japanese character + set) is known to have round trip issues with UTF-8 and is checked by + default. + - Reencoding content requires resources that might slow down certain Git operations (e.g 'git checkout' or 'git add').
config.c +5 -0
@@ -1172,6 +1172,11 @@ static int git_default_core_config(const char *var, const char *value)return 0 ; } +if (!strcmp (var,"core.checkroundtripencoding" )) { + check_roundtrip_encoding =xstrdup (value); +return 0 ; + } +if (!strcmp (var,"core.notesref" )) { notes_ref_name =xstrdup (value);return 0 ;
convert.c +74 -0
86 lines not on this page · raw message
convert.h +1 -0
@@ -56,6 +56,7 @@ struct delayed_checkout { };extern enum eol core_eol; +extern char *check_roundtrip_encoding;extern const char *get_cached_convert_stats_ascii (const struct index_state *istate,const char *path);extern const char *get_wt_convert_stats_ascii (const char *path);
environment.c +1 -0
@@ -50,6 +50,7 @@ int check_replace_refs = 1;char *git_replace_ref_base;enum eol core_eol =EOL_UNSET ;int global_conv_flags_eol =CONV_EOL_RNDTRP_WARN ; +char *check_roundtrip_encoding ="SHIFT-JIS" ;unsigned whitespace_rule_cfg =WS_DEFAULT_RULE ;enum branch_track git_branch_track =BRANCH_TRACK_REMOTE ;enum rebase_setup_type autorebase =AUTOREBASE_NEVER ;
t/t0028-working-tree-encoding.sh +41 -0
45 lines not on this page · raw message
Signature · 1 line
2.16.1
Re: [PATCH v8 7/7] convert: add round trip check based on 'core.checkRoundtripEncoding'
Quoted 18 lines · On Sat, Feb 24, 2018 at 11:28 AM, <[email protected]> wrote:
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]>---
convert.c static void trace_encoding(const char *context, const char *path, · line 297
Quoted 9 lines
convert.c +9
-0@@ -289,6 +289,39 @@ static void trace_encoding(const char *context, const char *path, +static int check_roundtrip (const char * enc_name) +{ +/* +* check_roundtrip_encoding contains a string of space and/or +* comma separated encodings (eg. "UTF-16, ASCII, CP1125"). +* Search for the given encoding in that string. +*/ +const char *found =strcasestr (check_roundtrip_encoding, enc_name); +const char *next = found +strlen (enc_name);
Is this pointer arithmetic undefined behavior (according to the C standard) if NULL is returned by strcasestr()? If so, how comfortable are we with this? Perhaps if you add an 'if' into the mix, you can avoid it:
if (found) {
const char *next = found + strlen(enc_name);
return ...long complicated expression...;
}
return false;convert.c static void trace_encoding(const char *context, const char *path, · line 310
Quoted 10 lines
convert.c +10
-0+int len =strlen (check_roundtrip_encoding); + return (found && ( + /* + * check that the found encoding is at the + * beginning of check_roundtrip_encoding or + * that it is prefixed with a space or comma + */ + found == check_roundtrip_encoding || ( + found > check_roundtrip_encoding && + (*(found-1) == ' ' || *(found-1) == ',')
Although the 'found > check_roundtrip_encoding' expression is effectively dead code in this case, it does document that the programmer didn't just blindly use '*(found-1)' without taking boundary conditions into consideration.
(It's dead code because, at this point, we know that strcasestr() didn't return NULL and we know that 'found' doesn't equal 'check_roundtrip_encoding', therefore it _must_ be greater than 'check_roundtrip_encoding'.)
Just an observation; not a request to remove the dead code since it has some documentation value.
Quoted 18 lines
convert.c +17
-0+ ) + ) && ( +/* +* check that the found encoding is at the +* end of check_roundtrip_encoding or +* that it is suffixed with a space or comma +*/ + next == check_roundtrip_encoding + len || ( + next < check_roundtrip_encoding + len && + (*next ==' ' || *next ==',' ) + ) + )); +}@@ -366,6 +399,47 @@ static int encode_to_git(const char *path, const char *src, size_t src_len, +if ((conv_flags &CONV_WRITE_OBJECT ) &&check_roundtrip (enc->name)) { + re_src =reencode_string_len (dst, dst_len, + enc->name, default_encoding, + &re_src_len);
convert.c static int encode_to_git(const char *path, const char *src, size_t src_len, · line 412
Quoted 10 lines
convert.c +10
-0+ +trace_printf ("Checking roundtrip encoding for %s...\n" , enc->name); +trace_encoding ("reencoded source" , path, enc->name, + re_src, re_src_len); + +if (!re_src || src_len != re_src_len || +memcmp (src, re_src, src_len)) { +const char * msg =_ ("encoding '%s' from %s to %s and " +"back is not the same" ); +die (msg, path, enc->name, default_encoding);
Last two arguments need to be swapped.
Quoted 10 lines
convert.c +5
-0+ } + +free (re_src); + } +strbuf_attach (buf, dst, dst_len, dst_len +1 );return 1 ; }
t/t0028-working-tree-encoding.sh test_expect_success 'error if encoding garbage is already in Git' ' · line 233
Quoted 9 lines
t/t0028-working-tree-encoding.sh +9
-0@@ -225,4 +225,45 @@ test_expect_success 'error if encoding garbage is already in Git' ' +test_expect_success 'check roundtrip encoding' ' + text="hallo there!\nroundtrip test here!" && + printf "$text" | iconv -f UTF-8 -t SHIFT-JIS >roundtrip.shift && + printf "$text" | iconv -f UTF-8 -t UTF-16 >roundtrip.utf16 && + echo "*.shift text working-tree-encoding=SHIFT-JIS" >>.gitattributes && + + # SHIFT-JIS encoded files are round-trip checked by default... + GIT_TRACE=1 git add .gitattributes roundtrip.shift 2>&1 >/dev/null | + grep "Checking roundtrip encoding for SHIFT-JIS" &&
Why redirect to /dev/null? If something does go wrong somewhere, the more output available for debugging the problem, the better, so throwing it away unnecessarily seems contraindicated.
t/t0028-working-tree-encoding.sh test_expect_success 'error if encoding garbage is already in Git' ' · line 243
Quoted 7 lines
t/t0028-working-tree-encoding.sh +7
-0+ git reset && + + # ... unless we overwrite the Git config! + test_config core.checkRoundtripEncoding "garbage" && + ! GIT_TRACE=1 git add .gitattributes roundtrip.shift 2>&1 >/dev/null | + grep "Checking roundtrip encoding for SHIFT-JIS" && + test_unconfig core.checkRoundtripEncoding &&
The "unconfig" won't take place if the test fails. Instead of test_config/test_unconfig, you could use '-c' to set the config transiently for the git-add operation:
! GIT_TRACE=1 git -c core.checkRoundtripEncoding=garbage add ...
Quoted 14 lines
t/t0028-working-tree-encoding.sh +14
-0+ git reset && + + # UTF-16 encoded files should not be round-trip checked by default... + ! GIT_TRACE=1 git add roundtrip.utf16 2>&1 >/dev/null | + grep "Checking roundtrip encoding for UTF-16" && + git reset && + + # ... unless we tell Git to check it! + test_config_global core.checkRoundtripEncoding "UTF-16, UTF-32" && + GIT_TRACE=1 git add roundtrip.utf16 2>&1 >/dev/null | + grep "Checking roundtrip encoding for UTF-16" && + git reset && + + # ... unless we tell Git to check it!
t/t0028-working-tree-encoding.sh test_expect_success 'error if encoding garbage is already in Git' ' · line 267
Quoted 10 lines
t/t0028-working-tree-encoding.sh +10
-0+ # (here we also check that the casing of the encoding is irrelevant) + test_config_global core.checkRoundtripEncoding "UTF-32, utf-16" && + GIT_TRACE=1 git add roundtrip.utf16 2>&1 >/dev/null | + grep "Checking roundtrip encoding for UTF-16" && + git reset && + + # cleanup + rm roundtrip.shift roundtrip.utf16 && + git reset --hard HEAD +'
Same comment as for patch 5/7: This cleanup won't happen if the test fails. Instead, use test_when_finished() earlier in the test:
test_expect_success 'check roundtrip encoding' '
test_when_finished "rm -f roundtrip.shift roundtrip.utf16; git
reset --hard HEAD" &&
...
Re: [PATCH v8 7/7] convert: add round trip check based on 'core.checkRoundtripEncoding'
Quoted 37 lines · Eric Sunshine
Quoted 18 lines · On Sat, Feb 24, 2018 at 11:28 AM, <[email protected]> wrote:
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]>---
convert.c static void trace_encoding(const char *context, const char *path, · line 297
Quoted 9 lines
convert.c +9
-0@@ -289,6 +289,39 @@ static void trace_encoding(const char *context, const char *path, +static int check_roundtrip (const char * enc_name) +{ +/* +* check_roundtrip_encoding contains a string of space and/or +* comma separated encodings (eg. "UTF-16, ASCII, CP1125"). +* Search for the given encoding in that string. +*/ +const char *found =strcasestr (check_roundtrip_encoding, enc_name); +const char *next = found +strlen (enc_name);Is this pointer arithmetic undefined behavior (according to the C standard) if NULL is returned by
strcasestr()? If so, how comfortable are we with this? Perhaps if you add an 'if' into the mix, you can avoid it:
Quoted 5 lines
if (found) {
const char *next = found + strlen(enc_name);
return ...long complicated expression...;
}
return false;OK. I've fixed it this way:
if (!found) return 0;
[...]
Quoted 8 lines
convert.c static int encode_to_git(const char *path, const char *src, size_t src_len, · line 437
Quoted 6 lines
convert.c +6
-0+ +if (!re_src || src_len != re_src_len || +memcmp (src, re_src, src_len)) { +const char * msg =_ ("encoding '%s' from %s to %s and " +"back is not the same" ); +die (msg, path, enc->name, default_encoding);Last two arguments need to be swapped.
Hm. Are you sure? I think it is correct as it is. We are in encode_to_git() here and that means we encode *to* "default encoding", no?
Quoted 15 lines
Quoted 15 lines
convert.c +5
-0+ } + +free (re_src); + } +strbuf_attach (buf, dst, dst_len, dst_len +1 );return 1 ;}
t/t0028-working-tree-encoding.sh +5
-0@@ -225,4 +225,45 @@ test_expect_success 'error if encoding garbage is already in Git' ' +test_expect_success 'check roundtrip encoding' ' + text="hallo there!\nroundtrip test here!" && + printf "$text" | iconv -f UTF-8 -t SHIFT-JIS >roundtrip.shift && + printf "$text" | iconv -f UTF-8 -t UTF-16 >roundtrip.utf16 && + echo "*.shift text working-tree-encoding=SHIFT-JIS" >>.gitattributes &&
Quoted 8 lines
t/t0028-working-tree-encoding.sh test_expect_success 'error if encoding garbage is already in Git' ' · line 236
Quoted 4 lines
t/t0028-working-tree-encoding.sh +4
-0+ + # SHIFT-JIS encoded files are round-trip checked by default... + GIT_TRACE=1 git add .gitattributes roundtrip.shift 2>&1 >/dev/null | + grep "Checking roundtrip encoding for SHIFT-JIS" &&Why redirect to
/dev/null? If something does go wrong somewhere, the more output available for debugging the problem, the better, so throwing it away unnecessarily seems contraindicated.
OK!
Quoted 8 lines
t/t0028-working-tree-encoding.sh test_expect_success 'error if encoding garbage is already in Git' ' · line 243
Quoted 7 lines
t/t0028-working-tree-encoding.sh +7
-0+ git reset && + + # ... unless we overwrite the Git config! + test_config core.checkRoundtripEncoding "garbage" && + ! GIT_TRACE=1 git add .gitattributes roundtrip.shift 2>&1 >/dev/null | + grep "Checking roundtrip encoding for SHIFT-JIS" && + test_unconfig core.checkRoundtripEncoding &&
Quoted 5 lines
The "unconfig" won't take place if the test fails. Instead of test_config/test_unconfig, you could use '-c' to set the config transiently for the git-add operation:
! GIT_TRACE=1 git -c core.checkRoundtripEncoding=garbage add ...
Agreed. Although test_config (in t/test-lib-functions.sh) automatically unsets itself after the test is over.
Quoted 28 lines
Quoted 14 lines
t/t0028-working-tree-encoding.sh +14
-0+ git reset && + + # UTF-16 encoded files should not be round-trip checked by default... + ! GIT_TRACE=1 git add roundtrip.utf16 2>&1 >/dev/null | + grep "Checking roundtrip encoding for UTF-16" && + git reset && + + # ... unless we tell Git to check it! + test_config_global core.checkRoundtripEncoding "UTF-16, UTF-32" && + GIT_TRACE=1 git add roundtrip.utf16 2>&1 >/dev/null | + grep "Checking roundtrip encoding for UTF-16" && + git reset && + + # ... unless we tell Git to check it!t/t0028-working-tree-encoding.sh test_expect_success 'error if encoding garbage is already in Git' ' · line 267
Quoted 10 lines
t/t0028-working-tree-encoding.sh +10
-0+ # (here we also check that the casing of the encoding is irrelevant) + test_config_global core.checkRoundtripEncoding "UTF-32, utf-16" && + GIT_TRACE=1 git add roundtrip.utf16 2>&1 >/dev/null | + grep "Checking roundtrip encoding for UTF-16" && + git reset && + + # cleanup + rm roundtrip.shift roundtrip.utf16 && + git reset --hard HEAD +'Same comment as for patch 5/7: This cleanup won't happen if the test fails. Instead, use
test_when_finished()earlier in the test:
Quoted 4 lines
test_expect_success'check roundtrip encoding' '
test_when_finished"rm -f roundtrip.shift roundtrip.utf16; git
reset --hard HEAD" &&...
Agreed!
Thanks you,
Lars
Re: [PATCH v8 7/7] convert: add round trip check based on 'core.checkRoundtripEncoding'
Quoted 8 lines · Lars Schneider
Quoted 8 lines · Eric Sunshine
Quoted 5 lines · On Sat, Feb 24, 2018 at 11:28 AM, <[email protected]> wrote:
convert.c +5
-0+if (!re_src || src_len != re_src_len || +memcmp (src, re_src, src_len)) { +const char * msg =_ ("encoding '%s' from %s to %s and " +"back is not the same" ); +die (msg, path, enc->name, default_encoding);
Quoted 4 lines
Quoted 1 line
Last two arguments need to be swapped.
Hm. Are you sure? I think it is correct as it is. We are in
encode_to_git()here and that means we encode *to* "default encoding", no?
Okay. I guess I was just looking at the most recent reencode_string_len() -- and maybe overlooked the "and back" -- and was thinking that this error message applied directly to it, but I see your point about the error saying something about encode_to_git() overall, in which case I agree with you.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
t/t0028-working-tree-encoding.sh +4
-0+ test_config core.checkRoundtripEncoding "garbage" && + ! GIT_TRACE=1 git add .gitattributes roundtrip.shift 2>&1 >/dev/null | + grep "Checking roundtrip encoding for SHIFT-JIS" && + test_unconfig core.checkRoundtripEncoding &&
Quoted 8 lines
Quoted 5 lines
The "unconfig" won't take place if the test fails. Instead of test_config/test_unconfig, you could use '-c' to set the config transiently for the git-add operation:
! GIT_TRACE=1 git -c core.checkRoundtripEncoding=garbage add ...
Agreed. Although
test_config(int/test-lib-functions.sh) automatically unsets itself after the test is over.
Yep, so you could get by with that alone. The test_unconfig() simply isn't needed.