Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
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
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.
  • Loading branch information
abhayjnayakk committed Apr 19, 2026
commit 46dd8f7309218444c9be84da1dea8dc7b3d23643
24 changes: 18 additions & 6 deletions pre_commit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading