Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8037b45

Browse files
authored
Merge pull request #1943 from pre-commit/binary_legacy
read legacy hooks in an encoding-agnostic way
2 parents b771946 + 0ed646e commit 8037b45

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

pre_commit/commands/install_uninstall.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
# This is used to identify the hook file we install
2323
PRIOR_HASHES = (
24-
'4d9958c90bc262f47553e2c073f14cfe',
25-
'd8ee923c46731b42cd95cc869add4062',
26-
'49fd668cb42069aa1b6048464be5d395',
27-
'79f09a650522a87b0da915d0d983b2de',
28-
'e358c9dae00eac5d06b38dfdb1e33a8c',
24+
b'4d9958c90bc262f47553e2c073f14cfe',
25+
b'd8ee923c46731b42cd95cc869add4062',
26+
b'49fd668cb42069aa1b6048464be5d395',
27+
b'79f09a650522a87b0da915d0d983b2de',
28+
b'e358c9dae00eac5d06b38dfdb1e33a8c',
2929
)
30-
CURRENT_HASH = '138fd403232d2ddd5efb44317e38bf03'
30+
CURRENT_HASH = b'138fd403232d2ddd5efb44317e38bf03'
3131
TEMPLATE_START = '# start templated\n'
3232
TEMPLATE_END = '# end templated\n'
3333
# Homebrew/homebrew-core#35825: be more timid about appropriate `PATH`
@@ -48,7 +48,7 @@ def _hook_paths(
4848
def is_our_script(filename: str) -> bool:
4949
if not os.path.exists(filename): # pragma: win32 no cover (symlink)
5050
return False
51-
with open(filename) as f:
51+
with open(filename, 'rb') as f:
5252
contents = f.read()
5353
return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES)
5454

tests/commands/install_uninstall_test.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_is_script():
3939

4040
def test_is_previous_pre_commit(tmpdir):
4141
f = tmpdir.join('foo')
42-
f.write(f'{PRIOR_HASHES[0]}\n')
42+
f.write(f'{PRIOR_HASHES[0].decode()}\n')
4343
assert is_our_script(f.strpath)
4444

4545

@@ -390,6 +390,19 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
390390
NORMAL_PRE_COMMIT_RUN.assert_matches(output[len('legacy hook\n'):])
391391

392392

393+
def test_install_with_existing_non_utf8_script(tmpdir, store):
394+
cmd_output('git', 'init', str(tmpdir))
395+
tmpdir.join('.git/hooks').ensure_dir()
396+
tmpdir.join('.git/hooks/pre-commit').write_binary(
397+
b'#!/usr/bin/env bash\n'
398+
b'# garbage: \xa0\xef\x12\xf2\n'
399+
b'echo legacy hook\n',
400+
)
401+
402+
with tmpdir.as_cwd():
403+
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
404+
405+
393406
FAIL_OLD_HOOK = re_assert.Matches(
394407
r'fail!\n'
395408
r'\[INFO\] Initializing environment for .+\.\n'
@@ -460,7 +473,7 @@ def test_replace_old_commit_script(tempdir_factory, store):
460473
# Install a script that looks like our old script
461474
pre_commit_contents = resource_text('hook-tmpl')
462475
new_contents = pre_commit_contents.replace(
463-
CURRENT_HASH, PRIOR_HASHES[-1],
476+
CURRENT_HASH.decode(), PRIOR_HASHES[-1].decode(),
464477
)
465478

466479
os.makedirs(os.path.join(path, '.git/hooks'), exist_ok=True)

0 commit comments

Comments
 (0)