From 3f02a66e377bedf8e72a4e7b1859c55196b1c052 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 17 Dec 2015 20:06:16 -0800 Subject: [PATCH] Use rev-parse --show-toplevel --- pre_commit/git.py | 17 +++++++---------- tests/git_test.py | 4 ++-- tests/runner_test.py | 8 ++++---- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/pre_commit/git.py b/pre_commit/git.py index 83714bde..8b9a0f96 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -7,6 +7,7 @@ import os.path import re from pre_commit.errors import FatalError +from pre_commit.util import CalledProcessError from pre_commit.util import cmd_output from pre_commit.util import memoize_by_cwd @@ -15,16 +16,12 @@ logger = logging.getLogger('pre_commit') def get_root(): - path = os.getcwd() - while path != os.path.normpath(os.path.join(path, '../')): - if os.path.exists(os.path.join(path, '.git')): - return path - else: - path = os.path.normpath(os.path.join(path, '../')) - raise FatalError( - 'Called from outside of the gits. ' - 'Please cd to a git repository.' - ) + try: + return cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip() + except CalledProcessError: + raise FatalError( + 'Called from outside of the gits. Please cd to a git repository.' + ) def is_in_merge_conflict(): diff --git a/tests/git_test.py b/tests/git_test.py index 781d6504..ffd405bb 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -15,7 +15,7 @@ from testing.fixtures import git_dir def test_get_root_at_root(tempdir_factory): path = git_dir(tempdir_factory) with cwd(path): - assert git.get_root() == path + assert os.path.normcase(git.get_root()) == os.path.normcase(path) def test_get_root_deeper(tempdir_factory): @@ -24,7 +24,7 @@ def test_get_root_deeper(tempdir_factory): foo_path = os.path.join(path, 'foo') os.mkdir(foo_path) with cwd(foo_path): - assert git.get_root() == path + assert os.path.normcase(git.get_root()) == os.path.normcase(path) def test_get_root_not_git_dir(tempdir_factory): diff --git a/tests/runner_test.py b/tests/runner_test.py index 09255023..58efab51 100644 --- a/tests/runner_test.py +++ b/tests/runner_test.py @@ -24,8 +24,8 @@ def test_create_sets_correct_directory(tempdir_factory): path = git_dir(tempdir_factory) with cwd(path): runner = Runner.create() - assert runner.git_root == path - assert os.getcwd() == path + assert os.path.normcase(runner.git_root) == os.path.normcase(path) + assert os.path.normcase(os.getcwd()) == os.path.normcase(path) def test_create_changes_to_git_root(tempdir_factory): @@ -38,8 +38,8 @@ def test_create_changes_to_git_root(tempdir_factory): assert os.getcwd() != path runner = Runner.create() - assert runner.git_root == path - assert os.getcwd() == path + assert os.path.normcase(runner.git_root) == os.path.normcase(path) + assert os.path.normcase(os.getcwd()) == os.path.normcase(path) def test_config_file_path():