overwrite.c 4.66 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
	/* Overwrite an existing symbolic reference */
Ben Straub committed
27 28 29
	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, NULL));
	cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_branch_name, 0, NULL));
Ben Straub committed
32 33 34 35
	git_reference_free(ref);

	/* Ensure it points to the right place*/
	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
36
	cl_assert(git_reference_type(ref) & GIT_REFERENCE_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, NULL));
	cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 1, NULL));
Ben Straub committed
43 44 45 46
	git_reference_free(ref);

	/* Ensure it points to the right place */
	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
47
	cl_assert(git_reference_type(ref) & GIT_REFERENCE_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
	/* Overwrite an existing object id reference */
Ben Straub committed
57 58 59 60
	git_reference *ref;
	git_oid id;

	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
61
	cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
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, NULL));
Ben Straub committed
67 68 69
	git_reference_free(ref);

	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_test_name));
70
	cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
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, NULL));
	cl_git_pass(git_reference_create(&ref, g_repo, ref_name, &id, 1, NULL));
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_equal_oid(&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
	/* Overwrite an existing object id reference with a symbolic one */
Ben Straub committed
89 90 91 92
	git_reference *ref;
	git_oid id;

	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
93
	cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
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, NULL));
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, NULL));
	cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 1, NULL));
Ben Straub committed
101 102 103 104
	git_reference_free(ref);

	/* Ensure it points to the right place */
	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
105
	cl_assert(git_reference_type(ref) & GIT_REFERENCE_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
	/* Overwrite an existing symbolic reference with an object id one */
Ben Straub committed
114 115 116 117
	git_reference *ref;
	git_oid id;

	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
118
	cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
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, NULL));
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, NULL));
	cl_git_pass(git_reference_create(&ref, g_repo, ref_name, &id, 1, NULL));
Ben Straub committed
128 129 130 131
	git_reference_free(ref);

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

	git_reference_free(ref);
}