diff --git a/pre_commit/main.py b/pre_commit/main.py index 0c3eefda..68c7282e 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -188,7 +188,14 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None: toplevel = git.get_root() os.chdir(toplevel) - args.config = os.path.relpath(args.config) + # https://github.com/pre-commit/pre-commit/issues/2530 + # `os.relpath` raises a ValueError on Windows when the paths given are on + # separate drives. + try: + args.config = os.path.relpath(args.config) + except ValueError: + pass + if args.command in {'run', 'try-repo'}: args.files = [os.path.relpath(filename) for filename in args.files] if args.commit_msg_filename is not None: diff --git a/tests/main_test.py b/tests/main_test.py index fed085fc..3263001b 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -29,6 +29,13 @@ def test_adjust_args_and_chdir_not_in_git_dir(in_tmpdir): main._adjust_args_and_chdir(_args()) +def test_adjust_args_and_chdir_relpath_to_different_drive(): + args = _args(command='run', files=['f1', 'f2']) + with mock.patch.object(os.path, 'relpath', side_effect=ValueError): + with pytest.raises(ValueError): + 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)