Skip to content
Closed
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
7 changes: 6 additions & 1 deletion pre_commit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
toplevel = git.get_root()
os.chdir(toplevel)

args.config = os.path.relpath(args.config)
try:
args.config = os.path.relpath(args.config)
except ValueError:
# A config file on a different drive cannot be made relative to the
# repository. Keep the absolute path so it remains usable.
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:
Expand Down
9 changes: 9 additions & 0 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ def test_adjust_args_and_chdir_non_relative_config(in_git_dir):
assert args.config == C.CONFIG_FILE


def test_adjust_args_and_chdir_config_on_different_drive(in_git_dir):
args = _args(config=r'C:\config\.pre-commit-config.yaml')

with mock.patch.object(main.os.path, 'relpath', side_effect=ValueError):
main._adjust_args_and_chdir(args)

assert args.config == r'C:\config\.pre-commit-config.yaml'


def test_adjust_args_try_repo_repo_relative(in_git_dir):
with in_git_dir.join('foo').ensure_dir().as_cwd():
args = _args(command='try-repo', repo='../foo', files=[])
Expand Down
Loading