Fix the non-ASCII filename check

Merged
SA
8d ago
+65-3

What changed and why

On macOS, the pre-commit hook refused every commit with "Non-ASCII filename detected", even when every file name was plain ASCII. The same check also let a non-ASCII name through when a file was renamed to it.

Check 7 of scripts/pre-commit.hook counts the bytes left after removing printable ASCII, and compares the count with 0 as a string. It had two faults:

  1. Padding. The BSD wc of macOS pads its count with spaces, and the quotes around the command substitution keep them. The test compared " 0" with 0, so it failed even when no non-ASCII name was staged. CI runs on Ubuntu, where GNU wc prints a bare 0, so the fault never showed there.
  2. Renames. --diff-filter=A lists only added paths. git mv report.txt 報告.txt is staged as a rename, so the new name was never counted.

This change does three things:

  • It strips the padding with tr -d ' ', as scripts/chain.sh already does with wc -w. The test stays a string comparison, so it fails closed: an empty or malformed count still refuses the commit, where an integer test (-ne) would let it pass.

  • It lists the paths with --diff-filter=ACR: the added, copied and renamed paths, which are all the paths that gain a name (a rename with edits is staged as a rename). Modified paths stay out, so a tracked file whose name is not ASCII can still be edited. Deleted paths stay out too, so such a file can be removed.

    if test "$(git diff --cached --name-only --diff-filter=ACR -z |
      LC_ALL=C tr -d '[ -~]\0' | wc -c | tr -d ' ')" != 0; then
    
  • It adds FilenameTest to scripts/test_hooks.py. An added or a renamed non-ASCII name must be refused. ASCII names must pass, and so must an edit of a tracked non-ASCII name. Each case runs twice: once with the wc of the host, and once with a wc that pads its count to eight columns, as the one of macOS does. That way CI on Ubuntu sees the padding fault too.

What it touches

  • scripts/pre-commit.hook: the two lines of check 7.
  • scripts/test_hooks.py: the new FilenameTest class (3 tests, 8 cases), and one line of the module docstring.

No task, exercise or gate is affected. The series is a single commit on top of 9047337, the current main.

What was run

These were run on macOS 15 (arm64) with bash 5.3, at 96d8206.

make unittest, which now discovers the new tests:

$ make unittest
...
----------------------------------------------------------------------
Ran 47 tests in 66.158s

OK

FilenameTest against the hook before this change (9047337). Four of the eight cases fail, one for each fault:

FAIL: test_a_non_ascii_name_is_refused_when_added_or_renamed (how='renamed', padded=False)
FAIL: test_a_non_ascii_name_is_refused_when_added_or_renamed (how='renamed', padded=True)
FAIL: test_a_tracked_non_ascii_name_can_still_be_edited (padded=True)
FAIL: test_ascii_names_pass_also_with_a_padded_count (padded=True)
FAILED (failures=4)

The hook run by hand from .git/hooks/pre-commit, with the BSD wc (/usr/bin/wc) first on PATH:

stagedcheck 7
an edit of README.md[7/7] Checking filenames... [ OK ]
the same, plus git mv LICENSE 報告.txt[ FAIL ] Non-ASCII filename detected
$ make history
Commit history: 41 commit(s) continue the hash chain.

What was not run

  • The task specs (make -C <task> test) were not run, because no task source or spec changed.
  • The hook was not run on a Linux host here. CI on Ubuntu runs make unittest, which now includes FilenameTest and its padded-wc cases.