Skip to content

allow calling index.add, index.move and index.remove with single items #840

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 29 additions & 17 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,16 +569,23 @@ def _preprocess_add_items(self, items):
""" Split the items into two lists of path strings and BaseEntries. """
paths = []
entries = []

for item in items:
if isinstance(item, string_types):
paths.append(self._to_relative_path(item))
elif isinstance(item, (Blob, Submodule)):
entries.append(BaseIndexEntry.from_blob(item))
elif isinstance(item, BaseIndexEntry):
entries.append(item)
else:
raise TypeError("Invalid Type: %r" % item)

if isinstance(items, string_types):
paths.append(self._to_relative_path(items))
elif isinstance(items, (Blob, Submodule)):
entries.append(BaseIndexEntry.from_blob(items))
elif isinstance(items, BaseIndexEntry):
entries.append(items)
else:
for item in items:
if isinstance(item, string_types):
paths.append(self._to_relative_path(item))
elif isinstance(item, (Blob, Submodule)):
entries.append(BaseIndexEntry.from_blob(item))
elif isinstance(item, BaseIndexEntry):
entries.append(item)
else:
raise TypeError("Invalid Type: %r" % item)
# END for each item
return (paths, entries)

Expand Down Expand Up @@ -801,13 +808,18 @@ def _items_to_rela_paths(self, items):
"""Returns a list of repo-relative paths from the given items which
may be absolute or relative paths, entries or blobs"""
paths = []
for item in items:
if isinstance(item, (BaseIndexEntry, (Blob, Submodule))):
paths.append(self._to_relative_path(item.path))
elif isinstance(item, string_types):
paths.append(self._to_relative_path(item))
else:
raise TypeError("Invalid item type: %r" % item)
if isinstance(items, (BaseIndexEntry, (Blob, Submodule))):
paths.append(self._to_relative_path(items.path))
elif isinstance(items, string_types):
paths.append(self._to_relative_path(items))
else:
for item in items:
if isinstance(item, (BaseIndexEntry, (Blob, Submodule))):
paths.append(self._to_relative_path(item.path))
elif isinstance(item, string_types):
paths.append(self._to_relative_path(item))
else:
raise TypeError("Invalid item type: %r" % item)
# END for each item
return paths

Expand Down
9 changes: 8 additions & 1 deletion git/test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,14 @@ def test_compare_write_tree(self, rw_repo):
orig_tree = commit.tree
self.assertEqual(index.write_tree(), orig_tree)
# END for each commit


@with_rw_repo('HEAD', bare=False)
def test_index_single_addremove(self, rw_repo):
path = osp.join('git', 'test', 'test_index.py')
self._assert_entries(rw_repo.index.add(path))
deleted_files = rw_repo.index.remove(path)
assert deleted_files

def test_index_new(self):
B = self.rorepo.tree("6d9b1f4f9fa8c9f030e3207e7deacc5d5f8bba4e")
H = self.rorepo.tree("25dca42bac17d511b7e2ebdd9d1d679e7626db5f")
Expand Down