Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read legacy hooks in an encoding-agnostic way #1943

Merged
merged 1 commit into from Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions pre_commit/commands/install_uninstall.py
Expand Up @@ -21,13 +21,13 @@

# 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`
Expand All @@ -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)

Expand Down
17 changes: 15 additions & 2 deletions tests/commands/install_uninstall_test.py
Expand Up @@ -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)


Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down