From 0ed646ed0912f709e555a1e90b5457cb1d251dd7 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 15 Jun 2021 08:32:44 -0700 Subject: [PATCH] read legacy hooks in an encoding-agnostic way --- pre_commit/commands/install_uninstall.py | 14 +++++++------- tests/commands/install_uninstall_test.py | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index 684b5980..73c8d605 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -21,13 +21,13 @@ logger = logging.getLogger(__name__) # This is used to identify the hook file we install PRIOR_HASHES = ( - '4d9958c90bc262f47553e2c073f14cfe', - 'd8ee923c46731b42cd95cc869add4062', - '49fd668cb42069aa1b6048464be5d395', - '79f09a650522a87b0da915d0d983b2de', - 'e358c9dae00eac5d06b38dfdb1e33a8c', + b'4d9958c90bc262f47553e2c073f14cfe', + b'd8ee923c46731b42cd95cc869add4062', + b'49fd668cb42069aa1b6048464be5d395', + b'79f09a650522a87b0da915d0d983b2de', + b'e358c9dae00eac5d06b38dfdb1e33a8c', ) -CURRENT_HASH = '138fd403232d2ddd5efb44317e38bf03' +CURRENT_HASH = b'138fd403232d2ddd5efb44317e38bf03' TEMPLATE_START = '# start templated\n' TEMPLATE_END = '# end templated\n' # Homebrew/homebrew-core#35825: be more timid about appropriate `PATH` @@ -48,7 +48,7 @@ def _hook_paths( def is_our_script(filename: str) -> bool: if not os.path.exists(filename): # pragma: win32 no cover (symlink) return False - with open(filename) as f: + with open(filename, 'rb') as f: contents = f.read() return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES) diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index bd28654f..314b8b96 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -39,7 +39,7 @@ def test_is_script(): def test_is_previous_pre_commit(tmpdir): f = tmpdir.join('foo') - f.write(f'{PRIOR_HASHES[0]}\n') + f.write(f'{PRIOR_HASHES[0].decode()}\n') assert is_our_script(f.strpath) @@ -390,6 +390,19 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store): NORMAL_PRE_COMMIT_RUN.assert_matches(output[len('legacy hook\n'):]) +def test_install_with_existing_non_utf8_script(tmpdir, store): + cmd_output('git', 'init', str(tmpdir)) + tmpdir.join('.git/hooks').ensure_dir() + tmpdir.join('.git/hooks/pre-commit').write_binary( + b'#!/usr/bin/env bash\n' + b'# garbage: \xa0\xef\x12\xf2\n' + b'echo legacy hook\n', + ) + + with tmpdir.as_cwd(): + assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0 + + FAIL_OLD_HOOK = re_assert.Matches( r'fail!\n' r'\[INFO\] Initializing environment for .+\.\n' @@ -460,7 +473,7 @@ def test_replace_old_commit_script(tempdir_factory, store): # Install a script that looks like our old script pre_commit_contents = resource_text('hook-tmpl') new_contents = pre_commit_contents.replace( - CURRENT_HASH, PRIOR_HASHES[-1], + CURRENT_HASH.decode(), PRIOR_HASHES[-1].decode(), ) os.makedirs(os.path.join(path, '.git/hooks'), exist_ok=True)