GitPython icon indicating copy to clipboard operation
GitPython copied to clipboard

index.add with path_rewriter conflicts with directory paths expansion

Open zhiltsov-max opened this issue 4 years ago • 2 comments

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 avatar May 14 '21 10:05 zhiltsov-max

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:

https://github.com/gitpython-developers/GitPython/blob/33346b25c3a4fb5ea37202d88d6a6c66379099c5/git/index/base.py#L620

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

Byron avatar May 15 '21 01:05 Byron

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)]

zhiltsov-max avatar May 15 '21 07:05 zhiltsov-max