code: def write_tree_from_cache(entries, odb, sl, si=0):
      tree_items = []
      tree_items_append = tree_items.append
      ci = sl.start
      end = sl.stop
      while ci < end:
          entry = entries[ci]
          if entry.stage != 0:
              raise UnmergedEntriesError(entry)
          ci += 1
          rbound = entry.path.find('/', si)
          if rbound == -1:
              tree_items_append((entry.binsha, entry.mode, entry.path[si:]))
          else:
              base = entry.path[si:rbound]
              xi = ci
              while xi < end:
                  oentry = entries[xi]
                  orbound = oentry.path.find('/', si)
                  if orbound == -1 or oentry.path[si:orbound] != base:
                      break
                  xi += 1
              sha, tree_entry_list = write_tree_from_cache(entries, odb, slice(ci - 1, xi), rbound + 1)
              tree_items_append((sha, S_IFDIR, base))
              ci = xi
      sio = BytesIO()
      tree_to_stream(tree_items, sio.write)
      sio.seek(0)
      istream = odb.store(IStream(str_tree_type, len(sio.getvalue()), sio))
      return (istream.binsha, tree_items)