diff --git a/pre_commit/git.py b/pre_commit/git.py index 6c359b66..bd6eb3d7 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -22,11 +22,6 @@ def get_root(): return _get_root_new() -def get_head_sha(git_repo_path): - with local.cwd(git_repo_path): - return local['git']['rev-parse', 'HEAD']().strip() - - @memoize_by_cwd def get_staged_files(): return local['git']['diff', '--staged', '--name-only']().splitlines() diff --git a/testing/util.py b/testing/util.py index 50390cbe..7af18e71 100644 --- a/testing/util.py +++ b/testing/util.py @@ -2,6 +2,7 @@ import jsonschema import os import os.path import shutil +from plumbum import local TESTING_DIR = os.path.abspath(os.path.dirname(__file__)) @@ -29,6 +30,11 @@ def copy_tree_to_path(src_dir, dest_dir): shutil.copy(srcname, destname) +def get_head_sha(dir): + with local.cwd(dir): + return local['git']['rev-parse', 'HEAD']().strip() + + def is_valid_according_to_schema(obj, schema): try: jsonschema.validate(obj, schema) diff --git a/tests/commands_test.py b/tests/commands_test.py index e3e6eb17..55cd3f15 100644 --- a/tests/commands_test.py +++ b/tests/commands_test.py @@ -9,15 +9,14 @@ from asottile.ordereddict import OrderedDict from asottile.yaml import ordered_dump from plumbum import local - import pre_commit.constants as C from pre_commit import commands -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.jsonschema_extensions import apply_defaults from pre_commit.runner import Runner from testing.auto_namedtuple import auto_namedtuple +from testing.util import get_head_sha from testing.util import get_resource_path @@ -53,7 +52,7 @@ def test_uninstall(empty_git_dir): def up_to_date_repo(python_hooks_repo): config = OrderedDict(( ('repo', python_hooks_repo), - ('sha', git.get_head_sha(python_hooks_repo)), + ('sha', get_head_sha(python_hooks_repo)), ('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]), )) wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA) @@ -90,14 +89,14 @@ def test_autoupdate_up_to_date_repo(up_to_date_repo): def out_of_date_repo(python_hooks_repo): config = OrderedDict(( ('repo', python_hooks_repo), - ('sha', git.get_head_sha(python_hooks_repo)), + ('sha', get_head_sha(python_hooks_repo)), ('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]), )) config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) validate_config_extra(config_wrapped) config = config_wrapped[0] local['git']['commit', '--allow-empty', '-m', 'foo']() - head_sha = git.get_head_sha(python_hooks_repo) + head_sha = get_head_sha(python_hooks_repo) with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj: file_obj.write( @@ -136,7 +135,7 @@ def test_autoupdate_out_of_date_repo(out_of_date_repo): def hook_disappearing_repo(python_hooks_repo): config = OrderedDict(( ('repo', python_hooks_repo), - ('sha', git.get_head_sha(python_hooks_repo)), + ('sha', get_head_sha(python_hooks_repo)), ('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]), )) config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) diff --git a/tests/conftest.py b/tests/conftest.py index e7be74a0..27f5d2f6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,11 +6,11 @@ import yaml from plumbum import local import pre_commit.constants as C -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.jsonschema_extensions import apply_defaults from testing.util import copy_tree_to_path +from testing.util import get_head_sha from testing.util import get_resource_path @@ -78,7 +78,7 @@ def failing_hook_repo(dummy_git_repo): def _make_config(path, hook_id, file_regex): config = { 'repo': path, - 'sha': git.get_head_sha(path), + 'sha': get_head_sha(path), 'hooks': [{'id': hook_id, 'files': file_regex}], } config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) diff --git a/tests/repository_test.py b/tests/repository_test.py index 4c93f354..ab3ea71f 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -3,13 +3,13 @@ import os import pytest import pre_commit.constants as C -from pre_commit import git from pre_commit import repository from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA from pre_commit.clientlib.validate_config import validate_config_extra from pre_commit.jsonschema_extensions import apply_defaults from pre_commit.prefixed_command_runner import PrefixedCommandRunner from pre_commit.repository import Repository +from testing.util import get_head_sha @pytest.fixture @@ -17,7 +17,7 @@ def dummy_repo_config(dummy_git_repo): # This is not a valid config, but it is pretty close return { 'repo': dummy_git_repo, - 'sha': git.get_head_sha(dummy_git_repo), + 'sha': get_head_sha(dummy_git_repo), 'hooks': [], }