summaryrefslogtreecommitdiff
path: root/src/odb.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/odb.c')
-rw-r--r--src/odb.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/odb.c b/src/odb.c
index b2988c429..02f97915d 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -465,6 +465,13 @@ int git_odb_new(git_odb **out)
git__free(db);
return -1;
}
+ if (git_buf_init(&db->objects_dir, 0) < 0) {
+ git_vector_free(&db->backends);
+ git_cache_dispose(&db->own_cache);
+ git_mutex_free(&db->lock);
+ git__free(db);
+ return -1;
+ }
*out = db;
GIT_REFCOUNT_INC(db);
@@ -612,6 +619,17 @@ int git_odb__add_default_backends(
git_mutex_unlock(&db->lock);
#endif
+ if (git_mutex_lock(&db->lock) < 0) {
+ git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock");
+ return -1;
+ }
+ if (git_buf_len(&db->objects_dir) == 0 && git_buf_sets(&db->objects_dir, objects_dir) < 0) {
+ git_mutex_unlock(&db->lock);
+ git_odb_free(db);
+ return -1;
+ }
+ git_mutex_unlock(&db->lock);
+
/* add the loose object backend */
if (git_odb_backend_loose(&loose, objects_dir, -1, db->do_fsync, 0, 0) < 0 ||
add_backend_internal(db, loose, GIT_LOOSE_PRIORITY, as_alternates, inode) < 0)
@@ -742,6 +760,8 @@ static void odb_free(git_odb *db)
if (locked)
git_mutex_unlock(&db->lock);
+ git_buf_dispose(&db->objects_dir);
+ git_commit_graph_free(db->cgraph);
git_vector_free(&db->backends);
git_cache_dispose(&db->own_cache);
git_mutex_free(&db->lock);
@@ -786,6 +806,53 @@ static int odb_exists_1(
return (int)found;
}
+int git_odb__get_commit_graph(git_commit_graph_file **out, git_odb *db)
+{
+ int error = 0;
+
+ if ((error = git_mutex_lock(&db->lock)) < 0) {
+ git_error_set(GIT_ERROR_ODB, "failed to acquire the db lock");
+ return error;
+ }
+ if (!db->cgraph_checked) {
+ git_buf commit_graph_path = GIT_BUF_INIT;
+ git_commit_graph_file *cgraph = NULL;
+
+ /* We only check once, no matter the result. */
+ db->cgraph_checked = 1;
+
+ if (git_buf_len(&db->objects_dir) == 0) {
+ /*
+ * This odb was not opened with an objects directory
+ * associated. Skip opening the commit graph.
+ */
+ goto done;
+ }
+
+ if ((error = git_buf_joinpath(
+ &commit_graph_path,
+ git_buf_cstr(&db->objects_dir),
+ "info/commit-graph"))
+ < 0) {
+ git_buf_dispose(&commit_graph_path);
+ goto done;
+ }
+ /* Best effort */
+ error = git_commit_graph_open(&cgraph, git_buf_cstr(&commit_graph_path));
+ git_buf_dispose(&commit_graph_path);
+
+ if (error < 0)
+ goto done;
+
+ db->cgraph = cgraph;
+ }
+
+done:
+ *out = db->cgraph;
+ git_mutex_unlock(&db->lock);
+ return 0;
+}
+
static int odb_freshen_1(
git_odb *db,
const git_oid *id,
@@ -1695,6 +1762,13 @@ int git_odb_refresh(struct git_odb *db)
}
}
}
+ if (db->cgraph && git_commit_graph_needs_refresh(db->cgraph, NULL)) {
+ /* We just free the commit graph. The next time it is requested, it will be re-loaded. */
+ git_commit_graph_free(db->cgraph);
+ db->cgraph = NULL;
+ }
+ /* Force a lazy re-check next time it is needed. */
+ db->cgraph_checked = 0;
git_mutex_unlock(&db->lock);
return 0;