mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #1778 from pre-commit/bare_worktree
fix execution in worktrees in subdirectories of bare repositories
This commit is contained in:
commit
588b6ed3fc
3 changed files with 21 additions and 6 deletions
|
|
@ -61,7 +61,7 @@ def get_root() -> str:
|
||||||
'git failed. Is it installed, and are you in a Git repository '
|
'git failed. Is it installed, and are you in a Git repository '
|
||||||
'directory?',
|
'directory?',
|
||||||
)
|
)
|
||||||
if os.path.commonpath((root, git_dir)) == git_dir:
|
if os.path.samefile(root, git_dir):
|
||||||
raise FatalError(
|
raise FatalError(
|
||||||
'git toplevel unexpectedly empty! make sure you are not '
|
'git toplevel unexpectedly empty! make sure you are not '
|
||||||
'inside the `.git` directory of your repository.',
|
'inside the `.git` directory of your repository.',
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import os.path
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
|
from pre_commit.error_handler import FatalError
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from testing.util import git_commit
|
from testing.util import git_commit
|
||||||
|
|
||||||
|
|
@ -18,6 +19,25 @@ def test_get_root_deeper(in_git_dir):
|
||||||
assert os.path.normcase(git.get_root()) == expected
|
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):
|
def test_get_staged_files_deleted(in_git_dir):
|
||||||
in_git_dir.join('test').ensure()
|
in_git_dir.join('test').ensure()
|
||||||
cmd_output('git', 'add', 'test')
|
cmd_output('git', 'add', 'test')
|
||||||
|
|
|
||||||
|
|
@ -35,11 +35,6 @@ def test_adjust_args_and_chdir_not_in_git_dir(in_tmpdir):
|
||||||
main._adjust_args_and_chdir(_args())
|
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):
|
def test_adjust_args_and_chdir_noop(in_git_dir):
|
||||||
args = _args(command='run', files=['f1', 'f2'])
|
args = _args(command='run', files=['f1', 'f2'])
|
||||||
main._adjust_args_and_chdir(args)
|
main._adjust_args_and_chdir(args)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue