Skip to content

Commit cc7bf96

Browse files
authored
Merge pull request #2743 from adamchainz/issue_2742
Add `--no-textconv` to `git diff` calls
2 parents e846829 + 7783a3e commit cc7bf96

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

pre_commit/commands/run.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ def _all_filenames(args: argparse.Namespace) -> Collection[str]:
272272

273273
def _get_diff() -> bytes:
274274
_, out, _ = cmd_output_b(
275-
'git', 'diff', '--no-ext-diff', '--ignore-submodules', check=False,
275+
'git', 'diff', '--no-ext-diff', '--no-textconv', '--ignore-submodules',
276+
check=False,
276277
)
277278
return out
278279

@@ -326,8 +327,7 @@ def _has_unmerged_paths() -> bool:
326327

327328
def _has_unstaged_config(config_file: str) -> bool:
328329
retcode, _, _ = cmd_output_b(
329-
'git', 'diff', '--no-ext-diff', '--exit-code', config_file,
330-
check=False,
330+
'git', 'diff', '--quiet', '--no-ext-diff', config_file, check=False,
331331
)
332332
# be explicit, other git errors don't mean it has an unstaged config.
333333
return retcode == 1

tests/commands/run_test.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,47 @@ def test_lots_of_files(store, tempdir_factory):
766766
)
767767

768768

769+
def test_no_textconv(cap_out, store, repo_with_passing_hook):
770+
# git textconv filters can hide changes from hooks
771+
with open('.gitattributes', 'w') as fp:
772+
fp.write('*.jpeg diff=empty\n')
773+
774+
with open('.git/config', 'a') as fp:
775+
fp.write('[diff "empty"]\n')
776+
fp.write('textconv = "true"\n')
777+
778+
config = {
779+
'repo': 'local',
780+
'hooks': [
781+
{
782+
'id': 'extend-jpeg',
783+
'name': 'extend-jpeg',
784+
'language': 'system',
785+
'entry': (
786+
f'{shlex.quote(sys.executable)} -c "import sys; '
787+
'open(sys.argv[1], \'ab\').write(b\'\\x00\')"'
788+
),
789+
'types': ['jpeg'],
790+
},
791+
],
792+
}
793+
add_config_to_repo(repo_with_passing_hook, config)
794+
795+
stage_a_file('example.jpeg')
796+
797+
_test_run(
798+
cap_out,
799+
store,
800+
repo_with_passing_hook,
801+
{},
802+
(
803+
b'Failed',
804+
),
805+
expected_ret=1,
806+
stage=False,
807+
)
808+
809+
769810
def test_stages(cap_out, store, repo_with_passing_hook):
770811
config = {
771812
'repo': 'local',

0 commit comments

Comments
 (0)