diff --git a/object.c b/object.c
index cfc4969ed9b..16793d9958a 100644
--- a/object.c
+++ b/object.c
@@ -160,8 +160,11 @@ struct object *parse_object_buffer(const unsigned char *sha1, enum object_type t
 		parse_tag_buffer(tag, buffer, size);
 		obj = &tag->object;
 	} else {
+		warning("object %s has unknown type id %d\n", sha1_to_hex(sha1), type);
 		obj = NULL;
 	}
+	if (obj && obj->type == OBJ_NONE)
+		obj->type = type;
 	*eaten_p = eaten;
 	return obj;
 }
diff --git a/tree.c b/tree.c
index e4a39aa3c36..e946dac0693 100644
--- a/tree.c
+++ b/tree.c
@@ -173,8 +173,13 @@ static void track_tree_refs(struct tree *item)
 			continue;
 		if (S_ISDIR(entry.mode))
 			obj = &lookup_tree(entry.sha1)->object;
-		else
+		else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
 			obj = &lookup_blob(entry.sha1)->object;
+		else {
+			warning("in tree %s: entry %s has bad mode %.6o\n",
+			     sha1_to_hex(item->object.sha1), entry.path, entry.mode);
+			obj = lookup_unknown_object(entry.sha1);
+		}
 		refs->ref[i++] = obj;
 	}
 	set_object_refs(&item->object, refs);