_to_relative_path to support mixing slashes and backslashes#1961
Merged
Byron merged 4 commits intogitpython-developers:mainfrom Sep 14, 2024
Andrej730:main
Merged
_to_relative_path to support mixing slashes and backslashes#1961Byron merged 4 commits intogitpython-developers:mainfrom Andrej730:main
Byron merged 4 commits intogitpython-developers:mainfrom
Andrej730:main
Conversation
Working on Windows you sometime end up having some paths with backslashes (windows native) and some with slashes - this PR will resolve the issue using gitpython for those kind of cases (see example below). It will also fix the issues if paths contain redundant separators or "..".
```
import git
repo = git.Repo(r"C:\gittest")
repo.index.add(r"C:\gittest\1.txt")
# Traceback (most recent call last):
# File "c:\second_test.py", line 5, in <module>
# repo.index.add(r"C:/gittest/2.txt")
# File "Python311\Lib\site-packages\git\index\base.py", line 879, in add
# paths, entries = self._preprocess_add_items(items)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# File "Python311\Lib\site-packages\git\index\base.py", line 672, in _preprocess_add_items
# paths.append(self._to_relative_path(item))
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# File "Python311\Lib\site-packages\git\index\base.py", line 657, in _to_relative_path
# raise ValueError("Absolute path %r is not in git repository at %r" % (path, self.repo.working_tree_dir))
# ValueError: Absolute path 'C:/gittest/2.txt' is not in git repository at 'C:\\gittest'
repo.index.add(r"C:/gittest/2.txt")
repo.index.commit("test")
```
Byron
requested changes
Sep 14, 2024
Member
Byron
left a comment
There was a problem hiding this comment.
Thanks a lot - this looks like an improvement.
Could you also add a test that fails with the previous implementation?
Maybe as a question on the side: what prevents the caller from normalizing the input paths right away?
Contributor
Author
Added.
Well, normalizing the inputs is one error traceback back away indeed but usually when some method has a string-path argument user expects the method to handle any kind of paths. |
Member
|
Thanks for the test! Once CI passes I think this can be merged. |
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Working on Windows you sometime end up having some paths with backslashes (windows native) and some with slashes - this PR will resolve the issue using gitpython for those kind of cases (see example below). It will also fix the issues if paths contain redundant separators or "..".