eliminate redundant cache search in got_object_open_as_commit()
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
diff --git a/lib/object.c b/lib/object.c
index 5d6b4ff..ef9404c 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -271,6 +271,51 @@ got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
return got_object_open(obj, repo, &id);
}
+static const struct got_error *
+open_commit(struct got_commit_object **commit,
+ struct got_repository *repo, struct got_object *obj, int check_cache)
+{
+ const struct got_error *err = NULL;
+
+ if (check_cache) {
+ *commit = got_repo_get_cached_commit(repo, &obj->id);
+ if (*commit != NULL) {
+ (*commit)->refcnt++;
+ return NULL;
+ }
+ } else
+ *commit = NULL;
+
+ if (obj->type != GOT_OBJ_TYPE_COMMIT)
+ return got_error(GOT_ERR_OBJ_TYPE);
+
+ if (obj->flags & GOT_OBJ_FLAG_PACKED) {
+ struct got_pack *pack;
+ pack = got_repo_get_cached_pack(repo, obj->path_packfile);
+ if (pack == NULL) {
+ err = got_repo_cache_pack(&pack, repo,
+ obj->path_packfile, NULL);
+ if (err)
+ return err;
+ }
+ err = got_object_read_packed_commit_privsep(commit, obj, pack);
+ } else {
+ int fd;
+ err = open_loose_object(&fd, obj, repo);
+ if (err)
+ return err;
+ err = got_object_read_commit_privsep(commit, obj, fd, repo);
+ close(fd);
+ }
+
+ if (err == NULL) {
+ (*commit)->refcnt++;
+ err = got_repo_cache_commit(repo, &obj->id, *commit);
+ }
+
+ return err;
+}
+
const struct got_error *
got_object_open_as_commit(struct got_commit_object **commit,
struct got_repository *repo, struct got_object_id *id)
@@ -292,13 +337,20 @@ got_object_open_as_commit(struct got_commit_object **commit,
goto done;
}
- err = got_object_commit_open(commit, repo, obj);
+ err = open_commit(commit, repo, obj, 0);
done:
got_object_close(obj);
return err;
}
const struct got_error *
+got_object_commit_open(struct got_commit_object **commit,
+ struct got_repository *repo, struct got_object *obj)
+{
+ return open_commit(commit, repo, obj, 1);
+}
+
+const struct got_error *
got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
{
const struct got_error *err = NULL;
@@ -319,48 +371,6 @@ got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
}
const struct got_error *
-got_object_commit_open(struct got_commit_object **commit,
- struct got_repository *repo, struct got_object *obj)
-{
- const struct got_error *err = NULL;
-
- *commit = got_repo_get_cached_commit(repo, &obj->id);
- if (*commit != NULL) {
- (*commit)->refcnt++;
- return NULL;
- }
-
- if (obj->type != GOT_OBJ_TYPE_COMMIT)
- return got_error(GOT_ERR_OBJ_TYPE);
-
- if (obj->flags & GOT_OBJ_FLAG_PACKED) {
- struct got_pack *pack;
- pack = got_repo_get_cached_pack(repo, obj->path_packfile);
- if (pack == NULL) {
- err = got_repo_cache_pack(&pack, repo,
- obj->path_packfile, NULL);
- if (err)
- return err;
- }
- err = got_object_read_packed_commit_privsep(commit, obj, pack);
- } else {
- int fd;
- err = open_loose_object(&fd, obj, repo);
- if (err)
- return err;
- err = got_object_read_commit_privsep(commit, obj, fd, repo);
- close(fd);
- }
-
- if (err == NULL) {
- (*commit)->refcnt++;
- err = got_repo_cache_commit(repo, &obj->id, *commit);
- }
-
- return err;
-}
-
-const struct got_error *
got_object_tree_open(struct got_tree_object **tree,
struct got_repository *repo, struct got_object *obj)
{