commit: Fix `GetTreeEntries()` failing hard when reading a blob
The GetTreeEntries()
will, given a commit and a path inside that
commit, return all tree entries below that path. While behaviour is
obvious in the case where the path does point to a subtree of the commit
in question, it's less obvious in case it's pointing to a blob given
that there cannot be any tree entries returned by such a query. While it
would be feasible to just return an error in that case given that the
query is misformed, we historically handled such queries gracefully by
returning no results at all.
This behaviour broke when converting the tree entries helper to use the
new catfile interfaces: the old Batch
interface used to return a
NotFoundError
if the expected type didn't match the actual type, and
we explicitly handle that case to gracefully fail and swallow the error.
Wtih the conversion, We do not check for the type of the object returned
by the catfile process anymore, but just blindly try to parse it as a
tree object even if it refers to a blob. Luckily, this regression was
catched by Rails' test suite.
Fix the regression by properly checking for the object type again: in case it is not a tree, we just return nothing at all. While at it, this commit also makes sure to always discard objects in case we fail with an error such that the object reader is not dirty on failure.
Changelog: fixed
Fixes: 31205084 (commit: Convert tree entry helpers to accept catfile readers, 2021-10-12)
Part of create-stage#12938 (closed)
Fixes #3861 (closed)