From 46dd8f7309218444c9be84da1dea8dc7b3d23643 Mon Sep 17 00:00:00 2001 From: Abhay Date: Sun, 19 Apr 2026 22:33:24 +0530 Subject: [PATCH] fix: handle ValueError from os.path.relpath on cross-drive Windows paths On Windows, os.path.relpath raises ValueError when the path and the current working directory are on different drives. Wrap all relpath calls in try/except to fall back to the absolute path. --- pre_commit/main.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/pre_commit/main.py b/pre_commit/main.py index 0c3eefdaa..13297006b 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -188,15 +188,27 @@ 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: + pass if args.command in {'run', 'try-repo'}: - args.files = [os.path.relpath(filename) for filename in args.files] + try: + args.files = [os.path.relpath(filename) for filename in args.files] + except ValueError: + pass if args.commit_msg_filename is not None: - args.commit_msg_filename = os.path.relpath( - args.commit_msg_filename, - ) + try: + args.commit_msg_filename = os.path.relpath( + args.commit_msg_filename, + ) + except ValueError: + pass if args.command == 'try-repo' and os.path.exists(args.repo): - args.repo = os.path.relpath(args.repo) + try: + args.repo = os.path.relpath(args.repo) + except ValueError: + pass def main(argv: Sequence[str] | None = None) -> int: