Skip to content

Commit

Permalink
fixed code repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelBeining authored and Byron committed Oct 15, 2019
1 parent 3face90 commit 1fa1ce2
Showing 1 changed file with 29 additions and 30 deletions.
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

0 comments on commit 1fa1ce2

Please sign in to comment.