odb: Implement new helper to read types without refreshing
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
diff --git a/src/odb.c b/src/odb.c
index 9c35fd0..c67c547 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -49,8 +49,37 @@ static git_cache *odb_cache(git_odb *odb)
return &odb->own_cache;
}
+static int odb_otype_fast(git_otype *type_p, git_odb *db, const git_oid *id);
static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth);
+static git_otype odb_hardcoded_type(const git_oid *id)
+{
+ static git_oid empty_blob = {{ 0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1, 0xd6, 0x43, 0x4b, 0x8b,
+ 0x29, 0xae, 0x77, 0x5a, 0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91 }};
+ static git_oid empty_tree = {{ 0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
+ 0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04 }};
+
+ if (!git_oid_cmp(id, &empty_blob))
+ return GIT_OBJ_BLOB;
+
+ if (!git_oid_cmp(id, &empty_tree))
+ return GIT_OBJ_TREE;
+
+ return GIT_OBJ_BAD;
+}
+
+static int odb_read_hardcoded(git_rawobj *raw, const git_oid *id)
+{
+ git_otype type = odb_hardcoded_type(id);
+ if (type == GIT_OBJ_BAD)
+ return -1;
+
+ raw->type = type;
+ raw->len = 0;
+ raw->data = git__calloc(1, sizeof(uint8_t));
+ return 0;
+}
+
int git_odb__format_object_header(char *hdr, size_t n, git_off_t obj_len, git_otype obj_type)
{
const char *type_str = git_object_type2string(obj_type);
@@ -747,7 +776,7 @@ int git_odb_expand_ids(
git_odb_expand_id *ids,
size_t count)
{
- size_t len, i;
+ size_t i;
assert(db && ids);
@@ -760,7 +789,7 @@ int git_odb_expand_ids(
/* if we were given a full object ID, simply look it up */
if (query->length >= GIT_OID_HEXSZ) {
- error = git_odb_read_header(&len, &actual_type, db, &query->id);
+ error = odb_otype_fast(&actual_type, db, &query->id);
git_oid_cpy(&actual_id, &query->id);
}
@@ -771,7 +800,7 @@ int git_odb_expand_ids(
else if (query->length >= GIT_OID_MINPREFIXLEN) {
error = odb_exists_prefix_1(&actual_id, db, &query->id, query->length, false);
if (!error)
- error = git_odb_read_header(&len, &actual_type, db, &actual_id);
+ error = odb_otype_fast(&actual_type, db, &actual_id);
}
/* Ensure that the looked up type matches the type we were expecting */
@@ -816,11 +845,38 @@ int git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *db, const git
return error;
}
+static int odb_read_header_1(
+ size_t *len_p, git_otype *type_p, git_odb *db,
+ const git_oid *id, bool only_refreshed)
+{
+ size_t i;
+ int error = GIT_PASSTHROUGH;
+ git_otype ht;
+
+ if (!only_refreshed && (ht = odb_hardcoded_type(id)) != GIT_OBJ_BAD) {
+ *type_p = ht;
+ *len_p = 0;
+ return 0;
+ }
+
+ for (i = 0; i < db->backends.length && error < 0; ++i) {
+ backend_internal *internal = git_vector_get(&db->backends, i);
+ git_odb_backend *b = internal->backend;
+
+ if (only_refreshed && !b->refresh)
+ continue;
+
+ if (b->read_header != NULL)
+ error = b->read_header(len_p, type_p, b, id);
+ }
+
+ return error;
+}
+
int git_odb__read_header_or_object(
git_odb_object **out, size_t *len_p, git_otype *type_p,
git_odb *db, const git_oid *id)
{
- size_t i;
int error = GIT_ENOTFOUND;
git_odb_object *object;
@@ -834,52 +890,32 @@ int git_odb__read_header_or_object(
}
*out = NULL;
+ error = odb_read_header_1(len_p, type_p, db, id, false);
- for (i = 0; i < db->backends.length && error < 0; ++i) {
- backend_internal *internal = git_vector_get(&db->backends, i);
- git_odb_backend *b = internal->backend;
+ if (error == GIT_ENOTFOUND && !git_odb_refresh(db))
+ error = odb_read_header_1(len_p, type_p, db, id, true);
- if (b->read_header != NULL)
- error = b->read_header(len_p, type_p, b, id);
- }
+ if (error == GIT_ENOTFOUND)
+ return git_odb__error_notfound("cannot read header for", id, GIT_OID_HEXSZ);
- if (!error || error == GIT_PASSTHROUGH)
+ /* we found the header; return early */
+ if (!error)
return 0;
- /*
- * no backend could read only the header.
- * try reading the whole object and freeing the contents
- */
- if ((error = git_odb_read(&object, db, id)) < 0)
- return error; /* error already set - pass along */
-
- *len_p = object->cached.size;
- *type_p = object->cached.type;
- *out = object;
-
- return 0;
-}
-
-static git_oid empty_blob = {{ 0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1, 0xd6, 0x43, 0x4b, 0x8b,
- 0x29, 0xae, 0x77, 0x5a, 0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91 }};
-static git_oid empty_tree = {{ 0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
- 0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04 }};
-
-static int hardcoded_objects(git_rawobj *raw, const git_oid *id)
-{
- if (!git_oid_cmp(id, &empty_blob)) {
- raw->type = GIT_OBJ_BLOB;
- raw->len = 0;
- raw->data = git__calloc(1, sizeof(uint8_t));
- return 0;
- } else if (!git_oid_cmp(id, &empty_tree)) {
- raw->type = GIT_OBJ_TREE;
- raw->len = 0;
- raw->data = git__calloc(1, sizeof(uint8_t));
- return 0;
- } else {
- return GIT_ENOTFOUND;
+ if (error == GIT_PASSTHROUGH) {
+ /*
+ * no backend has header-reading functionality
+ * so try using `git_odb_read` instead
+ */
+ error = git_odb_read(&object, db, id);
+ if (!error) {
+ *len_p = object->cached.size;
+ *type_p = object->cached.type;
+ *out = object;
+ }
}
+
+ return error;
}
static int odb_read_1(git_odb_object **out, git_odb *db, const git_oid *id,
@@ -890,7 +926,7 @@ static int odb_read_1(git_odb_object **out, git_odb *db, const git_oid *id,
git_odb_object *object;
bool found = false;
- if (!hardcoded_objects(&raw, id))
+ if (!only_refreshed && odb_read_hardcoded(&raw, id) == 0)
found = true;
for (i = 0; i < db->backends.length && !found; ++i) {
@@ -944,6 +980,29 @@ int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
return error;
}
+static int odb_otype_fast(git_otype *type_p, git_odb *db, const git_oid *id)
+{
+ git_odb_object *object;
+ size_t _unused;
+ int error;
+
+ if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
+ *type_p = object->cached.type;
+ return 0;
+ }
+
+ error = odb_read_header_1(&_unused, type_p, db, id, false);
+
+ if (error == GIT_PASSTHROUGH) {
+ error = odb_read_1(&object, db, id, false);
+ if (!error)
+ *type_p = object->cached.type;
+ git_odb_object_free(object);
+ }
+
+ return error;
+}
+
static int read_prefix_1(git_odb_object **out, git_odb *db,
const git_oid *key, size_t len, bool only_refreshed)
{