Fix: os.path.relpath (ntpath.relpath on Windows) raises ValueError when the... - #3742
Closed
M001N wants to merge 2 commits into
Closed
Fix: os.path.relpath (ntpath.relpath on Windows) raises ValueError when the...#3742M001N wants to merge 2 commits into
M001N wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Factored out a _relpath_or_abspath(path) helper that tries os.path.relpath and falls back to os.path.abspath on ValueError. Applied it uniformly to args.config, args.files, args.commit_msg_filename, and args.repo in _adjust_args_and_chdir, replacing the previous config-only try/except.
Problem
pre-commit/pre-commit issue reference: #2530
Root Cause
os.path.relpath (ntpath.relpath on Windows) raises ValueError when the target path and the current working directory are on different drives/mounts. _adjust_args_and_chdir in pre_commit/main.py calls relpath on args.config, args.files, args.commit_msg_filename, and args.repo after chdir'ing to the git toplevel — all four share the identical cross-drive precondition, but only args.config was previously guarded (from the prior attempt); files/commit_msg_filename/repo were left unguarded.
Testing
PASS - all 32 tests in tests/main_test.py pass, including the 3 cross-drive ValueError regression tests
Related Issue
#2530