Move git_reference__alloc to include/git2/sys Create a new include/git2/sys/refs.h and move the reference alloc functions there. Also fix some documentation issues and some minor code cleanups.
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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
diff --git a/include/git2/refdb.h b/include/git2/refdb.h
index 9278b11..a315876 100644
--- a/include/git2/refdb.h
+++ b/include/git2/refdb.h
@@ -22,25 +22,6 @@
GIT_BEGIN_DECL
/**
- * Create a new reference. Either an oid or a symbolic target must be
- * specified.
- *
- * @param refdb the reference database to associate with this reference
- * @param name the reference name
- * @param oid the object id for a direct reference
- * @param symbolic the target for a symbolic reference
- * @return the created git_reference or NULL on error
- */
-GIT_EXTERN(git_reference *) git_reference__alloc(
- const char *name,
- const git_oid *oid,
- const git_oid *peel);
-
-GIT_EXTERN(git_reference *) git_reference__alloc_symbolic(
- const char *name,
- const char *target);
-
-/**
* Create a new reference database with no backends.
*
* Before the Ref DB can be used for read/writing, a custom database
diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h
index dcdf7bc..d5f599f 100644
--- a/include/git2/sys/refdb_backend.h
+++ b/include/git2/sys/refdb_backend.h
@@ -4,8 +4,8 @@
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
-#ifndef INCLUDE_git_refdb_backend_h__
-#define INCLUDE_git_refdb_backend_h__
+#ifndef INCLUDE_sys_git_refdb_backend_h__
+#define INCLUDE_sys_git_refdb_backend_h__
#include "git2/common.h"
#include "git2/types.h"
@@ -30,7 +30,7 @@ struct git_refdb_backend {
*/
int (*exists)(
int *exists,
- struct git_refdb_backend *backend,
+ git_refdb_backend *backend,
const char *ref_name);
/**
@@ -39,7 +39,7 @@ struct git_refdb_backend {
*/
int (*lookup)(
git_reference **out,
- struct git_refdb_backend *backend,
+ git_refdb_backend *backend,
const char *ref_name);
/**
@@ -47,7 +47,7 @@ struct git_refdb_backend {
* provide this function.
*/
int (*foreach)(
- struct git_refdb_backend *backend,
+ git_refdb_backend *backend,
unsigned int list_flags,
git_reference_foreach_cb callback,
void *payload);
@@ -59,7 +59,7 @@ struct git_refdb_backend {
* against the glob.
*/
int (*foreach_glob)(
- struct git_refdb_backend *backend,
+ git_refdb_backend *backend,
const char *glob,
unsigned int list_flags,
git_reference_foreach_cb callback,
@@ -69,13 +69,13 @@ struct git_refdb_backend {
* Writes the given reference to the refdb. A refdb implementation
* must provide this function.
*/
- int (*write)(struct git_refdb_backend *backend, const git_reference *ref);
+ int (*write)(git_refdb_backend *backend, const git_reference *ref);
/**
* Deletes the given reference from the refdb. A refdb implementation
* must provide this function.
*/
- int (*delete)(struct git_refdb_backend *backend, const git_reference *ref);
+ int (*delete)(git_refdb_backend *backend, const git_reference *ref);
/**
* Suggests that the given refdb compress or optimize its references.
@@ -84,31 +84,41 @@ struct git_refdb_backend {
* implementation may provide this function; if it is not provided,
* nothing will be done.
*/
- int (*compress)(struct git_refdb_backend *backend);
+ int (*compress)(git_refdb_backend *backend);
/**
* Frees any resources held by the refdb. A refdb implementation may
* provide this function; if it is not provided, nothing will be done.
*/
- void (*free)(struct git_refdb_backend *backend);
+ void (*free)(git_refdb_backend *backend);
};
#define GIT_ODB_BACKEND_VERSION 1
#define GIT_ODB_BACKEND_INIT {GIT_ODB_BACKEND_VERSION}
/**
- * Constructors for default refdb backends.
+ * Constructors for default filesystem-based refdb backend
+ *
+ * Under normal usage, this is called for you when the repository is
+ * opened / created, but you can use this to explicitly construct a
+ * filesystem refdb backend for a repository.
+ *
+ * @param backend_out Output pointer to the git_refdb_backend object
+ * @param repo Git repository to access
+ * @return 0 on success, <0 error code on failure
*/
GIT_EXTERN(int) git_refdb_backend_fs(
- struct git_refdb_backend **backend_out,
+ git_refdb_backend **backend_out,
git_repository *repo);
/**
* Sets the custom backend to an existing reference DB
*
+ * The `git_refdb` will take ownership of the `git_refdb_backend` so you
+ * should NOT free it after calling this function.
+ *
* @param refdb database to add the backend to
* @param backend pointer to a git_refdb_backend instance
- * @param priority Value for ordering the backends queue
* @return 0 on success; error code otherwise
*/
GIT_EXTERN(int) git_refdb_set_backend(
diff --git a/include/git2/sys/refs.h b/include/git2/sys/refs.h
new file mode 100644
index 0000000..8596325
--- /dev/null
+++ b/include/git2/sys/refs.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_sys_git_refdb_h__
+#define INCLUDE_sys_git_refdb_h__
+
+#include "git2/common.h"
+#include "git2/types.h"
+#include "git2/oid.h"
+
+/**
+ * Create a new direct reference from an OID.
+ *
+ * @param name the reference name
+ * @param oid the object id for a direct reference
+ * @param symbolic the target for a symbolic reference
+ * @return the created git_reference or NULL on error
+ */
+GIT_EXTERN(git_reference *) git_reference__alloc(
+ const char *name,
+ const git_oid *oid,
+ const git_oid *peel);
+
+/**
+ * Create a new symbolic reference.
+ *
+ * @param name the reference name
+ * @param symbolic the target for a symbolic reference
+ * @return the created git_reference or NULL on error
+ */
+GIT_EXTERN(git_reference *) git_reference__alloc_symbolic(
+ const char *name,
+ const char *target);
+
+#endif
diff --git a/src/refdb.c b/src/refdb.c
index 0675be6..33a1934 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -58,15 +58,19 @@ int git_refdb_open(git_refdb **out, git_repository *repo)
return 0;
}
-int git_refdb_set_backend(git_refdb *db, git_refdb_backend *backend)
+static void refdb_free_backend(git_refdb *db)
{
if (db->backend) {
- if(db->backend->free)
+ if (db->backend->free)
db->backend->free(db->backend);
else
git__free(db->backend);
}
+}
+int git_refdb_set_backend(git_refdb *db, git_refdb_backend *backend)
+{
+ refdb_free_backend(db);
db->backend = backend;
return 0;
@@ -76,22 +80,15 @@ int git_refdb_compress(git_refdb *db)
{
assert(db);
- if (db->backend->compress) {
+ if (db->backend->compress)
return db->backend->compress(db->backend);
- }
return 0;
}
static void refdb_free(git_refdb *db)
{
- if (db->backend) {
- if(db->backend->free)
- db->backend->free(db->backend);
- else
- git__free(db->backend);
- }
-
+ refdb_free_backend(db);
git__free(db);
}
@@ -115,14 +112,13 @@ int git_refdb_lookup(git_reference **out, git_refdb *db, const char *ref_name)
git_reference *ref;
int error;
- assert(db && db->backend && ref_name);
-
- *out = NULL;
+ assert(db && db->backend && out && ref_name);
- if ((error = db->backend->lookup(&ref, db->backend, ref_name)) == 0)
- {
+ if (!(error = db->backend->lookup(&ref, db->backend, ref_name))) {
ref->db = db;
*out = ref;
+ } else {
+ *out = NULL;
}
return error;
diff --git a/src/refdb.h b/src/refdb.h
index 0969711..047113a 100644
--- a/src/refdb.h
+++ b/src/refdb.h
@@ -41,6 +41,6 @@ int git_refdb_foreach_glob(
int git_refdb_write(git_refdb *refdb, const git_reference *ref);
-int git_refdb_delete(struct git_refdb *refdb, const git_reference *ref);
+int git_refdb_delete(git_refdb *refdb, const git_reference *ref);
#endif
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 4d5d600..4438710 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -19,6 +19,7 @@
#include <git2/object.h>
#include <git2/refdb.h>
#include <git2/sys/refdb_backend.h>
+#include <git2/sys/refs.h>
GIT__USE_STRMAP;
diff --git a/src/refs.c b/src/refs.c
index d9291e5..9c6684a 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -19,6 +19,7 @@
#include <git2/branch.h>
#include <git2/refs.h>
#include <git2/refdb.h>
+#include <git2/sys/refs.h>
GIT__USE_STRMAP;
@@ -44,8 +45,7 @@ static git_reference *alloc_ref(const char *name)
}
git_reference *git_reference__alloc_symbolic(
- const char *name,
- const char *target)
+ const char *name, const char *target)
{
git_reference *ref;
diff --git a/tests-clar/refdb/inmemory.c b/tests-clar/refdb/inmemory.c
index 2c76d41..243b5bb 100644
--- a/tests-clar/refdb/inmemory.c
+++ b/tests-clar/refdb/inmemory.c
@@ -63,7 +63,6 @@ void test_refdb_inmemory__initialize(void)
cl_git_pass(git_repository_refdb(&refdb, repo));
cl_git_pass(refdb_backend_test(&refdb_backend, repo));
cl_git_pass(git_refdb_set_backend(refdb, refdb_backend));
- git_refdb_free(refdb);
ref_file_foreach(repo, unlink_ref);
diff --git a/tests-clar/refdb/testdb.h b/tests-clar/refdb/testdb.h
index 49d1cb1..a0d1bbc 100644
--- a/tests-clar/refdb/testdb.h
+++ b/tests-clar/refdb/testdb.h
@@ -1,6 +1,7 @@
#include <git2/errors.h>
#include <git2/repository.h>
#include <git2/refdb.h>
+#include <git2/sys/refs.h>
#include <git2/sys/refdb_backend.h>
int refdb_backend_test(