From 888787fb2de4cbf6772a98ca006a0e8d5b270d15 Mon Sep 17 00:00:00 2001 From: DanielChabrowski Date: Sun, 17 Mar 2019 22:09:38 +0100 Subject: [PATCH 1/5] Fix try-repo for staged untracked changes --- pre_commit/commands/try_repo.py | 6 ++++++ pre_commit/git.py | 3 ++- tests/commands/try_repo_test.py | 12 ++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pre_commit/commands/try_repo.py b/pre_commit/commands/try_repo.py index c9849ea4..4bffd754 100644 --- a/pre_commit/commands/try_repo.py +++ b/pre_commit/commands/try_repo.py @@ -32,9 +32,15 @@ def _repo_ref(tmpdir, repo, ref): shadow = os.path.join(tmpdir, 'shadow-repo') cmd_output('git', 'clone', repo, shadow) cmd_output('git', 'checkout', ref, '-b', '_pc_tmp', cwd=shadow) + idx = git.git_path('index', repo=shadow) objs = git.git_path('objects', repo=shadow) env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs) + + staged_files = git.get_staged_files(cwd=repo) + if (len(staged_files) > 0): + cmd_output('git', 'add', *staged_files, cwd=repo, env=env) + cmd_output('git', 'add', '-u', cwd=repo, env=env) git.commit(repo=shadow) diff --git a/pre_commit/git.py b/pre_commit/git.py index c24ca86e..3b97bfd9 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -91,11 +91,12 @@ def get_conflicted_files(): return set(merge_conflict_filenames) | set(merge_diff_filenames) -def get_staged_files(): +def get_staged_files(cwd=None): return zsplit(cmd_output( 'git', 'diff', '--staged', '--name-only', '--no-ext-diff', '-z', # Everything except for D '--diff-filter=ACMRTUXB', + cwd=cwd, )[1]) diff --git a/tests/commands/try_repo_test.py b/tests/commands/try_repo_test.py index 5b50f420..d9a0401a 100644 --- a/tests/commands/try_repo_test.py +++ b/tests/commands/try_repo_test.py @@ -123,3 +123,15 @@ def test_try_repo_uncommitted_changes(cap_out, tempdir_factory): config, ) assert rest == 'modified name!...........................................................Passed\n' # noqa: E501 + + +def test_try_repo_staged_changes(tempdir_factory): + repo = make_repo(tempdir_factory, 'modified_file_returns_zero_repo') + + with cwd(repo): + open('staged-file', 'a').close() + open('second-staged-file', 'a').close() + cmd_output('git', 'add', '.') + + with cwd(git_dir(tempdir_factory)): + assert not try_repo(try_repo_opts(repo, hook='bash_hook')) From a18b683d12feb95a966c46ca0e8a78ef62e89f80 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 18 Mar 2019 02:31:47 +0100 Subject: [PATCH 2/5] Add review suggestion Co-Authored-By: DanielChabrowski --- pre_commit/commands/try_repo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pre_commit/commands/try_repo.py b/pre_commit/commands/try_repo.py index 4bffd754..e55739e0 100644 --- a/pre_commit/commands/try_repo.py +++ b/pre_commit/commands/try_repo.py @@ -38,7 +38,7 @@ def _repo_ref(tmpdir, repo, ref): env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs) staged_files = git.get_staged_files(cwd=repo) - if (len(staged_files) > 0): + if staged_files: cmd_output('git', 'add', *staged_files, cwd=repo, env=env) cmd_output('git', 'add', '-u', cwd=repo, env=env) From 24a2c3d8db74d2dcec9818fa944a63bc2a66d1f5 Mon Sep 17 00:00:00 2001 From: DanielChabrowski Date: Tue, 19 Mar 2019 08:33:41 +0100 Subject: [PATCH 3/5] Add support for passing cwd and env to xargs --- pre_commit/xargs.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pre_commit/xargs.py b/pre_commit/xargs.py index a382759c..f32cb32c 100644 --- a/pre_commit/xargs.py +++ b/pre_commit/xargs.py @@ -109,6 +109,7 @@ def xargs(cmd, varargs, **kwargs): """ negate = kwargs.pop('negate', False) target_concurrency = kwargs.pop('target_concurrency', 1) + max_length = kwargs.pop('_max_length', _get_platform_max_length()) retcode = 0 stdout = b'' stderr = b'' @@ -118,10 +119,10 @@ def xargs(cmd, varargs, **kwargs): except parse_shebang.ExecutableNotFoundError as e: return e.to_output() - partitions = partition(cmd, varargs, target_concurrency, **kwargs) + partitions = partition(cmd, varargs, target_concurrency, max_length) def run_cmd_partition(run_cmd): - return cmd_output(*run_cmd, encoding=None, retcode=None) + return cmd_output(*run_cmd, encoding=None, retcode=None, **kwargs) threads = min(len(partitions), target_concurrency) with _thread_mapper(threads) as thread_map: From 7023caba944a6480d3a83cd4dd8e5d64b70e29dd Mon Sep 17 00:00:00 2001 From: DanielChabrowski Date: Tue, 19 Mar 2019 08:34:30 +0100 Subject: [PATCH 4/5] Execute with xargs in try_repo --- pre_commit/commands/try_repo.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pre_commit/commands/try_repo.py b/pre_commit/commands/try_repo.py index e55739e0..3e256ad8 100644 --- a/pre_commit/commands/try_repo.py +++ b/pre_commit/commands/try_repo.py @@ -15,6 +15,7 @@ from pre_commit.commands.run import run from pre_commit.store import Store from pre_commit.util import cmd_output from pre_commit.util import tmpdir +from pre_commit.xargs import xargs logger = logging.getLogger(__name__) @@ -39,7 +40,7 @@ def _repo_ref(tmpdir, repo, ref): staged_files = git.get_staged_files(cwd=repo) if staged_files: - cmd_output('git', 'add', *staged_files, cwd=repo, env=env) + xargs(('git', 'add', '--'), staged_files, cwd=repo, env=env) cmd_output('git', 'add', '-u', cwd=repo, env=env) git.commit(repo=shadow) From c7b369a7be37094e41a1737eb2057caf0245392e Mon Sep 17 00:00:00 2001 From: DanielChabrowski Date: Tue, 19 Mar 2019 09:30:18 +0100 Subject: [PATCH 5/5] Add test for xargs propagating kwargs to cmd_output --- tests/xargs_test.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/xargs_test.py b/tests/xargs_test.py index a6cffd72..71f5454c 100644 --- a/tests/xargs_test.py +++ b/tests/xargs_test.py @@ -208,3 +208,13 @@ def test_thread_mapper_concurrency_uses_threadpoolexecutor_map(): def test_thread_mapper_concurrency_uses_regular_map(): with xargs._thread_mapper(1) as thread_map: assert thread_map is map + + +def test_xargs_propagate_kwargs_to_cmd(): + env = {'PRE_COMMIT_TEST_VAR': 'Pre commit is awesome'} + cmd = ('bash', '-c', 'echo $PRE_COMMIT_TEST_VAR', '--') + cmd = parse_shebang.normalize_cmd(cmd) + + ret, stdout, _ = xargs.xargs(cmd, ('1',), env=env) + assert ret == 0 + assert b'Pre commit is awesome' in stdout