read legacy hooks in an encoding-agnostic way

This commit is contained in:
Anthony Sottile 2021-06-15 08:32:44 -07:00
parent b77194644e
commit 0ed646ed09
2 changed files with 22 additions and 9 deletions

View file

@ -21,13 +21,13 @@ logger = logging.getLogger(__name__)
# This is used to identify the hook file we install # This is used to identify the hook file we install
PRIOR_HASHES = ( PRIOR_HASHES = (
'4d9958c90bc262f47553e2c073f14cfe', b'4d9958c90bc262f47553e2c073f14cfe',
'd8ee923c46731b42cd95cc869add4062', b'd8ee923c46731b42cd95cc869add4062',
'49fd668cb42069aa1b6048464be5d395', b'49fd668cb42069aa1b6048464be5d395',
'79f09a650522a87b0da915d0d983b2de', b'79f09a650522a87b0da915d0d983b2de',
'e358c9dae00eac5d06b38dfdb1e33a8c', b'e358c9dae00eac5d06b38dfdb1e33a8c',
) )
CURRENT_HASH = '138fd403232d2ddd5efb44317e38bf03' CURRENT_HASH = b'138fd403232d2ddd5efb44317e38bf03'
TEMPLATE_START = '# start templated\n' TEMPLATE_START = '# start templated\n'
TEMPLATE_END = '# end templated\n' TEMPLATE_END = '# end templated\n'
# Homebrew/homebrew-core#35825: be more timid about appropriate `PATH` # Homebrew/homebrew-core#35825: be more timid about appropriate `PATH`
@ -48,7 +48,7 @@ def _hook_paths(
def is_our_script(filename: str) -> bool: def is_our_script(filename: str) -> bool:
if not os.path.exists(filename): # pragma: win32 no cover (symlink) if not os.path.exists(filename): # pragma: win32 no cover (symlink)
return False return False
with open(filename) as f: with open(filename, 'rb') as f:
contents = f.read() contents = f.read()
return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES) return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES)

View file

@ -39,7 +39,7 @@ def test_is_script():
def test_is_previous_pre_commit(tmpdir): def test_is_previous_pre_commit(tmpdir):
f = tmpdir.join('foo') 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) 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'):]) 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( FAIL_OLD_HOOK = re_assert.Matches(
r'fail!\n' r'fail!\n'
r'\[INFO\] Initializing environment for .+\.\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 # Install a script that looks like our old script
pre_commit_contents = resource_text('hook-tmpl') pre_commit_contents = resource_text('hook-tmpl')
new_contents = pre_commit_contents.replace( 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) os.makedirs(os.path.join(path, '.git/hooks'), exist_ok=True)