races.c 5.53 KB
Newer Older
1 2 3 4 5 6 7 8 9
#include "clar_libgit2.h"

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

static const char *commit_id = "099fabac3a9ea935598528c27f866e34089c2eff";
static const char *refname = "refs/heads/master";
10
static const char *other_refname = "refs/heads/foo";
11 12 13 14 15 16 17 18 19 20 21 22 23 24
static const char *other_commit_id = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";

static git_repository *g_repo;

void test_refs_races__initialize(void)
{
   g_repo = cl_git_sandbox_init("testrepo");
}

void test_refs_races__cleanup(void)
{
   cl_git_sandbox_cleanup();
}

25 26 27 28 29 30 31 32
void test_refs_races__create_matching_zero_old(void)
{
	git_reference *ref;
	git_oid id, zero_id;

	git_oid_fromstr(&id, commit_id);
	git_oid_fromstr(&zero_id, "0000000000000000000000000000000000000000");

33
	cl_git_fail(git_reference_create_matching(&ref, g_repo, refname, &id, 1, &zero_id, NULL));
34
	git_reference_free(ref);
35

36
	cl_git_pass(git_reference_create_matching(&ref, g_repo, other_refname, &id, 1, &zero_id, NULL));
37
	git_reference_free(ref);
38

39
	cl_git_fail(git_reference_create_matching(&ref, g_repo, other_refname, &id, 1, &zero_id, NULL));
40 41 42
	git_reference_free(ref);
}

43 44 45 46 47 48 49 50
void test_refs_races__create_matching(void)
{
	git_reference *ref, *ref2, *ref3;
	git_oid id, other_id;

	git_oid_fromstr(&id, commit_id);
	git_oid_fromstr(&other_id, other_commit_id);

51
	cl_git_fail_with(GIT_EMODIFIED, git_reference_create_matching(&ref, g_repo, refname, &other_id, 1, &other_id, NULL));
52 53

	cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
54 55
	cl_git_pass(git_reference_create_matching(&ref2, g_repo, refname, &other_id, 1, &id, NULL));
	cl_git_fail_with(GIT_EMODIFIED, git_reference_set_target(&ref3, ref, &other_id, NULL));
56 57 58 59 60

	git_reference_free(ref);
	git_reference_free(ref2);
	git_reference_free(ref3);
}
61 62 63 64 65 66 67 68 69

void test_refs_races__symbolic_create_matching(void)
{
	git_reference *ref, *ref2, *ref3;
	git_oid id, other_id;

	git_oid_fromstr(&id, commit_id);
	git_oid_fromstr(&other_id, other_commit_id);

70
	cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_create_matching(&ref, g_repo, "HEAD", other_refname, 1, other_refname, NULL));
71 72

	cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
73 74
	cl_git_pass(git_reference_symbolic_create_matching(&ref2, g_repo, "HEAD", other_refname, 1, NULL, refname));
	cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_set_target(&ref3, ref, other_refname, NULL));
75 76 77 78 79

	git_reference_free(ref);
	git_reference_free(ref2);
	git_reference_free(ref3);
}
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

void test_refs_races__delete(void)
{
	git_reference *ref, *ref2;
	git_oid id, other_id;

	git_oid_fromstr(&id, commit_id);
	git_oid_fromstr(&other_id, other_commit_id);

	/* We can delete a value that matches */
	cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
	cl_git_pass(git_reference_delete(ref));
	git_reference_free(ref);

	/* We cannot delete a symbolic value that doesn't match */
95 96
	cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/symref"));
	cl_git_pass(git_reference_symbolic_create_matching(&ref2, g_repo, "refs/symref", other_refname, 1, NULL, refname));
97 98 99 100 101
	cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));

	git_reference_free(ref);
	git_reference_free(ref2);

102
	cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL));
103 104 105 106
	git_reference_free(ref);

	/* We cannot delete an oid value that doesn't match */
	cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
107
	cl_git_pass(git_reference_create_matching(&ref2, g_repo, refname, &other_id, 1, &id, NULL));
108 109 110 111 112
	cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));

	git_reference_free(ref);
	git_reference_free(ref2);
}
113 114 115 116 117 118 119 120 121 122 123

void test_refs_races__switch_oid_to_symbolic(void)
{
	git_reference *ref, *ref2, *ref3;
	git_oid id, other_id;

	git_oid_fromstr(&id, commit_id);
	git_oid_fromstr(&other_id, other_commit_id);

	/* Removing a direct ref when it's currently symbolic should fail */
	cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
124
	cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, refname, other_refname, 1, NULL));
125 126 127 128 129
	cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));

	git_reference_free(ref);
	git_reference_free(ref2);

130
	cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL));
131 132 133 134
	git_reference_free(ref);

	/* Updating a direct ref when it's currently symbolic should fail */
	cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
135 136
	cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, refname, other_refname, 1, NULL));
	cl_git_fail_with(GIT_EMODIFIED, git_reference_set_target(&ref3, ref, &other_id, NULL));
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

	git_reference_free(ref);
	git_reference_free(ref2);
	git_reference_free(ref3);
}

void test_refs_races__switch_symbolic_to_oid(void)
{
	git_reference *ref, *ref2, *ref3;
	git_oid id, other_id;

	git_oid_fromstr(&id, commit_id);
	git_oid_fromstr(&other_id, other_commit_id);

	/* Removing a symbolic ref when it's currently direct should fail */
152 153
	cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/symref"));
	cl_git_pass(git_reference_create(&ref2, g_repo, "refs/symref", &id, 1, NULL));
154 155 156 157 158
	cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));

	git_reference_free(ref);
	git_reference_free(ref2);

159
	cl_git_pass(git_reference_symbolic_create(&ref, g_repo, "refs/symref", refname, 1, NULL));
160 161 162
	git_reference_free(ref);

	/* Updating a symbolic ref when it's currently direct should fail */
163 164
	cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/symref"));
	cl_git_pass(git_reference_create(&ref2, g_repo, "refs/symref", &id, 1, NULL));
165
	cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_set_target(&ref3, ref, other_refname, NULL));
166 167 168 169 170

	git_reference_free(ref);
	git_reference_free(ref2);
	git_reference_free(ref3);
}