Skip to content

index.add with path_rewriter conflicts with directory paths expansion #1242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zhiltsov-max opened this issue May 14, 2021 · 2 comments
Open

Comments

@zhiltsov-max
Copy link

zhiltsov-max commented May 14, 2021

It seems that if index.add() is called with path_rewriter, it would cancel out the logic for automatic directory paths expansion and traversal. My use case is to call add with a directory and replace (truncate the dir prefix) the paths of the upcoming commit.

/repo/a/b/c/<files and dirs>  <-- I want these files in a commit with a/b/c/ removed

Current behaviour:
Paths replaced first and then directories are not expanded.

Current workaround:
Expand directory paths manually with os.walk, glob etc.

Expected behavior:

repo.index.add('a/b/c/', path_rewriter=lambda p: osp.relpath(p, 'a/b/c/'))
# index includes <files> with a/b/c/ prefix removed in paths
@zhiltsov-max zhiltsov-max changed the title index.add with path_rewriter conflicts with directory paths expansion and traversal index.add with path_rewriter conflicts with directory paths expansion May 14, 2021
@Byron
Copy link
Member

Byron commented May 15, 2021

Thanks for posting. When taking a glance at the code it wasn't immediately obvious how the automatic expansion is cancelled, but maybe it has something to do with this line:

del(paths[:])

That said, if a test could be devised to reproduce the issue, a fix certainly can't be far either.

@zhiltsov-max
Copy link
Author

Hi, thanks for the response. Yes, I'm also thinking about this del call. The example:

mkdir -p test_git
cd test_git
mkdir -p a
touch a/f.txt

python

import git
rewriter = lambda e: 'rewritten'
r = git.Repo.init()
r.index.add('a', path_rewriter=rewriter)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/venv/lib/python3.6/site-packages/git/index/base.py", line 766, in add
    handle_null_entries(self)
  File "/venv/lib/python3.6/site-packages/git/index/util.py", line 91, in set_git_working_dir
    return func(self, *args, **kwargs)
  File "/venv/lib/python3.6/site-packages/git/index/base.py", line 759, in handle_null_entries
    new_entry = self._store_path(null_entry.path, fprogress)
  File "/venv/lib/python3.6/site-packages/git/index/base.py", line 594, in _store_path
    with open_stream() as stream:
  File "/venv/lib/python3.6/site-packages/git/index/base.py", line 593, in <lambda>
    open_stream = lambda: open(filepath, 'rb')
IsADirectoryError: [Errno 21] Is a directory: 'a'

r.index.add('a/f.txt', path_rewriter=rewriter)
[(100644, e69de29bb2d1d6434b8b29ae775ad8c2e48c5391, 0, rewritten)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants