From fa953d4c2d4c676b52b4254539e28aa4e86a70d3 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 30 Mar 2014 18:34:57 -0700 Subject: [PATCH] Implement the clean command. --- pre_commit/commands.py | 8 ++++++++ pre_commit/run.py | 4 ++++ tests/commands_test.py | 13 +++++++++++++ 3 files changed, 25 insertions(+) diff --git a/pre_commit/commands.py b/pre_commit/commands.py index 456aa9b0..d7ea314d 100644 --- a/pre_commit/commands.py +++ b/pre_commit/commands.py @@ -3,6 +3,7 @@ from __future__ import print_function import os import pkg_resources +import shutil import stat from plumbum import local @@ -117,3 +118,10 @@ def autoupdate(runner): ) return retv + + +def clean(runner): + if os.path.exists(runner.hooks_workspace_path): + shutil.rmtree(runner.hooks_workspace_path) + print('Cleaned {0}.'.format(runner.hooks_workspace_path)) + return 0 diff --git a/pre_commit/run.py b/pre_commit/run.py index 0eda1584..63247658 100644 --- a/pre_commit/run.py +++ b/pre_commit/run.py @@ -100,6 +100,8 @@ def run(argv): subparsers.add_parser('uninstall', help='Uninstall the pre-commit script.') + subparsers.add_parser('clean', help='Clean out pre-commit files.') + subparsers.add_parser('autoupdate', help='Auto-update hooks config.') run = subparsers.add_parser('run', help='Run hooks.') @@ -123,6 +125,8 @@ def run(argv): return commands.install(runner) elif args.command == 'uninstall': return commands.uninstall(runner) + elif args.command == 'clean': + return commands.clean(runner) elif args.command == 'autoupdate': return commands.autoupdate(runner) elif args.command == 'run': diff --git a/tests/commands_test.py b/tests/commands_test.py index 7cb54d76..6dba08d0 100644 --- a/tests/commands_test.py +++ b/tests/commands_test.py @@ -13,6 +13,7 @@ from pre_commit import git from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA from pre_commit.clientlib.validate_config import validate_config_extra from pre_commit.commands import autoupdate +from pre_commit.commands import clean from pre_commit.commands import install from pre_commit.commands import RepositoryCannotBeUpdatedError from pre_commit.commands import uninstall @@ -163,3 +164,15 @@ def test_autoupdate_hook_disappearing_repo(hook_disappearing_repo): after = open(C.CONFIG_FILE).read() assert ret == 1 assert before == after + + +def test_clean(empty_git_dir): + os.mkdir(C.HOOKS_WORKSPACE) + clean(Runner(empty_git_dir)) + assert not os.path.exists(C.HOOKS_WORKSPACE) + + +def test_clean_empty(empty_git_dir): + assert not os.path.exists(C.HOOKS_WORKSPACE) + clean(Runner(empty_git_dir)) + assert not os.path.exists(C.HOOKS_WORKSPACE)