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
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
Prev Previous commit
fixed code repetition
  • Loading branch information
MarcelBeining committed Sep 19, 2019
commit e36f324b054e3ae7b56aa4890beb68a0e955635e
59 changes: 29 additions & 30 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,25 +569,23 @@ def _preprocess_add_items(self, items):
""" Split the items into two lists of path strings and BaseEntries. """
paths = []
entries = []

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)
# check if is iterable, else put in list
try:
test_item = iter(items)
except TypeError:
items = [items]

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)
return paths, entries

def _store_path(self, filepath, fprogress):
"""Store file at filepath in the database and return the base index entry
Expand Down Expand Up @@ -808,18 +806,19 @@ 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 = []
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)
# check if is iterable, else put in list
try:
test_item = iter(items)
except TypeError:
items = [items]

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