avoid a round-trip via tempfile when reading packed commits
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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);