code: def write_tree(self):
          
          # we obtain no lock as we just flush our contents to disk as tree
          # If we are a new index, the entries access will load our data accordingly
          mdb = MemoryDB()
          entries = self._entries_sorted()
          binsha, tree_items = write_tree_from_cache(entries, mdb, slice(0, len(entries)))
  
          # copy changed trees only
          mdb.stream_copy(mdb.sha_iter(), self.repo.odb)
  
          # note: additional deserialization could be saved if write_tree_from_cache
          # would return sorted tree entries
          root_tree = Tree(self.repo, binsha, path='')
          root_tree._cache = tree_items
          return root_tree
docs: Writes this index to a corresponding Tree object into the repository's
          object database and return it.
  
          :return: Tree object representing this index
          :note: The tree will be written even if one or more objects the tree refers to
              does not yet exist in the object database. This could happen if you added
              Entries to the index directly.
          :raise ValueError: if there are no entries in the cache
          :raise UnmergedEntriesError: