mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #1224 from pre-commit/better_error_inside_dotgit
Show better error message when running inside `.git`
This commit is contained in:
commit
023b337ff0
2 changed files with 14 additions and 1 deletions
|
|
@ -122,12 +122,20 @@ def _adjust_args_and_chdir(args):
|
|||
args.repo = os.path.abspath(args.repo)
|
||||
|
||||
try:
|
||||
os.chdir(git.get_root())
|
||||
toplevel = git.get_root()
|
||||
except CalledProcessError:
|
||||
raise FatalError(
|
||||
'git failed. Is it installed, and are you in a Git repository '
|
||||
'directory?',
|
||||
)
|
||||
else:
|
||||
if toplevel == '':
|
||||
raise FatalError(
|
||||
'git toplevel unexpectedly empty! make sure you are not '
|
||||
'inside the `.git` directory of your repository.',
|
||||
)
|
||||
else:
|
||||
os.chdir(toplevel)
|
||||
|
||||
args.config = os.path.relpath(args.config)
|
||||
if args.command in {'run', 'try-repo'}:
|
||||
|
|
|
|||
|
|
@ -39,6 +39,11 @@ 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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue