Commit ea35256be57fbb7d6afbedd8382c9e6e36b39718

Stefan Sperling 2018-03-16T19:27:48

avoid a round-trip via tempfile when reading packed commits

diff --git a/lib/object.c b/lib/object.c
index 66fcf36..1cd78bd 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -615,20 +615,27 @@ got_object_commit_open(struct got_commit_object **commit,
     struct got_repository *repo, struct got_object *obj)
 {
 	const struct got_error *err = NULL;
-	FILE *f;
 
 	if (obj->type != GOT_OBJ_TYPE_COMMIT)
 		return got_error(GOT_ERR_OBJ_TYPE);
 
-	if (obj->flags & GOT_OBJ_FLAG_PACKED)
-		err = got_packfile_extract_object(&f, obj, repo);
-	else
+	if (obj->flags & GOT_OBJ_FLAG_PACKED) {
+		uint8_t *buf;
+		size_t len;
+		err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
+		if (err)
+			return err;
+		len -= obj->hdrlen;
+		err = parse_commit_object(commit, buf + obj->hdrlen, len);
+		free(buf);
+	} else {
+		FILE *f;
 		err = open_loose_object(&f, obj, repo);
-	if (err)
-		return err;
-
-	err = read_commit_object(commit, repo, obj, f);
-	fclose(f);
+		if (err)
+			return err;
+		err = read_commit_object(commit, repo, obj, f);
+		fclose(f);
+	}
 	return err;
 }
 
diff --git a/lib/pack.c b/lib/pack.c
index a9f4e70..df43739 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -1426,9 +1426,6 @@ got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
 	const struct got_error *err = NULL;
 	FILE *packfile = NULL;
 
-	if (obj->type != GOT_OBJ_TYPE_TREE)
-		return got_error(GOT_ERR_OBJ_TYPE);
-
 	if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
 		return got_error(GOT_ERR_OBJ_NOT_PACKED);