diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index 5f9f5c39..701afccb 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -5,6 +5,7 @@ import io import itertools import logging import os.path +import shutil import sys from pre_commit import git @@ -84,7 +85,7 @@ def install( # If we have an existing hook, move it to pre-commit.legacy if os.path.lexists(hook_path) and not is_our_script(hook_path): - os.rename(hook_path, legacy_path) + shutil.move(hook_path, legacy_path) # If we specify overwrite, we simply delete the legacy file if overwrite and os.path.exists(legacy_path): diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index e253dd4b..3bb0a3ea 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -325,6 +325,16 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory, store): assert NORMAL_PRE_COMMIT_RUN.match(output[len('legacy hook\n'):]) +def test_legacy_overwriting_legacy_hook(tempdir_factory, store): + path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') + with cwd(path): + _write_legacy_hook(path) + assert install(C.CONFIG_FILE, store) == 0 + _write_legacy_hook(path) + # this previously crashed on windows. See #1010 + assert install(C.CONFIG_FILE, store) == 0 + + def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store): path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(path):