From 2e4cde9cb0f16030e20cbb5c08fe3814d42a9c38 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 13 Apr 2014 23:52:52 -0700 Subject: [PATCH] Simplify has_unmerged_paths --- pre_commit/commands.py | 10 ++-------- tests/commands_test.py | 10 +++------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/pre_commit/commands.py b/pre_commit/commands.py index defd4199..33e36879 100644 --- a/pre_commit/commands.py +++ b/pre_commit/commands.py @@ -30,9 +30,6 @@ COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicat PASS_FAIL_LENGTH = 6 -# Grabbed from `git help status` -CONFLICTING_GIT_STATUSES = set(('DD', 'AU', 'UD', 'UA', 'DU', 'AA', 'UU')) - def install(runner): """Install the pre-commit hooks.""" @@ -233,11 +230,8 @@ def _run_hook(runner, hook_id, args, write): def _has_unmerged_paths(runner): - _, stdout, _ = runner.cmd_runner.run( - ['git', 'status', '--short'], retcode=None, - ) - codes = set(line[:2] for line in stdout.splitlines()) - return codes & CONFLICTING_GIT_STATUSES > set() + _, stdout, _ = runner.cmd_runner.run(['git', 'ls-files', '--unmerged']) + return bool(stdout.strip()) def run(runner, args, write=sys.stdout.write): diff --git a/tests/commands_test.py b/tests/commands_test.py index 37fe3aea..e3e6eb17 100644 --- a/tests/commands_test.py +++ b/tests/commands_test.py @@ -277,14 +277,10 @@ def test_no_stash(repo_with_passing_hook, no_stash, all_files, expect_stash): assert warning_msg not in printed -@pytest.mark.parametrize( - ('mode', 'expected'), - [(status, True) for status in commands.CONFLICTING_GIT_STATUSES] + - [(' A', False), (' D', False), (' M', False)] -) -def test_has_unmerged_paths(mode, expected): +@pytest.mark.parametrize(('output', 'expected'), (('some', True), ('', False))) +def test_has_unmerged_paths(output, expected): mock_runner = mock.Mock() - mock_runner.cmd_runner.run.return_value = (1, mode + ' foo', '') + mock_runner.cmd_runner.run.return_value = (1, output, '') assert commands._has_unmerged_paths(mock_runner) is expected