mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Use plumbum a bit better.
This commit is contained in:
parent
92aa55f44e
commit
7b1230df27
9 changed files with 44 additions and 44 deletions
|
|
@ -30,8 +30,8 @@ def _update_repository(repo_config, runner):
|
||||||
repo = Repository.create(repo_config, runner.store)
|
repo = Repository.create(repo_config, runner.store)
|
||||||
|
|
||||||
with local.cwd(repo.repo_path_getter.repo_path):
|
with local.cwd(repo.repo_path_getter.repo_path):
|
||||||
local['git']['fetch']()
|
local['git']('fetch')
|
||||||
head_sha = local['git']['rev-parse', 'origin/master']().strip()
|
head_sha = local['git']('rev-parse', 'origin/master').strip()
|
||||||
|
|
||||||
# Don't bother trying to update if our sha is the same
|
# Don't bother trying to update if our sha is the same
|
||||||
if head_sha == repo_config['sha']:
|
if head_sha == repo_config['sha']:
|
||||||
|
|
|
||||||
|
|
@ -49,21 +49,21 @@ def get_conflicted_files():
|
||||||
# This will get the rest of the changes made after the merge.
|
# This will get the rest of the changes made after the merge.
|
||||||
# If they resolved the merge conflict by choosing a mesh of both sides
|
# If they resolved the merge conflict by choosing a mesh of both sides
|
||||||
# this will also include the conflicted files
|
# this will also include the conflicted files
|
||||||
tree_hash = local['git']['write-tree']().strip()
|
tree_hash = local['git']('write-tree').strip()
|
||||||
merge_diff_filenames = local['git'][
|
merge_diff_filenames = local['git'](
|
||||||
'diff', '-m', tree_hash, 'HEAD', 'MERGE_HEAD', '--name-only',
|
'diff', '-m', tree_hash, 'HEAD', 'MERGE_HEAD', '--name-only',
|
||||||
]().splitlines()
|
).splitlines()
|
||||||
return set(merge_conflict_filenames) | set(merge_diff_filenames)
|
return set(merge_conflict_filenames) | set(merge_diff_filenames)
|
||||||
|
|
||||||
|
|
||||||
@memoize_by_cwd
|
@memoize_by_cwd
|
||||||
def get_staged_files():
|
def get_staged_files():
|
||||||
return local['git']['diff', '--staged', '--name-only']().splitlines()
|
return local['git']('diff', '--staged', '--name-only').splitlines()
|
||||||
|
|
||||||
|
|
||||||
@memoize_by_cwd
|
@memoize_by_cwd
|
||||||
def get_all_files():
|
def get_all_files():
|
||||||
return local['git']['ls-files']().splitlines()
|
return local['git']('ls-files').splitlines()
|
||||||
|
|
||||||
|
|
||||||
def get_files_matching(all_file_list_strategy):
|
def get_files_matching(all_file_list_strategy):
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ def copy_tree_to_path(src_dir, dest_dir):
|
||||||
|
|
||||||
def get_head_sha(dir):
|
def get_head_sha(dir):
|
||||||
with local.cwd(dir):
|
with local.cwd(dir):
|
||||||
return local['git']['rev-parse', 'HEAD']().strip()
|
return local['git']('rev-parse', 'HEAD').strip()
|
||||||
|
|
||||||
|
|
||||||
def is_valid_according_to_schema(obj, schema):
|
def is_valid_according_to_schema(obj, schema):
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ def out_of_date_repo(python_hooks_repo):
|
||||||
config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA)
|
config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA)
|
||||||
validate_config_extra(config_wrapped)
|
validate_config_extra(config_wrapped)
|
||||||
config = config_wrapped[0]
|
config = config_wrapped[0]
|
||||||
local['git']['commit', '--allow-empty', '-m', 'foo']()
|
local['git']('commit', '--allow-empty', '-m', 'foo')
|
||||||
head_sha = 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:
|
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
|
||||||
|
|
@ -125,8 +125,8 @@ def hook_disappearing_repo(python_hooks_repo):
|
||||||
get_resource_path('manifest_without_foo.yaml'),
|
get_resource_path('manifest_without_foo.yaml'),
|
||||||
C.MANIFEST_FILE,
|
C.MANIFEST_FILE,
|
||||||
)
|
)
|
||||||
local['git']['add', '.']()
|
local['git']('add', '.')
|
||||||
local['git']['commit', '-m', 'Remove foo']()
|
local['git']('commit', '-m', 'Remove foo')
|
||||||
|
|
||||||
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
|
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
|
||||||
file_obj.write(
|
file_obj.write(
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,8 @@ from testing.auto_namedtuple import auto_namedtuple
|
||||||
|
|
||||||
|
|
||||||
def stage_a_file():
|
def stage_a_file():
|
||||||
local['touch']['foo.py']()
|
local['touch']('foo.py')
|
||||||
local['git']['add', 'foo.py']()
|
local['git']('add', 'foo.py')
|
||||||
|
|
||||||
|
|
||||||
def get_write_mock_output(write_mock):
|
def get_write_mock_output(write_mock):
|
||||||
|
|
@ -153,7 +153,7 @@ def test_merge_conflict_modified(in_merge_conflict, mock_out_store_directory):
|
||||||
|
|
||||||
|
|
||||||
def test_merge_conflict_resolved(in_merge_conflict, mock_out_store_directory):
|
def test_merge_conflict_resolved(in_merge_conflict, mock_out_store_directory):
|
||||||
local['git']['add', '.']()
|
local['git']('add', '.')
|
||||||
ret, printed = _do_run(in_merge_conflict, _get_opts())
|
ret, printed = _do_run(in_merge_conflict, _get_opts())
|
||||||
for msg in ('Checking merge-conflict files only.', 'Bash hook', 'Passed'):
|
for msg in ('Checking merge-conflict files only.', 'Bash hook', 'Passed'):
|
||||||
assert msg in printed
|
assert msg in printed
|
||||||
|
|
|
||||||
|
|
@ -45,19 +45,19 @@ def in_tmpdir(tmpdir_factory):
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def empty_git_dir(in_tmpdir):
|
def empty_git_dir(in_tmpdir):
|
||||||
local['git']['init']()
|
local['git']('init')
|
||||||
yield in_tmpdir
|
yield in_tmpdir
|
||||||
|
|
||||||
|
|
||||||
def add_and_commit():
|
def add_and_commit():
|
||||||
local['git']['add', '.']()
|
local['git']('add', '.')
|
||||||
local['git']['commit', '-m', 'random commit {0}'.format(time.time())]()
|
local['git']('commit', '-m', 'random commit {0}'.format(time.time()))
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def dummy_git_repo(empty_git_dir):
|
def dummy_git_repo(empty_git_dir):
|
||||||
# This is needed otherwise there is no `HEAD`
|
# This is needed otherwise there is no `HEAD`
|
||||||
local['touch']['dummy']()
|
local['touch']('dummy')
|
||||||
add_and_commit()
|
add_and_commit()
|
||||||
yield empty_git_dir
|
yield empty_git_dir
|
||||||
|
|
||||||
|
|
@ -205,27 +205,27 @@ def repo_with_failing_hook(failing_hook_repo, empty_git_dir):
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def in_merge_conflict(repo_with_passing_hook):
|
def in_merge_conflict(repo_with_passing_hook):
|
||||||
local['git']['add', C.CONFIG_FILE]()
|
local['git']('add', C.CONFIG_FILE)
|
||||||
local['git']['commit', '-m' 'add hooks file']()
|
local['git']('commit', '-m' 'add hooks file')
|
||||||
local['git']['clone', '.', 'foo']()
|
local['git']('clone', '.', 'foo')
|
||||||
with local.cwd('foo'):
|
with local.cwd('foo'):
|
||||||
local['git']['checkout', 'origin/master', '-b', 'foo']()
|
local['git']('checkout', 'origin/master', '-b', 'foo')
|
||||||
with open('conflict_file', 'w') as conflict_file:
|
with open('conflict_file', 'w') as conflict_file:
|
||||||
conflict_file.write('herp\nderp\n')
|
conflict_file.write('herp\nderp\n')
|
||||||
local['git']['add', 'conflict_file']()
|
local['git']('add', 'conflict_file')
|
||||||
with open('foo_only_file', 'w') as foo_only_file:
|
with open('foo_only_file', 'w') as foo_only_file:
|
||||||
foo_only_file.write('foo')
|
foo_only_file.write('foo')
|
||||||
local['git']['add', 'foo_only_file']()
|
local['git']('add', 'foo_only_file')
|
||||||
local['git']['commit', '-m', 'conflict_file']()
|
local['git']('commit', '-m', 'conflict_file')
|
||||||
local['git']['checkout', 'origin/master', '-b', 'bar']()
|
local['git']('checkout', 'origin/master', '-b', 'bar')
|
||||||
with open('conflict_file', 'w') as conflict_file:
|
with open('conflict_file', 'w') as conflict_file:
|
||||||
conflict_file.write('harp\nddrp\n')
|
conflict_file.write('harp\nddrp\n')
|
||||||
local['git']['add', 'conflict_file']()
|
local['git']('add', 'conflict_file')
|
||||||
with open('bar_only_file', 'w') as bar_only_file:
|
with open('bar_only_file', 'w') as bar_only_file:
|
||||||
bar_only_file.write('bar')
|
bar_only_file.write('bar')
|
||||||
local['git']['add', 'bar_only_file']()
|
local['git']('add', 'bar_only_file')
|
||||||
local['git']['commit', '-m', 'conflict_file']()
|
local['git']('commit', '-m', 'conflict_file')
|
||||||
local['git']['merge', 'foo'](retcode=None)
|
local['git']('merge', 'foo', retcode=None)
|
||||||
yield os.path.join(repo_with_passing_hook, 'foo')
|
yield os.path.join(repo_with_passing_hook, 'foo')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -77,14 +77,14 @@ def test_exclude_removes_files(get_files_matching_func):
|
||||||
def resolve_conflict():
|
def resolve_conflict():
|
||||||
with open('conflict_file', 'w') as conflicted_file:
|
with open('conflict_file', 'w') as conflicted_file:
|
||||||
conflicted_file.write('herp\nderp\n')
|
conflicted_file.write('herp\nderp\n')
|
||||||
local['git']['add', 'conflict_file']()
|
local['git']('add', 'conflict_file')
|
||||||
|
|
||||||
|
|
||||||
def test_get_conflicted_files(in_merge_conflict):
|
def test_get_conflicted_files(in_merge_conflict):
|
||||||
resolve_conflict()
|
resolve_conflict()
|
||||||
with open('other_file', 'w') as other_file:
|
with open('other_file', 'w') as other_file:
|
||||||
other_file.write('oh hai')
|
other_file.write('oh hai')
|
||||||
local['git']['add', 'other_file']()
|
local['git']('add', 'other_file')
|
||||||
|
|
||||||
ret = set(git.get_conflicted_files())
|
ret = set(git.get_conflicted_files())
|
||||||
assert ret == set(('conflict_file', 'other_file'))
|
assert ret == set(('conflict_file', 'other_file'))
|
||||||
|
|
|
||||||
|
|
@ -149,7 +149,7 @@ def test_reinstall(config_for_python_hooks_repo, store):
|
||||||
@pytest.mark.integration
|
@pytest.mark.integration
|
||||||
def test_really_long_file_paths(config_for_python_hooks_repo, store):
|
def test_really_long_file_paths(config_for_python_hooks_repo, store):
|
||||||
path = 'really_long' * 10
|
path = 'really_long' * 10
|
||||||
local['git']['init', path]()
|
local['git']('init', path)
|
||||||
with local.cwd(path):
|
with local.cwd(path):
|
||||||
repo = Repository.create(config_for_python_hooks_repo, store)
|
repo = Repository.create(config_for_python_hooks_repo, store)
|
||||||
repo.require_installed()
|
repo.require_installed()
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ FOO_CONTENTS = '\n'.join(('1', '2', '3', '4', '5', '6', '7', '8', ''))
|
||||||
|
|
||||||
|
|
||||||
def get_short_git_status():
|
def get_short_git_status():
|
||||||
git_status = local['git']['status', '-s']()
|
git_status = local['git']('status', '-s')
|
||||||
return dict(reversed(line.split()) for line in git_status.splitlines())
|
return dict(reversed(line.split()) for line in git_status.splitlines())
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -25,7 +25,7 @@ def get_short_git_status():
|
||||||
def foo_staged(empty_git_dir):
|
def foo_staged(empty_git_dir):
|
||||||
with io.open('foo', 'w') as foo_file:
|
with io.open('foo', 'w') as foo_file:
|
||||||
foo_file.write(FOO_CONTENTS)
|
foo_file.write(FOO_CONTENTS)
|
||||||
local['git']['add', 'foo']()
|
local['git']('add', 'foo')
|
||||||
foo_filename = os.path.join(empty_git_dir, 'foo')
|
foo_filename = os.path.join(empty_git_dir, 'foo')
|
||||||
yield auto_namedtuple(path=empty_git_dir, foo_filename=foo_filename)
|
yield auto_namedtuple(path=empty_git_dir, foo_filename=foo_filename)
|
||||||
|
|
||||||
|
|
@ -99,7 +99,7 @@ def test_foo_both_modify_conflicting(foo_staged, cmd_runner):
|
||||||
def img_staged(empty_git_dir):
|
def img_staged(empty_git_dir):
|
||||||
img_filename = os.path.join(empty_git_dir, 'img.jpg')
|
img_filename = os.path.join(empty_git_dir, 'img.jpg')
|
||||||
shutil.copy(get_resource_path('img1.jpg'), img_filename)
|
shutil.copy(get_resource_path('img1.jpg'), img_filename)
|
||||||
local['git']['add', 'img.jpg']()
|
local['git']('add', 'img.jpg')
|
||||||
yield auto_namedtuple(path=empty_git_dir, img_filename=img_filename)
|
yield auto_namedtuple(path=empty_git_dir, img_filename=img_filename)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -150,23 +150,23 @@ def test_img_conflict(img_staged, cmd_runner):
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def submodule_with_commits(empty_git_dir):
|
def submodule_with_commits(empty_git_dir):
|
||||||
local['git']['commit', '--allow-empty', '-m', 'foo']()
|
local['git']('commit', '--allow-empty', '-m', 'foo')
|
||||||
sha1 = local['git']['rev-parse', 'HEAD']().strip()
|
sha1 = local['git']('rev-parse', 'HEAD').strip()
|
||||||
local['git']['commit', '--allow-empty', '-m', 'bar']()
|
local['git']('commit', '--allow-empty', '-m', 'bar')
|
||||||
sha2 = local['git']['rev-parse', 'HEAD']().strip()
|
sha2 = local['git']('rev-parse', 'HEAD').strip()
|
||||||
yield auto_namedtuple(path=empty_git_dir, sha1=sha1, sha2=sha2)
|
yield auto_namedtuple(path=empty_git_dir, sha1=sha1, sha2=sha2)
|
||||||
|
|
||||||
|
|
||||||
def checkout_submodule(sha):
|
def checkout_submodule(sha):
|
||||||
with local.cwd('sub'):
|
with local.cwd('sub'):
|
||||||
local['git']['checkout', sha]()
|
local['git']('checkout', sha)
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def sub_staged(submodule_with_commits, empty_git_dir):
|
def sub_staged(submodule_with_commits, empty_git_dir):
|
||||||
local['git']['submodule', 'add', submodule_with_commits.path, 'sub']()
|
local['git']('submodule', 'add', submodule_with_commits.path, 'sub')
|
||||||
checkout_submodule(submodule_with_commits.sha1)
|
checkout_submodule(submodule_with_commits.sha1)
|
||||||
local['git']['add', 'sub']()
|
local['git']('add', 'sub')
|
||||||
yield auto_namedtuple(
|
yield auto_namedtuple(
|
||||||
path=empty_git_dir,
|
path=empty_git_dir,
|
||||||
sub_path=os.path.join(empty_git_dir, 'sub'),
|
sub_path=os.path.join(empty_git_dir, 'sub'),
|
||||||
|
|
@ -177,7 +177,7 @@ def sub_staged(submodule_with_commits, empty_git_dir):
|
||||||
def _test_sub_state(path, sha='sha1', status='A'):
|
def _test_sub_state(path, sha='sha1', status='A'):
|
||||||
assert os.path.exists(path.sub_path)
|
assert os.path.exists(path.sub_path)
|
||||||
with local.cwd(path.sub_path):
|
with local.cwd(path.sub_path):
|
||||||
actual_sha = local['git']['rev-parse', 'HEAD']().strip()
|
actual_sha = local['git']('rev-parse', 'HEAD').strip()
|
||||||
assert actual_sha == getattr(path.submodule, sha)
|
assert actual_sha == getattr(path.submodule, sha)
|
||||||
actual_status = get_short_git_status()['sub']
|
actual_status = get_short_git_status()['sub']
|
||||||
assert actual_status == status
|
assert actual_status == status
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue