Skip to contentSkip to conversation

convert: add support for different encodings

PATCH Superseded [email protected] 6 participants 41 messages Feb 15, 2018 – Mar 4, 2018 Documentation

v7 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v7 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 v6:

* use consistent casing for core.checkRoundtripEncoding (Junio)
* fix gibberish in commit message (Junio)
* improve documentation (Torsten)
* improve advise messages (Torsten)

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]/

Base Ref:
Web-Diff: https://github.com/larsxschneider/git/commit/2b94bec353
Checkout: git fetch https://github.com/larsxschneider/git encoding-v7 && git checkout 2b94bec353

### Interdiff (v6..v7):

Documentation/gitattributes.txt +12 -11
index ea5a9509c6..10cb37795d 100644
@@ -291,19 +291,20 @@ the content is reencoded back to the specified encoding.
 Please note that using the `working-tree-encoding` attribute may have a
 number of pitfalls:
 
-- Git clients 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).
+- 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).
 
 - 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.
+  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').
@@ -327,7 +328,7 @@ explicitly define the line endings with `eol` if the `working-tree-encoding`
 attribute is used to avoid ambiguity.
 
 ------------------------
-*.proj 		working-tree-encoding=UTF-16LE text eol=CRLF
+*.proj 		text working-tree-encoding=UTF-16LE eol=CRLF
 ------------------------
 
 You can get a list of all available encodings on your platform with the
convert.c +15 -15
index 71dffc7167..398cd9cf7b 100644
@@ -352,29 +352,29 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,
 
 	if (has_prohibited_utf_bom(enc->name, src, src_len)) {
 		const char *error_msg = _(
-			"BOM is prohibited for '%s' if encoded as %s");
+			"BOM is prohibited in '%s' if encoded as %s");
+		/*
+		 * This advise 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.
+		 */
 		const char *advise_msg = _(
-			"You told Git to treat '%s' as %s. A byte order mark "
-			"(BOM) is prohibited with this encoding. Either use "
-			"%.6s as working tree encoding or remove the BOM from the "
-			"file.");
-
-		advise(advise_msg, path, enc->name, enc->name, enc->name);
+			"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);
 
-
 	} else if (is_missing_required_utf_bom(enc->name, src, src_len)) {
 		const char *error_msg = _(
-			"BOM is required for '%s' if encoded as %s");
+			"BOM is required in '%s' if encoded as %s");
 		const char *advise_msg = _(
-			"You told Git to treat '%s' as %s. A byte order mark "
-			"(BOM) is required with this encoding. Either use "
-			"%sBE/%sLE as working tree encoding or add a BOM to the "
-			"file.");
-		advise(advise_msg, path, enc->name, enc->name, enc->name);
+			"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
@@ -405,7 +405,7 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,
 	 * 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 [2]. Check the round trip conversion for all encodings
-	 * listed in core.checkRoundTripEncoding.
+	 * listed in core.checkRoundtripEncoding.
 	 *
 	 * The round trip check is only performed if content is written to Git.
 	 * This ensures that no information is lost during conversion to/from
t/t0028-working-tree-encoding.sh +4 -4
index 5dcdd5f899..e4717402a5 100755
@@ -221,10 +221,10 @@ test_expect_success 'check roundtrip encoding' '
 	git reset &&
 
 	# ... unless we overwrite the Git config!
-	test_config core.checkRoundTripEncoding "garbage" &&
+	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 &&
+	test_unconfig core.checkRoundtripEncoding &&
 	git reset &&
 
 	# UTF-16 encoded files should not be round-trip checked by default...
@@ -233,14 +233,14 @@ test_expect_success 'check roundtrip encoding' '
 	git reset &&
 
 	# ... unless we tell Git to check it!
-	test_config_global core.checkRoundTripEncoding "UTF-16, UTF-32" &&
+	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!
 	# (here we also check that the casing of the encoding is irrelevant)
-	test_config_global core.checkRoundTripEncoding "UTF-32, utf-16" &&
+	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 &&

### 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  |  74 +++++++++++
 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 | 253 ++++++++++++++++++++++++++++++++++++++
 utf8.c                           |  37 ++++++
 utf8.h                           |  25 ++++
 12 files changed, 672 insertions(+), 3 deletions(-)
 create mode 100755 t/t0028-working-tree-encoding.sh

base-commit: 8a2f0888555ce46ac87452b194dec5cb66fb1417 --
2.16.1

#1 lore raw

PATCH v7 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
index 1df674e919..55b7daeb35 100644
@@ -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
#2 lore raw

Re: [PATCH v7 1/7] strbuf: remove unnecessary NUL assignment in xstrdup_tolower()

Quoted 11 lines · On Thu, Feb 15, 2018 at 4:27 PM, <[email protected]> wrote:

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.
[...]

        for (i = 0; i < len; i++)
                result[i] = tolower(string[i]);
-       result[i] = '\0';
        return result;

}

I agree with this approach, but it's worth noting for other reviewers that there's been some disagreement here on-list (between Eric & I) about whether these sorts of patterns should be removed or kept (although the calloc() case is slightly different from mallocz()),
see: https://public-inbox.org/git/[email protected]/

#3 lore raw

Re: [PATCH v7 1/7] strbuf: remove unnecessary NUL assignment in xstrdup_tolower()

Quoted 13 lines · Ævar Arnfjörð Bjarmason
Quoted 11 lines · On Thu, Feb 15, 2018 at 4:27 PM, <[email protected]> wrote:

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.
[...]

        for (i = 0; i < len; i++)
                result[i] = tolower(string[i]);
-       result[i] = '\0';
        return result;

}

Quoted 5 lines

I agree with this approach, but it's worth noting for other reviewers that there's been some disagreement here on-list (between Eric & I) about whether these sorts of patterns should be removed or kept (although the calloc() case is slightly different from mallocz()),
see: https://public-inbox.org/git/[email protected]/

Hmm. I do think xmallocz is a bit more explicit instruction of "please NUL-terminate this for me" than xcalloc is. So I don't think it's inconsistent to say this one is OK, but the trailing-NULL one that you linked is not.

I'm not sure that I have a strong opinion on either case. But in general I'd probably err on the side of leaving such lines in, for the sake of being explicit.

Of course this particular case could just be:

char *result = xstrdup(string);
for (i = 0; result[i]; i++)

	result[i] = tolower(result[i]);

I picked the current implementation in 88d5a6f6cd (daemon/config: factor out duplicate xstrdup_tolower, 2014-05-22) because it might be more efficient (it avoids an extra copy), but I doubt it matters much in practice.

-Peff

#4 lore raw

Re: [PATCH v7 1/7] strbuf: remove unnecessary NUL assignment in xstrdup_tolower()

Quoted 7 lines · Jeff King
Quoted 2 lines

(although the calloc() case is slightly different from mallocz()),
see: https://public-inbox.org/git/[email protected]/

Hmm. I do think xmallocz is a bit more explicit instruction of "please NUL-terminate this for me" than xcalloc is. So I don't think it's inconsistent to say this one is OK, but the trailing-NULL one that you linked is not.

Yeah, I too thought "slightly different" was an understatement of the week ;-).

#5 lore raw

PATCH v7 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
index 55b7daeb35..b635f0bdc4 100644
@@ -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
index 14c8c10d66..df7ced53ed 100644
@@ -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
#6 lore raw

PATCH v7 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
index 2c27ce0137..914881cd1f 100644
@@ -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
index 6bbcf31a83..4711429af9 100644
@@ -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
#7 lore raw

PATCH v7 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
index 914881cd1f..5113d26e56 100644
@@ -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
index 4711429af9..62f86fba64 100644
@@ -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
#8 lore raw

PATCH v7 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  |  66 ++++++++++++
 convert.c                        | 157 ++++++++++++++++++++++++++++-
 convert.h                        |   1 +
 sha1_file.c                      |   2 +-
 t/t0028-working-tree-encoding.sh | 210 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 434 insertions(+), 2 deletions(-)
 create mode 100755 t/t0028-working-tree-encoding.sh
Documentation/gitattributes.txt +66 -0
index 30687de81a..5ec179d631 100644
@@ -272,6 +272,72 @@ few exceptions.  Even though...
   catch potential problems early, safety triggers.
 
 
+`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.
+
+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:
+
+- 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).
+
+- 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
index 65ab3e5167..1d9539ed0b 100644
@@ -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
index 6bc7c6ada9..e2f319d677 100644
@@ -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 +210 -0

210 lines not on this page · raw message

Signature · 1 line
2.16.1
#9 lore raw

PATCH v7 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
index d20c341b6d..c4e2fd5fa5 100644
@@ -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
index f9ce3e5ef5..01789ae1b8 100755
@@ -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
#10 lore raw

PATCH v7 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
index 0e25b2c92b..d7a56054a5 100644
@@ -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
index 5ec179d631..10cb37795d 100644
@@ -298,6 +298,14 @@ number of pitfalls:
   in particular the case for older Git versions and alternative Git
   implementations such as JGit or libgit2 (as of February 2018).
 
+- 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
index 1f003fbb90..d0ada9fcd4 100644
@@ -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
index 1d9539ed0b..765abfbd60 100644
@@ -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
index 10a32c20ac..5bae9131ad 100644
@@ -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
#11 lore raw
In reply to [email protected]

Documentation/gitattributes.txt the content is reencoded back to the specified encoding. · line 299

Quoted 12 lines · [email protected] writes:

-- Git clients that do not support the working-tree-encoding attribute

Documentation/gitattributes.txt +6 -5
-  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).
+- 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).

I know somebody found "clients" misleading in the original, but the ones that do not understand w-t-e do not have to be third party reimplementations and imitations. All existing Git implementations, including ours, don't.

One thing I find more problematic is that the above places *too* much stress on the UTF-8 centric worldview. It is perfectly valid to store your text contents encoded in ShiftJIS and check them out as-is, with or without this patch. It is grossly misleading to say that older versions of Git will check them out in UTF-8. "will checkout these files as-is without encoding conversion" is a better way to say it, probably.

Also notice that even in the world with w-t-e, such a project won't benefit from w-t-e at all. After all, they have been happy using ShiftJIS in repository and using the same encoding on the working tree, and because w-t-e assumes that everybody should be using UTF-8 internally, such a project cannot take advantage of the new mechanism.

And from that point of view, perhaps w-t-e attribute is somewhat misdesigned.

In general, an attribute is about the project's contents in the manner independent of platform or environment. You define "this file is a C source" or "this file has JPEG image" there. What exact program you use to present diffs between the two versions of such a file (external diff command) or what exact program you use to extract the textual representations (textconv filter) is environment and platform dependent and is left to the configuration mechanism for each repository.

To be in line with the above design principle, I think the attribute ought to be "the in-tree contents of this path is encoded in ..." whose values could be things like UTF-8, ShiftJIS, etc. What external encoding the paths should be checked out is not a project-wide matter, especially when talking about cross platform projects. Perhaps a project in Japanese language wants to check out its contents in EUC-jp on Unices and in ShiftJIS on DOS derived systems. The participants all need to know what in-repository encoding is used, which is a sensible use of attributes. They also need to know what the recommended external encoding to be used in the working tree is for their platforms, but that is more like what Makefile variable to set for their platforms, etc., and is not a good match to the attributes system.

#12 lore raw
Quoted 12 lines · Junio C Hamano

And from that point of view, perhaps w-t-e attribute is somewhat misdesigned.

In general, an attribute is about the project's contents in the manner independent of platform or environment. You define "this file is a C source" or "this file has JPEG image" there. What exact program you use to present diffs between the two versions of such a file (external diff command) or what exact program you use to extract the textual representations (textconv filter) is environment and platform dependent and is left to the configuration mechanism for each repository.

Quoted 13 lines

To be in line with the above design principle, I think the attribute ought to be "the in-tree contents of this path is encoded in ..." whose values could be things like UTF-8, ShiftJIS, etc. What external encoding the paths should be checked out is not a project-wide matter, especially when talking about cross platform projects. Perhaps a project in Japanese language wants to check out its contents in EUC-jp on Unices and in ShiftJIS on DOS derived systems. The participants all need to know what in-repository encoding is used, which is a sensible use of attributes. They also need to know what the recommended external encoding to be used in the working tree is for their platforms, but that is more like what Makefile variable to set for their platforms, etc., and is not a good match to the attributes system.

While I agree what you're saying philosophically here, I suspect you'd still need another attribute for "no really, this needs to be checked out as encoding X". The same way we treat line endings as a platform decision, but we still need to have eol=crlf for those files which really, no matter what platform you're on, have external tools depending on them to have some particular line ending.

So a full proposal would support both cases: "check this out in the local platform's preferred encoding" and "always check this out in _this_ encoding". And Lars's proposal is just the second half of that.

But I'm not sure anybody even really cares about the first part; I don't think we've seen anybody actually ask for it.

-Peff

#13 lore raw
Quoted 3 lines · Jeff King

So a full proposal would support both cases: "check this out in the local platform's preferred encoding" and "always check this out in _this_ encoding". And Lars's proposal is just the second half of that.

Actually, what you seem to take as a whole is just half of the story. The other half that is an ability to say "what is in the repository for this path is stored in this encoding". I agree that "check it out in this encoding" is a useful thing to have, and using the in-tree .gitattributes as a place to state the project-wide preference may be OK (and .git/info/attributes should be able to override it if needed -- this probably deserves to be added to a test somewhere by this series).

Luckily, lack of 'in-repository-encoding' attribute is not a show stopper for this series. A later topic could start with "earlier, in order to make use of w-t-e attribute, you had to have your contents in UTF-8. Teach the codepath to honor a new attribute that tells in what encoding the blob contents are stored." without having to be a part of this topic.

#14 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 3 lines · Jeff King

So a full proposal would support both cases: "check this out in the local platform's preferred encoding" and "always check this out in _this_ encoding". And Lars's proposal is just the second half of that.

Quoted 8 lines

Actually, what you seem to take as a whole is just half of the story. The other half that is an ability to say "what is in the repository for this path is stored in this encoding". I agree that "check it out in this encoding" is a useful thing to have, and using the in-tree .gitattributes as a place to state the project-wide preference may be OK (and .git/info/attributes should be able to override it if needed -- this probably deserves to be added to a test somewhere by this series).

If we are just talking about a check-out feature, I'm not sure that the in-repository encoding is all that interesting. As with CRLFs, we would be declaring UTF-8 as the "canonical" in-repo encoding for such conversions. Is there a reason you'd want something else?

If the feature were "the in-repo encoding is X, and I want you to show me a diff using encoding Y", then I could see the use of that (and I think for most people's purposes that would be an equally valid solution to their problem).

-Peff

#15 lore raw
Quoted 17 lines · Jeff King
Quoted 6 lines · Junio C Hamano
Quoted 3 lines · Jeff King

So a full proposal would support both cases: "check this out in the local platform's preferred encoding" and "always check this out in _this_ encoding". And Lars's proposal is just the second half of that.

Quoted 8 lines

Actually, what you seem to take as a whole is just half of the story. The other half that is an ability to say "what is in the repository for this path is stored in this encoding". I agree that "check it out in this encoding" is a useful thing to have, and using the in-tree .gitattributes as a place to state the project-wide preference may be OK (and .git/info/attributes should be able to override it if needed -- this probably deserves to be added to a test somewhere by this series).

Quoted 4 lines

If we are just talking about a check-out feature, I'm not sure that the in-repository encoding is all that interesting. As with CRLFs, we would be declaring UTF-8 as the "canonical" in-repo encoding for such conversions. Is there a reason you'd want something else?

Maybe answering my own question: because your encoding of choice does not round-trip to UTF-8?

In which case yeah, I could see choosing an in-repo encoding to possibly be useful (but it also seems like a feature that could easily be tacked on later if somebody cares).

-Peff

#16 lore raw
Quoted 3 lines · Jeff King

In which case yeah, I could see choosing an in-repo encoding to possibly be useful (but it also seems like a feature that could easily be tacked on later if somebody cares).

Yes, I think we are on the same page---in-repo-encoding that is a natural counterpart to w-t-e attribute can be added later if/when somebody finds it useful, and it is perfectly OK to declare that we cater only to UTF-8 users until that happens.

#17 lore raw
In reply to Junio C Hamano
Quoted 9 lines · Junio C Hamano
Quoted 3 lines · Jeff King

So a full proposal would support both cases: "check this out in the local platform's preferred encoding" and "always check this out in _this_ encoding". And Lars's proposal is just the second half of that.

Quoted 8 lines

Actually, what you seem to take as a whole is just half of the story. The other half that is an ability to say "what is in the repository for this path is stored in this encoding". I agree that "check it out in this encoding" is a useful thing to have, and using the in-tree .gitattributes as a place to state the project-wide preference may be OK (and .git/info/attributes should be able to override it if needed -- this probably deserves to be added to a test somewhere by this series).

Good call! I'll add this test case!

Quoted 6 lines

Luckily, lack of 'in-repository-encoding' attribute is not a show stopper for this series. A later topic could start with "earlier, in order to make use of w-t-e attribute, you had to have your contents in UTF-8. Teach the codepath to honor a new attribute that tells in what encoding the blob contents are stored." without having to be a part of this topic.

I have the impression that this is the purpose of the already existing "encoding" attribute, no? AFAIK this attribute is only respected by gitk, though. A future series could make Git respect this attribute too.

- Lars

#18 lore raw
In reply to Junio C Hamano
Quoted 18 lines · Junio C Hamano

Documentation/gitattributes.txt the content is reencoded back to the specified encoding. · line 299

Quoted 12 lines · [email protected] writes:

-- Git clients that do not support the working-tree-encoding attribute

Documentation/gitattributes.txt +6 -5
-  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).
+- 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).
Quoted 4 lines

I know somebody found "clients" misleading in the original, but the ones that do not understand w-t-e do not have to be third party reimplementations and imitations. All existing Git implementations, including ours, don't.

Agreed!

Quoted 7 lines

One thing I find more problematic is that the above places *too* much stress on the UTF-8 centric worldview. It is perfectly valid to store your text contents encoded in ShiftJIS and check them out as-is, with or without this patch. It is grossly misleading to say that older versions of Git will check them out in UTF-8. "will checkout these files as-is without encoding conversion" is a better way to say it, probably.

True. But that's not what I wanted to say in the "pitfalls" section. If my Git client supports w-t-e and I add the ShiftJIS encoded file "foo.bar" to my repository, then Git will store the file as UTF-8 _internally_. That means if you clone my repository and your Git client does _not_ support w-t-e, then you will see "foo.bar" as UTF-8 encoded.

Quoted 6 lines

Also notice that even in the world with w-t-e, such a project won't benefit from w-t-e at all. After all, they have been happy using ShiftJIS in repository and using the same encoding on the working tree, and because w-t-e assumes that everybody should be using UTF-8 internally, such a project cannot take advantage of the new mechanism.

Agreed. However, people using ShiftJIS are not my target audience. My target audience are:

(1) People that have to encode their text files in UTF-16 (for whatever reason - usually because of legacy processes or tools).

(2) People that want to see textual diffs of their UTF-16 encoded files in their Git tools without special adjustments (on the command line, on the web, etc).

That was my primary motivation. The fact that w-t-e supports any other encoding too is just a nice side effect. I don't foresee people using other w-t-encodings other than UTF-16 in my organization.

I have the suspicion that the feature could be useful for the Git community at large. Consider this Stack Overflow question:
https://stackoverflow.com/questions/777949/can-i-make-git-recognize-a-utf-16-file-as-text

This question was viewed 42k times and there is no good solution. I believe w-t-e could be a good solution.

With your comments in mind, I tried to improve the text like this:

Git recognizes files encoded with ASCII or one of its supersets (e.g. UTF-8, ISO-8859-1, ...) as text files. Files encoded with 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.

...

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 February 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.

...

Quoted 12 lines

And from that point of view, perhaps w-t-e attribute is somewhat misdesigned.

In general, an attribute is about the project's contents in the manner independent of platform or environment. You define "this file is a C source" or "this file has JPEG image" there. What exact program you use to present diffs between the two versions of such a file (external diff command) or what exact program you use to extract the textual representations (textconv filter) is environment and platform dependent and is left to the configuration mechanism for each repository.

Quoted 13 lines

To be in line with the above design principle, I think the attribute ought to be "the in-tree contents of this path is encoded in ..." whose values could be things like UTF-8, ShiftJIS, etc. What external encoding the paths should be checked out is not a project-wide matter, especially when talking about cross platform projects. Perhaps a project in Japanese language wants to check out its contents in EUC-jp on Unices and in ShiftJIS on DOS derived systems. The participants all need to know what in-repository encoding is used, which is a sensible use of attributes. They also need to know what the recommended external encoding to be used in the working tree is for their platforms, but that is more like what Makefile variable to set for their platforms, etc., and is not a good match to the attributes system.

As mentioned above, this is not my intended usecase here. As Peff mentioned elsewhere "always check this out in _this_ encoding" is the goal here.

Thanks a lot for feedback,
Lars

#19 lore raw

On Fri, Feb 16, 2018 at 03:42:35PM +0100, Lars Schneider wrote:
[]

Quoted 15 lines

Agreed. However, people using ShiftJIS are not my target audience. My target audience are:

(1) People that have to encode their text files in UTF-16 (for whatever reason - usually because of legacy processes or tools).

(2) People that want to see textual diffs of their UTF-16 encoded files in their Git tools without special adjustments (on the command line, on the web, etc).

That was my primary motivation. The fact that w-t-e supports any other encoding too is just a nice side effect. I don't foresee people using other w-t-encodings other than UTF-16 in my organization.

Quoted 7 lines

I have the suspicion that the feature could be useful for the Git community at large. Consider this Stack Overflow question:
https://stackoverflow.com/questions/777949/can-i-make-git-recognize-a-utf-16-file-as-text

This question was viewed 42k times and there is no good solution. I believe w-t-e could be a good solution.

If it was only about a diff of UTF-16 files, I may suggest a patch. I simply copy-paste it here for review, if someone thinks that it may be useful, I can send it as a real patch/RFC.

git show HEAD

commit 9f7d43f29eaf6017b7b16261ce91d8ef182cf415

Author: Torsten Bögershausen <[email protected]>
Date:   Fri Feb 2 15:35:23 2018 +0100

Auto diff of UTF-16 files in UTF-8

When an UTF-16 file is commited and later changed, git diff shows "Binary files XX and YY differ".

When the user wants a diff in UTF-8, a textconv needs to be specified in .gitattributes and the textconv must be configured.

A more user-friendly diff can be produced for UTF-16 if
- the user did not use git diff --binary
- the blob is identified as binary
- the blob has an UTF-16 BOM
- the blob can be converted into UTF-8

Enhance the diff machinery to auto-detect UTF-16 blobs and show them as UTF-8, unless the user specifies git diff --binary which creates a binary diff.

diff.c +41 -2
index fb22b19f09..51831ee94d 100644
@@ -3192,6 +3192,10 @@ static void builtin_diff(const char *name_a,
 			strbuf_reset(&header);
 		}
 
+		if (one && one->reencoded_from_utf16)
+			strbuf_addf(&header, "a is converted to UTF-8 from UTF-16\n");
+		if (two && two->reencoded_from_utf16)
+			strbuf_addf(&header, "b is converted to UTF-8 from UTF-16\n");
 		mf1.size = fill_textconv(textconv_one, one, &mf1.ptr);
 		mf2.size = fill_textconv(textconv_two, two, &mf2.ptr);
 
@@ -3611,8 +3615,25 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
 			s->size = size;
 			s->should_free = 1;
 		}
-	}
-	else {
+		if (!s->binary && buffer_is_binary(s->data, s->size) &&
+		    buffer_has_utf16_bom(s->data, s->size)) {
+			int outsz = 0;
+			char *outbuf;
+			outbuf = reencode_string_len(s->data, (int)s->size,
+						     "UTF-8", "UTF-16", &outsz);
+			if (outbuf) {
+				if (s->should_free)
+					free(s->data);
+				if (s->should_munmap)
+					munmap(s->data, s->size);
+				s->should_munmap = 0;
+				s->data = outbuf;
+				s->size = outsz;
+				s->reencoded_from_utf16 = 1;
+				s->should_free = 1;
+			}
+		}
+	} else {
 		enum object_type type;
 		if (size_only || (flags & CHECK_BINARY)) {
 			type = sha1_object_info(s->oid.hash, &s->size);
@@ -3629,6 +3650,19 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
 		s->data = read_sha1_file(s->oid.hash, &type, &s->size);
 		if (!s->data)
 			die("unable to read %s", oid_to_hex(&s->oid));
+		if (!s->binary && buffer_is_binary(s->data, s->size) &&
+		    buffer_has_utf16_bom(s->data, s->size)) {
+			int outsz = 0;
+			char *buf;
+			buf = reencode_string_len(s->data, (int)s->size,
+						  "UTF-8", "UTF-16", &outsz);
+			if (buf) {
+				free(s->data);
+				s->data = buf;
+				s->size = outsz;
+				s->reencoded_from_utf16 = 1;
+			}
+		}
 		s->should_free = 1;
 	}
 	return 0;
@@ -5695,6 +5729,10 @@ static int diff_filespec_is_identical(struct diff_filespec *one,
 
 static int diff_filespec_check_stat_unmatch(struct diff_filepair *p)
 {
+	if (p->binary) {
+		p->one->binary = 1;
+		p->two->binary = 1;
+	}
 	if (p->done_skip_stat_unmatch)
 		return p->skip_stat_unmatch_result;
 
@@ -5735,6 +5773,7 @@ static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
 	for (i = 0; i < q->nr; i++) {
 		struct diff_filepair *p = q->queue[i];
 
+		p->binary = diffopt->flags.binary;
 		if (diff_filespec_check_stat_unmatch(p))
 			diff_q(&outq, p);
 		else {
diffcore.h +3 -0
index a30da161da..3cd97bb93b 100644
@@ -47,6 +47,8 @@ struct diff_filespec {
 	unsigned has_more_entries : 1; /* only appear in combined diff */
 	/* data should be considered "binary"; -1 means "don't know yet" */
 	signed int is_binary : 2;
+	unsigned binary : 1;
+	unsigned reencoded_from_utf16 : 1;
 	struct userdiff_driver *driver;
 };
 
@@ -72,6 +74,7 @@ struct diff_filepair {
 	unsigned is_unmerged : 1;
 	unsigned done_skip_stat_unmatch : 1;
 	unsigned skip_stat_unmatch_result : 1;
+	unsigned binary : 1;
 };
 #define DIFF_PAIR_UNMERGED(p) ((p)->is_unmerged)
 
t/t4066-diff-encoding.sh new file +98 -0

98 lines not on this page · raw message

utf8.h +11 -0
index 6bbcf31a83..a2184d0300 100644
@@ -16,6 +16,17 @@ int utf8_fprintf(FILE *, const char *, ...);
 extern const char utf8_bom[];
 extern int skip_utf8_bom(char **, size_t);
 
+static inline int buffer_has_utf16_bom(const void *buf, size_t len) {
+  const unsigned char *text = (unsigned char *)buf;
+  if (!text ||  len < 2)
+    return 0;
+  if (text[0] == 0xff && text[1] == 0xfe)
+    return 1;
+  if (text[0] == 0xfe && text[1] == 0xff)
+    return 1;
+  return 0;
+}
+
 void strbuf_add_wrapped_text(struct strbuf *buf,
 		const char *text, int indent, int indent2, int width);
 void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,
#20 lore raw
Quoted 24 lines · Torsten Bögershausen

On Fri, Feb 16, 2018 at 03:42:35PM +0100, Lars Schneider wrote:
[]

Quoted 19 lines

Agreed. However, people using ShiftJIS are not my target audience. My target audience are:

(1) People that have to encode their text files in UTF-16 (for whatever reason - usually because of legacy processes or tools).

(2) People that want to see textual diffs of their UTF-16 encoded files in their Git tools without special adjustments (on the command line, on the web, etc).

That was my primary motivation. The fact that w-t-e supports any other encoding too is just a nice side effect. I don't foresee people using other w-t-encodings other than UTF-16 in my organization.

I have the suspicion that the feature could be useful for the Git community at large. Consider this Stack Overflow question:
https://stackoverflow.com/questions/777949/can-i-make-git-recognize-a-utf-16-file-as-text

Quoted 7 lines
Quoted 3 lines

This question was viewed 42k times and there is no good solution. I believe w-t-e could be a good solution.

If it was only about a diff of UTF-16 files, I may suggest a patch. I simply copy-paste it here for review, if someone thinks that it may be useful, I can send it as a real patch/RFC.

That's a nice idea but I see two potential problems:

(1) Git hosting services (GitLab, BitBucket, GitHub, ...) would still show these files as binary. That's a huge problem for my users as they interact more with these services than the Git command line. That's the main reason why I implemented the "UTF-8 as canonical form" approach in my series.

(2) You can only detect a BOM if the encoding is UTF-16. UTF-16BE and UTF-16LE must not have a BOM and therefore cannot be easily detected. Plus, even if you detect an UTF-16 BOM then it would be just a hint that the file is likely UTF-16 encoded as the sequence could be there by chance.

I still think it would be nice to see diffs for arbitrary encodings. Would it be an option to read the encoding attribute and use it in git diff?

- Lars

Quoted 255 lines

git show HEAD

commit 9f7d43f29eaf6017b7b16261ce91d8ef182cf415

Author: Torsten Bögershausen <[email protected]>
Date:   Fri Feb 2 15:35:23 2018 +0100

Auto diff of UTF-16 files in UTF-8

When an UTF-16 file is commited and later changed, git diff shows "Binary files XX and YY differ".

When the user wants a diff in UTF-8, a textconv needs to be specified in .gitattributes and the textconv must be configured.

A more user-friendly diff can be produced for UTF-16 if
- the user did not use git diff --binary
- the blob is identified as binary
- the blob has an UTF-16 BOM
- the blob can be converted into UTF-8

Enhance the diff machinery to auto-detect UTF-16 blobs and show them as UTF-8, unless the user specifies git diff --binary which creates a binary diff.

diff.c +41 -2
index fb22b19f09..51831ee94d 100644
@@ -3192,6 +3192,10 @@ static void builtin_diff(const char *name_a,
 			strbuf_reset(&header);
 		}
 
+		if (one && one->reencoded_from_utf16)
+			strbuf_addf(&header, "a is converted to UTF-8 from UTF-16\n");
+		if (two && two->reencoded_from_utf16)
+			strbuf_addf(&header, "b is converted to UTF-8 from UTF-16\n");
 		mf1.size = fill_textconv(textconv_one, one, &mf1.ptr);
 		mf2.size = fill_textconv(textconv_two, two, &mf2.ptr);
 
@@ -3611,8 +3615,25 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
 			s->size = size;
 			s->should_free = 1;
 		}
-	}
-	else {
+		if (!s->binary && buffer_is_binary(s->data, s->size) &&
+		    buffer_has_utf16_bom(s->data, s->size)) {
+			int outsz = 0;
+			char *outbuf;
+			outbuf = reencode_string_len(s->data, (int)s->size,
+						     "UTF-8", "UTF-16", &outsz);
+			if (outbuf) {
+				if (s->should_free)
+					free(s->data);
+				if (s->should_munmap)
+					munmap(s->data, s->size);
+				s->should_munmap = 0;
+				s->data = outbuf;
+				s->size = outsz;
+				s->reencoded_from_utf16 = 1;
+				s->should_free = 1;
+			}
+		}
+	} else {
 		enum object_type type;
 		if (size_only || (flags & CHECK_BINARY)) {
 			type = sha1_object_info(s->oid.hash, &s->size);
@@ -3629,6 +3650,19 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
 		s->data = read_sha1_file(s->oid.hash, &type, &s->size);
 		if (!s->data)
 			die("unable to read %s", oid_to_hex(&s->oid));
+		if (!s->binary && buffer_is_binary(s->data, s->size) &&
+		    buffer_has_utf16_bom(s->data, s->size)) {
+			int outsz = 0;
+			char *buf;
+			buf = reencode_string_len(s->data, (int)s->size,
+						  "UTF-8", "UTF-16", &outsz);
+			if (buf) {
+				free(s->data);
+				s->data = buf;
+				s->size = outsz;
+				s->reencoded_from_utf16 = 1;
+			}
+		}
 		s->should_free = 1;
 	}
 	return 0;
@@ -5695,6 +5729,10 @@ static int diff_filespec_is_identical(struct diff_filespec *one,
 
 static int diff_filespec_check_stat_unmatch(struct diff_filepair *p)
 {
+	if (p->binary) {
+		p->one->binary = 1;
+		p->two->binary = 1;
+	}
 	if (p->done_skip_stat_unmatch)
 		return p->skip_stat_unmatch_result;
 
@@ -5735,6 +5773,7 @@ static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
 	for (i = 0; i < q->nr; i++) {
 		struct diff_filepair *p = q->queue[i];
 
+		p->binary = diffopt->flags.binary;
 		if (diff_filespec_check_stat_unmatch(p))
 			diff_q(&outq, p);
 		else {
diffcore.h +3 -0
index a30da161da..3cd97bb93b 100644
@@ -47,6 +47,8 @@ struct diff_filespec {
 	unsigned has_more_entries : 1; /* only appear in combined diff */
 	/* data should be considered "binary"; -1 means "don't know yet" */
 	signed int is_binary : 2;
+	unsigned binary : 1;
+	unsigned reencoded_from_utf16 : 1;
 	struct userdiff_driver *driver;
 };
 
@@ -72,6 +74,7 @@ struct diff_filepair {
 	unsigned is_unmerged : 1;
 	unsigned done_skip_stat_unmatch : 1;
 	unsigned skip_stat_unmatch_result : 1;
+	unsigned binary : 1;
 };
 #define DIFF_PAIR_UNMERGED(p) ((p)->is_unmerged)
 
t/t4066-diff-encoding.sh new file +98 -0
new file mode 100755
index 0000000000..9bb3c70ada
@@ -0,0 +1,98 @@
+#!/bin/sh
+
+test_description='git diff with encoding attribute'
+
+. ./test-lib.sh
+
+printf '\303\244rger\n\303\266se\n\303\274bel\n' |
+	iconv -f UTF-8 -t UTF-16 >UTF-16
+printf '\303\266se\n\303\274bel\n\303\245st\n' |
+	iconv -f UTF-8 -t UTF-16 >file2
+
+test_expect_success 'setup' '
+	cp UTF-16 file &&
+	git add file &&
+	git commit -m "add file in UTF-16" &&
+	test_tick &&
+	echo "file encoding=UTF-16" >.gitattributes
+'
+
+test_expect_success 'diff against local change' '
+	cp file2 file &&
+	test_tick &&
+	cat >expect <<-\EOF &&
+	diff --git a/file b/file
+	index 26acf09..e98d27a 100644
+	a is converted to UTF-8 from UTF-16
+	b is converted to UTF-8 from UTF-16
+	--- a/file
+	+++ b/file
+	@@ -1,3 +1,3 @@
+	-ärger
+	 öse
+	 übel
+	+åst
+EOF
+	git diff file >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'diff --binary against local change' '
+	cp file2 file &&
+	test_tick &&
+	cat >expect <<-\EOF &&
+	diff --git a/file b/file
+	index 26acf09b0aad19fb22566956d1a39cb4e2a3b420..e98d27acfb90cfcfc84fcc5173baa4aa7828290f 100644
+	GIT binary patch
+	literal 28
+	ecmezW?;ArgLn;Fo!ykquAe{qbJq3!C0BHb{ln3Pi
+
+	literal 32
+	icmezW?+HT@Lpnn$kmO?c#!w7oaWVX1NCMJ1Ko$VA_z0~4
+
+EOF
+	git diff --binary file >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'commit local change' '
+	git add file &&
+	git commit -m "add file V2 in UTF-16" &&
+	test_tick
+'
+
+test_expect_success 'diff HEAD against HEAD^' '
+	cat >expect <<-\EOF &&
+	diff --git a/file b/file
+	index 26acf09..e98d27a 100644
+	a is converted to UTF-8 from UTF-16
+	b is converted to UTF-8 from UTF-16
+	--- a/file
+	+++ b/file
+	@@ -1,3 +1,3 @@
+	-ärger
+	 öse
+	 übel
+	+åst
+EOF
+	git diff HEAD^ HEAD -- file >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'diff --binary HEAD against HEAD^' '
+	cat >expect <<-\EOF &&
+	diff --git a/file b/file
+	index 26acf09b0aad19fb22566956d1a39cb4e2a3b420..e98d27acfb90cfcfc84fcc5173baa4aa7828290f 100644
+	GIT binary patch
+	literal 28
+	ecmezW?;ArgLn;Fo!ykquAe{qbJq3!C0BHb{ln3Pi
+
+	literal 32
+	icmezW?+HT@Lpnn$kmO?c#!w7oaWVX1NCMJ1Ko$VA_z0~4
+	
+EOF
+	git diff --binary HEAD^ HEAD -- file >actual &&
+	test_cmp expect actual
+'
+
+test_done
utf8.h +11 -0
index 6bbcf31a83..a2184d0300 100644
@@ -16,6 +16,17 @@ int utf8_fprintf(FILE *, const char *, ...);
 extern const char utf8_bom[];
 extern int skip_utf8_bom(char **, size_t);
 
+static inline int buffer_has_utf16_bom(const void *buf, size_t len) {
+  const unsigned char *text = (unsigned char *)buf;
+  if (!text ||  len < 2)
+    return 0;
+  if (text[0] == 0xff && text[1] == 0xfe)
+    return 1;
+  if (text[0] == 0xfe && text[1] == 0xff)
+    return 1;
+  return 0;
+}
+
 void strbuf_add_wrapped_text(struct strbuf *buf,
 		const char *text, int indent, int indent2, int width);
 void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,
#21 lore raw
Quoted 11 lines · Lars Schneider
Quoted 3 lines

If it was only about a diff of UTF-16 files, I may suggest a patch. I simply copy-paste it here for review, if someone thinks that it may be useful, I can send it as a real patch/RFC.

That's a nice idea but I see two potential problems:

(1) Git hosting services (GitLab, BitBucket, GitHub, ...) would still show these files as binary. That's a huge problem for my users as they interact more with these services than the Git command line. That's the main reason why I implemented the "UTF-8 as canonical form" approach in my series.

I can't speak for the other services, but I can tell you that GitHub would be pretty eager to enable such a feature if it existed.

I suspect most services providing human-readable diffs would want to do the same. Though there are still cases where you'd see a binary patch (e.g., format-patch in emails, or GitHub's .patch endpoint, since those are meant to be applied and must contain the "real" data).

-Peff

#22 lore raw
In reply to Lars Schneider
Quoted 3 lines · Lars Schneider

I still think it would be nice to see diffs for arbitrary encodings. Would it be an option to read the encoding attribute and use it in git diff?

Reusing that gitk-only thing and suddenly start doing so would break gitk users, no? The tool expects the diff to come out encoded in the encoding that is specified by that attribute (which is learned from get_path_encoding helper) and does its thing.

I guess that gitk uses diff-tree plumbing and you won't be applying this change to the plumbing, perhaps? If so, it might not be too bad, but those who decided to postprocess "git diff" output (instead of "git diff-tree" output) mimicking how gitk does it by thinking that is the safe and sane thing to do will be broken by such a change. You could do "use the encoding only when a command line option says so", but then people will add a configuration variable to turn it always on and these existing scripts will be broken.

I do not personally have much sympathy for the last case (i.e. those who scripted around 'git diff' instead of 'git diff-tree' to get broken), so making the new feature only work with the Porcelain "git diff" might be an option. I'll need a bit more time to formulate the rest of my thought ;-)

#23 lore raw
Quoted 20 lines · Junio C Hamano
Quoted 3 lines · Lars Schneider

I still think it would be nice to see diffs for arbitrary encodings. Would it be an option to read the encoding attribute and use it in git diff?

Reusing that gitk-only thing and suddenly start doing so would break gitk users, no? The tool expects the diff to come out encoded in the encoding that is specified by that attribute (which is learned from get_path_encoding helper) and does its thing.

I guess that gitk uses diff-tree plumbing and you won't be applying this change to the plumbing, perhaps? If so, it might not be too bad, but those who decided to postprocess "git diff" output (instead of "git diff-tree" output) mimicking how gitk does it by thinking that is the safe and sane thing to do will be broken by such a change. You could do "use the encoding only when a command line option says so", but then people will add a configuration variable to turn it always on and these existing scripts will be broken.

Quoted 5 lines

I do not personally have much sympathy for the last case (i.e. those who scripted around 'git diff' instead of 'git diff-tree' to get broken), so making the new feature only work with the Porcelain "git diff" might be an option. I'll need a bit more time to formulate the rest of my thought ;-)

So we are introducing in this series a way to say in what encoding the things should be placed in the working tree files (i.e. the w-t-e attribute attached to the paths). Currently there is no mechanism to say what encoding the in-repo contents are and UTF-8 is assumed when conversion from/to w-t-e is required, but there is no fundamental reason why it shouldn't be customizable (if anything, as a piece of fact on the in-repo data, in-repo-encoding is *more* appropriate to be an attribute than w-t-e that can merely be project preference at best, as I mentioned earlier in this thread).

We always use the in-repo contents when generating 'diff'. I think by "attribute to be used in diff", what you are reallying after is to convert the in-repo contents to that encoding _BEFORE_ running 'diff' on it. E.g. in-repo UTF-16 that can have NUL bytes all over the place will not diff well with the xdiff machinery, but if you first convert it to UTF-8 and have xdiff work on it, you can get reasonable result out of it. It is unclear what encoding you want your final diff output in (it is equally valid in such a set-up to desire your patch output in UTF-16 or UTF-8), but assuming that you want UTF-8 in your patch output, perhaps we do not have to break gitk users by hijacking the 'encoding' attribute. Instead what you want is a single bit that says between in-repo or working tree which representation should be given to the xdiff machinery. When that bit is set, then we

- First ensure that both sides of the diff input is in the working tree encoding by running it through convert_to_working_tree();

- Run xdiff on it;

- Take the xdiff result, and run it through convert_to_git(), before emitting (this is optional, making this a one-and-half bit option).

That would allow you to say "I have in-repo data in UTF-16 which I check out as UTF-8. xdiff machinery is unhappy. Please do something." perhaps?

The other way around (i.e. in-repo is UTF-8, but working tree encoding is UTF-16) won't need xdiff issues, I would imagine.

#24 lore raw
Quoted 31 lines · Junio C Hamano
Quoted 20 lines · Junio C Hamano
Quoted 3 lines · Lars Schneider

I still think it would be nice to see diffs for arbitrary encodings. Would it be an option to read the encoding attribute and use it in git diff?

Reusing that gitk-only thing and suddenly start doing so would break gitk users, no? The tool expects the diff to come out encoded in the encoding that is specified by that attribute (which is learned from get_path_encoding helper) and does its thing.

I guess that gitk uses diff-tree plumbing and you won't be applying this change to the plumbing, perhaps? If so, it might not be too bad, but those who decided to postprocess "git diff" output (instead of "git diff-tree" output) mimicking how gitk does it by thinking that is the safe and sane thing to do will be broken by such a change. You could do "use the encoding only when a command line option says so", but then people will add a configuration variable to turn it always on and these existing scripts will be broken.

Quoted 5 lines

I do not personally have much sympathy for the last case (i.e. those who scripted around 'git diff' instead of 'git diff-tree' to get broken), so making the new feature only work with the Porcelain "git diff" might be an option. I'll need a bit more time to formulate the rest of my thought ;-)

Quoted 9 lines

So we are introducing in this series a way to say in what encoding the things should be placed in the working tree files (i.e. the w-t-e attribute attached to the paths). Currently there is no mechanism to say what encoding the in-repo contents are and UTF-8 is assumed when conversion from/to w-t-e is required, but there is no fundamental reason why it shouldn't be customizable (if anything, as a piece of fact on the in-repo data, in-repo-encoding is *more* appropriate to be an attribute than w-t-e that can merely be project preference at best, as I mentioned earlier in this thread).

Correct.

Quoted 5 lines

We always use the in-repo contents when generating 'diff'. I think by "attribute to be used in diff", what you are reallying after is to convert the in-repo contents to that encoding _BEFORE_ running 'diff' on it. E.g. in-repo UTF-16 that can have NUL bytes all over the place will not diff well with the xdiff machinery, but if you

Quoted 8 lines

first convert it to UTF-8 and have xdiff work on it, you can get reasonable result out of it. It is unclear what encoding you want your final diff output in (it is equally valid in such a set-up to desire your patch output in UTF-16 or UTF-8), but assuming that you want UTF-8 in your patch output, perhaps we do not have to break gitk users by hijacking the 'encoding' attribute. Instead what you want is a single bit that says between in-repo or working tree which representation should be given to the xdiff machinery.

I fear that we could confuse users with an additional knob/bit that defines what we diff against. Git always diff'ed against in-repo content and I feel it should stay that way.

However, I agree with your earlier emails that "working-tree-encoding" is just one half of the feature. I also think it would be nice to be able to define the "in-repo-encoding" as well. Then we could define something like that:

    *.foo 		text in-repo-encoding=UTF-16LE

This tells Git that the file is stored as UTF-16LE. This would help Git generating a diff via UTF-8 conversion. I feel that the final patch should be in UTF-16LE again. Maybe over time we could then deprecate the "encoding" attribute as the "in-repo-encoding" attribute serves a similar purpose (maybe gitk can switch to it).

In that case we could also do things like that:

*.bar text working-tree-encoding=SHIFT-JIS in-repo-encoding=UTF-16LE

SHIFT-JIS encoded files would be reencoded to UTF-16LE on checkin. On checkout the opposite would happen. This way we would lift the "UTF-8 is the only in-repo encoding" limitation of the current w-t-e implementation.

Does this sound sensible to you? That being said, I think "in-repo-encoding" would deserve an own series.

- Lars

#25 lore raw
Quoted 14 lines · Lars Schneider
Quoted 5 lines

We always use the in-repo contents when generating 'diff'. I think by "attribute to be used in diff", what you are reallying after is to convert the in-repo contents to that encoding _BEFORE_ running 'diff' on it. E.g. in-repo UTF-16 that can have NUL bytes all over the place will not diff well with the xdiff machinery, but if you

Quoted 8 lines

first convert it to UTF-8 and have xdiff work on it, you can get reasonable result out of it. It is unclear what encoding you want your final diff output in (it is equally valid in such a set-up to desire your patch output in UTF-16 or UTF-8), but assuming that you want UTF-8 in your patch output, perhaps we do not have to break gitk users by hijacking the 'encoding' attribute. Instead what you want is a single bit that says between in-repo or working tree which representation should be given to the xdiff machinery.

Quoted 3 lines

I fear that we could confuse users with an additional knob/bit that defines what we diff against. Git always diff'ed against in-repo content and I feel it should stay that way.

Well, except for textconv. You can already do this:

echo "foo diff=utf16" >.gitattributes
git config diff.utf16.textconv 'iconv -f utf16 -t utf8'

We could make that easier to use and much more efficient by:

1. Allowing a special syntax for textconv filters that kicks off an internal iconv.

2. Providing baked-in config for utf16.

The patch below provides a sketch. But I think Torsten raised a good point that you might want the encoding conversion to be independent of other diff characteristics (so, e.g., you might say "this is utf16 but once converted treat it like C code for finding funcnames, etc").

---

diff.c +18 -0
index 21c3838b25..04032e059c 100644
@@ -5968,6 +5968,21 @@ struct diff_filepair *diff_unmerge(struct diff_options *options, const char *pat
 	return pair;
 }
 
+static char *iconv_textconv(const char *encoding, struct diff_filespec *spec,
+			    size_t *outsize)
+{
+	char *ret;
+	int outsize_int; /* this really should be a size_t */
+
+	if (diff_populate_filespec(spec, 0))
+		die("unable to load content for %s", spec->path);
+	ret = reencode_string_len(spec->data, spec->size,
+				  "utf-8", /* should be log_output_encoding? */
+				  encoding, &outsize_int);
+	*outsize = outsize_int;
+	return ret;
+}
+
 static char *run_textconv(const char *pgm, struct diff_filespec *spec,
 		size_t *outsize)
 {
@@ -5978,6 +5993,9 @@ static char *run_textconv(const char *pgm, struct diff_filespec *spec,
 	struct strbuf buf = STRBUF_INIT;
 	int err = 0;
 
+	if (skip_prefix(pgm, "iconv:", &pgm))
+		return iconv_textconv(pgm, spec, outsize);
+
 	temp = prepare_temp_file(spec->path, spec);
 	*arg++ = pgm;
 	*arg++ = temp->name;
userdiff.c +1 -0
index dbfb4e13cd..48fa7e8bdd 100644
@@ -161,6 +161,7 @@ IPATTERN("css",
 	 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
 	 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
 ),
+{ "utf16", NULL, -1, { NULL, 0 }, NULL, "iconv:utf16" },
 { "default", NULL, -1, { NULL, 0 } },
 };
 #undef PATTERNS
#26 lore raw
Quoted 79 lines · Jeff King
Quoted 14 lines · Lars Schneider
Quoted 5 lines

We always use the in-repo contents when generating 'diff'. I think by "attribute to be used in diff", what you are reallying after is to convert the in-repo contents to that encoding _BEFORE_ running 'diff' on it. E.g. in-repo UTF-16 that can have NUL bytes all over the place will not diff well with the xdiff machinery, but if you

Quoted 8 lines

first convert it to UTF-8 and have xdiff work on it, you can get reasonable result out of it. It is unclear what encoding you want your final diff output in (it is equally valid in such a set-up to desire your patch output in UTF-16 or UTF-8), but assuming that you want UTF-8 in your patch output, perhaps we do not have to break gitk users by hijacking the 'encoding' attribute. Instead what you want is a single bit that says between in-repo or working tree which representation should be given to the xdiff machinery.

Quoted 3 lines

I fear that we could confuse users with an additional knob/bit that defines what we diff against. Git always diff'ed against in-repo content and I feel it should stay that way.

Well, except for textconv. You can already do this:

echo "foo diff=utf16" >.gitattributes
git config diff.utf16.textconv 'iconv -f utf16 -t utf8'

We could make that easier to use and much more efficient by:

1. Allowing a special syntax for textconv filters that kicks off an internal iconv.

2. Providing baked-in config for utf16.

The patch below provides a sketch. But I think Torsten raised a good point that you might want the encoding conversion to be independent of other diff characteristics (so, e.g., you might say "this is utf16 but once converted treat it like C code for finding funcnames, etc").

---

diff.c +18 -0
index 21c3838b25..04032e059c 100644
@@ -5968,6 +5968,21 @@ struct diff_filepair *diff_unmerge(struct diff_options *options, const char *pat
 	return pair;
 }
 
+static char *iconv_textconv(const char *encoding, struct diff_filespec *spec,
+			    size_t *outsize)
+{
+	char *ret;
+	int outsize_int; /* this really should be a size_t */
+
+	if (diff_populate_filespec(spec, 0))
+		die("unable to load content for %s", spec->path);
+	ret = reencode_string_len(spec->data, spec->size,
+				  "utf-8", /* should be log_output_encoding? */
+				  encoding, &outsize_int);
+	*outsize = outsize_int;
+	return ret;
+}
+
 static char *run_textconv(const char *pgm, struct diff_filespec *spec,
 		size_t *outsize)
 {
@@ -5978,6 +5993,9 @@ static char *run_textconv(const char *pgm, struct diff_filespec *spec,
 	struct strbuf buf = STRBUF_INIT;
 	int err = 0;
 
+	if (skip_prefix(pgm, "iconv:", &pgm))
+		return iconv_textconv(pgm, spec, outsize);
+
 	temp = prepare_temp_file(spec->path, spec);
 	*arg++ = pgm;
 	*arg++ = temp->name;
userdiff.c +0 -0
index dbfb4e13cd..48fa7e8bdd 100644

userdiff.c IPATTERN("css", · line 167

Quoted 7 lines
userdiff.c +1 -0
@@ -161,6 +161,7 @@ IPATTERN("css",
 	 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
 	 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
 ),
+{ "utf16", NULL, -1, { NULL, 0 }, NULL, "iconv:utf16" },
 { "default", NULL, -1, { NULL, 0 } },
 };
 #undef PATTERNS

The patch looks like a possible step into the right direction - some minor notes: "utf8" is better written as "UTF-8", when talking to iconv.h, same for utf16.

But, how do I activate the diff ?
I have in .gitattributes
XXXenglish.txt diff=UTF-16

and in .git/config
[diff "UTF-16"]
command = iconv:UTF-16

What am I doing wrong ?

#27 lore raw
Quoted 21 lines · Torsten Bögershausen

userdiff.c IPATTERN("css", · line 167

Quoted 12 lines
userdiff.c +1 -0
index dbfb4e13cd..48fa7e8bdd 100644
@@ -161,6 +161,7 @@ IPATTERN("css",
 	 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
 	 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
 ),
+{ "utf16", NULL, -1, { NULL, 0 }, NULL, "iconv:utf16" },
 { "default", NULL, -1, { NULL, 0 } },
 };
 #undef PATTERNS

The patch looks like a possible step into the right direction - some minor notes: "utf8" is better written as "UTF-8", when talking to iconv.h, same for utf16.

But, how do I activate the diff ?
I have in .gitattributes
XXXenglish.txt diff=UTF-16

Quoted 6 lines

and in .git/config
[diff "UTF-16"]
command = iconv:UTF-16

What am I doing wrong ?

After applying the patch, if I do:

git init
echo hello | iconv -f utf8 -t utf16 >file
git add file
git commit -m one
echo goodbye | iconv -f utf8 -t utf16 >file
git add file
git commit -m two

then:

git log -p

shows "binary files differ" but:

echo "file diff=utf16" >.gitattributes
git log -p

shows text diffs. I assume you tweaked the patch before switching to the UTF-16 spelling in your example. Did you use a plumbing command to show the diff? textconv isn't enabled for plumbing, because the resulting patches cannot actually be applied (in that sense an encoding switch is potentially special, since in theory one could convert to the canonical text format, apply the patch, and then convert back).

-Peff

#28 lore raw
Quoted 49 lines · Jeff King
Quoted 21 lines · Torsten Bögershausen

userdiff.c IPATTERN("css", · line 167

Quoted 12 lines
userdiff.c +1 -0
index dbfb4e13cd..48fa7e8bdd 100644
@@ -161,6 +161,7 @@ IPATTERN("css",
 	 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
 	 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
 ),
+{ "utf16", NULL, -1, { NULL, 0 }, NULL, "iconv:utf16" },
 { "default", NULL, -1, { NULL, 0 } },
 };
 #undef PATTERNS

The patch looks like a possible step into the right direction - some minor notes: "utf8" is better written as "UTF-8", when talking to iconv.h, same for utf16.

But, how do I activate the diff ?
I have in .gitattributes
XXXenglish.txt diff=UTF-16

Quoted 6 lines

and in .git/config
[diff "UTF-16"]
command = iconv:UTF-16

What am I doing wrong ?

After applying the patch, if I do:

git init
echo hello | iconv -f utf8 -t utf16 >file
git add file
git commit -m one
echo goodbye | iconv -f utf8 -t utf16 >file
git add file
git commit -m two

then:

git log -p

shows "binary files differ" but:

echo "file diff=utf16" >.gitattributes
git log -p

Quoted 8 lines

shows text diffs. I assume you tweaked the patch before switching to the UTF-16 spelling in your example. Did you use a plumbing command to show the diff? textconv isn't enabled for plumbing, because the resulting patches cannot actually be applied (in that sense an encoding switch is potentially special, since in theory one could convert to the canonical text format, apply the patch, and then convert back).

-Peff

Thanks for helping me out.
I didn't use "git log -p", but a simple "git diff".
(And after re-using utf16 with lowercase, it works as you described it)

I wasn't aware of "git log -p", something learned (or re-learned)

The other question is:
Would this help showing diffs of UTF-16 encoded files on a "git hoster", github/bitbucket/.... ?

Or would the auto-magic UTF-16 avoid binary patch that I send out be more helpful ? Or both ?
Or the w-t-e encoding ?

Questions over questions.

#29 lore raw
Quoted 3 lines · Torsten Bögershausen

The other question is:
Would this help showing diffs of UTF-16 encoded files on a "git hoster", github/bitbucket/.... ?

Almost. There's probably one more thing needed. We don't currently read in-tree .gitattributes when doing a diff in a bare repository. And most hosting sites will store bare repositories.

And of course it would require the users to actually set the attributes themselves.

Quoted 3 lines

Or would the auto-magic UTF-16 avoid binary patch that I send out be more helpful ? Or both ?
Or the w-t-e encoding ?

Of the three solutions, I think the relative merits are something like this:

1. baked-in textconv (my patch)

- reuses an existing diff feature, so minimal code and not likely to break things

- requires people to add a .gitattributes entry

- needs work to make bare-repo .gitattributes work (though I think this would be useful for other features, too)

- has a run-time cost at each diff to do the conversion

- may sometimes annoy people when it doesn't kick in (e.g., emailed patches from format-patch won't have a readable diff)

- doesn't combine with other custom-diff config (e.g., utf-16 storing C code should still use diff=c funcname rules, but wouldn't with my patch)

2. auto-detect utf-16 (your patch)
- Just Works for existing repositories storing utf-16

- carries some risk of kicking in when people would like it not to (e.g., when they really do want a binary patch that can be applied).

I think it would probably be OK if this kicked in only when ALLOW_TEXTCONV is set (the default for porcelain), and --binary is not (i.e., when we would otherwise just say "binary files differ").

- similar to (1), carries a run-time cost for each diff, and users may sometimes still see binary diffs

3. w-t-e (Lars's patch)

- requires no server-side modifications; the diff is plain vanilla

- works everywhere you diff, plumbing and porcelain

- does require people to add a .gitattributes entry

- run-time cost is per-checkout, not per-diff

So I can see room for (3) to co-exist alongside the others. Between (1) and (2), I think (2) is probably the better direction.

-Peff

#30 lore raw
Quoted 4 lines · Jeff King

Of the three solutions, I think the relative merits are something like this:
...
3. w-t-e (Lars's patch)

I thought Lars's w-t-e was about keeping the in-repo contents in UTF-8 and externalize in whatever encoding (e.g. UTF-16), so it won't help the issue hosting folks want to deal with, i.e. showing in-repo data that is stored in a strange binary-looking encoding in a more reasonable encodign while diffing, no?

Usually we only work in-repo encoding when producing a diff and show the result in in-repo encoding, but I can imagine a new attribute, when set, we first convert in-repo to the specified encoding before passing the result to xdiff machinery. Then convert it back to in-repo encoding before showing the diff (or just show the result in that encoding xdiff machinery processed---I do not know which one should be the default).

#31 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Jeff King

Of the three solutions, I think the relative merits are something like this:
...
3. w-t-e (Lars's patch)

Quoted 5 lines

I thought Lars's w-t-e was about keeping the in-repo contents in UTF-8 and externalize in whatever encoding (e.g. UTF-16), so it won't help the issue hosting folks want to deal with, i.e. showing in-repo data that is stored in a strange binary-looking encoding in a more reasonable encodign while diffing, no?

I thought it solved that by the hosting folks never seeing the strange binary-looking data. They see only utf8, which diffs well.

-Peff

#32 lore raw
Quoted 9 lines · Jeff King
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Jeff King

Of the three solutions, I think the relative merits are something like this:
...
3. w-t-e (Lars's patch)

Quoted 8 lines
Quoted 5 lines

I thought Lars's w-t-e was about keeping the in-repo contents in UTF-8 and externalize in whatever encoding (e.g. UTF-16), so it won't help the issue hosting folks want to deal with, i.e. showing in-repo data that is stored in a strange binary-looking encoding in a more reasonable encodign while diffing, no?

I thought it solved that by the hosting folks never seeing the strange binary-looking data. They see only utf8, which diffs well.

Ah, OK, that is a "fix" in a wider context (in a narrower context, "work around" is a more appropriate term ;-).

The reason why I have been nudging people toward considering in-repo encoding attribute is because forcing projects that already have their contents in a strange binary-looking encoding to switch is costly. But perhaps having them pay one-time conversion pain is a better investment longer term.

#33 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 2 lines

I thought it solved that by the hosting folks never seeing the strange binary-looking data. They see only utf8, which diffs well.

Ah, OK, that is a "fix" in a wider context (in a narrower context, "work around" is a more appropriate term ;-).

The reason why I have been nudging people toward considering in-repo encoding attribute is because forcing projects that already have their contents in a strange binary-looking encoding to switch is costly. But perhaps having them pay one-time conversion pain is a better investment longer term.

Yeah, thanks for mentioning that. It should have gone in my "relative merits" list. The conversion flag-day is definitely going to be a pain for users, and doesn't help with diffing older versions.

-Peff

#34 lore raw
In reply to Jeff King
Quoted 39 lines · Jeff King
Quoted 3 lines · Torsten Bögershausen

The other question is:
Would this help showing diffs of UTF-16 encoded files on a "git hoster", github/bitbucket/.... ?

Almost. There's probably one more thing needed. We don't currently read in-tree .gitattributes when doing a diff in a bare repository. And most hosting sites will store bare repositories.

And of course it would require the users to actually set the attributes themselves.

Quoted 3 lines

Or would the auto-magic UTF-16 avoid binary patch that I send out be more helpful ? Or both ?
Or the w-t-e encoding ?

Of the three solutions, I think the relative merits are something like this:

1. baked-in textconv (my patch)

- reuses an existing diff feature, so minimal code and not likely to break things

- requires people to add a .gitattributes entry

- needs work to make bare-repo .gitattributes work (though I think this would be useful for other features, too)

- has a run-time cost at each diff to do the conversion

- may sometimes annoy people when it doesn't kick in (e.g., emailed patches from format-patch won't have a readable diff)

- doesn't combine with other custom-diff config (e.g., utf-16 storing C code should still use diff=c funcname rules, but wouldn't with my patch)

Quoted 6 lines

2. auto-detect utf-16 (your patch)
- Just Works for existing repositories storing utf-16

- carries some risk of kicking in when people would like it not to (e.g., when they really do want a binary patch that can be applied).

The binary patch is still supported, but that detail may need some more explanation in the commit message. Please see t4066-diff-encoding.sh
test_expect_success 'diff --binary against local change' '

         cp file2 file &&
         test_tick &&
         cat >expect <<-\EOF &&
         diff --git a/file b/file
         index 26acf09b0aad19fb22566956d1a39cb4e2a3b420..e98d27acfb90cfcfc84fcc5173baa4aa7828290f 100644
         GIT binary patch
         literal 28
         ecmezW?;ArgLn;Fo!ykquAe{qbJq3!C0BHb{ln3Pi

         literal 32
         icmezW?+HT@Lpnn$kmO?c#!w7oaWVX1NCMJ1Ko$VA_z0~4
  EOF
         git diff --binary file >actual &&
         test_cmp expect actual
Quoted 5 lines

I think it would probably be OK if this kicked in only when ALLOW_TEXTCONV is set (the default for porcelain), and --binary is not (i.e., when we would otherwise just say "binary files differ").

The user can still use "git diff" (Where auto-detection of UTF-16 kicks in and replaces "binary files differ" with an UTF-8 diff.
When the user wants a patch, "git diff --binary" will generate a binary patch, as before.
The only thing which is missing is the line "binary files differ", which may be a regression. I can re-add it in V2.

Quoted 18 lines

- similar to (1), carries a run-time cost for each diff, and users may sometimes still see binary diffs

3. w-t-e (Lars's patch)

- requires no server-side modifications; the diff is plain vanilla

- works everywhere you diff, plumbing and porcelain

- does require people to add a .gitattributes entry

- run-time cost is per-checkout, not per-diff

So I can see room for (3) to co-exist alongside the others. Between (1) and (2), I think (2) is probably the better direction.

-Peff

#35 lore raw
Quoted 9 lines · Torsten Bögershausen
Quoted 6 lines

2. auto-detect utf-16 (your patch)
- Just Works for existing repositories storing utf-16

- carries some risk of kicking in when people would like it not to (e.g., when they really do want a binary patch that can be applied).

The binary patch is still supported, but that detail may need some more explanation in the commit message. Please see t4066-diff-encoding.sh

Yeah, but if you don't have binary-patches enabled we'd generate a bogus patch. Which, granted, without that you wouldn't be able to apply the patch either. But somehow it feels funny to me to generate something that _looks_ like a patch but you can't actually apply.

I also think we'd want a plan for this to be used consistently in other diff-like tools. E.g., "git blame" uses textconv for the starting file content, and it would be nice for this to kick in then, too. Ditto for things like grep, pickaxe, etc.

I have some patches that reuse some of the textconv infrastructure for this, which should mostly make it "just work" everywhere. They need a little more polishing before I post them, but you can take a look at:

https://github.com/peff/git.git jk/textconv-utf16

if you want.

-Peff

#36 lore raw
Quoted 7 lines · Jeff King
Quoted 2 lines

The binary patch is still supported, but that detail may need some more explanation in the commit message. Please see t4066-diff-encoding.sh

Yeah, but if you don't have binary-patches enabled we'd generate a bogus patch. Which, granted, without that you wouldn't be able to apply the patch either. But somehow it feels funny to me to generate something that _looks_ like a patch but you can't actually apply.

True. And at least you _could_ apply a properly formatted binary patch to the original.

Quoted 4 lines

I also think we'd want a plan for this to be used consistently in other diff-like tools. E.g., "git blame" uses textconv for the starting file content, and it would be nice for this to kick in then, too. Ditto for things like grep, pickaxe, etc.

You probably do not want to limit your thinking to the generation side. It is entirely plausible to have "we deal with diff in this encoding X" in addition to "the in-repo encoding for this project is this encoding Y" and "the working tree encoding for this path is Z" and allow them to interact in "git diff | git apply" pipeline.

"diff/format-patch --stdout/etc." on the upstream would first iconv Y to X and feed the contents in X to xdiff machinery, which is sent down the pipe and received by apply, which reads the preimage from the disk or from the repository. If doing "apply" without "--cached/--index", the preimage data from the disk would go through iconv Z to X. If doing "apply --cached/--index", the preimage data from the repo would go through iconv Y to X. The incoming patch is in X, so we apply, and the resulting postimage will be re-encoded in Z in the working tree and Y in the repository.

#37 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 4 lines

I also think we'd want a plan for this to be used consistently in other diff-like tools. E.g., "git blame" uses textconv for the starting file content, and it would be nice for this to kick in then, too. Ditto for things like grep, pickaxe, etc.

You probably do not want to limit your thinking to the generation side. It is entirely plausible to have "we deal with diff in this encoding X" in addition to "the in-repo encoding for this project is this encoding Y" and "the working tree encoding for this path is Z" and allow them to interact in "git diff | git apply" pipeline.

Quoted 9 lines

"diff/format-patch --stdout/etc." on the upstream would first iconv Y to X and feed the contents in X to xdiff machinery, which is sent down the pipe and received by apply, which reads the preimage from the disk or from the repository. If doing "apply" without "--cached/--index", the preimage data from the disk would go through iconv Z to X. If doing "apply --cached/--index", the preimage data from the repo would go through iconv Y to X. The incoming patch is in X, so we apply, and the resulting postimage will be re-encoded in Z in the working tree and Y in the repository.

I agree that would be convenient, but I have to wonder if all the complexity is worth it to maintain the idea of a distinct in-repo representation. It seems like it would open up a ton of corner cases. And I suspect most people would be happy enough with either a clean/smudge style worktree conversion or a textconv-style view.

So if somebody wants to work on it, I don't want to stop them. But I think there's room for the simpler solutions in the meantime.

-Peff

#38 lore raw
In reply to Jeff King
Quoted 26 lines · Jeff King
Quoted 9 lines · Torsten Bögershausen
Quoted 6 lines

2. auto-detect utf-16 (your patch)
- Just Works for existing repositories storing utf-16

- carries some risk of kicking in when people would like it not to (e.g., when they really do want a binary patch that can be applied).

The binary patch is still supported, but that detail may need some more explanation in the commit message. Please see t4066-diff-encoding.sh

Yeah, but if you don't have binary-patches enabled we'd generate a bogus patch. Which, granted, without that you wouldn't be able to apply the patch either. But somehow it feels funny to me to generate something that _looks_ like a patch but you can't actually apply.

I also think we'd want a plan for this to be used consistently in other diff-like tools. E.g., "git blame" uses textconv for the starting file content, and it would be nice for this to kick in then, too. Ditto for things like grep, pickaxe, etc.

I have some patches that reuse some of the textconv infrastructure for this, which should mostly make it "just work" everywhere. They need a little more polishing before I post them, but you can take a look at:

Quoted 6 lines

https://github.com/peff/git.git jk/textconv-utf16

if you want.

-Peff

Thanks for your work (I actually found some time to take look)

I am looking at the code to put 2 or 3 things on top of it:
- test case(s)
- documentation
- teach diff to add a line "b is converted to UTF-8 from UTF-16"
- teach apply to reads & understands the encoding line and throws in a "reencode_string_len() like your patch does

This would keep "git diff | git apply" happy.
All in all the changes do not look too invasive, at least from my point of view.

#39 lore raw
In reply to Jeff King
Quoted 46 lines · Jeff King

On Tue, Feb 27, 2018 at 10:05:17PM +0100, Torsten Bögershausen wrote:

Of the three solutions, I think the relative merits are something like this:

1. baked-in textconv (my patch)

- reuses an existing diff feature, so minimal code and not likely to break things

- requires people to add a .gitattributes entry

- needs work to make bare-repo .gitattributes work (though I think this would be useful for other features, too)

- has a run-time cost at each diff to do the conversion

- may sometimes annoy people when it doesn't kick in (e.g., emailed patches from format-patch won't have a readable diff)

- doesn't combine with other custom-diff config (e.g., utf-16 storing C code should still use diff=c funcname rules, but wouldn't with my patch)

2. auto-detect utf-16 (your patch)
- Just Works for existing repositories storing utf-16

- carries some risk of kicking in when people would like it not to (e.g., when they really do want a binary patch that can be applied).

I think it would probably be OK if this kicked in only when ALLOW_TEXTCONV is set (the default for porcelain), and --binary is not (i.e., when we would otherwise just say "binary files differ").

- similar to (1), carries a run-time cost for each diff, and users may sometimes still see binary diffs

3. w-t-e (Lars's patch)

- requires no server-side modifications; the diff is plain vanilla

Quoted 8 lines

- works everywhere you diff, plumbing and porcelain

- does require people to add a .gitattributes entry

- run-time cost is per-checkout, not per-diff

So I can see room for (3) to co-exist alongside the others. Between (1) and (2), I think (2) is probably the better direction.

Thanks for the great summary! I agree they could co-exist and people could use whatever works best for them.

I'll incorporate Eric's feedback and send a w-t-e v9 soonish.

- Lars

#40 lore raw
In reply to Lars Schneider
Quoted 8 lines · Lars Schneider
Quoted 7 lines

One thing I find more problematic is that the above places *too* much stress on the UTF-8 centric worldview. It is perfectly valid to store your text contents encoded in ShiftJIS and check them out as-is, with or without this patch. It is grossly misleading to say that older versions of Git will check them out in UTF-8. "will checkout these files as-is without encoding conversion" is a better way to say it, probably.

Quoted 6 lines

True. But that's not what I wanted to say in the "pitfalls" section. If my Git client supports w-t-e and I add the ShiftJIS encoded file "foo.bar" to my repository, then Git will store the file as UTF-8 _internally_. That means if you clone my repository and your Git client does _not_ support w-t-e, then you will see "foo.bar" as UTF-8 encoded.

What you wrote implies *more* than that, which is what I had trouble with.

If you say "what you have is checked out as-is", then it is still clear that those who use w-t-e to convert non UTF-8 into UTF-8 when checking in will get UTF-8 out when they use an older version of Git. If you say "what you have will be checked out in UTF-8", it makes it sound as if pre w-t-e Git will somehow reject non UTF-8 in-tree contents, or magically convert anything to UTF-8 while checking out, which is *not* what you want to imply.

Quoted 8 lines
Quoted 6 lines

Also notice that even in the world with w-t-e, such a project won't benefit from w-t-e at all. After all, they have been happy using ShiftJIS in repository and using the same encoding on the working tree, and because w-t-e assumes that everybody should be using UTF-8 internally, such a project cannot take advantage of the new mechanism.

Agreed. However, people using ShiftJIS are not my target audience.

Be aware that you are writing *not* *solely* for your target audience. You write document for everybody, and make sure the description of a feature makes it clear who the feature primarily targets and how using (or not using) the feature affects users.