# Buggy code
def _is_tree_and_contains(obj, path):
    if obj.mode != GIT_MODE_DIR:
        return False
    # see https://github.com/gitpython-developers/GitPython/issues/851
    # `return (i in tree)` doesn't work so here is a workaround:
    for i in _iter_tree(obj):
        if i.name == path:
            return True
    return False


# Traceback (KeyError)
2020-03-16 09:53:37,900 ERROR: unexpected error - 'be23d78966ad0171e87879e051edf6eb3f446e12'
------------------------------------------------------------
Traceback (most recent call last):
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/main.py", line 50, in main
ret = cmd.run()
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/command/gc.py", line 60, in run
workspace=self.args.workspace,
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/repo/__init__.py", line 27, in wrapper
ret = f(repo, *args, **kwargs)
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/repo/gc.py", line 75, in gc
jobs=jobs,
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/repo/__init__.py", line 256, in used_cache
for stage, filter_info in pairs:
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/repo/__init__.py", line 252, in <genexpr>
for target in targets
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/repo/__init__.py", line 202, in collect_granular
return [(stage, None) for stage in self.stages]
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/funcy/objects.py", line 28, in __get__
res = instance.__dict__[self.fget.__name__] = self.fget(instance)
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/repo/__init__.py", line 397, in stages
for root, dirs, files in self.tree.walk(self.root_dir):
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/ignore.py", line 135, in walk
dirs[:], files[:] = self.dvcignore(root, dirs, files)
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/funcy/objects.py", line 28, in __get__
res = instance.__dict__[self.fget.__name__] = self.fget(instance)
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/ignore.py", line 115, in dvcignore
return DvcIgnoreFilter(self.tree)
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/ignore.py", line 93, in __init__
for root, dirs, files in self.tree.walk(self.tree.tree_root):
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/scm/git/tree.py", line 148, in walk
yield from self._walk(tree, topdown=topdown)
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/scm/git/tree.py", line 131, in _walk
yield from self._walk(tree[i], topdown=topdown)
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/scm/git/tree.py", line 121, in _walk
for i in _iter_tree(tree):
File "/home/my_user/data/env/my_project/lib/python3.6/site-packages/dvc/scm/git/tree.py", line 26, in _iter_tree
node = submodules[node.hexsha]
KeyError: 'be23d78966ad0171e87879e051edf6eb3f446e12'

# Fixed code
def _is_tree_and_contains(obj, path):
    if obj.mode != GIT_MODE_DIR:
        return False
    # see https://github.com/gitpython-developers/GitPython/issues/851
    # `return (i in tree)` doesn't work so here is a workaround:
    for item in obj:
        if _item_basename(item) == path:
            return True
    return False