Whole file

pre-commit/pre-commit-hooks

The author described this change as Fix parsing of git output with unusual characters. It counts as a record because the checks below fail on the code as it stood at 5372f44b8 and pass on 4faed34fb, with nothing else changed between the two runs.

Fix saved2020-07-30
Sharing licenceMIT · LICENSE
Change size+10 2

What the code was meant to do, written into the code itself as a save note

Fix parsing of git output with unusual characters

The change

1212 EXECUTABLE_VALUES = frozenset(('1', '3', '5', '7'))
1313
1414
15+def zsplit(s: str) -> List[str]:
16+ s = s.strip('\0')
17+ if s:
18+ return s.split('\0')
19+ else:
20+ return []
21+
22+
1523 def check_executables(paths: List[str]) -> int:
1624 if sys.platform == 'win32': # pragma: win32 cover
1725 return _check_git_filemode(paths)
2634
2735
2836 def _check_git_filemode(paths: Sequence[str]) -> int:
29- outs = cmd_output('git', 'ls-files', '--stage', '--', *paths)
37+ outs = cmd_output('git', 'ls-files', '-z', '--stage', '--', *paths)
3038 seen: Set[str] = set()
31- for out in outs.splitlines():
39+ for out in zsplit(outs):
3240 metadata, path = out.split('\t')
3341 tagmode = metadata.split(' ', 1)[0]
3442

The check that tells the two apart

failpass·tests/check_executables_have_shebangs_test.py::test_check_git_filemode_passing_unusual_characters
failpass·tests/check_executables_have_shebangs_test.py::test_check_zsplit_returns_empty[\x00\x00]
failpass·tests/check_executables_have_shebangs_test.py::test_check_zsplit_returns_empty[\x00]
failpass·tests/check_executables_have_shebangs_test.py::test_check_zsplit_returns_empty[]
failpass·tests/check_executables_have_shebangs_test.py::test_check_zsplits_correctly[\x00f1\x00f2\x00]
failpass·tests/check_executables_have_shebangs_test.py::test_check_zsplits_correctly[\x00f1\x00f2]
failpass·tests/check_executables_have_shebangs_test.py::test_check_zsplits_correctly[f1\x00f2\x00]

Check file tests/check_executables_have_shebangs_test.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it5372f44b858f6eef834d9632e9960c39c296d448
Broken version dated2020-07-01
Modulepre_commit_hooks.check_executables_have_shebangs
Units changed_check_git_filemode
Fingerprint545c7a8ed17c6392
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in pre-commit/pre-commit-hooks