tree: simplify parse_tree_indirect()

Reduce code duplication by turning parse_tree_indirect() into a wrapper of repo_peel_to_type(). This avoids a segfault when handling a broken tag where ->tagged is NULL. The new version also checks the return value of parse_object() that was ignored by the old one. Initial-patch-by: Stefan Sperling <stsp@stsp.name> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Aug 29, 2019 at 21:06 UTC 1577dc0f7c1326ce59e1e97fad8a0b1e8c826203
1 file changed +3 -15
tree.c
+3 -15
@@ -244,19 +244,7 @@ void free_tree_buffer(struct tree *tree)
244
245 struct tree *parse_tree_indirect(const struct object_id *oid)
246 {
247 - struct object *obj = parse_object(the_repository, oid);
248 - do {
249 - if (!obj)
250 - return NULL;
251 - if (obj->type == OBJ_TREE)
252 - return (struct tree *) obj;
253 - else if (obj->type == OBJ_COMMIT)
254 - obj = &(get_commit_tree(((struct commit *)obj))->object);
255 - else if (obj->type == OBJ_TAG)
256 - obj = ((struct tag *) obj)->tagged;
257 - else
258 - return NULL;
259 - if (!obj->parsed)
260 - parse_object(the_repository, &obj->oid);
261 - } while (1);
247 + struct repository *r = the_repository;
248 + struct object *obj = parse_object(r, oid);
249 + return (struct tree *)repo_peel_to_type(r, NULL, 0, obj, OBJ_TREE);
250 }