Commit cf97799ed31ebb8cc4d44f7fd6ff86a5bc1b2e8b

Vicent Martí 2013-04-20T01:50:43

Merge pull request #1491 from ethomson/backends_dont_refdb alloc doesn't take a refdb

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);
 }