Skip to content

Commit

Permalink
Merge pull request #1943 from pre-commit/binary_legacy
Browse files Browse the repository at this point in the history
read legacy hooks in an encoding-agnostic way
  • Loading branch information
asottile committed Jun 15, 2021
2 parents b771946 + 0ed646e commit 8037b45
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
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

0 comments on commit 8037b45

Please sign in to comment.