convert: add support for different encodings
v11 cover convert: add support for different encodings
From: Lars Schneider <[email protected]>
Hi,
Patches 1-5,9 are preparation and helper functions. Patch 6-8,10 are the actual change. Patch 8 is new.
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 v10:
* rename startscase_with() to istarts_with() (Duy)
* validate_encoding() advises the canonical form of the UTF encoding name to the user (Junio)
--> I added it this as a separate commit that you could be dropped
if desired by the reviewers.
* fix documentation for roundtrip check (Junio)
* use isspace() to check whitespace/tab delimiter
in core.checkRoundtripEncoding (Junio)
* remove dead code in roundtrip check (Junio)
* fix invalid # in comment (Eric)
* detect UTF8 and UTF-8 as default encoding (Eric)
* make asterisk stick to the variable, not type (Junio)
* print an error if "w-t-e" does not have a proper value (Junio) --> BTW: I noticed that the attribute is not set to "git_attr__false" even if I define "-working-tree-encoding". I haven't investigated further yet. Might that be a bug? If yes, then this should be addresses in a separate patch series.
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]/
v8: https://public-inbox.org/git/[email protected]/
v9: https://public-inbox.org/git/[email protected]/
v10: https://public-inbox.org/git/[email protected]/
Base Ref:
Web-Diff: https://github.com/larsxschneider/git/commit/afc02ce2e0
Checkout: git fetch https://github.com/larsxschneider/git encoding-v11 && git checkout afc02ce2e0
### Interdiff (v10..v11):
Documentation/config.txt +4 -4
@@ -531,10 +531,10 @@ core.autocrlf:: 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`. + A comma and/or whitespace 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
convert.c +27 -18
93 lines not on this page · raw message
git-compat-util.h +1 -1
@@ -455,7 +455,7 @@ extern void (*get_warn_routine(void))(const char *warn, va_list params);extern void set_die_is_recursing_routine (int (*routine)(void ));extern int starts_with (const char *str,const char *prefix); -extern int startscase_with (const char *str,const char *prefix); +extern int istarts_with (const char *str,const char *prefix);/* * If the string "str" begins with the string found in "prefix", return 1.
strbuf.c +1 -1
@@ -11,7 +11,7 @@ int starts_with(const char *str, const char *prefix)return 0 ; } -int startscase_with (const char *str,const char *prefix) +int istarts_with (const char *str,const char *prefix) {for (; ; str++, prefix++)if (!*prefix)
t/t0028-working-tree-encoding.sh +24 -7
@@ -68,7 +68,7 @@ do test_when_finished "git reset --hard HEAD" && echo "*.utf${i}be text working-tree-encoding=utf-${i}be" >>.gitattributes && - echo "*.utf${i}le text working-tree-encoding=utf-${i}le" >>.gitattributes && + echo "*.utf${i}le text working-tree-encoding=utf-${i}LE" >>.gitattributes && # Here we add a UTF-16 (resp. UTF-32) files with BOM (big/little-endian) # but we tell Git to treat it as UTF-16BE/UTF-16LE (resp. UTF-32).
@@ -76,18 +76,22 @@ do cp bebom.utf${i}be.raw bebom.utf${i}be && test_must_fail git add bebom.utf${i}be 2>err.out && test_i18ngrep "fatal: BOM is prohibited .* utf-${i}be" err.out && + test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out && cp lebom.utf${i}le.raw lebom.utf${i}be && test_must_fail git add lebom.utf${i}be 2>err.out && test_i18ngrep "fatal: BOM is prohibited .* utf-${i}be" err.out && + test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out && cp bebom.utf${i}be.raw bebom.utf${i}le && test_must_fail git add bebom.utf${i}le 2>err.out && - test_i18ngrep "fatal: BOM is prohibited .* utf-${i}le" err.out && + test_i18ngrep "fatal: BOM is prohibited .* utf-${i}LE" err.out && + test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out && cp lebom.utf${i}le.raw lebom.utf${i}le && test_must_fail git add lebom.utf${i}le 2>err.out && - test_i18ngrep "fatal: BOM is prohibited .* utf-${i}le" err.out + test_i18ngrep "fatal: BOM is prohibited .* utf-${i}LE" err.out && + test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out ' test_expect_success "check required UTF-${i} BOM" '
@@ -98,10 +102,12 @@ do cp nobom.utf${i}be.raw nobom.utf${i} && test_must_fail git add nobom.utf${i} 2>err.out && test_i18ngrep "fatal: BOM is required .* utf-${i}" err.out && + test_i18ngrep "use UTF-${i}BE or UTF-${i}LE" err.out && cp nobom.utf${i}le.raw nobom.utf${i} && test_must_fail git add nobom.utf${i} 2>err.out && - test_i18ngrep "fatal: BOM is required .* utf-${i}" err.out + test_i18ngrep "fatal: BOM is required .* utf-${i}" err.out && + test_i18ngrep "use UTF-${i}BE or UTF-${i}LE" err.out ' test_expect_success "eol conversion for UTF-${i} encoded files on checkout" '
@@ -143,9 +149,20 @@ done test_expect_success 'check unsupported encodings' ' test_when_finished "git reset --hard HEAD" && - echo "*.nothing text working-tree-encoding=" >>.gitattributes && - printf "nothing" >t.nothing && - git add t.nothing && + echo "*.set text working-tree-encoding" >>.gitattributes && + printf "set" >t.set && + git add t.set 2>err.out && + test_i18ngrep "error: working-tree-encoding attribute requires a value" err.out && + + echo "*.unset text -working-tree-encoding" >>.gitattributes && + printf "unset" >t.unset && + git add t.unset 2>err.out && + test_i18ngrep "error: working-tree-encoding attribute requires a value" err.out && + + echo "*.empty text working-tree-encoding=" >>.gitattributes && + printf "empty" >t.empty && + git add t.empty 2>err.out && + test_i18ngrep "error: working-tree-encoding attribute requires a value" err.out && echo "*.garbage text working-tree-encoding=garbage" >>.gitattributes && printf "garbage" >t.garbage &&
### Patches
Lars Schneider (10):
strbuf: remove unnecessary NUL assignment in xstrdup_tolower()
strbuf: add xstrdup_toupper()
strbuf: add a case insensitive starts_with()
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: check for detectable errors in UTF encodings
convert: advise canonical UTF encoding names
convert: add tracing for 'working-tree-encoding' attribute
convert: add round trip check based on 'core.checkRoundtripEncoding'
Documentation/config.txt | 6 + Documentation/gitattributes.txt | 88 +++++++++++++ config.c | 5 + convert.c | 277 ++++++++++++++++++++++++++++++++++++++- convert.h | 2 + environment.c | 1 + git-compat-util.h | 1 + sha1_file.c | 2 +- strbuf.c | 22 +++- strbuf.h | 1 + t/t0028-working-tree-encoding.sh | 247 ++++++++++++++++++++++++++++++++++ utf8.c | 39 ++++++ utf8.h | 28 ++++ 13 files changed, 716 insertions(+), 3 deletions(-) create mode 100755 t/t0028-working-tree-encoding.sh
base-commit: 8a2f0888555ce46ac87452b194dec5cb66fb1417 --
2.16.2
PATCH v11 1/10 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.2
PATCH v11 2/10 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.2
PATCH v11 3/10 strbuf: add a case insensitive starts_with()
In reply to [email protected]From: Lars Schneider <[email protected]>
Check in a case insensitive manner if one string is a prefix of another string.
This function is used in a subsequent commit.
--- git-compat-util.h | 1 + strbuf.c | 9 +++++++++ 2 files changed, 10 insertions(+)
git-compat-util.h +1 -0
@@ -455,6 +455,7 @@ extern void (*get_warn_routine(void))(const char *warn, va_list params);extern void set_die_is_recursing_routine (int (*routine)(void ));extern int starts_with (const char *str,const char *prefix); +extern int istarts_with (const char *str,const char *prefix);/* * If the string "str" begins with the string found in "prefix", return 1.
strbuf.c +9 -0
@@ -11,6 +11,15 @@ int starts_with(const char *str, const char *prefix)return 0 ; } +int istarts_with (const char *str,const char *prefix) +{ +for (; ; str++, prefix++) +if (!*prefix) +return 1 ; +else if (tolower (*str) !=tolower (*prefix)) +return 0 ; +} +int skip_to_optional_arg_default (const char *str,const char *prefix,const char **arg,const char *def) {
Signature · 1 line
2.16.2
PATCH v11 4/10 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 | 26 ++++++++++++++++++++++++++ utf8.h | 9 +++++++++ 2 files changed, 35 insertions(+)
utf8.c +26 -0
@@ -538,6 +538,32 @@ 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 ( + (!strcasecmp (enc,"UTF-16BE" ) || !strcasecmp (enc,"UTF-16LE" ) || + !strcasecmp (enc,"UTF16BE" ) || !strcasecmp (enc,"UTF16LE" )) && + (has_bom_prefix (data, len, utf16_be_bom,sizeof (utf16_be_bom)) || +has_bom_prefix (data, len, utf16_le_bom,sizeof (utf16_le_bom))) + ) || ( + (!strcasecmp (enc,"UTF-32BE" ) || !strcasecmp (enc,"UTF-32LE" ) || + !strcasecmp (enc,"UTF32BE" ) || !strcasecmp (enc,"UTF32LE" )) && + (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); +/* +* 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 +*/ +int has_prohibited_utf_bom (const char *enc,const char *data,size_t len); +#endif
Signature · 1 line
2.16.2
PATCH v11 5/10 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 | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+)
utf8.c +13 -0
@@ -564,6 +564,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 ( + (!strcasecmp (enc,"UTF-16" ) || !strcasecmp (enc,"UTF16" )) && + !(has_bom_prefix (data, len, utf16_be_bom,sizeof (utf16_be_bom)) || +has_bom_prefix (data, len, utf16_le_bom,sizeof (utf16_le_bom))) + ) || ( + (!strcasecmp (enc,"UTF-32" ) || !strcasecmp (enc,"UTF32" )) && + !(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 +19 -0
@@ -79,4 +79,23 @@ 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]. +* +* Therefore, strictly requiring a BOM seems to be the safest option for +* content in Git. +* +* [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.2
PATCH v11 6/10 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 | 80 ++++++++++++++++++++++ convert.c | 114 ++++++++++++++++++++++++++++++- convert.h | 1 + sha1_file.c | 2 +- t/t0028-working-tree-encoding.sh | 144 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 339 insertions(+), 2 deletions(-) create mode 100755 t/t0028-working-tree-encoding.sh
Documentation/gitattributes.txt +80 -0
@@ -272,6 +272,86 @@ 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. + + 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. + + If a Git client, that does not support the `working-tree-encoding` + attribute, adds a new file `bar.ps1`, then `bar.ps1` 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 '*.ps1' files are +UTF-16 encoded with byte order mark (BOM) and you want Git to perform +automatic line ending conversion based on your platform. + +------------------------ +*.ps1 text working-tree-encoding=UTF-16 +------------------------ + +Use the following attributes if your '*.ps1' 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. + +------------------------ +*.ps1 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.ps1 +------------------------ + + `ident` ^^^^^^^
convert.c +113 -1
174 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 +144 -0
144 lines not on this page · raw message
Signature · 1 line
2.16.2
Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute
Quoted 4 lines · [email protected] writes:
convert.c +3
-0+static const char *default_encoding ="UTF-8" ; + +static const char *git_path_check_encoding (struct attr_check_item *check)
convert.c static int ident_to_worktree(const char *path, const char *src, size_t len, · line 1064
Quoted 10 lines
convert.c +10
-0+{ +const char *value = check->value; + +if (ATTR_UNSET (value) || !strlen (value)) +return NULL ; + +if (ATTR_TRUE (value) ||ATTR_FALSE (value)) { +error (_ ("working-tree-encoding attribute requires a value" )); +return NULL ; + }
Hmph, so we decide to be loud but otherwise ignore an undefined configuration? Shouldn't we rather die instead to avoid touching the user data in unexpected ways?
convert.c static int ident_to_worktree(const char *path, const char *src, size_t len, · line 1068
Quoted 4 lines
convert.c +4
-0+ +/* Don't encode to the default encoding */ +if (!strcasecmp (value, default_encoding)) +return NULL ;
Is this an optimization to avoid "recode one encoding to the same encoding" no-op overhead? We already have the optimization in the same spirit in may existing codepaths that has nothing to do with w-t-e, and I think we should share the code. Two pieces of thought comes to mind.
One is a lot smaller in scale: Is same_encoding() sufficient for this callsite instead of strcasecmp()?
The other one is a lot bigger: Looking at all the existing callers of same_encoding() that call reencode_string() when it returns false, would it make sense to drop same_encoding() and move the optimization to reencode_string() instead?
I suspect that the answer to the smaller one is "yes, and even if not, it should be easy to enhance/extend same_encoding() to make it do what we want it to, and such a change will benefit even existing callers." The answer to the larger one is likely "the optimization is not about skipping only reencode_string() call but other things are subtly different among callers of same_encoding(), so such a refactoring would not be all that useful."
The above still holds for the code after 10/10 touches this part.
Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute
Quoted 15 lines · Junio C Hamano
Quoted 10 lines · [email protected] writes:
convert.c +9
-0+static const char *default_encoding ="UTF-8" ; + +static const char *git_path_check_encoding (struct attr_check_item *check) +{ +const char *value = check->value; + +if (ATTR_UNSET (value) || !strlen (value)) +return NULL ; +
Quoted 8 lines
convert.c static int ident_to_worktree(const char *path, const char *src, size_t len, · line 1064
Quoted 4 lines
convert.c +4
-0+if (ATTR_TRUE (value) ||ATTR_FALSE (value)) { +error (_ ("working-tree-encoding attribute requires a value" )); +return NULL ; + }Hmph, so we decide to be loud but otherwise ignore an undefined configuration? Shouldn't we rather die instead to avoid touching the user data in unexpected ways?
OK.
Quoted 7 lines
convert.c static int ident_to_worktree(const char *path, const char *src, size_t len, · line 1068
Quoted 4 lines
convert.c +4
-0+ +/* Don't encode to the default encoding */ +if (!strcasecmp (value, default_encoding)) +return NULL ;Is this an optimization to avoid "recode one encoding to the same encoding" no-op overhead?
Correct.
Quoted 7 lines
We already have the optimization in the
same spirit in may existing codepaths that has nothing to do with w-t-e, and I think we should share the code. Two pieces of thought comes to mind.One is a lot smaller in scale: Is
same_encoding()sufficient for this callsite instead ofstrcasecmp()?
Yes!
Quoted 5 lines
The other one is a lot bigger: Looking at all the existing callers of
same_encoding()that callreencode_string()when it returns false, would it make sense to dropsame_encoding()and move the optimization toreencode_string()instead?
Quoted 7 lines
I suspect that the answer to the smaller one is "yes, and even if not, it should be easy to enhance/extend
same_encoding()to make it do what we want it to, and such a change will benefit even existing callers." The answer to the larger one is likely "the optimization is not about skipping onlyreencode_string()call but other things are subtly different among callers ofsame_encoding(), so such a refactoring would not be all that useful."
I agree. reencode_string() would need to signal 3 cases:
1. reencode performed
2. reencode not necessary
3. reencode failed
We could model "reencode not necessary" as "char *in == char *return". However, I think this should be tackled in a separate series.
Thanks
Lars
Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute
In reply to [email protected]Some comments inline
Quoted 10 lines · On Fri, Mar 09, 2018 at 06:35:32PM +0100, [email protected] wrote:
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
Minor comment:
"Git converts the content"
Everywhere else (?) "encodes or reencodes" is used. "Git reencodes the content" may be more consistent.
[No comments on the .gitattributes]
Quoted 22 lines
convert.c +6
-0@@ -7,6 +7,7 @@#include "sigchain.h" #include "pkt-line.h" #include "sub-process.h" +#include "utf8.h" /* * convert.c - convert a file when checking it out and checking it in. @@ -265,6 +266,78 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, } +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,const char *enc,int conv_flags) +{
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 283
Quoted 10 lines
convert.c +10
-0+char *dst; +int dst_len; +int die_on_error = conv_flags &CONV_WRITE_OBJECT ; + +/* +* No encoding is specified or there is nothing to encode. +* Tell the caller that the content was not modified. +*/ +if (!enc || (src && !src_len)) +return 0 ;
(This may have been discussed before.
As we checked (enc != NULL) I think we can add here:)
if (is_encoding_utf8(enc)) return 0;
Quoted 34 lines
convert.c +34
-0+ +/* +* Looks like we got called from "would_convert_to_git()". +* This means Git wants to know if it would encode (= modify!) +* the content. Let's answer with "yes", since an encoding was +* specified. +*/ +if (!buf && !src) +return 1 ; + + dst =reencode_string_len (src, src_len, default_encoding, enc, + &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 (die_on_error) +die (msg, path, enc, default_encoding); +else { +error (msg, path, enc, default_encoding); +return 0 ; + } + } + +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,const char *enc)
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 327
Quoted 10 lines
convert.c +10
-0+{ +char *dst; +int dst_len; + +/* +* No encoding is specified or there is nothing to encode. +* Tell the caller that the content was not modified. +*/ +if (!enc || (src && !src_len)) +return 0 ;
Same as above:
if (is_encoding_utf8(enc)) return 0;
Quoted 23 lines
convert.c +16
-0+ + dst =reencode_string_len (src, src_len, enc, default_encoding, + &dst_len); +if (!dst) { +error ("failed to encode '%s' from %s to %s" , + path, default_encoding, enc); +return 0 ; + } + +strbuf_attach (buf, dst, dst_len, dst_len +1 ); +return 1 ; +} + static int crlf_to_git(const struct index_state *istate, const char *path, const char *src, size_t len, struct strbuf *buf,@@ -978,6 +1051,25 @@ static int ident_to_worktree(const char *path, const char *src, size_t len,return 1 ; } +static const char *git_path_check_encoding (struct attr_check_item *check) +{ +const char *value = check->value;
convert.c static int ident_to_worktree(const char *path, const char *src, size_t len, · line 1066
Quoted 10 lines
convert.c +8
-0+ +if (ATTR_UNSET (value) || !strlen (value)) +return NULL ; + +if (ATTR_TRUE (value) ||ATTR_FALSE (value)) { +error (_ ("working-tree-encoding attribute requires a value" )); +return NULL ; + }
TRUE or false are values, but just wrong ones.
If this test is removed, the user will see "failed to encode "TRUE" to "UTF-8", which should give enough information to fix it.
convert.c static int ident_to_worktree(const char *path, const char *src, size_t len, · line 1068
Quoted 4 lines
convert.c +4
-0+ +/* Don't encode to the default encoding */ +if (!strcasecmp (value, default_encoding)) +return NULL ;
Same as above ?:
if (is_encoding_utf8(value)) return 0;
Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute
Quoted 14 lines · Torsten Bögershausen
Some comments inline
Quoted 8 lines · On Fri, Mar 09, 2018 at 06:35:32PM +0100, [email protected] wrote:
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.
Quoted 7 lines
Quoted 2 lines
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
Minor comment:
"Git converts the content"
Everywhere else (?) "encodes or reencodes" is used. "Git reencodes the content" may be more consistent.
OK, will change.
Quoted 19 lines
Quoted 6 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,const char *enc,int conv_flags) +{convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 283
Quoted 10 lines
convert.c +10
-0+char *dst; +int dst_len; +int die_on_error = conv_flags &CONV_WRITE_OBJECT ; + +/* +* No encoding is specified or there is nothing to encode. +* Tell the caller that the content was not modified. +*/ +if (!enc || (src && !src_len)) +return 0 ;(This may have been discussed before.
As we checked (enc != NULL) I think we can add here:)
Quoted 2 lines
if (is_encoding_utf8(enc)) return 0;
This should be covered in git_path_check_encoding(), introduced in v12:
/* Don't encode to the default encoding */ if (same_encoding(value, default_encoding)) return NULL;
In that function the encoding of a certain file is read from the .gitattributes. If the encoding matches the compile-time defined default encoding (= UTF-8), then the encoding is set to NULL.
Quoted 48 lines
Quoted 4 lines
convert.c +3
-0+ +static int encode_to_worktree (const char *path,const char *src,size_t src_len, +struct strbuf *buf,const char *enc)convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 327
Quoted 10 lines
convert.c +10
-0+{ +char *dst; +int dst_len; + +/* +* No encoding is specified or there is nothing to encode. +* Tell the caller that the content was not modified. +*/ +if (!enc || (src && !src_len)) +return 0 ;Same as above:
if (is_encoding_utf8(enc)) return 0;Quoted 27 lines
convert.c +13
-0+ + dst =reencode_string_len (src, src_len, enc, default_encoding, + &dst_len); +if (!dst) { +error ("failed to encode '%s' from %s to %s" , + path, default_encoding, enc); +return 0 ; + } + +strbuf_attach (buf, dst, dst_len, dst_len +1 ); +return 1 ; +} +static int crlf_to_git(const struct
index_state*istate,
const char *path, const char *src,size_tlen, struct strbuf *buf,convert.c +7
-0@@ -978,6 +1051,25 @@ static int ident_to_worktree(const char *path, const char *src, size_t len,return 1 ; } +static const char *git_path_check_encoding (struct attr_check_item *check) +{ +const char *value = check->value; + +if (ATTR_UNSET (value) || !strlen (value)) +return NULL ; +
Quoted 8 lines
convert.c static int ident_to_worktree(const char *path, const char *src, size_t len, · line 1064
Quoted 4 lines
convert.c +4
-0+if (ATTR_TRUE (value) ||ATTR_FALSE (value)) { +error (_ ("working-tree-encoding attribute requires a value" )); +return NULL ; + }TRUE or false are values, but just wrong ones.
If this test is removed, the user will see "failed to encode "TRUE" to "UTF-8", which should give enough information to fix it.
I see your point. However, I would like to stop the processing right there for these invalid values. How about
error(_("true/false are no valid working-tree-encodings"));I think that is the most straight forward/helpful error message for the enduser (I consider the term "boolean" but dismissed it as potentially confusing to folks not familiar with the term).
OK with you?
Quoted 8 lines
convert.c static int ident_to_worktree(const char *path, const char *src, size_t len, · line 1068
Quoted 5 lines
convert.c +4
-0+ +/* Don't encode to the default encoding */ +if (!strcasecmp (value, default_encoding)) +return NULL ;Same as above ?:
if (is_encoding_utf8(value)) return 0;
Yes, that was fixed in v12 as mentioned above :-)
- Lars
Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute
Quoted 7 lines · Lars Schneider
Quoted 3 lines
TRUE or false are values, but just wrong ones.
If this test is removed, the user will see "failed to encode "TRUE" to "UTF-8", which should give enough information to fix it.I see your point. However, I would like to stop the processing right there for these invalid values. How about
Quoted 7 lines
error(_("true/false are no valid working-tree-encodings"));I think that is the most straight forward/helpful error message for the enduser (I consider the term "boolean" but dismissed it as potentially confusing to folks not familiar with the term).
OK with you?
Yes.
Another thing that came up recently, independent of your series:
What should happen if a user specifies "UTF-8" and the file has an UTF-8 encoded BOM ?
I ask because I stumbled over such a file coming from a Windows which the java compiler under Linux didn't accept.
And because some tools love to put an UTF-8 encoded BOM into text files.
The clearest thing would be to extend the BOM check in 5/9 to cover UTF-32, UTF-16 and UTF-8.
Are there any plans to do so?
And thanks for the work.
Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute
Quoted 13 lines · Torsten Bögershausen
Quoted 9 lines · Lars Schneider
Quoted 3 lines
TRUE or false are values, but just wrong ones.
If this test is removed, the user will see "failed to encode "TRUE" to "UTF-8", which should give enough information to fix it.I see your point. However, I would like to stop the processing right there for these invalid values. How about
error(_("true/false are no valid working-tree-encodings"));
Quoted 7 lines
Quoted 5 lines
I think that is the most straight forward/helpful error message for the enduser (I consider the term "boolean" but dismissed it as potentially confusing to folks not familiar with the term).
OK with you?
Yes.
Great!
Quoted 7 lines
Another thing that came up recently, independent of your series:
What should happen if a user specifies "UTF-8" and the file has an UTF-8 encoded BOM ?
I ask because I stumbled over such a file coming from a Windows which the java compiler under Linux didn't accept.
Quoted 7 lines
And because some tools love to put an UTF-8 encoded BOM into text files.
The clearest thing would be to extend the BOM check in 5/9 to cover UTF-32, UTF-16 and UTF-8.
Are there any plans to do so?
If working-tree-encoding is not defined or defined as UTF-8, then we would return from encode_to_git() early. That means we would never run validate_encoding() which would check the BOM.
However, adding the UTF-8 BOM would still make sense. This way Git could scream if a user set working-tree-encoding to UTF-16 but the file is really UTF-8 encoded.
Quoted 1 line
And thanks for the work.
Thanks :-)
- Lars
PATCH v11 7/10 convert: check for detectable errors in UTF encodings
In reply to [email protected]From: Lars Schneider <[email protected]>
Check that new content is valid with respect to the user defined 'working-tree-encoding' attribute.
--- convert.c | 48 ++++++++++++++++++++++++++++++++++ t/t0028-working-tree-encoding.sh | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+)
convert.c +48 -0
@@ -266,6 +266,51 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, } +static int validate_encoding (const char *path,const char *enc, +const char *data,size_t len,int die_on_error) +{ +/* We only check for UTF here as UTF?? can be an alias for UTF-?? */ +if (istarts_with (enc,"UTF" )) { +/* +* Check for detectable errors in UTF encodings +*/ +if (has_prohibited_utf_bom (enc, data, len)) { +const char *error_msg =_ ( +"BOM is prohibited in '%s' if encoded as %s" ); +/* +* This advice is shown for UTF-??BE and UTF-??LE encodings. +*/ +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); +if (die_on_error) +die (error_msg, path, enc); +else { +return error (error_msg, path, enc); + } + + }else if (is_missing_required_utf_bom (enc, data, len)) { +const char *error_msg =_ ( +"BOM is required in '%s' if encoded as %s" ); +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, enc); +if (die_on_error) +die (error_msg, path, enc); +else { +return error (error_msg, path, enc); + } + } + + } +return 0 ; +} +static const char *default_encoding ="UTF-8" ; static int encode_to_git(const char *path, const char *src, size_t src_len,
@@ -291,6 +336,9 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,if (!buf && !src)return 1 ; +if (validate_encoding (path, enc, src, src_len, die_on_error)) +return 0 ; + dst =reencode_string_len (src, src_len, default_encoding, enc, &dst_len);if (!dst) {
t/t0028-working-tree-encoding.sh +56 -0
66 lines not on this page · raw message
Signature · 1 line
2.16.2
Re: [PATCH v11 07/10] convert: check for detectable errors in UTF encodings
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 286
Quoted 4 lines · [email protected] writes:
convert.c +4
-0+const char *advise_msg =_ ( +"The file '%s' contains a byte order " +"mark (BOM). Please use %.6s as " +"working-tree-encoding." );
I know that this will go away in a later step, but why ".6"?
Quoted 1 line
convert.c +1
-0+advise (advise_msg, path, enc);
Re: [PATCH v11 07/10] convert: check for detectable errors in UTF encodings
Quoted 11 lines · Junio C Hamano
convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 286
Quoted 4 lines · [email protected] writes:
convert.c +4
-0+const char *advise_msg =_ ( +"The file '%s' contains a byte order " +"mark (BOM). Please use %.6s as " +"working-tree-encoding." );I know that this will go away in a later step, but why ".6"?
I deleted the original comment in the rebase, sorry:
/*
* This advice is shown for UTF-??BE and UTF-??LE
* encodings. We truncate the encoding name to 6
* chars with %.6s to cut off the last two "byte
* order" characters.
*/
- Lars
Re: [PATCH v11 07/10] convert: check for detectable errors in UTF encodings
In reply to [email protected]convert.c static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, · line 286
Quoted 4 lines · [email protected] writes:
convert.c +4
-0+const char *advise_msg =_ ( +"The file '%s' contains a byte order " +"mark (BOM). Please use %.6s as " +"working-tree-encoding." );
I know that this will go away in a later step, but why ".6"?
Quoted 1 line
convert.c +1
-0+advise (advise_msg, path, enc);
PATCH v11 8/10 convert: advise canonical UTF encoding names
In reply to [email protected]From: Lars Schneider <[email protected]>
The canonical name of an UTF encoding has the format UTF, dash, number, and an optionally byte order in upper case (e.g. UTF-8 or UTF-16BE). Some iconv versions support alternative names without a dash or with lower case characters.
To avoid problems between different iconv version always suggest the canonical UTF names in advise messages.
--- convert.c | 21 +++++++++++++++++---- t/t0028-working-tree-encoding.sh | 10 ++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-)
convert.c +17 -4
@@ -279,12 +279,20 @@ static int validate_encoding(const char *path, const char *enc, "BOM is prohibited in '%s' if encoded as %s");/* * This advice is shown for UTF-??BE and UTF-??LE encodings. +* We cut off the last two characters of the encoding name +# to generate the encoding name suitable for BOMs. */ const char *advise_msg =_ ("The file '%s' contains a byte order " -"mark (BOM). Please use %.6s as " +"mark (BOM). Please use UTF-%s as " "working-tree-encoding." ); -advise (advise_msg, path, enc); +const char *stripped ="" ; +char *upper =xstrdup_toupper (enc); + upper[strlen (upper)-2 ] ='\0' ; +if (!skip_prefix (upper,"UTF-" , &stripped)) +skip_prefix (stripped,"UTF" , &stripped); +advise (advise_msg, path, stripped); +free (upper);if (die_on_error)die (error_msg, path, enc);else {
@@ -296,10 +304,15 @@ static int validate_encoding(const char *path, const char *enc, "BOM is required in '%s' if encoded as %s");const char *advise_msg =_ ("The file '%s' is missing a byte order " -"mark (BOM). Please use %sBE or %sLE " +"mark (BOM). Please use UTF-%sBE or UTF-%sLE " "(depending on the byte order) as " "working-tree-encoding." ); -advise (advise_msg, path, enc, enc); +const char *stripped ="" ; +char *upper =xstrdup_toupper (enc); +if (!skip_prefix (upper,"UTF-" , &stripped)) +skip_prefix (stripped,"UTF" , &stripped); +advise (advise_msg, path, stripped, stripped); +free (upper);if (die_on_error)die (error_msg, path, enc);else {
t/t0028-working-tree-encoding.sh +8 -2
@@ -74,18 +74,22 @@ do cp bebom.utf${i}be.raw bebom.utf${i}be && test_must_fail git add bebom.utf${i}be 2>err.out && test_i18ngrep "fatal: BOM is prohibited .* utf-${i}be" err.out && + test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out && cp lebom.utf${i}le.raw lebom.utf${i}be && test_must_fail git add lebom.utf${i}be 2>err.out && test_i18ngrep "fatal: BOM is prohibited .* utf-${i}be" err.out && + test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out && cp bebom.utf${i}be.raw bebom.utf${i}le && test_must_fail git add bebom.utf${i}le 2>err.out && test_i18ngrep "fatal: BOM is prohibited .* utf-${i}LE" err.out && + test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out && cp lebom.utf${i}le.raw lebom.utf${i}le && test_must_fail git add lebom.utf${i}le 2>err.out && - test_i18ngrep "fatal: BOM is prohibited .* utf-${i}LE" err.out + test_i18ngrep "fatal: BOM is prohibited .* utf-${i}LE" err.out && + test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out ' test_expect_success "check required UTF-${i} BOM" '
@@ -96,10 +100,12 @@ do cp nobom.utf${i}be.raw nobom.utf${i} && test_must_fail git add nobom.utf${i} 2>err.out && test_i18ngrep "fatal: BOM is required .* utf-${i}" err.out && + test_i18ngrep "use UTF-${i}BE or UTF-${i}LE" err.out && cp nobom.utf${i}le.raw nobom.utf${i} && test_must_fail git add nobom.utf${i} 2>err.out && - test_i18ngrep "fatal: BOM is required .* utf-${i}" err.out + test_i18ngrep "fatal: BOM is required .* utf-${i}" err.out && + test_i18ngrep "use UTF-${i}BE or UTF-${i}LE" err.out ' test_expect_success "eol conversion for UTF-${i} encoded files on checkout" '
Signature · 1 line
2.16.2
Re: [PATCH v11 08/10] convert: advise canonical UTF encoding names
Quoted 7 lines · [email protected] writes:
From: Lars Schneider <[email protected]>
The canonical name of an UTF encoding has the format UTF, dash, number, and an optionally byte order in upper case (e.g. UTF-8 or UTF-16BE). Some iconv versions support alternative names without a dash or with lower case characters.
Quoted 5 lines
To avoid problems between different iconv version always suggest the canonical UTF names in advise messages.
Signed-off-by Lars Schneider <[email protected]>---
I think it is probably better to squash this to earlier step, i.e. jumping straight to the endgame solution.
convert.c static int validate_encoding(const char *path, const char *enc, · line 284
Quoted 11 lines
convert.c +2
-0@@ -279,12 +279,20 @@ static int validate_encoding(const char *path, const char *enc, "BOM is prohibited in '%s' if encoded as %s");/* * This advice is shown for UTF-??BE and UTF-??LE encodings. +* We cut off the last two characters of the encoding name +# to generate the encoding name suitable for BOMs. */
I somehow thought that I saw "s/#/*/" in somebody's response during the previous round?
convert.c static int validate_encoding(const char *path, const char *enc, · line 295
Quoted 13 lines
convert.c +8
-2const char *advise_msg =_ ("The file '%s' contains a byte order " -"mark (BOM). Please use %.6s as " +"mark (BOM). Please use UTF-%s as " "working-tree-encoding." ); -advise (advise_msg, path, enc); +const char *stripped ="" ; +char *upper =xstrdup_toupper (enc); + upper[strlen (upper)-2 ] ='\0' ; +if (!skip_prefix (upper,"UTF-" , &stripped)) +skip_prefix (stripped,"UTF" , &stripped); +advise (advise_msg, path, stripped); +free (upper);
If this codepath is ever entered with "enc" that does not begin with "UTF" (e.g. "Shift_JIS", which is impossible in the current code, but I'll talk about future-proofing here), then neither of these skip_prefix will trigger, and then you'd end up suggesting to use "UTF-" that is nonsense. Perhaps initialize stripped to NULL and force advise to segv to catch such a programmer error?
Re: [PATCH v11 08/10] convert: advise canonical UTF encoding names
Quoted 12 lines · Junio C Hamano
Quoted 7 lines · [email protected] writes:
From: Lars Schneider <[email protected]>
The canonical name of an UTF encoding has the format UTF, dash, number, and an optionally byte order in upper case (e.g. UTF-8 or UTF-16BE). Some iconv versions support alternative names without a dash or with lower case characters.
Quoted 8 lines
Quoted 5 lines
To avoid problems between different iconv version always suggest the canonical UTF names in advise messages.
Signed-off-by Lars Schneider <[email protected]>---
I think it is probably better to squash this to earlier step, i.e. jumping straight to the endgame solution.
ok!
Quoted 6 lines
Quoted 6 lines
convert.c +0
-0@@ -279,12 +279,20 @@ static int validate_encoding(const char *path, const char *enc, "BOM is prohibited in '%s' if encoded as %s");
Quoted 8 lines
convert.c static int validate_encoding(const char *path, const char *enc, · line 284
Quoted 5 lines
convert.c +2
-0/* * This advice is shown for UTF-??BE and UTF-??LE encodings. +* We cut off the last two characters of the encoding name +# to generate the encoding name suitable for BOMs. */ I somehow thought that I saw "s/#/*/" in somebody's response during the previous round?
Oops. Will fix!
Quoted 12 lines
Quoted 12 lines
convert.c +7
-2const char *advise_msg =_ ("The file '%s' contains a byte order " -"mark (BOM). Please use %.6s as " +"mark (BOM). Please use UTF-%s as " "working-tree-encoding." ); -advise (advise_msg, path, enc); +const char *stripped ="" ; +char *upper =xstrdup_toupper (enc); + upper[strlen (upper)-2 ] ='\0' ; +if (!skip_prefix (upper,"UTF-" , &stripped)) +skip_prefix (stripped,"UTF" , &stripped); +advise (advise_msg, path, stripped);
Quoted 8 lines
convert.c static int validate_encoding(const char *path, const char *enc, · line 295
Quoted 1 line
convert.c +1
-0+free (upper);If this codepath is ever entered with "enc" that does not begin with "UTF" (e.g. "
Shift_JIS", which is impossible in the current code, but I'll talk about future-proofing here), then neither of theseskip_prefixwill trigger, and then you'd end up suggesting to use "UTF-" that is nonsense. Perhaps initialize stripped to NULL and force advise to segv to catch such a programmer error?
Agreed!
Thanks,
Lars
PATCH v11 9/10 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
@@ -324,6 +324,29 @@ static int validate_encoding(const char *path, const char *enc,return 0 ; } +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 const char *default_encoding ="UTF-8" ; static int encode_to_git(const char *path, const char *src, size_t src_len,
@@ -352,6 +375,7 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,if (validate_encoding (path, enc, src, src_len, die_on_error))return 0 ; +trace_encoding ("source" , path, enc, src, src_len); dst =reencode_string_len (src, src_len, default_encoding, enc, &dst_len);if (!dst) {
@@ -369,6 +393,7 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,return 0 ; } } +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 files' ' git config core.eol lf &&
Signature · 1 line
2.16.2
PATCH v11 10/10 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 | 79 +++++++++++++++++++++++++++++++++++++++- convert.h | 1 + environment.c | 1 + t/t0028-working-tree-encoding.sh | 39 ++++++++++++++++++++ 7 files changed, 138 insertions(+), 1 deletion(-)
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 and/or whitespace 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
@@ -312,6 +312,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 +78 -1
97 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 +39 -0
@@ -205,4 +205,43 @@ test_expect_success 'error if encoding garbage is already in Git' ' test_i18ngrep "error: BOM is required" err.out ' +test_expect_success 'check roundtrip encoding' ' + test_when_finished "rm -f roundtrip.shift roundtrip.utf16" && + test_when_finished "git reset --hard HEAD" && + + 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 | + grep "Checking roundtrip encoding for SHIFT-JIS" && + git reset && + + # ... unless we overwrite the Git config! + ! GIT_TRACE=1 git -c core.checkRoundtripEncoding=garbage \ + add .gitattributes roundtrip.shift 2>&1 | + grep "Checking roundtrip encoding for SHIFT-JIS" && + git reset && + + # UTF-16 encoded files should not be round-trip checked by default... + ! GIT_TRACE=1 git add roundtrip.utf16 2>&1 | + grep "Checking roundtrip encoding for UTF-16" && + git reset && + + # ... unless we tell Git to check it! + GIT_TRACE=1 git -c core.checkRoundtripEncoding="UTF-16, UTF-32" \ + add roundtrip.utf16 2>&1 | + grep "Checking roundtrip encoding for utf-16" && + git reset && + + # ... unless we tell Git to check it! + # (here we also check that the casing of the encoding is irrelevant) + GIT_TRACE=1 git -c core.checkRoundtripEncoding="UTF-32, utf-16" \ + add roundtrip.utf16 2>&1 | + grep "Checking roundtrip encoding for utf-16" && + git reset +' + test_done
Signature · 1 line
2.16.2
Re: [PATCH v11 10/10] convert: add round trip check based on 'core.checkRoundtripEncoding'
Quoted 10 lines · On Fri, Mar 9, 2018 at 12:35 PM, <[email protected]> wrote:
[...]
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.[...]Signed-off-by Lars Schneider <[email protected]>---
convert.c static const char *git_path_check_encoding(struct attr_check_item *check) · line 1229
Quoted 4 lines
convert.c +1
-1@@ -1150,7 +1227,7 @@ static const char *git_path_check_encoding(struct attr_check_item *check)/* Don't encode to the default encoding */ -if (!strcasecmp (value, default_encoding)) +if (is_encoding_utf8 (value) &&is_encoding_utf8 (default_encoding))return NULL ;
This change belongs in 6/10, not 10/10, methinks.
Re: [PATCH v11 10/10] convert: add round trip check based on 'core.checkRoundtripEncoding'
Quoted 17 lines · Eric Sunshine
Quoted 10 lines · On Fri, Mar 9, 2018 at 12:35 PM, <[email protected]> wrote:
[...]
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.[...]Signed-off-by Lars Schneider <[email protected]>---
convert.c static const char *git_path_check_encoding(struct attr_check_item *check) · line 1229
Quoted 4 lines
convert.c +1
-1@@ -1150,7 +1227,7 @@ static const char *git_path_check_encoding(struct attr_check_item *check)/* Don't encode to the default encoding */ -if (!strcasecmp (value, default_encoding)) +if (is_encoding_utf8 (value) &&is_encoding_utf8 (default_encoding))return NULL ;This change belongs in 6/10, not 10/10, methinks.
It is actually worse than that, no? When default_encoding is (somehow) configured not to be UTF-8, e.g. "Shift_JIS", we used to avoid converting from Shift_JIS to Shift_JIS, but the optimization no longer happens with this code.
In any case, I think same_encoding() is probably a good thing to use here at step 6/10, so the point is moot, I guess.
Re: [PATCH v11 10/10] convert: add round trip check based on 'core.checkRoundtripEncoding'
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Eric Sunshine
convert.c static const char *git_path_check_encoding(struct attr_check_item *check) · line 1231
Quoted 4 lines · On Fri, Mar 9, 2018 at 12:35 PM, <[email protected]> wrote:
/* Don't encode to the default encoding */convert.c +1
-1-if (!strcasecmp (value, default_encoding)) +if (is_encoding_utf8 (value) &&is_encoding_utf8 (default_encoding))return NULL ;This change belongs in 6/10, not 10/10, methinks.
Quoted 7 lines
It is actually worse than that, no? When
default_encodingis (somehow) configured not to be UTF-8, e.g. "Shift_JIS", we used to avoid converting fromShift_JIStoShift_JIS, but the optimization no longer happens with this code.In any case, I think
same_encoding()is probably a good thing to use here at step 6/10, so the point is moot, I guess.
Agreed, same_encoding() would be superior.