Add new Repository initialization method Lets the user specify the ODB that will be used by the repository manually. Signed-off-by: Vicent Marti <tanoku@gmail.com>
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
diff --git a/src/git2/repository.h b/src/git2/repository.h
index 4eaa6f2..37a541f 100644
--- a/src/git2/repository.h
+++ b/src/git2/repository.h
@@ -98,6 +98,42 @@ GIT_EXTERN(int) git_repository_open2(git_repository **repository,
/**
+ * Open a git repository by manually specifying its paths and
+ * the object database it will use.
+ *
+ * @param repository pointer to the repo which will be opened
+ *
+ * @param git_dir The full path to the repository folder
+ * e.g. a '.git' folder for live repos, any folder for bare
+ * Equivalent to $GIT_DIR.
+ * Cannot be NULL.
+ *
+ * @param object_database A pointer to a git_odb created & initialized
+ * by the user (e.g. with custom backends). This object database
+ * will be owned by the repository and will be automatically free'd.
+ * It should not be manually free'd by the user, or this
+ * git_repository object will become invalid.
+ *
+ * @param git_index_file The full path to the index (dircache) file
+ * Equivalent to $GIT_INDEX_FILE.
+ * If NULL, "$GIT_DIR/index" is assumed.
+ *
+ * @param git_work_tree The full path to the working tree of the repository,
+ * if the repository is not bare.
+ * Equivalent to $GIT_WORK_TREE.
+ * If NULL, the repository is assumed to be bare.
+ *
+ * @return 0 on success; error code otherwise
+ */
+
+GIT_EXTERN(int) git_repository_open3(git_repository **repository,
+ const char *git_dir,
+ git_odb *object_database,
+ const char *git_index_file,
+ const char *git_work_tree);
+
+
+/**
* Lookup a reference to one of the objects in the repostory.
*
* The generated reference is owned by the repository and
diff --git a/src/odb.c b/src/odb.c
index d54ad83..26b457b 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -277,8 +277,8 @@ int git_odb_read(git_rawobj *out, git_odb *db, const git_oid *id)
for (i = 0; i < db->backends.length && error < 0; ++i) {
git_odb_backend *b = git_vector_get(&db->backends, i);
- assert(b->read != NULL);
- error = b->read(out, b, id);
+ if (b->read != NULL)
+ error = b->read(out, b, id);
}
return error;
diff --git a/src/repository.c b/src/repository.c
index f0c822f..7e56a4b 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -225,6 +225,49 @@ static git_repository *repository_alloc()
return repo;
}
+static int init_odb(git_repository *repo)
+{
+ return git_odb_open(&repo->db, repo->path_odb);
+}
+
+int git_repository_open3(git_repository **repo_out,
+ const char *git_dir,
+ git_odb *object_database,
+ const char *git_index_file,
+ const char *git_work_tree)
+{
+ git_repository *repo;
+ int error = GIT_SUCCESS;
+
+ assert(repo_out);
+
+ if (object_database == NULL)
+ return GIT_ERROR;
+
+ repo = repository_alloc();
+ if (repo == NULL)
+ return GIT_ENOMEM;
+
+ error = assign_repository_DIRs(repo,
+ git_dir,
+ NULL,
+ git_index_file,
+ git_work_tree);
+
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ repo->db = object_database;
+
+ *repo_out = repo;
+ return GIT_SUCCESS;
+
+cleanup:
+ git_repository_free(repo);
+ return error;
+}
+
+
int git_repository_open2(git_repository **repo_out,
const char *git_dir,
const char *git_object_directory,
@@ -249,7 +292,7 @@ int git_repository_open2(git_repository **repo_out,
if (error < GIT_SUCCESS)
goto cleanup;
- error = git_odb_open(&repo->db, repo->path_odb);
+ error = init_odb(repo);
if (error < GIT_SUCCESS)
goto cleanup;
@@ -276,8 +319,7 @@ int git_repository_open(git_repository **repo_out, const char *path)
if (error < GIT_SUCCESS)
goto cleanup;
-
- error = git_odb_open(&repo->db, repo->path_odb);
+ error = init_odb(repo);
if (error < GIT_SUCCESS)
goto cleanup;