convert: add tracing for 'working-tree-encoding' attribute

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. Signed-off-by: Lars Schneider <larsxschneider@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lars Schneider committed Apr 15, 2018 at 20:16 UTC 541d059cd903bb8e510f876ea2bc33719b76b33c
2 files changed +27
convert.c
+25
@@ -324,6 +324,29 @@ static int validate_encoding(const char *path, const char *enc,
324 return 0;
325 }
326
327 +static void trace_encoding(const char *context, const char *path,
328 + const char *encoding, const char *buf, size_t len)
329 +{
330 + static struct trace_key coe = TRACE_KEY_INIT(WORKING_TREE_ENCODING);
331 + struct strbuf trace = STRBUF_INIT;
332 + int i;
333 +
334 + strbuf_addf(&trace, "%s (%s, considered %s):\n", context, path, encoding);
335 + for (i = 0; i < len && buf; ++i) {
336 + strbuf_addf(
337 + &trace,"| \e[2m%2i:\e[0m %2x \e[2m%c\e[0m%c",
338 + i,
339 + (unsigned char) buf[i],
340 + (buf[i] > 32 && buf[i] < 127 ? buf[i] : ' '),
341 + ((i+1) % 8 && (i+1) < len ? ' ' : '\n')
342 + );
343 + }
344 + strbuf_addchars(&trace, '\n', 1);
345 +
346 + trace_strbuf(&coe, &trace);
347 + strbuf_release(&trace);
348 +}
349 +
350 static const char *default_encoding = "UTF-8";
351
352 static int encode_to_git(const char *path, const char *src, size_t src_len,
@@ -352,6 +375,7 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,
375 if (validate_encoding(path, enc, src, src_len, die_on_error))
376 return 0;
377
378 + trace_encoding("source", path, enc, src, src_len);
379 dst = reencode_string_len(src, src_len, default_encoding, enc,
380 &dst_len);
381 if (!dst) {
@@ -369,6 +393,7 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,
393 return 0;
394 }
395 }
396 + trace_encoding("destination", path, default_encoding, dst, dst_len);
397
398 strbuf_attach(buf, dst, dst_len, dst_len + 1);
399 return 1;
t/t0028-working-tree-encoding.sh
+2
@@ -4,6 +4,8 @@ test_description='working-tree-encoding conversion via gitattributes'
4
5 . ./test-lib.sh
6
7 +GIT_TRACE_WORKING_TREE_ENCODING=1 && export GIT_TRACE_WORKING_TREE_ENCODING
8 +
9 test_expect_success 'setup test files' '
10 git config core.eol lf &&
11