Merge branch 'lp/diff-stat-utf8-display-width-fix' into jch

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
This commit is contained in:
Junio C Hamano 2026-01-19 16:44:22 -08:00
commit fa6fbc85ca
3 changed files with 68 additions and 11 deletions

17
diff.c
View File

@ -2859,17 +2859,12 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
char *slash;
prefix = "...";
len -= 3;
/*
* NEEDSWORK: (name_len - len) counts the display
* width, which would be shorter than the byte
* length of the corresponding substring.
* Advancing "name" by that number of bytes does
* *NOT* skip over that many columns, so it is
* very likely that chomping the pathname at the
* slash we will find starting from "name" will
* leave the resulting string still too long.
*/
name += name_len - len;
if (len < 0)
len = 0;
while (name_len > len)
name_len -= utf8_width((const char**)&name, NULL);
slash = strchr(name, '/');
if (slash)
name = slash;

View File

@ -498,6 +498,7 @@ integration_tests = [
't4070-diff-pairs.sh',
't4071-diff-minimal.sh',
't4072-diff-max-depth.sh',
't4073-diff-stat-name-width.sh',
't4100-apply-stat.sh',
't4101-apply-nonl.sh',
't4102-apply-rename.sh',

61
t/t4073-diff-stat-name-width.sh Executable file
View File

@ -0,0 +1,61 @@
#!/bin/sh
test_description='git-diff check diffstat filepaths length when containing UTF-8 chars'
. ./test-lib.sh
create_files () {
mkdir -p "d你好" &&
touch "d你好/f再见"
}
test_expect_success 'setup' '
git init &&
git config core.quotepath off &&
git commit -m "Initial commit" --allow-empty &&
create_files &&
git add . &&
git commit -m "Added files"
'
test_expect_success 'test name-width long enough for filepath' '
git diff HEAD~1 HEAD --stat --stat-name-width=12 >out &&
grep "d你好/f再见 |" out &&
git diff HEAD~1 HEAD --stat --stat-name-width=11 >out &&
grep "d你好/f再见 |" out
'
test_expect_success 'test name-width not long enough for dir name' '
git diff HEAD~1 HEAD --stat --stat-name-width=10 >out &&
grep ".../f再见 |" out &&
git diff HEAD~1 HEAD --stat --stat-name-width=9 >out &&
grep ".../f再见 |" out
'
test_expect_success 'test name-width not long enough for slash' '
git diff HEAD~1 HEAD --stat --stat-name-width=8 >out &&
grep "...f再见 |" out
'
test_expect_success 'test name-width not long enough for file name' '
git diff HEAD~1 HEAD --stat --stat-name-width=7 >out &&
grep "...再见 |" out &&
git diff HEAD~1 HEAD --stat --stat-name-width=6 >out &&
grep "...见 |" out &&
git diff HEAD~1 HEAD --stat --stat-name-width=5 >out &&
grep "...见 |" out &&
git diff HEAD~1 HEAD --stat --stat-name-width=4 >out &&
grep "... |" out
'
test_expect_success 'test name-width minimum length' '
git diff HEAD~1 HEAD --stat --stat-name-width=3 >out &&
grep "... |" out &&
git diff HEAD~1 HEAD --stat --stat-name-width=2 >out &&
grep "... |" out &&
git diff HEAD~1 HEAD --stat --stat-name-width=1 >out &&
grep "... |" out
'
test_done