Skip to content

Commit 1f17604

Browse files
committed
Merge branch 'lp/diff-stat-utf8-display-width-fix'
The computation of column width made by "git diff --stat" was confused when pathnames contain non-ASCII characters. * lp/diff-stat-utf8-display-width-fix: t4073: add test for diffstat paths length when containing UTF-8 chars diff: improve scaling of filenames in diffstat to handle UTF-8 chars
2 parents d83491a + 04f5d95 commit 1f17604

3 files changed

Lines changed: 68 additions & 11 deletions

File tree

diff.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,17 +2859,12 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
28592859
char *slash;
28602860
prefix = "...";
28612861
len -= 3;
2862-
/*
2863-
* NEEDSWORK: (name_len - len) counts the display
2864-
* width, which would be shorter than the byte
2865-
* length of the corresponding substring.
2866-
* Advancing "name" by that number of bytes does
2867-
* *NOT* skip over that many columns, so it is
2868-
* very likely that chomping the pathname at the
2869-
* slash we will find starting from "name" will
2870-
* leave the resulting string still too long.
2871-
*/
2872-
name += name_len - len;
2862+
if (len < 0)
2863+
len = 0;
2864+
2865+
while (name_len > len)
2866+
name_len -= utf8_width((const char**)&name, NULL);
2867+
28732868
slash = strchr(name, '/');
28742869
if (slash)
28752870
name = slash;

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ integration_tests = [
498498
't4070-diff-pairs.sh',
499499
't4071-diff-minimal.sh',
500500
't4072-diff-max-depth.sh',
501+
't4073-diff-stat-name-width.sh',
501502
't4100-apply-stat.sh',
502503
't4101-apply-nonl.sh',
503504
't4102-apply-rename.sh',

t/t4073-diff-stat-name-width.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/sh
2+
3+
test_description='git-diff check diffstat filepaths length when containing UTF-8 chars'
4+
5+
. ./test-lib.sh
6+
7+
8+
create_files () {
9+
mkdir -p "d你好" &&
10+
touch "d你好/f再见"
11+
}
12+
13+
test_expect_success 'setup' '
14+
git init &&
15+
git config core.quotepath off &&
16+
git commit -m "Initial commit" --allow-empty &&
17+
create_files &&
18+
git add . &&
19+
git commit -m "Added files"
20+
'
21+
22+
test_expect_success 'test name-width long enough for filepath' '
23+
git diff HEAD~1 HEAD --stat --stat-name-width=12 >out &&
24+
grep "d你好/f再见 |" out &&
25+
git diff HEAD~1 HEAD --stat --stat-name-width=11 >out &&
26+
grep "d你好/f再见 |" out
27+
'
28+
29+
test_expect_success 'test name-width not long enough for dir name' '
30+
git diff HEAD~1 HEAD --stat --stat-name-width=10 >out &&
31+
grep ".../f再见 |" out &&
32+
git diff HEAD~1 HEAD --stat --stat-name-width=9 >out &&
33+
grep ".../f再见 |" out
34+
'
35+
36+
test_expect_success 'test name-width not long enough for slash' '
37+
git diff HEAD~1 HEAD --stat --stat-name-width=8 >out &&
38+
grep "...f再见 |" out
39+
'
40+
41+
test_expect_success 'test name-width not long enough for file name' '
42+
git diff HEAD~1 HEAD --stat --stat-name-width=7 >out &&
43+
grep "...再见 |" out &&
44+
git diff HEAD~1 HEAD --stat --stat-name-width=6 >out &&
45+
grep "...见 |" out &&
46+
git diff HEAD~1 HEAD --stat --stat-name-width=5 >out &&
47+
grep "...见 |" out &&
48+
git diff HEAD~1 HEAD --stat --stat-name-width=4 >out &&
49+
grep "... |" out
50+
'
51+
52+
test_expect_success 'test name-width minimum length' '
53+
git diff HEAD~1 HEAD --stat --stat-name-width=3 >out &&
54+
grep "... |" out &&
55+
git diff HEAD~1 HEAD --stat --stat-name-width=2 >out &&
56+
grep "... |" out &&
57+
git diff HEAD~1 HEAD --stat --stat-name-width=1 >out &&
58+
grep "... |" out
59+
'
60+
61+
test_done

0 commit comments

Comments
 (0)