Skip to content

Commit 541d059

Browse files
larsxschneidergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a17918 commit 541d059

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

convert.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,29 @@ static int validate_encoding(const char *path, const char *enc,
324324
return 0;
325325
}
326326

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+
327350
static const char *default_encoding = "UTF-8";
328351

329352
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,
352375
if (validate_encoding(path, enc, src, src_len, die_on_error))
353376
return 0;
354377

378+
trace_encoding("source", path, enc, src, src_len);
355379
dst = reencode_string_len(src, src_len, default_encoding, enc,
356380
&dst_len);
357381
if (!dst) {
@@ -369,6 +393,7 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,
369393
return 0;
370394
}
371395
}
396+
trace_encoding("destination", path, default_encoding, dst, dst_len);
372397

373398
strbuf_attach(buf, dst, dst_len, dst_len + 1);
374399
return 1;

t/t0028-working-tree-encoding.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ test_description='working-tree-encoding conversion via gitattributes'
44

55
. ./test-lib.sh
66

7+
GIT_TRACE_WORKING_TREE_ENCODING=1 && export GIT_TRACE_WORKING_TREE_ENCODING
8+
79
test_expect_success 'setup test files' '
810
git config core.eol lf &&
911

0 commit comments

Comments
 (0)