Support pre-commit from inside submodules

This commit is contained in:
Anthony Sottile 2015-12-18 14:21:30 -08:00
parent d6cf62532d
commit c3c98afe4f
7 changed files with 92 additions and 26 deletions

View file

@ -170,6 +170,21 @@ def test_install_pre_commit_and_run(tempdir_factory):
assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_install_in_submodule_and_run(tempdir_factory):
src_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
parent_path = git_dir(tempdir_factory)
with cwd(parent_path):
cmd_output('git', 'submodule', 'add', src_path, 'sub')
cmd_output('git', 'commit', '-am', 'foo')
sub_pth = os.path.join(parent_path, 'sub')
with cwd(sub_pth):
assert install(Runner(sub_pth)) == 0
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_install_idempotent(tempdir_factory):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(path):

View file

@ -15,6 +15,7 @@ from pre_commit.runner import Runner
from pre_commit.store import Store
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo
@ -40,6 +41,26 @@ def in_tmpdir(tempdir_factory):
yield path
def _make_conflict():
cmd_output('git', 'checkout', 'origin/master', '-b', 'foo')
with io.open('conflict_file', 'w') as conflict_file:
conflict_file.write('herp\nderp\n')
cmd_output('git', 'add', 'conflict_file')
with io.open('foo_only_file', 'w') as foo_only_file:
foo_only_file.write('foo')
cmd_output('git', 'add', 'foo_only_file')
cmd_output('git', 'commit', '-m', 'conflict_file')
cmd_output('git', 'checkout', 'origin/master', '-b', 'bar')
with io.open('conflict_file', 'w') as conflict_file:
conflict_file.write('harp\nddrp\n')
cmd_output('git', 'add', 'conflict_file')
with io.open('bar_only_file', 'w') as bar_only_file:
bar_only_file.write('bar')
cmd_output('git', 'add', 'bar_only_file')
cmd_output('git', 'commit', '-m', 'conflict_file')
cmd_output('git', 'merge', 'foo', retcode=None)
@pytest.yield_fixture
def in_merge_conflict(tempdir_factory):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
@ -51,26 +72,23 @@ def in_merge_conflict(tempdir_factory):
conflict_path = tempdir_factory.get()
cmd_output('git', 'clone', path, conflict_path)
with cwd(conflict_path):
cmd_output('git', 'checkout', 'origin/master', '-b', 'foo')
with io.open('conflict_file', 'w') as conflict_file:
conflict_file.write('herp\nderp\n')
cmd_output('git', 'add', 'conflict_file')
with io.open('foo_only_file', 'w') as foo_only_file:
foo_only_file.write('foo')
cmd_output('git', 'add', 'foo_only_file')
cmd_output('git', 'commit', '-m', 'conflict_file')
cmd_output('git', 'checkout', 'origin/master', '-b', 'bar')
with io.open('conflict_file', 'w') as conflict_file:
conflict_file.write('harp\nddrp\n')
cmd_output('git', 'add', 'conflict_file')
with io.open('bar_only_file', 'w') as bar_only_file:
bar_only_file.write('bar')
cmd_output('git', 'add', 'bar_only_file')
cmd_output('git', 'commit', '-m', 'conflict_file')
cmd_output('git', 'merge', 'foo', retcode=None)
_make_conflict()
yield os.path.join(conflict_path)
@pytest.yield_fixture
def in_conflicting_submodule(tempdir_factory):
git_dir_1 = git_dir(tempdir_factory)
git_dir_2 = git_dir(tempdir_factory)
with cwd(git_dir_2):
cmd_output('git', 'commit', '--allow-empty', '-m', 'init!')
with cwd(git_dir_1):
cmd_output('git', 'submodule', 'add', git_dir_2, 'sub')
with cwd(os.path.join(git_dir_1, 'sub')):
_make_conflict()
yield
@pytest.yield_fixture(scope='session', autouse=True)
def dont_write_to_home_directory():
"""pre_commit.store.Store will by default write to the home directory

View file

@ -43,6 +43,10 @@ def test_is_in_merge_conflict(in_merge_conflict):
assert git.is_in_merge_conflict() is True
def test_is_in_merge_conflict_submodule(in_conflicting_submodule):
assert git.is_in_merge_conflict() is True
def test_cherry_pick_conflict(in_merge_conflict):
cmd_output('git', 'merge', '--abort')
foo_ref = cmd_output('git', 'rev-parse', 'foo')[1].strip()
@ -111,6 +115,11 @@ def test_get_conflicted_files(in_merge_conflict):
assert ret == set(('conflict_file', 'other_file'))
def test_get_conflicted_files_in_submodule(in_conflicting_submodule):
resolve_conflict()
assert set(git.get_conflicted_files()) == set(('conflict_file',))
def test_get_conflicted_files_unstaged_files(in_merge_conflict):
# If they for whatever reason did pre-commit run --no-stash during a
# conflict

View file

@ -7,6 +7,7 @@ import os.path
import pre_commit.constants as C
from pre_commit.ordereddict import OrderedDict
from pre_commit.runner import Runner
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from testing.fixtures import add_config_to_repo
from testing.fixtures import git_dir
@ -79,15 +80,19 @@ def test_local_hooks(tempdir_factory, mock_out_store_directory):
assert len(runner.repositories[0].hooks) == 2
def test_pre_commit_path():
runner = Runner(os.path.join('foo', 'bar'))
expected_path = os.path.join('foo', 'bar', '.git', 'hooks', 'pre-commit')
def test_pre_commit_path(in_tmpdir):
path = os.path.join('foo', 'bar')
cmd_output('git', 'init', path)
runner = Runner(path)
expected_path = os.path.join(path, '.git', 'hooks', 'pre-commit')
assert runner.pre_commit_path == expected_path
def test_pre_push_path():
runner = Runner(os.path.join('foo', 'bar'))
expected_path = os.path.join('foo', 'bar', '.git', 'hooks', 'pre-push')
path = os.path.join('foo', 'bar')
cmd_output('git', 'init', path)
runner = Runner(path)
expected_path = os.path.join(path, '.git', 'hooks', 'pre-push')
assert runner.pre_push_path == expected_path