Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pre_commit/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_root() -> str:
'git failed. Is it installed, and are you in a Git repository '
'directory?',
)
if os.path.commonpath((root, git_dir)) == git_dir:
if os.path.samefile(root, git_dir):
raise FatalError(
'git toplevel unexpectedly empty! make sure you are not '
'inside the `.git` directory of your repository.',
Expand Down
20 changes: 20 additions & 0 deletions tests/git_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from pre_commit import git
from pre_commit.error_handler import FatalError
from pre_commit.util import cmd_output
from testing.util import git_commit

Expand All @@ -18,6 +19,25 @@ def test_get_root_deeper(in_git_dir):
assert os.path.normcase(git.get_root()) == expected


def test_in_exactly_dot_git(in_git_dir):
with in_git_dir.join('.git').as_cwd(), pytest.raises(FatalError):
git.get_root()


def test_get_root_bare_worktree(tmpdir):
src = tmpdir.join('src').ensure_dir()
cmd_output('git', 'init', str(src))
git_commit(cwd=str(src))

bare = tmpdir.join('bare.git').ensure_dir()
cmd_output('git', 'clone', '--bare', str(src), str(bare))

cmd_output('git', 'worktree', 'add', 'foo', 'HEAD', cwd=bare)

with bare.join('foo').as_cwd():
assert git.get_root() == os.path.abspath('.')


def test_get_staged_files_deleted(in_git_dir):
in_git_dir.join('test').ensure()
cmd_output('git', 'add', 'test')
Expand Down
5 changes: 0 additions & 5 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ def test_adjust_args_and_chdir_not_in_git_dir(in_tmpdir):
main._adjust_args_and_chdir(_args())


def test_adjust_args_and_chdir_in_dot_git_dir(in_git_dir):
with in_git_dir.join('.git').as_cwd(), pytest.raises(FatalError):
main._adjust_args_and_chdir(_args())


def test_adjust_args_and_chdir_noop(in_git_dir):
args = _args(command='run', files=['f1', 'f2'])
main._adjust_args_and_chdir(args)
Expand Down