From 0cde0fdc480a5579645c8a32fce89fbad57d0a1a Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 16 Jun 2014 17:48:56 -0700 Subject: [PATCH] Uninstall restores hooks. --- pre_commit/commands/install_uninstall.py | 5 +++++ tests/commands/install_uninstall_test.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index abc6c8ad..4698ee1e 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -61,4 +61,9 @@ def uninstall(runner): if os.path.exists(runner.pre_commit_path): os.remove(runner.pre_commit_path) print('pre-commit uninstalled') + + if os.path.exists(runner.pre_commit_legacy_path): + os.rename(runner.pre_commit_legacy_path, runner.pre_commit_path) + print('Restored previous hooks to {0}'.format(runner.pre_commit_path)) + return 0 diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index bd0780ac..4832afbb 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -256,3 +256,23 @@ def test_install_overwrite(tmpdir_factory): ret, output = _get_commit_output(tmpdir_factory) assert ret == 0 assert NORMAL_PRE_COMMIT_RUN.match(output) + + +def test_uninstall_restores_legacy_hooks(tmpdir_factory): + path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') + with local.cwd(path): + runner = Runner(path) + + # Write out an "old" hook + with io.open(runner.pre_commit_path, 'w') as hook_file: + hook_file.write('#!/usr/bin/env bash\necho "legacy hook"\n') + make_executable(runner.pre_commit_path) + + # Now install and uninstall pre-commit + assert install(runner) == 0 + assert uninstall(runner) == 0 + + # Make sure we installed the "old" hook correctly + ret, output = _get_commit_output(tmpdir_factory, touch_file='baz') + assert ret == 0 + assert EXISTING_COMMIT_RUN.match(output)