Sane refresh logic All the ODB backends have a specific refresh interface. When reading an object, first we attempt every single backend: if the read fails, then we refresh all the backends and retry the read one more time to see if the object has appeared.
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
diff --git a/include/git2/odb.h b/include/git2/odb.h
index f39e7b5..8fd1a95 100644
--- a/include/git2/odb.h
+++ b/include/git2/odb.h
@@ -192,6 +192,26 @@ GIT_EXTERN(int) git_odb_read_header(size_t *len_out, git_otype *type_out, git_od
GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
/**
+ * Refresh the object database to load newly added files.
+ *
+ * If the object databases have changed on disk while the library
+ * is running, this function will force a reload of the underlying
+ * indexes.
+ *
+ * Use this function when you're confident that an external
+ * application has tampered with the ODB.
+ *
+ * NOTE that it is not necessary to call this function at all. The
+ * library will automatically attempt to refresh the ODB
+ * when a lookup fails, to see if the looked up object exists
+ * on disk but hasn't been loaded yet.
+ *
+ * @param db database to refresh
+ * @return 0 on success, error code otherwise
+ */
+GIT_EXTERN(int) git_odb_refresh(struct git_odb *db);
+
+/**
* List all objects available in the database
*
* The callback will be called for each object available in the
diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h
index 029c61b..dbc3981 100644
--- a/include/git2/odb_backend.h
+++ b/include/git2/odb_backend.h
@@ -89,6 +89,8 @@ struct git_odb_backend {
struct git_odb_backend *,
const git_oid *);
+ int (* refresh)(struct git_odb_backend *);
+
int (* foreach)(
struct git_odb_backend *,
git_odb_foreach_cb cb,
diff --git a/src/odb.c b/src/odb.c
index 216715a..d0b2323 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -609,14 +609,22 @@ int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
{
unsigned int i;
int error = GIT_ENOTFOUND;
+ bool refreshed = false;
git_rawobj raw;
assert(out && db && id);
+ if (db->backends.length == 0) {
+ giterr_set(GITERR_ODB, "Failed to lookup object: no backends loaded");
+ return GIT_ENOTFOUND;
+ }
+
*out = git_cache_get(&db->cache, id);
if (*out != NULL)
return 0;
+attempt_lookup:
+
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;
@@ -625,9 +633,13 @@ int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
error = b->read(&raw.data, &raw.len, &raw.type, b, id);
}
- /* TODO: If no backends are configured, this returns GIT_ENOTFOUND but
- * will never have called giterr_set().
- */
+ if (error == GIT_ENOTFOUND && !refreshed) {
+ if ((error = git_odb_refresh(db)) < 0)
+ return error;
+
+ refreshed = true;
+ goto attempt_lookup;
+ }
if (error && error != GIT_PASSTHROUGH)
return error;
@@ -644,7 +656,7 @@ int git_odb_read_prefix(
git_oid found_full_oid = {{0}};
git_rawobj raw;
void *data = NULL;
- bool found = false;
+ bool found = false, refreshed = false;
assert(out && db);
@@ -660,11 +672,13 @@ int git_odb_read_prefix(
return 0;
}
+attempt_lookup:
+
for (i = 0; i < db->backends.length; ++i) {
backend_internal *internal = git_vector_get(&db->backends, i);
git_odb_backend *b = internal->backend;
- if (b->read != NULL) {
+ if (b->read_prefix != NULL) {
git_oid full_oid;
error = b->read_prefix(&full_oid, &raw.data, &raw.len, &raw.type, b, short_id, len);
if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH)
@@ -675,13 +689,23 @@ int git_odb_read_prefix(
git__free(data);
data = raw.data;
+
if (found && git_oid_cmp(&full_oid, &found_full_oid))
return git_odb__error_ambiguous("multiple matches for prefix");
+
found_full_oid = full_oid;
found = true;
}
}
+ if (!found && !refreshed) {
+ if ((error = git_odb_refresh(db)) < 0)
+ return error;
+
+ refreshed = true;
+ goto attempt_lookup;
+ }
+
if (!found)
return git_odb__error_notfound("no match for prefix", short_id);
@@ -820,12 +844,31 @@ int git_odb_write_pack(struct git_odb_writepack **out, git_odb *db, git_transfer
return error;
}
-void * git_odb_backend_malloc(git_odb_backend *backend, size_t len)
+void *git_odb_backend_malloc(git_odb_backend *backend, size_t len)
{
GIT_UNUSED(backend);
return git__malloc(len);
}
+int git_odb_refresh(struct git_odb *db)
+{
+ unsigned int i;
+ assert(db);
+
+ for (i = 0; i < db->backends.length; ++i) {
+ backend_internal *internal = git_vector_get(&db->backends, i);
+ git_odb_backend *b = internal->backend;
+
+ if (b->refresh != NULL) {
+ int error = b->refresh(b);
+ if (error < 0)
+ return error;
+ }
+ }
+
+ return 0;
+}
+
int git_odb__error_notfound(const char *message, const git_oid *oid)
{
if (oid != NULL) {
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 0cdf552..e1b44d9 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -138,7 +138,6 @@ static int pack_window_contains(git_mwindow *win, off_t offset);
static int packfile_sort__cb(const void *a_, const void *b_);
static int packfile_load__cb(void *_data, git_buf *path);
-static int packfile_refresh_all(struct pack_backend *backend);
static int pack_entry_find(struct git_pack_entry *e,
struct pack_backend *backend, const git_oid *oid);
@@ -237,33 +236,6 @@ static int packfile_load__cb(void *_data, git_buf *path)
return git_vector_insert(&backend->packs, pack);
}
-static int packfile_refresh_all(struct pack_backend *backend)
-{
- int error;
- struct stat st;
- git_buf path = GIT_BUF_INIT;
-
- if (backend->pack_folder == NULL)
- return 0;
-
- if (p_stat(backend->pack_folder, &st) < 0 || !S_ISDIR(st.st_mode))
- return git_odb__error_notfound("failed to refresh packfiles", NULL);
-
- git_buf_sets(&path, backend->pack_folder);
-
- /* reload all packs */
- error = git_path_direach(&path, packfile_load__cb, (void *)backend);
-
- git_buf_free(&path);
-
- if (error < 0)
- return error;
-
- git_vector_sort(&backend->packs);
-
- return 0;
-}
-
static int pack_entry_find_inner(
struct git_pack_entry *e,
struct pack_backend *backend,
@@ -294,7 +266,6 @@ static int pack_entry_find_inner(
static int pack_entry_find(struct git_pack_entry *e, struct pack_backend *backend, const git_oid *oid)
{
- int error;
struct git_pack_file *last_found = backend->last_found;
if (backend->last_found &&
@@ -303,10 +274,6 @@ static int pack_entry_find(struct git_pack_entry *e, struct pack_backend *backen
if (!pack_entry_find_inner(e, backend, oid, last_found))
return 0;
- if ((error = packfile_refresh_all(backend)) < 0)
- return error;
- if (!pack_entry_find_inner(e, backend, oid, last_found))
- return 0;
return git_odb__error_notfound("failed to find pack entry", oid);
}
@@ -356,17 +323,9 @@ static int pack_entry_find_prefix(
const git_oid *short_oid,
size_t len)
{
- unsigned found = 0;
- int error;
struct git_pack_file *last_found = backend->last_found;
+ unsigned int found = pack_entry_find_prefix_inner(e, backend, short_oid, len, last_found);
- if ((found = pack_entry_find_prefix_inner(e, backend, short_oid, len, last_found)) > 0)
- goto cleanup;
- if ((error = packfile_refresh_all(backend)) < 0)
- return error;
- found = pack_entry_find_prefix_inner(e, backend, short_oid, len, last_found);
-
-cleanup:
if (!found)
return git_odb__error_notfound("no matching pack entry for prefix", short_oid);
else if (found > 1)
@@ -383,6 +342,34 @@ cleanup:
* Implement the git_odb_backend API calls
*
***********************************************************/
+static int pack_backend__refresh(git_odb_backend *_backend)
+{
+ struct pack_backend *backend = (struct pack_backend *)_backend;
+
+ int error;
+ struct stat st;
+ git_buf path = GIT_BUF_INIT;
+
+ if (backend->pack_folder == NULL)
+ return 0;
+
+ if (p_stat(backend->pack_folder, &st) < 0 || !S_ISDIR(st.st_mode))
+ return git_odb__error_notfound("failed to refresh packfiles", NULL);
+
+ git_buf_sets(&path, backend->pack_folder);
+
+ /* reload all packs */
+ error = git_path_direach(&path, packfile_load__cb, (void *)backend);
+
+ git_buf_free(&path);
+
+ if (error < 0)
+ return error;
+
+ git_vector_sort(&backend->packs);
+ return 0;
+}
+
static int pack_backend__read_header(size_t *len_p, git_otype *type_p, struct git_odb_backend *backend, const git_oid *oid)
{
@@ -468,7 +455,7 @@ static int pack_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb c
backend = (struct pack_backend *)_backend;
/* Make sure we know about the packfiles */
- if ((error = packfile_refresh_all(backend)) < 0)
+ if ((error = pack_backend__refresh(_backend)) < 0)
return error;
git_vector_foreach(&backend->packs, i, p) {
@@ -581,6 +568,7 @@ int git_odb_backend_one_pack(git_odb_backend **backend_out, const char *idx)
backend->parent.read_prefix = &pack_backend__read_prefix;
backend->parent.read_header = &pack_backend__read_header;
backend->parent.exists = &pack_backend__exists;
+ backend->parent.refresh = &pack_backend__refresh;
backend->parent.foreach = &pack_backend__foreach;
backend->parent.free = &pack_backend__free;
@@ -619,6 +607,7 @@ int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
backend->parent.read_prefix = &pack_backend__read_prefix;
backend->parent.read_header = &pack_backend__read_header;
backend->parent.exists = &pack_backend__exists;
+ backend->parent.refresh = &pack_backend__refresh;
backend->parent.foreach = &pack_backend__foreach;
backend->parent.writepack = &pack_backend__writepack;
backend->parent.free = &pack_backend__free;