summaryrefslogtreecommitdiff
path: root/tests-clar
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2013-03-01 15:37:33 -0600
committerEdward Thomson <ethomson@edwardthomson.com>2013-03-07 11:01:52 -0600
commitd00d54645d931c77a9b401518c0d73e3f640454b (patch)
treee1932dcc97172a53524e9db1ba4923cf137a4f9c /tests-clar
parent6a9ef012376e8a21dcfd0499ab16048eb6e954c3 (diff)
downloadlibgit2-d00d54645d931c77a9b401518c0d73e3f640454b.tar.gz
immutable references and a pluggable ref database
Diffstat (limited to 'tests-clar')
-rw-r--r--tests-clar/commit/write.c5
-rw-r--r--tests-clar/object/tag/write.c1
-rw-r--r--tests-clar/refdb/inmemory.c213
-rw-r--r--tests-clar/refdb/testdb.c217
-rw-r--r--tests-clar/refdb/testdb.h3
-rw-r--r--tests-clar/refs/branches/delete.c7
-rw-r--r--tests-clar/refs/branches/move.c80
-rw-r--r--tests-clar/refs/crashes.c5
-rw-r--r--tests-clar/refs/create.c5
-rw-r--r--tests-clar/refs/delete.c16
-rw-r--r--tests-clar/refs/pack.c17
-rw-r--r--tests-clar/refs/read.c11
-rw-r--r--tests-clar/refs/ref_helpers.c25
-rw-r--r--tests-clar/refs/ref_helpers.h1
-rw-r--r--tests-clar/refs/reflog/reflog.c14
-rw-r--r--tests-clar/refs/rename.c87
-rw-r--r--tests-clar/refs/revparse.c7
-rw-r--r--tests-clar/refs/setter.c99
-rw-r--r--tests-clar/refs/update.c7
-rw-r--r--tests-clar/stash/save.c3
20 files changed, 730 insertions, 93 deletions
diff --git a/tests-clar/commit/write.c b/tests-clar/commit/write.c
index 88e2f32fb..e9946af89 100644
--- a/tests-clar/commit/write.c
+++ b/tests-clar/commit/write.c
@@ -114,8 +114,9 @@ void test_commit_write__root(void)
cl_assert(git_reference_type(head) == GIT_REF_SYMBOLIC);
head_old = git__strdup(git_reference_symbolic_target(head));
cl_assert(head_old != NULL);
-
- cl_git_pass(git_reference_symbolic_set_target(head, branch_name));
+ git_reference_free(head);
+
+ cl_git_pass(git_reference_symbolic_create(&head, g_repo, "HEAD", branch_name, 1));
cl_git_pass(git_commit_create_v(
&commit_id, /* out id */
diff --git a/tests-clar/object/tag/write.c b/tests-clar/object/tag/write.c
index eb0ac2897..cd69bea89 100644
--- a/tests-clar/object/tag/write.c
+++ b/tests-clar/object/tag/write.c
@@ -60,6 +60,7 @@ void test_object_tag_write__basic(void)
cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/the-tag"));
cl_assert(git_oid_cmp(git_reference_target(ref_tag), &tag_id) == 0);
cl_git_pass(git_reference_delete(ref_tag));
+ git_reference_free(ref_tag);
git_tag_free(tag);
}
diff --git a/tests-clar/refdb/inmemory.c b/tests-clar/refdb/inmemory.c
new file mode 100644
index 000000000..6ee09c0c7
--- /dev/null
+++ b/tests-clar/refdb/inmemory.c
@@ -0,0 +1,213 @@
+#include "clar_libgit2.h"
+#include "refdb.h"
+#include "repository.h"
+#include "testdb.h"
+
+#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)
+{
+ GIT_UNUSED(payload);
+ return p_unlink(git_buf_cstr(file));
+}
+
+int empty(void *payload, git_buf *file)
+{
+ GIT_UNUSED(payload);
+ GIT_UNUSED(file);
+ return -1;
+}
+
+int ref_file_foreach(git_repository *repo, int (* cb)(void *payload, git_buf *filename))
+{
+ const char *repo_path;
+ git_buf repo_refs_dir = GIT_BUF_INIT;
+ int error = 0;
+
+ repo_path = git_repository_path(repo);
+
+ git_buf_joinpath(&repo_refs_dir, repo_path, "HEAD");
+ if (git_path_exists(git_buf_cstr(&repo_refs_dir)) &&
+ cb(NULL, &repo_refs_dir) < 0)
+ return -1;
+
+ git_buf_joinpath(&repo_refs_dir, repo_path, "refs");
+ git_buf_joinpath(&repo_refs_dir, git_buf_cstr(&repo_refs_dir), "heads");
+ if (git_path_direach(&repo_refs_dir, cb, NULL) != 0)
+ return -1;
+
+ git_buf_joinpath(&repo_refs_dir, repo_path, "packed-refs");
+ if (git_path_exists(git_buf_cstr(&repo_refs_dir)) &&
+ cb(NULL, &repo_refs_dir) < 0)
+ return -1;
+
+ git_buf_free(&repo_refs_dir);
+
+ return error;
+}
+
+void test_refdb_inmemory__initialize(void)
+{
+ git_buf repo_refs_dir = GIT_BUF_INIT;
+
+ repo = cl_git_sandbox_init(TEST_REPO_PATH);
+
+ 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));
+
+
+ ref_file_foreach(repo, unlink_ref);
+
+ git_buf_free(&repo_refs_dir);
+}
+
+void test_refdb_inmemory__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+void test_refdb_inmemory__doesnt_write_ref_file(void)
+{
+ git_reference *ref;
+ git_oid oid;
+
+ cl_git_pass(git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
+ cl_git_pass(git_reference_create(&ref, repo, GIT_REFS_HEADS_DIR "test1", &oid, 0));
+
+ ref_file_foreach(repo, empty);
+
+ git_reference_free(ref);
+}
+
+void test_refdb_inmemory__read(void)
+{
+ git_reference *write1, *write2, *write3, *read1, *read2, *read3;
+ git_oid oid1, oid2, oid3;
+
+ cl_git_pass(git_oid_fromstr(&oid1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
+ cl_git_pass(git_reference_create(&write1, repo, GIT_REFS_HEADS_DIR "test1", &oid1, 0));
+
+ cl_git_pass(git_oid_fromstr(&oid2, "e90810b8df3e80c413d903f631643c716887138d"));
+ cl_git_pass(git_reference_create(&write2, repo, GIT_REFS_HEADS_DIR "test2", &oid2, 0));
+
+ cl_git_pass(git_oid_fromstr(&oid3, "763d71aadf09a7951596c9746c024e7eece7c7af"));
+ cl_git_pass(git_reference_create(&write3, repo, GIT_REFS_HEADS_DIR "test3", &oid3, 0));
+
+
+ cl_git_pass(git_reference_lookup(&read1, repo, GIT_REFS_HEADS_DIR "test1"));
+ cl_assert(strcmp(git_reference_name(read1), git_reference_name(write1)) == 0);
+ cl_assert(git_oid_cmp(git_reference_target(read1), git_reference_target(write1)) == 0);
+
+ cl_git_pass(git_reference_lookup(&read2, repo, GIT_REFS_HEADS_DIR "test2"));
+ cl_assert(strcmp(git_reference_name(read2), git_reference_name(write2)) == 0);
+ cl_assert(git_oid_cmp(git_reference_target(read2), git_reference_target(write2)) == 0);
+
+ cl_git_pass(git_reference_lookup(&read3, repo, GIT_REFS_HEADS_DIR "test3"));
+ cl_assert(strcmp(git_reference_name(read3), git_reference_name(write3)) == 0);
+ cl_assert(git_oid_cmp(git_reference_target(read3), git_reference_target(write3)) == 0);
+
+ git_reference_free(write1);
+ git_reference_free(write2);
+ git_reference_free(write3);
+
+ git_reference_free(read1);
+ git_reference_free(read2);
+ git_reference_free(read3);
+}
+
+int foreach_test(const char *ref_name, void *payload)
+{
+ git_reference *ref;
+ git_oid expected;
+ int *i = payload;
+
+ cl_git_pass(git_reference_lookup(&ref, repo, ref_name));
+
+ if (*i == 0)
+ cl_git_pass(git_oid_fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
+ else if (*i == 1)
+ cl_git_pass(git_oid_fromstr(&expected, "e90810b8df3e80c413d903f631643c716887138d"));
+ else if (*i == 2)
+ cl_git_pass(git_oid_fromstr(&expected, "763d71aadf09a7951596c9746c024e7eece7c7af"));
+
+ cl_assert(git_oid_cmp(&expected, &ref->target.oid) == 0);
+
+ ++(*i);
+
+ git_reference_free(ref);
+
+ return 0;
+}
+
+void test_refdb_inmemory__foreach(void)
+{
+ git_reference *write1, *write2, *write3;
+ git_oid oid1, oid2, oid3;
+ size_t i = 0;
+
+ cl_git_pass(git_oid_fromstr(&oid1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
+ cl_git_pass(git_reference_create(&write1, repo, GIT_REFS_HEADS_DIR "test1", &oid1, 0));
+
+ cl_git_pass(git_oid_fromstr(&oid2, "e90810b8df3e80c413d903f631643c716887138d"));
+ cl_git_pass(git_reference_create(&write2, repo, GIT_REFS_HEADS_DIR "test2", &oid2, 0));
+
+ cl_git_pass(git_oid_fromstr(&oid3, "763d71aadf09a7951596c9746c024e7eece7c7af"));
+ cl_git_pass(git_reference_create(&write3, repo, GIT_REFS_HEADS_DIR "test3", &oid3, 0));
+
+ cl_git_pass(git_reference_foreach(repo, GIT_REF_LISTALL, foreach_test, &i));
+ cl_assert(i == 3);
+
+ git_reference_free(write1);
+ git_reference_free(write2);
+ git_reference_free(write3);
+}
+
+int delete_test(const char *ref_name, void *payload)
+{
+ git_reference *ref;
+ git_oid expected;
+ int *i = payload;
+
+ cl_git_pass(git_reference_lookup(&ref, repo, ref_name));
+
+ cl_git_pass(git_oid_fromstr(&expected, "e90810b8df3e80c413d903f631643c716887138d"));
+ cl_assert(git_oid_cmp(&expected, &ref->target.oid) == 0);
+
+ ++(*i);
+
+ git_reference_free(ref);
+
+ return 0;
+}
+
+void test_refdb_inmemory__delete(void)
+{
+ git_reference *write1, *write2, *write3;
+ git_oid oid1, oid2, oid3;
+ size_t i = 0;
+
+ cl_git_pass(git_oid_fromstr(&oid1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
+ cl_git_pass(git_reference_create(&write1, repo, GIT_REFS_HEADS_DIR "test1", &oid1, 0));
+
+ cl_git_pass(git_oid_fromstr(&oid2, "e90810b8df3e80c413d903f631643c716887138d"));
+ cl_git_pass(git_reference_create(&write2, repo, GIT_REFS_HEADS_DIR "test2", &oid2, 0));
+
+ cl_git_pass(git_oid_fromstr(&oid3, "763d71aadf09a7951596c9746c024e7eece7c7af"));
+ cl_git_pass(git_reference_create(&write3, repo, GIT_REFS_HEADS_DIR "test3", &oid3, 0));
+
+ git_reference_delete(write1);
+ git_reference_free(write1);
+
+ git_reference_delete(write3);
+ git_reference_free(write3);
+
+ cl_git_pass(git_reference_foreach(repo, GIT_REF_LISTALL, delete_test, &i));
+ cl_assert(i == 1);
+
+ git_reference_free(write2);
+}
diff --git a/tests-clar/refdb/testdb.c b/tests-clar/refdb/testdb.c
new file mode 100644
index 000000000..a8e7ba5fe
--- /dev/null
+++ b/tests-clar/refdb/testdb.c
@@ -0,0 +1,217 @@
+#include "common.h"
+#include "vector.h"
+#include "util.h"
+#include <git2/refdb.h>
+#include <git2/refdb_backend.h>
+#include <git2/errors.h>
+#include <git2/repository.h>
+
+typedef struct refdb_test_backend {
+ git_refdb_backend parent;
+
+ git_repository *repo;
+ git_refdb *refdb;
+ git_vector refs;
+} refdb_test_backend;
+
+typedef struct refdb_test_entry {
+ char *name;
+ git_ref_t type;
+
+ union {
+ git_oid oid;
+ char *symbolic;
+ } target;
+} refdb_test_entry;
+
+static int ref_name_cmp(const void *a, const void *b)
+{
+ return strcmp(git_reference_name((git_reference *)a),
+ git_reference_name((git_reference *)b));
+}
+
+static int refdb_test_backend__exists(
+ int *exists,
+ git_refdb_backend *_backend,
+ const char *ref_name)
+{
+ refdb_test_backend *backend;
+ refdb_test_entry *entry;
+ size_t i;
+
+ assert(_backend);
+ backend = (refdb_test_backend *)_backend;
+
+ *exists = 0;
+
+ git_vector_foreach(&backend->refs, i, entry) {
+ if (strcmp(entry->name, ref_name) == 0) {
+ *exists = 1;
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static int refdb_test_backend__write(
+ git_refdb_backend *_backend,
+ const git_reference *ref)
+{
+ refdb_test_backend *backend;
+ refdb_test_entry *entry;
+
+ assert(_backend);
+ backend = (refdb_test_backend *)_backend;
+
+ entry = git__calloc(1, sizeof(refdb_test_entry));
+ GITERR_CHECK_ALLOC(entry);
+
+ entry->name = git__strdup(git_reference_name(ref));
+ GITERR_CHECK_ALLOC(entry->name);
+
+ entry->type = git_reference_type(ref);
+
+ if (entry->type == GIT_REF_OID)
+ git_oid_cpy(&entry->target.oid, git_reference_target(ref));
+ else {
+ entry->target.symbolic = git__strdup(git_reference_symbolic_target(ref));
+ GITERR_CHECK_ALLOC(entry->target.symbolic);
+ }
+
+ git_vector_insert(&backend->refs, entry);
+
+ return 0;
+}
+
+static int refdb_test_backend__lookup(
+ git_reference **out,
+ git_refdb_backend *_backend,
+ const char *ref_name)
+{
+ refdb_test_backend *backend;
+ refdb_test_entry *entry;
+ size_t i;
+
+ assert(_backend);
+ backend = (refdb_test_backend *)_backend;
+
+ git_vector_foreach(&backend->refs, i, entry) {
+ if (strcmp(entry->name, ref_name) == 0) {
+ const git_oid *oid =
+ entry->type == GIT_REF_OID ? &entry->target.oid : NULL;
+ const char *symbolic =
+ entry->type == GIT_REF_SYMBOLIC ? entry->target.symbolic : NULL;
+
+ if ((*out = git_reference__alloc(backend->refdb, ref_name, oid, symbolic)) == NULL)
+ return -1;
+
+ return 0;
+ }
+ }
+
+ return GIT_ENOTFOUND;
+}
+
+static int refdb_test_backend__foreach(
+ git_refdb_backend *_backend,
+ unsigned int list_flags,
+ git_reference_foreach_cb callback,
+ void *payload)
+{
+ refdb_test_backend *backend;
+ refdb_test_entry *entry;
+ size_t i;
+
+ assert(_backend);
+ backend = (refdb_test_backend *)_backend;
+
+ git_vector_foreach(&backend->refs, i, entry) {
+ if (entry->type == GIT_REF_OID && (list_flags & GIT_REF_OID) == 0)
+ continue;
+
+ if (entry->type == GIT_REF_SYMBOLIC && (list_flags & GIT_REF_SYMBOLIC) == 0)
+ continue;
+
+ if (callback(entry->name, payload) != 0)
+ return GIT_EUSER;
+ }
+
+ return 0;
+}
+
+static void refdb_test_entry_free(refdb_test_entry *entry)
+{
+ if (entry->type == GIT_REF_SYMBOLIC)
+ git__free(entry->target.symbolic);
+
+ git__free(entry->name);
+ git__free(entry);
+}
+
+static int refdb_test_backend__delete(
+ git_refdb_backend *_backend,
+ const git_reference *ref)
+{
+ refdb_test_backend *backend;
+ refdb_test_entry *entry;
+ size_t i;
+
+ assert(_backend);
+ backend = (refdb_test_backend *)_backend;
+
+ git_vector_foreach(&backend->refs, i, entry) {
+ if (strcmp(entry->name, git_reference_name(ref)) == 0) {
+ git_vector_remove(&backend->refs, i);
+ refdb_test_entry_free(entry);
+ }
+ }
+
+ return GIT_ENOTFOUND;
+}
+
+static void refdb_test_backend__free(git_refdb_backend *_backend)
+{
+ refdb_test_backend *backend;
+ refdb_test_entry *entry;
+ size_t i;
+
+ assert(_backend);
+ backend = (refdb_test_backend *)_backend;
+
+ git_vector_foreach(&backend->refs, i, entry)
+ refdb_test_entry_free(entry);
+
+ git_vector_free(&backend->refs);
+ git__free(backend);
+}
+
+int refdb_backend_test(
+ git_refdb_backend **backend_out,
+ 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);
+
+ 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;
+ backend->parent.foreach = &refdb_test_backend__foreach;
+ backend->parent.write = &refdb_test_backend__write;
+ backend->parent.delete = &refdb_test_backend__delete;
+ backend->parent.free = &refdb_test_backend__free;
+
+ *backend_out = (git_refdb_backend *)backend;
+ return 0;
+}
diff --git a/tests-clar/refdb/testdb.h b/tests-clar/refdb/testdb.h
new file mode 100644
index 000000000..e38abd967
--- /dev/null
+++ b/tests-clar/refdb/testdb.h
@@ -0,0 +1,3 @@
+int refdb_backend_test(
+ git_refdb_backend **backend_out,
+ git_repository *repo);
diff --git a/tests-clar/refs/branches/delete.c b/tests-clar/refs/branches/delete.c
index 21fbc09bb..7af5a3e86 100644
--- a/tests-clar/refs/branches/delete.c
+++ b/tests-clar/refs/branches/delete.c
@@ -50,9 +50,11 @@ void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
git_reference_delete(head);
+ git_reference_free(head);
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
+ git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_orphaned(void)
@@ -63,6 +65,7 @@ void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_orphaned(void)
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
+ git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
@@ -79,6 +82,7 @@ void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(
cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
+ git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_local_branch(void)
@@ -86,6 +90,7 @@ void test_refs_branches_delete__can_delete_a_local_branch(void)
git_reference *branch;
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
+ git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_remote_branch(void)
@@ -93,6 +98,7 @@ void test_refs_branches_delete__can_delete_a_remote_branch(void)
git_reference *branch;
cl_git_pass(git_branch_lookup(&branch, repo, "nulltoken/master", GIT_BRANCH_REMOTE));
cl_git_pass(git_branch_delete(branch));
+ git_reference_free(branch);
}
void test_refs_branches_delete__deleting_a_branch_removes_related_configuration_data(void)
@@ -104,6 +110,7 @@ void test_refs_branches_delete__deleting_a_branch_removes_related_configuration_
cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
+ git_reference_free(branch);
assert_config_entry_existence(repo, "branch.track-local.remote", false);
assert_config_entry_existence(repo, "branch.track-local.merge", false);
diff --git a/tests-clar/refs/branches/move.c b/tests-clar/refs/branches/move.c
index 17fb6dfe6..7267f941d 100644
--- a/tests-clar/refs/branches/move.c
+++ b/tests-clar/refs/branches/move.c
@@ -3,20 +3,14 @@
#include "config/config_helpers.h"
static git_repository *repo;
-static git_reference *ref;
void test_refs_branches_move__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
-
- cl_git_pass(git_reference_lookup(&ref, repo, "refs/heads/br2"));
}
void test_refs_branches_move__cleanup(void)
{
- git_reference_free(ref);
- ref = NULL;
-
cl_git_sandbox_cleanup();
}
@@ -24,56 +18,99 @@ void test_refs_branches_move__cleanup(void)
void test_refs_branches_move__can_move_a_local_branch(void)
{
- cl_git_pass(git_branch_move(ref, NEW_BRANCH_NAME, 0));
- cl_assert_equal_s(GIT_REFS_HEADS_DIR NEW_BRANCH_NAME, git_reference_name(ref));
+ git_reference *original_ref, *new_ref;
+
+ cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
+
+ cl_git_pass(git_branch_move(&new_ref, original_ref, NEW_BRANCH_NAME, 0));
+ cl_assert_equal_s(GIT_REFS_HEADS_DIR NEW_BRANCH_NAME, git_reference_name(new_ref));
+
+ git_reference_free(original_ref);
+ git_reference_free(new_ref);
}
void test_refs_branches_move__can_move_a_local_branch_to_a_different_namespace(void)
{
+ git_reference *original_ref, *new_ref, *newer_ref;
+
+ cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
+
/* Downward */
- cl_git_pass(git_branch_move(ref, "somewhere/" NEW_BRANCH_NAME, 0));
+ cl_git_pass(git_branch_move(&new_ref, original_ref, "somewhere/" NEW_BRANCH_NAME, 0));
+ git_reference_free(original_ref);
/* Upward */
- cl_git_pass(git_branch_move(ref, "br2", 0));
+ cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0));
+ git_reference_free(new_ref);
+
+ git_reference_free(newer_ref);
}
void test_refs_branches_move__can_move_a_local_branch_to_a_partially_colliding_namespace(void)
{
+ git_reference *original_ref, *new_ref, *newer_ref;
+
+ cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
+
/* Downward */
- cl_git_pass(git_branch_move(ref, "br2/" NEW_BRANCH_NAME, 0));
+ cl_git_pass(git_branch_move(&new_ref, original_ref, "br2/" NEW_BRANCH_NAME, 0));
+ git_reference_free(original_ref);
/* Upward */
- cl_git_pass(git_branch_move(ref, "br2", 0));
+ cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0));
+ git_reference_free(new_ref);
+
+ git_reference_free(newer_ref);
}
void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one(void)
{
- cl_assert_equal_i(GIT_EEXISTS, git_branch_move(ref, "master", 0));
+ git_reference *original_ref, *new_ref;
+
+ cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
+
+ cl_assert_equal_i(GIT_EEXISTS, git_branch_move(&new_ref, original_ref, "master", 0));
+
+ git_reference_free(original_ref);
}
void test_refs_branches_move__moving_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
- cl_assert_equal_i(GIT_EINVALIDSPEC, git_branch_move(ref, "Inv@{id", 0));
+ git_reference *original_ref, *new_ref;
+
+ cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
+
+ cl_assert_equal_i(GIT_EINVALIDSPEC, git_branch_move(&new_ref, original_ref, "Inv@{id", 0));
+
+ git_reference_free(original_ref);
}
void test_refs_branches_move__can_not_move_a_non_branch(void)
{
- git_reference *tag;
+ git_reference *tag, *new_ref;
cl_git_pass(git_reference_lookup(&tag, repo, "refs/tags/e90810b"));
- cl_git_fail(git_branch_move(tag, NEW_BRANCH_NAME, 0));
+ cl_git_fail(git_branch_move(&new_ref, tag, NEW_BRANCH_NAME, 0));
git_reference_free(tag);
}
void test_refs_branches_move__can_force_move_over_an_existing_branch(void)
{
- cl_git_pass(git_branch_move(ref, "master", 1));
+ git_reference *original_ref, *new_ref;
+
+ cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
+
+ cl_git_pass(git_branch_move(&new_ref, original_ref, "master", 1));
+
+ git_reference_free(original_ref);
+ git_reference_free(new_ref);
}
void test_refs_branches_move__moving_a_branch_moves_related_configuration_data(void)
{
git_reference *branch;
+ git_reference *new_branch;
cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
@@ -82,23 +119,26 @@ void test_refs_branches_move__moving_a_branch_moves_related_configuration_data(v
assert_config_entry_existence(repo, "branch.moved.remote", false);
assert_config_entry_existence(repo, "branch.moved.merge", false);
- cl_git_pass(git_branch_move(branch, "moved", 0));
+ cl_git_pass(git_branch_move(&new_branch, branch, "moved", 0));
+ git_reference_free(branch);
assert_config_entry_existence(repo, "branch.track-local.remote", false);
assert_config_entry_existence(repo, "branch.track-local.merge", false);
assert_config_entry_existence(repo, "branch.moved.remote", true);
assert_config_entry_existence(repo, "branch.moved.merge", true);
- git_reference_free(branch);
+ git_reference_free(new_branch);
}
void test_refs_branches_move__moving_the_branch_pointed_at_by_HEAD_updates_HEAD(void)
{
git_reference *branch;
+ git_reference *new_branch;
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
- cl_git_pass(git_branch_move(branch, "master2", 0));
+ cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0));
git_reference_free(branch);
+ git_reference_free(new_branch);
cl_git_pass(git_repository_head(&branch, repo));
cl_assert_equal_s("refs/heads/master2", git_reference_name(branch));
diff --git a/tests-clar/refs/crashes.c b/tests-clar/refs/crashes.c
index 9fb5ff627..5a1885a7a 100644
--- a/tests-clar/refs/crashes.c
+++ b/tests-clar/refs/crashes.c
@@ -10,8 +10,11 @@ void test_refs_crashes__double_free(void)
cl_git_pass(git_reference_symbolic_create(&ref, repo, REFNAME, "refs/heads/master", 0));
cl_git_pass(git_reference_lookup(&ref2, repo, REFNAME));
cl_git_pass(git_reference_delete(ref));
+ git_reference_free(ref);
+ git_reference_free(ref2);
+
/* reference is gone from disk, so reloading it will fail */
- cl_git_fail(git_reference_reload(ref2));
+ cl_git_fail(git_reference_lookup(&ref2, repo, REFNAME));
git_repository_free(repo);
}
diff --git a/tests-clar/refs/create.c b/tests-clar/refs/create.c
index 56c323d8a..85ff05aa9 100644
--- a/tests-clar/refs/create.c
+++ b/tests-clar/refs/create.c
@@ -3,6 +3,7 @@
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
+#include "ref_helpers.h"
static const char *current_master_tip = "099fabac3a9ea935598528c27f866e34089c2eff";
static const char *current_head_target = "refs/heads/master";
@@ -36,7 +37,7 @@ void test_refs_create__symbolic(void)
/* Ensure the reference can be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker));
cl_assert(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC);
- cl_assert(git_reference_is_packed(looked_up_ref) == 0);
+ cl_assert(reference_is_packed(looked_up_ref) == 0);
cl_assert_equal_s(looked_up_ref->name, new_head_tracker);
/* ...peeled.. */
@@ -99,7 +100,7 @@ void test_refs_create__oid(void)
/* Ensure the reference can be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head));
cl_assert(git_reference_type(looked_up_ref) & GIT_REF_OID);
- cl_assert(git_reference_is_packed(looked_up_ref) == 0);
+ cl_assert(reference_is_packed(looked_up_ref) == 0);
cl_assert_equal_s(looked_up_ref->name, new_head);
/* ...and that it points to the current master tip */
diff --git a/tests-clar/refs/delete.c b/tests-clar/refs/delete.c
index cc5ab3940..ac517d869 100644
--- a/tests-clar/refs/delete.c
+++ b/tests-clar/refs/delete.c
@@ -3,6 +3,7 @@
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
+#include "ref_helpers.h"
static const char *packed_test_head_name = "refs/heads/packed-test";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
@@ -37,10 +38,11 @@ void test_refs_delete__packed_loose(void)
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));
/* Ensure it's the loose version that has been found */
- cl_assert(git_reference_is_packed(looked_up_ref) == 0);
+ cl_assert(reference_is_packed(looked_up_ref) == 0);
/* Now that the reference is deleted... */
cl_git_pass(git_reference_delete(looked_up_ref));
+ git_reference_free(looked_up_ref);
/* Looking up the reference once again should not retrieve it */
cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name));
@@ -56,6 +58,7 @@ void test_refs_delete__packed_only(void)
{
// can delete a just packed reference
git_reference *ref;
+ git_refdb *refdb;
git_oid id;
const char *new_ref = "refs/heads/new_ref";
@@ -69,17 +72,20 @@ void test_refs_delete__packed_only(void)
cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
/* Ensure it's a loose reference */
- cl_assert(git_reference_is_packed(ref) == 0);
+ cl_assert(reference_is_packed(ref) == 0);
/* Pack all existing references */
- cl_git_pass(git_reference_packall(g_repo));
+ cl_git_pass(git_repository_refdb(&refdb, g_repo));
+ cl_git_pass(git_refdb_compress(refdb));
/* Reload the reference from disk */
- cl_git_pass(git_reference_reload(ref));
+ git_reference_free(ref);
+ cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
/* Ensure it's a packed reference */
- cl_assert(git_reference_is_packed(ref) == 1);
+ cl_assert(reference_is_packed(ref) == 1);
/* This should pass */
cl_git_pass(git_reference_delete(ref));
+ git_reference_free(ref);
}
diff --git a/tests-clar/refs/pack.c b/tests-clar/refs/pack.c
index 305594c28..8da36ccd4 100644
--- a/tests-clar/refs/pack.c
+++ b/tests-clar/refs/pack.c
@@ -3,6 +3,7 @@
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
+#include "ref_helpers.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
@@ -18,6 +19,14 @@ void test_refs_pack__cleanup(void)
cl_git_sandbox_cleanup();
}
+void packall()
+{
+ git_refdb *refdb;
+
+ cl_git_pass(git_repository_refdb(&refdb, g_repo));
+ cl_git_pass(git_refdb_compress(refdb));
+}
+
void test_refs_pack__empty(void)
{
// create a packfile for an empty folder
@@ -27,7 +36,7 @@ void test_refs_pack__empty(void)
cl_git_pass(git_futils_mkdir_r(temp_path.ptr, NULL, GIT_REFS_DIR_MODE));
git_buf_free(&temp_path);
- cl_git_pass(git_reference_packall(g_repo));
+ packall();
}
void test_refs_pack__loose(void)
@@ -38,7 +47,7 @@ void test_refs_pack__loose(void)
/* Ensure a known loose ref can be looked up */
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
- cl_assert(git_reference_is_packed(reference) == 0);
+ cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, loose_tag_ref_name);
git_reference_free(reference);
@@ -47,7 +56,7 @@ void test_refs_pack__loose(void)
* called `points_to_blob`, to make sure we can properly
* pack weak tags
*/
- cl_git_pass(git_reference_packall(g_repo));
+ packall();
/* Ensure the packed-refs file exists */
cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, GIT_PACKEDREFS_FILE));
@@ -55,7 +64,7 @@ void test_refs_pack__loose(void)
/* Ensure the known ref can still be looked up but is now packed */
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
- cl_assert(git_reference_is_packed(reference));
+ cl_assert(reference_is_packed(reference));
cl_assert_equal_s(reference->name, loose_tag_ref_name);
/* Ensure the known ref has been removed from the loose folder structure */
diff --git a/tests-clar/refs/read.c b/tests-clar/refs/read.c
index 3e2a59afd..afb6be008 100644
--- a/tests-clar/refs/read.c
+++ b/tests-clar/refs/read.c
@@ -3,6 +3,7 @@
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
+#include "ref_helpers.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
static const char *non_existing_tag_ref_name = "refs/tags/i-do-not-exist";
@@ -34,7 +35,7 @@ void test_refs_read__loose_tag(void)
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
cl_assert(git_reference_type(reference) & GIT_REF_OID);
- cl_assert(git_reference_is_packed(reference) == 0);
+ cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, loose_tag_ref_name);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJ_ANY));
@@ -71,7 +72,7 @@ void test_refs_read__symbolic(void)
cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC);
- cl_assert(git_reference_is_packed(reference) == 0);
+ cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, GIT_HEAD_FILE);
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
@@ -99,7 +100,7 @@ void test_refs_read__nested_symbolic(void)
cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name));
cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC);
- cl_assert(git_reference_is_packed(reference) == 0);
+ cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, head_tracker_sym_ref_name);
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
@@ -167,7 +168,7 @@ void test_refs_read__packed(void)
cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name));
cl_assert(git_reference_type(reference) & GIT_REF_OID);
- cl_assert(git_reference_is_packed(reference));
+ cl_assert(reference_is_packed(reference));
cl_assert_equal_s(reference->name, packed_head_name);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJ_ANY));
@@ -188,7 +189,7 @@ void test_refs_read__loose_first(void)
git_reference_free(reference);
cl_git_pass(git_reference_lookup(&reference, g_repo, packed_test_head_name));
cl_assert(git_reference_type(reference) & GIT_REF_OID);
- cl_assert(git_reference_is_packed(reference) == 0);
+ cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, packed_test_head_name);
git_reference_free(reference);
diff --git a/tests-clar/refs/ref_helpers.c b/tests-clar/refs/ref_helpers.c
new file mode 100644
index 000000000..16ab9e6ef
--- /dev/null
+++ b/tests-clar/refs/ref_helpers.c
@@ -0,0 +1,25 @@
+#include "git2/repository.h"
+#include "git2/refs.h"
+#include "common.h"
+#include "util.h"
+#include "buffer.h"
+#include "path.h"
+
+int reference_is_packed(git_reference *ref)
+{
+ git_buf ref_path = GIT_BUF_INIT;
+ int packed;
+
+ assert(ref);
+
+ if (git_buf_joinpath(&ref_path,
+ git_repository_path(git_reference_owner(ref)),
+ git_reference_name(ref)) < 0)
+ return -1;
+
+ packed = !git_path_isfile(ref_path.ptr);
+
+ git_buf_free(&ref_path);
+
+ return packed;
+}
diff --git a/tests-clar/refs/ref_helpers.h b/tests-clar/refs/ref_helpers.h
new file mode 100644
index 000000000..0ef55bfce
--- /dev/null
+++ b/tests-clar/refs/ref_helpers.h
@@ -0,0 +1 @@
+int reference_is_packed(git_reference *ref);
diff --git a/tests-clar/refs/reflog/reflog.c b/tests-clar/refs/reflog/reflog.c
index 19ee53567..1cd0ddd92 100644
--- a/tests-clar/refs/reflog/reflog.c
+++ b/tests-clar/refs/reflog/reflog.c
@@ -90,7 +90,7 @@ void test_refs_reflog_reflog__append_then_read(void)
void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void)
{
- git_reference *master;
+ git_reference *master, *new_master;
git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT;
git_buf_joinpath(&master_log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
@@ -102,12 +102,13 @@ void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void)
cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&moved_log_path)));
cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
- cl_git_pass(git_reference_rename(master, "refs/moved", 0));
+ cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0));
+ git_reference_free(master);
cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&master_log_path)));
cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&moved_log_path)));
- git_reference_free(master);
+ git_reference_free(new_master);
git_buf_free(&moved_log_path);
git_buf_free(&master_log_path);
}
@@ -152,7 +153,7 @@ void test_refs_reflog_reflog__reading_the_reflog_from_a_reference_with_no_log_re
void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void)
{
- git_reference *master;
+ git_reference *master, *new_master;
git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT;
git_reflog *reflog;
@@ -161,12 +162,13 @@ void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void)
cl_git_pass(git_reflog_write(reflog));
- cl_git_pass(git_reference_rename(master, "refs/moved", 0));
+ cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0));
+ git_reference_free(master);
cl_git_fail(git_reflog_write(reflog));
git_reflog_free(reflog);
- git_reference_free(master);
+ git_reference_free(new_master);
git_buf_free(&moved_log_path);
git_buf_free(&master_log_path);
}
diff --git a/tests-clar/refs/rename.c b/tests-clar/refs/rename.c
index 5c1e8a798..e39abeb05 100644
--- a/tests-clar/refs/rename.c
+++ b/tests-clar/refs/rename.c
@@ -3,6 +3,7 @@
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
+#include "ref_helpers.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
static const char *packed_head_name = "refs/heads/packed";
@@ -32,7 +33,7 @@ void test_refs_rename__cleanup(void)
void test_refs_rename__loose(void)
{
// rename a loose reference
- git_reference *looked_up_ref, *another_looked_up_ref;
+ git_reference *looked_up_ref, *new_ref, *another_looked_up_ref;
git_buf temp_path = GIT_BUF_INIT;
const char *new_name = "refs/tags/Nemo/knows/refs.kung-fu";
@@ -44,28 +45,29 @@ void test_refs_rename__loose(void)
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, loose_tag_ref_name));
/* ... which is indeed loose */
- cl_assert(git_reference_is_packed(looked_up_ref) == 0);
+ cl_assert(reference_is_packed(looked_up_ref) == 0);
/* Now that the reference is renamed... */
- cl_git_pass(git_reference_rename(looked_up_ref, new_name, 0));
- cl_assert_equal_s(looked_up_ref->name, new_name);
+ cl_git_pass(git_reference_rename(&new_ref, looked_up_ref, new_name, 0));
+ cl_assert_equal_s(new_ref->name, new_name);
+ git_reference_free(looked_up_ref);
/* ...It can't be looked-up with the old name... */
cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, loose_tag_ref_name));
/* ...but the new name works ok... */
cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, new_name));
- cl_assert_equal_s(another_looked_up_ref->name, new_name);
+ cl_assert_equal_s(new_ref->name, new_name);
- /* .. the ref is still loose... */
- cl_assert(git_reference_is_packed(another_looked_up_ref) == 0);
- cl_assert(git_reference_is_packed(looked_up_ref) == 0);
+ /* .. the new ref is loose... */
+ cl_assert(reference_is_packed(another_looked_up_ref) == 0);
+ cl_assert(reference_is_packed(new_ref) == 0);
/* ...and the ref can be found in the file system */
cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, new_name));
cl_assert(git_path_exists(temp_path.ptr));
- git_reference_free(looked_up_ref);
+ git_reference_free(new_ref);
git_reference_free(another_looked_up_ref);
git_buf_free(&temp_path);
}
@@ -73,7 +75,7 @@ void test_refs_rename__loose(void)
void test_refs_rename__packed(void)
{
// rename a packed reference (should make it loose)
- git_reference *looked_up_ref, *another_looked_up_ref;
+ git_reference *looked_up_ref, *new_ref, *another_looked_up_ref;
git_buf temp_path = GIT_BUF_INIT;
const char *brand_new_name = "refs/heads/brand_new_name";
@@ -85,11 +87,12 @@ void test_refs_rename__packed(void)
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
/* .. and it's packed */
- cl_assert(git_reference_is_packed(looked_up_ref) != 0);
+ cl_assert(reference_is_packed(looked_up_ref) != 0);
/* Now that the reference is renamed... */
- cl_git_pass(git_reference_rename(looked_up_ref, brand_new_name, 0));
- cl_assert_equal_s(looked_up_ref->name, brand_new_name);
+ cl_git_pass(git_reference_rename(&new_ref, looked_up_ref, brand_new_name, 0));
+ cl_assert_equal_s(new_ref->name, brand_new_name);
+ git_reference_free(looked_up_ref);
/* ...It can't be looked-up with the old name... */
cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_head_name));
@@ -99,14 +102,14 @@ void test_refs_rename__packed(void)
cl_assert_equal_s(another_looked_up_ref->name, brand_new_name);
/* .. the ref is no longer packed... */
- cl_assert(git_reference_is_packed(another_looked_up_ref) == 0);
- cl_assert(git_reference_is_packed(looked_up_ref) == 0);
+ cl_assert(reference_is_packed(another_looked_up_ref) == 0);
+ cl_assert(reference_is_packed(new_ref) == 0);
/* ...and the ref now happily lives in the file system */
cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, brand_new_name));
cl_assert(git_path_exists(temp_path.ptr));
- git_reference_free(looked_up_ref);
+ git_reference_free(new_ref);
git_reference_free(another_looked_up_ref);
git_buf_free(&temp_path);
}
@@ -114,7 +117,7 @@ void test_refs_rename__packed(void)
void test_refs_rename__packed_doesnt_pack_others(void)
{
// renaming a packed reference does not pack another reference which happens to be in both loose and pack state
- git_reference *looked_up_ref, *another_looked_up_ref;
+ git_reference *looked_up_ref, *another_looked_up_ref, *renamed_ref;
git_buf temp_path = GIT_BUF_INIT;
const char *brand_new_name = "refs/heads/brand_new_name";
@@ -126,28 +129,29 @@ void test_refs_rename__packed_doesnt_pack_others(void)
cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name));
/* Ensure it's loose */
- cl_assert(git_reference_is_packed(another_looked_up_ref) == 0);
+ cl_assert(reference_is_packed(another_looked_up_ref) == 0);
git_reference_free(another_looked_up_ref);
/* Lookup the reference to rename */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
/* Ensure it's packed */
- cl_assert(git_reference_is_packed(looked_up_ref) != 0);
+ cl_assert(reference_is_packed(looked_up_ref) != 0);
/* Now that the reference is renamed... */
- cl_git_pass(git_reference_rename(looked_up_ref, brand_new_name, 0));
+ cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, brand_new_name, 0));
+ git_reference_free(looked_up_ref);
/* Lookup the other reference */
cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name));
/* Ensure it's loose */
- cl_assert(git_reference_is_packed(another_looked_up_ref) == 0);
+ cl_assert(reference_is_packed(another_looked_up_ref) == 0);
/* Ensure the other ref still exists on the file system */
cl_assert(git_path_exists(temp_path.ptr));
- git_reference_free(looked_up_ref);
+ git_reference_free(renamed_ref);
git_reference_free(another_looked_up_ref);
git_buf_free(&temp_path);
}
@@ -155,13 +159,13 @@ void test_refs_rename__packed_doesnt_pack_others(void)
void test_refs_rename__name_collision(void)
{
// can not rename a reference with the name of an existing reference
- git_reference *looked_up_ref;
+ git_reference *looked_up_ref, *renamed_ref;
/* An existing reference... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
/* Can not be renamed to the name of another existing reference. */
- cl_git_fail(git_reference_rename(looked_up_ref, packed_test_head_name, 0));
+ cl_git_fail(git_reference_rename(&renamed_ref, looked_up_ref, packed_test_head_name, 0));
git_reference_free(looked_up_ref);
/* Failure to rename it hasn't corrupted its state */
@@ -174,7 +178,7 @@ void test_refs_rename__name_collision(void)
void test_refs_rename__invalid_name(void)
{
// can not rename a reference with an invalid name
- git_reference *looked_up_ref;
+ git_reference *looked_up_ref, *renamed_ref;
/* An existing oid reference... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));
@@ -182,12 +186,12 @@ void test_refs_rename__invalid_name(void)
/* Can not be renamed with an invalid name. */
cl_assert_equal_i(
GIT_EINVALIDSPEC,
- git_reference_rename(looked_up_ref, "Hello! I'm a very invalid name.", 0));
+ git_reference_rename(&renamed_ref, looked_up_ref, "Hello! I'm a very invalid name.", 0));
/* Can not be renamed outside of the refs hierarchy
* unless it's ALL_CAPS_AND_UNDERSCORES.
*/
- cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_rename(looked_up_ref, "i-will-sudo-you", 0));
+ cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_rename(&renamed_ref, looked_up_ref, "i-will-sudo-you", 0));
/* Failure to rename it hasn't corrupted its state */
git_reference_free(looked_up_ref);
@@ -200,7 +204,7 @@ void test_refs_rename__invalid_name(void)
void test_refs_rename__force_loose_packed(void)
{
// can force-rename a packed reference with the name of an existing loose and packed reference
- git_reference *looked_up_ref;
+ git_reference *looked_up_ref, *renamed_ref;
git_oid oid;
/* An existing reference... */
@@ -208,8 +212,9 @@ void test_refs_rename__force_loose_packed(void)
git_oid_cpy(&oid, git_reference_target(looked_up_ref));
/* Can be force-renamed to the name of another existing reference. */
- cl_git_pass(git_reference_rename(looked_up_ref, packed_test_head_name, 1));
+ cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, packed_test_head_name, 1));
git_reference_free(looked_up_ref);
+ git_reference_free(renamed_ref);
/* Check we actually renamed it */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));
@@ -224,7 +229,7 @@ void test_refs_rename__force_loose_packed(void)
void test_refs_rename__force_loose(void)
{
// can force-rename a loose reference with the name of an existing loose reference
- git_reference *looked_up_ref;
+ git_reference *looked_up_ref, *renamed_ref;
git_oid oid;
/* An existing reference... */
@@ -232,8 +237,9 @@ void test_refs_rename__force_loose(void)
git_oid_cpy(&oid, git_reference_target(looked_up_ref));
/* Can be force-renamed to the name of another existing reference. */
- cl_git_pass(git_reference_rename(looked_up_ref, "refs/heads/test", 1));
+ cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, "refs/heads/test", 1));
git_reference_free(looked_up_ref);
+ git_reference_free(renamed_ref);
/* Check we actually renamed it */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, "refs/heads/test"));
@@ -252,6 +258,7 @@ void test_refs_rename__overwrite(void)
{
// can not overwrite name of existing reference
git_reference *ref, *ref_one, *ref_one_new, *ref_two;
+ git_refdb *refdb;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
@@ -264,7 +271,8 @@ void test_refs_rename__overwrite(void)
cl_git_pass(git_reference_create(&ref_two, g_repo, ref_two_name, &id, 0));
/* Pack everything */
- cl_git_pass(git_reference_packall(g_repo));
+ cl_git_pass(git_repository_refdb(&refdb, g_repo));
+ cl_git_pass(git_refdb_compress(refdb));
/* Attempt to create illegal reference */
cl_git_fail(git_reference_create(&ref_one_new, g_repo, ref_one_name_new, &id, 0));
@@ -282,7 +290,7 @@ void test_refs_rename__overwrite(void)
void test_refs_rename__prefix(void)
{
// can be renamed to a new name prefixed with the old name
- git_reference *ref, *ref_two, *looked_up_ref;
+ git_reference *ref, *ref_two, *looked_up_ref, *renamed_ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
@@ -297,8 +305,9 @@ void test_refs_rename__prefix(void)
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name));
/* Can be rename to a new name starting with the old name. */
- cl_git_pass(git_reference_rename(looked_up_ref, ref_two_name_new, 0));
+ cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, ref_two_name_new, 0));
git_reference_free(looked_up_ref);
+ git_reference_free(renamed_ref);
/* Check we actually renamed it */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new));
@@ -314,7 +323,7 @@ void test_refs_rename__prefix(void)
void test_refs_rename__move_up(void)
{
// can move a reference to a upper reference hierarchy
- git_reference *ref, *ref_two, *looked_up_ref;
+ git_reference *ref, *ref_two, *looked_up_ref, *renamed_ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
@@ -330,13 +339,15 @@ void test_refs_rename__move_up(void)
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new));
/* Can be renamed upward the reference tree. */
- cl_git_pass(git_reference_rename(looked_up_ref, ref_two_name, 0));
+ cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, ref_two_name, 0));
git_reference_free(looked_up_ref);
+ git_reference_free(renamed_ref);
/* Check we actually renamed it */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name));
cl_assert_equal_s(looked_up_ref->name, ref_two_name);
git_reference_free(looked_up_ref);
+
cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new));
git_reference_free(ref);
git_reference_free(looked_up_ref);
@@ -344,11 +355,11 @@ void test_refs_rename__move_up(void)
void test_refs_rename__propagate_eexists(void)
{
- git_reference *ref;
+ git_reference *ref, *new_ref;
cl_git_pass(git_reference_lookup(&ref, g_repo, packed_head_name));
- cl_assert_equal_i(GIT_EEXISTS, git_reference_rename(ref, packed_test_head_name, 0));
+ cl_assert_equal_i(GIT_EEXISTS, git_reference_rename(&new_ref, ref, packed_test_head_name, 0));
git_reference_free(ref);
}
diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c
index be92c1956..e0bccf4a9 100644
--- a/tests-clar/refs/revparse.c
+++ b/tests-clar/refs/revparse.c
@@ -227,7 +227,7 @@ void test_refs_revparse__previous_head(void)
static void create_fake_stash_reference_and_reflog(git_repository *repo)
{
- git_reference *master;
+ git_reference *master, *new_master;
git_buf log_path = GIT_BUF_INIT;
git_buf_joinpath(&log_path, git_repository_path(repo), "logs/refs/fakestash");
@@ -235,12 +235,13 @@ static void create_fake_stash_reference_and_reflog(git_repository *repo)
cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&log_path)));
cl_git_pass(git_reference_lookup(&master, repo, "refs/heads/master"));
- cl_git_pass(git_reference_rename(master, "refs/fakestash", 0));
+ cl_git_pass(git_reference_rename(&new_master, master, "refs/fakestash", 0));
+ git_reference_free(master);
cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&log_path)));
git_buf_free(&log_path);
- git_reference_free(master);
+ git_reference_free(new_master);
}
void test_refs_revparse__reflog_of_a_ref_under_refs(void)
diff --git a/tests-clar/refs/setter.c b/tests-clar/refs/setter.c
new file mode 100644
index 000000000..713af814f
--- /dev/null
+++ b/tests-clar/refs/setter.c
@@ -0,0 +1,99 @@
+#include "clar_libgit2.h"
+
+#include "repository.h"
+#include "git2/reflog.h"
+#include "reflog.h"
+#include "git2/refs.h"
+
+static const char *ref_name = "refs/heads/other";
+static const char *ref_master_name = "refs/heads/master";
+static const char *ref_test_name = "refs/heads/test";
+
+static git_repository *g_repo;
+
+void test_refs_setter__initialize(void)
+{
+ g_repo = cl_git_sandbox_init("testrepo");
+}
+
+void test_refs_setter__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+void test_refs_setter__update_direct(void)
+{
+ git_reference *ref, *test_ref, *new_ref;
+ git_oid id;
+
+ cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
+ cl_assert(git_reference_type(ref) == GIT_REF_OID);
+ git_oid_cpy(&id, git_reference_target(ref));
+ git_reference_free(ref);
+
+ cl_git_pass(git_reference_lookup(&test_ref, g_repo, ref_test_name));
+ cl_assert(git_reference_type(test_ref) == GIT_REF_OID);
+
+ cl_git_pass(git_reference_set_target(&new_ref, test_ref, &id));
+
+ git_reference_free(test_ref);
+ git_reference_free(new_ref);
+
+ cl_git_pass(git_reference_lookup(&test_ref, g_repo, ref_test_name));
+ cl_assert(git_reference_type(test_ref) == GIT_REF_OID);
+ cl_assert(git_oid_cmp(&id, git_reference_target(test_ref)) == 0);
+ git_reference_free(test_ref);
+}
+
+void test_refs_setter__update_symbolic(void)
+{
+ git_reference *head, *new_head;
+
+ cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
+ cl_assert(git_reference_type(head) == GIT_REF_SYMBOLIC);
+ cl_assert(strcmp(git_reference_symbolic_target(head), ref_master_name) == 0);
+
+ cl_git_pass(git_reference_symbolic_set_target(&new_head, head, ref_test_name));
+ git_reference_free(new_head);
+ git_reference_free(head);
+
+ cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
+ cl_assert(git_reference_type(head) == GIT_REF_SYMBOLIC);
+ cl_assert(strcmp(git_reference_symbolic_target(head), ref_test_name) == 0);
+ git_reference_free(head);
+}
+
+void test_refs_setter__cant_update_direct_with_symbolic(void)
+{
+ // Overwrite an existing object id reference with a symbolic one
+ git_reference *ref, *new;
+ git_oid id;
+
+ cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
+ cl_assert(git_reference_type(ref) == GIT_REF_OID);
+ git_oid_cpy(&id, git_reference_target(ref));
+
+ cl_git_fail(git_reference_symbolic_set_target(&new, ref, ref_name));
+
+ git_reference_free(ref);
+}
+
+void test_refs_setter__cant_update_symbolic_with_direct(void)
+{
+ // Overwrite an existing symbolic reference with an object id one
+ git_reference *ref, *new;
+ git_oid id;
+
+ cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
+ cl_assert(git_reference_type(ref) == GIT_REF_OID);
+ git_oid_cpy(&id, git_reference_target(ref));
+ git_reference_free(ref);
+
+ /* Create the symbolic ref */
+ cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 0));
+
+ /* Can't set an OID on a direct ref */
+ cl_git_fail(git_reference_set_target(&new, ref, &id));
+
+ git_reference_free(ref);
+}
diff --git a/tests-clar/refs/update.c b/tests-clar/refs/update.c
index 6c2107ee2..205b526a2 100644
--- a/tests-clar/refs/update.c
+++ b/tests-clar/refs/update.c
@@ -19,11 +19,8 @@ void test_refs_update__updating_the_target_of_a_symref_with_an_invalid_name_retu
git_reference *head;
cl_git_pass(git_reference_lookup(&head, g_repo, GIT_HEAD_FILE));
-
cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
-
- cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_symbolic_set_target(
- head, "refs/heads/inv@{id"));
-
git_reference_free(head);
+
+ cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_symbolic_create(&head, g_repo, GIT_HEAD_FILE, "refs/heads/inv@{id", 1));
}
diff --git a/tests-clar/stash/save.c b/tests-clar/stash/save.c
index ea2eb282d..588dfc3ea 100644
--- a/tests-clar/stash/save.c
+++ b/tests-clar/stash/save.c
@@ -193,8 +193,7 @@ void test_stash_save__cannot_stash_against_an_unborn_branch(void)
{
git_reference *head;
- cl_git_pass(git_reference_lookup(&head, repo, "HEAD"));
- cl_git_pass(git_reference_symbolic_set_target(head, "refs/heads/unborn"));
+ cl_git_pass(git_reference_symbolic_create(&head, repo, "HEAD", "refs/heads/unborn", 1));
cl_assert_equal_i(GIT_EORPHANEDHEAD,
git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));