Merge pull request #1491 from ethomson/backends_dont_refdb alloc doesn't take a refdb
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 76b8fda..0e3ec5e 100644
--- a/include/git2/refdb.h
+++ b/include/git2/refdb.h
@@ -32,13 +32,11 @@ GIT_BEGIN_DECL
* @return the created git_reference or NULL on error
*/
GIT_EXTERN(git_reference *) git_reference__alloc(
- git_refdb *refdb,
const char *name,
const git_oid *oid,
const git_oid *peel);
GIT_EXTERN(git_reference *) git_reference__alloc_symbolic(
- git_refdb *refdb,
const char *name,
const char *target);
diff --git a/include/git2/refdb_backend.h b/include/git2/refdb_backend.h
index bf33817..20eb6a9 100644
--- a/include/git2/refdb_backend.h
+++ b/include/git2/refdb_backend.h
@@ -101,8 +101,7 @@ struct git_refdb_backend {
*/
GIT_EXTERN(int) git_refdb_backend_fs(
struct git_refdb_backend **backend_out,
- git_repository *repo,
- git_refdb *refdb);
+ git_repository *repo);
GIT_END_DECL
diff --git a/src/refdb.c b/src/refdb.c
index d9b73c6..2a0fd70 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -45,7 +45,7 @@ int git_refdb_open(git_refdb **out, git_repository *repo)
return -1;
/* Add the default (filesystem) backend */
- if (git_refdb_backend_fs(&dir, repo, db) < 0) {
+ if (git_refdb_backend_fs(&dir, repo) < 0) {
git_refdb_free(db);
return -1;
}
@@ -111,9 +111,20 @@ int git_refdb_exists(int *exists, git_refdb *refdb, const char *ref_name)
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);
- return db->backend->lookup(out, db->backend, ref_name);
+ *out = NULL;
+
+ if ((error = db->backend->lookup(&ref, db->backend, ref_name)) == 0)
+ {
+ ref->db = db;
+ *out = ref;
+ }
+
+ return error;
}
int git_refdb_foreach(
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 784749f..56b2b6a 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -42,7 +42,6 @@ typedef struct refdb_fs_backend {
git_repository *repo;
const char *path;
- git_refdb *refdb;
git_refcache refcache;
} refdb_fs_backend;
@@ -430,12 +429,12 @@ static int loose_lookup(
goto done;
}
- *out = git_reference__alloc_symbolic(backend->refdb, ref_name, target);
+ *out = git_reference__alloc_symbolic(ref_name, target);
} else {
if ((error = loose_parse_oid(&oid, &ref_file)) < 0)
goto done;
- *out = git_reference__alloc(backend->refdb, ref_name, &oid, NULL);
+ *out = git_reference__alloc(ref_name, &oid, NULL);
}
if (*out == NULL)
@@ -484,7 +483,7 @@ static int packed_lookup(
if ((error = packed_map_entry(&entry, &pos, backend, ref_name)) < 0)
return error;
- if ((*out = git_reference__alloc(backend->refdb, ref_name,
+ if ((*out = git_reference__alloc(ref_name,
&entry->oid, &entry->peel)) == NULL)
return -1;
@@ -999,8 +998,7 @@ static void refdb_fs_backend__free(git_refdb_backend *_backend)
int git_refdb_backend_fs(
git_refdb_backend **backend_out,
- git_repository *repository,
- git_refdb *refdb)
+ git_repository *repository)
{
refdb_fs_backend *backend;
@@ -1009,7 +1007,6 @@ int git_refdb_backend_fs(
backend->repo = repository;
backend->path = repository->path_repository;
- backend->refdb = refdb;
backend->parent.exists = &refdb_fs_backend__exists;
backend->parent.lookup = &refdb_fs_backend__lookup;
diff --git a/src/refs.c b/src/refs.c
index 29d1c4f..5b5812a 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -31,7 +31,7 @@ enum {
GIT_PACKREF_WAS_LOOSE = 2
};
-static git_reference *alloc_ref(git_refdb *refdb, const char *name)
+static git_reference *alloc_ref(const char *name)
{
git_reference *ref;
size_t namelen = strlen(name);
@@ -39,22 +39,20 @@ static git_reference *alloc_ref(git_refdb *refdb, const char *name)
if ((ref = git__calloc(1, sizeof(git_reference) + namelen + 1)) == NULL)
return NULL;
- ref->db = refdb;
memcpy(ref->name, name, namelen + 1);
return ref;
}
git_reference *git_reference__alloc_symbolic(
- git_refdb *refdb,
const char *name,
const char *target)
{
git_reference *ref;
- assert(refdb && name && target);
+ assert(name && target);
- ref = alloc_ref(refdb, name);
+ ref = alloc_ref(name);
if (!ref)
return NULL;
@@ -69,16 +67,15 @@ git_reference *git_reference__alloc_symbolic(
}
git_reference *git_reference__alloc(
- git_refdb *refdb,
const char *name,
const git_oid *oid,
const git_oid *peel)
{
git_reference *ref;
- assert(refdb && name && oid);
+ assert(name && oid);
- ref = alloc_ref(refdb, name);
+ ref = alloc_ref(name);
if (!ref)
return NULL;
@@ -368,12 +365,13 @@ static int reference__create(
if (oid != NULL) {
assert(symbolic == NULL);
- ref = git_reference__alloc(refdb, name, oid, NULL);
+ ref = git_reference__alloc(name, oid, NULL);
} else {
- ref = git_reference__alloc_symbolic(refdb, name, symbolic);
+ ref = git_reference__alloc_symbolic(name, symbolic);
}
GITERR_CHECK_ALLOC(ref);
+ ref->db = refdb;
if ((error = git_refdb_write(refdb, ref)) < 0) {
git_reference_free(ref);
@@ -490,10 +488,9 @@ int git_reference_rename(
* Create the new reference.
*/
if (ref->type == GIT_REF_OID) {
- result = git_reference__alloc(ref->db, new_name,
- &ref->target.oid, &ref->peel);
+ result = git_reference__alloc(new_name, &ref->target.oid, &ref->peel);
} else if (ref->type == GIT_REF_SYMBOLIC) {
- result = git_reference__alloc_symbolic(ref->db, new_name, ref->target.symbolic);
+ result = git_reference__alloc_symbolic(new_name, ref->target.symbolic);
} else {
assert(0);
}
@@ -501,6 +498,8 @@ int git_reference_rename(
if (result == NULL)
return -1;
+ result->db = ref->db;
+
/* Check if we have to update HEAD. */
if ((error = git_branch_is_head(ref)) < 0)
goto on_error;
diff --git a/tests-clar/refdb/inmemory.c b/tests-clar/refdb/inmemory.c
index ca721e9..ea76172 100644
--- a/tests-clar/refdb/inmemory.c
+++ b/tests-clar/refdb/inmemory.c
@@ -6,7 +6,6 @@
#define TEST_REPO_PATH "testrepo"
static git_repository *repo;
-static git_refdb *refdb;
static git_refdb_backend *refdb_backend;
int unlink_ref(void *payload, git_buf *file)
@@ -52,6 +51,8 @@ int ref_file_foreach(git_repository *repo, int (* cb)(void *payload, git_buf *fi
void test_refdb_inmemory__initialize(void)
{
+ git_refdb *refdb;
+
git_buf repo_refs_dir = GIT_BUF_INIT;
repo = cl_git_sandbox_init(TEST_REPO_PATH);
@@ -60,10 +61,10 @@ void test_refdb_inmemory__initialize(void)
cl_git_pass(refdb_backend_test(&refdb_backend, repo));
cl_git_pass(git_refdb_set_backend(refdb, refdb_backend));
-
ref_file_foreach(repo, unlink_ref);
git_buf_free(&repo_refs_dir);
+ git_refdb_free(refdb);
}
void test_refdb_inmemory__cleanup(void)
diff --git a/tests-clar/refdb/testdb.c b/tests-clar/refdb/testdb.c
index e60f679..4bca398 100644
--- a/tests-clar/refdb/testdb.c
+++ b/tests-clar/refdb/testdb.c
@@ -10,7 +10,6 @@ typedef struct refdb_test_backend {
git_refdb_backend parent;
git_repository *repo;
- git_refdb *refdb;
git_vector refs;
} refdb_test_backend;
@@ -100,10 +99,10 @@ static int refdb_test_backend__lookup(
if (strcmp(entry->name, ref_name) == 0) {
if (entry->type == GIT_REF_OID) {
- *out = git_reference__alloc(backend->refdb, ref_name,
+ *out = git_reference__alloc(ref_name,
&entry->target.oid, NULL);
} else if (entry->type == GIT_REF_SYMBOLIC) {
- *out = git_reference__alloc_symbolic(backend->refdb, ref_name,
+ *out = git_reference__alloc_symbolic(ref_name,
entry->target.symbolic);
}
@@ -195,11 +194,6 @@ int refdb_backend_test(
git_repository *repo)
{
refdb_test_backend *backend;
- git_refdb *refdb;
- int error = 0;
-
- if ((error = git_repository_refdb(&refdb, repo)) < 0)
- return error;
backend = git__calloc(1, sizeof(refdb_test_backend));
GITERR_CHECK_ALLOC(backend);
@@ -207,7 +201,6 @@ int refdb_backend_test(
git_vector_init(&backend->refs, 0, ref_name_cmp);
backend->repo = repo;
- backend->refdb = refdb;
backend->parent.exists = &refdb_test_backend__exists;
backend->parent.lookup = &refdb_test_backend__lookup;
diff --git a/tests-clar/refs/delete.c b/tests-clar/refs/delete.c
index ac517d8..053f412 100644
--- a/tests-clar/refs/delete.c
+++ b/tests-clar/refs/delete.c
@@ -88,4 +88,5 @@ void test_refs_delete__packed_only(void)
/* This should pass */
cl_git_pass(git_reference_delete(ref));
git_reference_free(ref);
+ git_refdb_free(refdb);
}
diff --git a/tests-clar/refs/pack.c b/tests-clar/refs/pack.c
index 973abae..412c4c5 100644
--- a/tests-clar/refs/pack.c
+++ b/tests-clar/refs/pack.c
@@ -25,6 +25,7 @@ static void packall(void)
cl_git_pass(git_repository_refdb(&refdb, g_repo));
cl_git_pass(git_refdb_compress(refdb));
+ git_refdb_free(refdb);
}
void test_refs_pack__empty(void)
diff --git a/tests-clar/refs/rename.c b/tests-clar/refs/rename.c
index e39abeb..5ab84c4 100644
--- a/tests-clar/refs/rename.c
+++ b/tests-clar/refs/rename.c
@@ -284,6 +284,7 @@ void test_refs_rename__overwrite(void)
git_reference_free(ref_one);
git_reference_free(ref_one_new);
git_reference_free(ref_two);
+ git_refdb_free(refdb);
}