From 7e74821a5dd2683935f1691453998d7c969fdb88 Mon Sep 17 00:00:00 2001 From: Ken Struys Date: Thu, 13 Mar 2014 13:59:05 -0700 Subject: [PATCH] added install and uninstall hooks --- pre-commit.py | 5 ---- pre_commit/git.py | 27 +++++++++++++++++++ pre_commit/resources/pre-commit.sh | 3 +++ pre_commit/run.py | 5 ++-- scripts/pre-commit.py | 8 ------ setup.py | 8 +++--- tests/git_test.py | 42 ++++++++++++++++++++++++++++++ 7 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 pre_commit/git.py create mode 100755 pre_commit/resources/pre-commit.sh delete mode 100755 scripts/pre-commit.py create mode 100644 tests/git_test.py diff --git a/pre-commit.py b/pre-commit.py index 5fd8c737..27bae700 100755 --- a/pre-commit.py +++ b/pre-commit.py @@ -79,11 +79,6 @@ TESTS = [ "All - No tabs", True, 'testtabs', ), - Test( - "make test", - "Py - Tests", - False, 'testtests', - ), ] def get_git_config(config_name): diff --git a/pre_commit/git.py b/pre_commit/git.py new file mode 100644 index 00000000..7f206125 --- /dev/null +++ b/pre_commit/git.py @@ -0,0 +1,27 @@ +import os +import pkg_resources +from plumbum import local + + +def get_root(): + return local['git']['rev-parse', '--show-toplevel']().strip() + + +def get_pre_commit_path(): + return os.path.join(get_root(), '.git/hooks/pre-commit') + + +def create_pre_commit(): + path = get_pre_commit_path() + pre_commit_file = pkg_resources.resource_filename('pre_commit', 'resources/pre-commit.sh') + local.path(path).write(local.path(pre_commit_file).read()) + + +def remove_pre_commit(): + local.path(get_pre_commit_path()).delete() + + + + + + diff --git a/pre_commit/resources/pre-commit.sh b/pre_commit/resources/pre-commit.sh new file mode 100755 index 00000000..9b24294a --- /dev/null +++ b/pre_commit/resources/pre-commit.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +pre-commit \ No newline at end of file diff --git a/pre_commit/run.py b/pre_commit/run.py index c281ff72..5fa1741d 100644 --- a/pre_commit/run.py +++ b/pre_commit/run.py @@ -1,11 +1,10 @@ import argparse - +from pre_commit import git def install(): """Install the pre-commit hook.""" - raise NotImplementedError - + git.create_pre_commit() def uninstall(): """Uninstall the pre-commit hook.""" diff --git a/scripts/pre-commit.py b/scripts/pre-commit.py deleted file mode 100755 index 063ad9c2..00000000 --- a/scripts/pre-commit.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env python - -if __name__ == '__main__': - import sys - - from pre_commit.run import run - - sys.exit(run(sys.argv[1:])) \ No newline at end of file diff --git a/setup.py b/setup.py index acce3581..1f456312 100644 --- a/setup.py +++ b/setup.py @@ -5,12 +5,14 @@ setup( name='pre_commit', version='0.0.0', packages=find_packages('.', exclude=('tests*', 'testing*')), + package_data={ + 'pre_commit': [ + 'resources/pre-commit.sh' + ] + }, install_requires=[ 'argparse', 'plumbum', 'simplejson', ], - scripts=[ - 'scripts/pre-commit.py', - ], ) diff --git a/tests/git_test.py b/tests/git_test.py new file mode 100644 index 00000000..5a21fec5 --- /dev/null +++ b/tests/git_test.py @@ -0,0 +1,42 @@ + +import os +import pytest + +from plumbum import local +from pre_commit import git + +@pytest.fixture +def empty_git_dir(tmpdir): + local.cwd.chdir(tmpdir.strpath) + local['git']['init']() + return tmpdir.strpath + + +def test_get_root(empty_git_dir): + assert git.get_root() == empty_git_dir + + foo = local.path('foo') + foo.mkdir() + local.cwd.chdir(foo) + + assert git.get_root() == empty_git_dir + + +def test_get_pre_commit_path(empty_git_dir): + assert git.get_pre_commit_path() == '{0}/.git/hooks/pre-commit'.format(empty_git_dir) + + +def test_create_pre_commit(empty_git_dir): + git.create_pre_commit() + assert len(open(git.get_pre_commit_path(), 'r').read()) > 0 + + +def test_remove_pre_commit(empty_git_dir): + git.remove_pre_commit() + + assert not os.path.exists(git.get_pre_commit_path()) + + git.create_pre_commit() + git.remove_pre_commit() + + assert not os.path.exists(git.get_pre_commit_path())