overwrite.c 4.53 KB
Newer Older
Ben Straub committed
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "clar_libgit2.h"

#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"

static const char *ref_name = "refs/heads/other";
static const char *ref_master_name = "refs/heads/master";
static const char *ref_branch_name = "refs/heads/branch";
static const char *ref_test_name = "refs/heads/test";

static git_repository *g_repo;

14
void test_refs_overwrite__initialize(void)
Ben Straub committed
15 16 17 18
{
   g_repo = cl_git_sandbox_init("testrepo");
}

19
void test_refs_overwrite__cleanup(void)
Ben Straub committed
20 21 22 23
{
   cl_git_sandbox_cleanup();
}

24
void test_refs_overwrite__symbolic(void)
Ben Straub committed
25 26 27 28 29
{
   // Overwrite an existing symbolic reference
	git_reference *ref, *branch_ref;

	/* The target needds to exist and we need to check the name has changed */
30 31
	cl_git_pass(git_reference_symbolic_create(&branch_ref, g_repo, ref_branch_name, ref_master_name, 0));
	cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_branch_name, 0));
Ben Straub committed
32 33 34 35 36
	git_reference_free(ref);

	/* Ensure it points to the right place*/
	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
	cl_assert(git_reference_type(ref) & GIT_REF_SYMBOLIC);
37
	cl_assert_equal_s(git_reference_symbolic_target(ref), ref_branch_name);
Ben Straub committed
38 39 40
	git_reference_free(ref);

	/* Ensure we can't create it unless we force it to */
41 42
	cl_git_fail(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 0));
	cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 1));
Ben Straub committed
43 44 45 46 47
	git_reference_free(ref);

	/* Ensure it points to the right place */
	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
	cl_assert(git_reference_type(ref) & GIT_REF_SYMBOLIC);
48
	cl_assert_equal_s(git_reference_symbolic_target(ref), ref_master_name);
Ben Straub committed
49 50 51 52 53

	git_reference_free(ref);
	git_reference_free(branch_ref);
}

54
void test_refs_overwrite__object_id(void)
Ben Straub committed
55 56 57 58 59 60 61
{
   // Overwrite an existing object id reference
	git_reference *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);
62
	git_oid_cpy(&id, git_reference_target(ref));
Ben Straub committed
63 64 65
	git_reference_free(ref);

	/* Create it */
66
	cl_git_pass(git_reference_create(&ref, g_repo, ref_name, &id, 0));
Ben Straub committed
67 68 69 70
	git_reference_free(ref);

	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_test_name));
	cl_assert(git_reference_type(ref) & GIT_REF_OID);
71
	git_oid_cpy(&id, git_reference_target(ref));
Ben Straub committed
72 73 74
	git_reference_free(ref);

	/* Ensure we can't overwrite unless we force it */
75 76
	cl_git_fail(git_reference_create(&ref, g_repo, ref_name, &id, 0));
	cl_git_pass(git_reference_create(&ref, g_repo, ref_name, &id, 1));
Ben Straub committed
77 78 79 80
	git_reference_free(ref);

	/* Ensure it has been overwritten */
	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
81
	cl_assert(!git_oid_cmp(&id, git_reference_target(ref)));
Ben Straub committed
82 83 84 85

	git_reference_free(ref);
}

86
void test_refs_overwrite__object_id_with_symbolic(void)
Ben Straub committed
87 88 89 90 91 92 93
{
   // Overwrite an existing object id reference with a symbolic one
	git_reference *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);
94
	git_oid_cpy(&id, git_reference_target(ref));
Ben Straub committed
95 96
	git_reference_free(ref);

97
	cl_git_pass(git_reference_create(&ref, g_repo, ref_name, &id, 0));
Ben Straub committed
98
	git_reference_free(ref);
99 100
	cl_git_fail(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 0));
	cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 1));
Ben Straub committed
101 102 103 104 105
	git_reference_free(ref);

	/* Ensure it points to the right place */
	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
	cl_assert(git_reference_type(ref) & GIT_REF_SYMBOLIC);
106
	cl_assert_equal_s(git_reference_symbolic_target(ref), ref_master_name);
Ben Straub committed
107 108 109 110

	git_reference_free(ref);
}

111
void test_refs_overwrite__symbolic_with_object_id(void)
Ben Straub committed
112 113 114 115 116 117 118
{
   // Overwrite an existing symbolic reference with an object id one
	git_reference *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);
119
	git_oid_cpy(&id, git_reference_target(ref));
Ben Straub committed
120 121 122
	git_reference_free(ref);

	/* Create the symbolic ref */
123
	cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 0));
Ben Straub committed
124 125
	git_reference_free(ref);
	/* It shouldn't overwrite unless we tell it to */
126 127
	cl_git_fail(git_reference_create(&ref, g_repo, ref_name, &id, 0));
	cl_git_pass(git_reference_create(&ref, g_repo, ref_name, &id, 1));
Ben Straub committed
128 129 130 131 132
	git_reference_free(ref);

	/* Ensure it points to the right place */
	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
	cl_assert(git_reference_type(ref) & GIT_REF_OID);
133
	cl_assert(!git_oid_cmp(git_reference_target(ref), &id));
Ben Straub committed
134 135 136

	git_reference_free(ref);
}