]>
git.proxmox.com Git - libgit2.git/blob - tests/refs/races.c
1 #include "clar_libgit2.h"
3 #include "repository.h"
4 #include "git2/reflog.h"
6 #include "ref_helpers.h"
8 static const char *commit_id
= "099fabac3a9ea935598528c27f866e34089c2eff";
9 static const char *refname
= "refs/heads/master";
10 static const char *other_refname
= "refs/heads/foo";
11 static const char *other_commit_id
= "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
13 static git_repository
*g_repo
;
15 void test_refs_races__initialize(void)
17 g_repo
= cl_git_sandbox_init("testrepo");
20 void test_refs_races__cleanup(void)
22 cl_git_sandbox_cleanup();
25 void test_refs_races__create_matching_zero_old(void)
30 git_oid_fromstr(&id
, commit_id
);
31 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
);
36 cl_git_pass(git_reference_create_matching(&ref
, g_repo
, other_refname
, &id
, 1, &zero_id
, NULL
));
37 git_reference_free(ref
);
39 cl_git_fail(git_reference_create_matching(&ref
, g_repo
, other_refname
, &id
, 1, &zero_id
, NULL
));
40 git_reference_free(ref
);
43 void test_refs_races__create_matching(void)
45 git_reference
*ref
, *ref2
, *ref3
;
48 git_oid_fromstr(&id
, commit_id
);
49 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
));
53 cl_git_pass(git_reference_lookup(&ref
, g_repo
, refname
));
54 cl_git_pass(git_reference_create_matching(&ref2
, g_repo
, refname
, &other_id
, 1, &id
, NULL
));
55 cl_git_fail_with(GIT_EMODIFIED
, git_reference_set_target(&ref3
, ref
, &other_id
, NULL
));
57 git_reference_free(ref
);
58 git_reference_free(ref2
);
59 git_reference_free(ref3
);
62 void test_refs_races__symbolic_create_matching(void)
64 git_reference
*ref
, *ref2
, *ref3
;
67 git_oid_fromstr(&id
, commit_id
);
68 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
));
72 cl_git_pass(git_reference_lookup(&ref
, g_repo
, "HEAD"));
73 cl_git_pass(git_reference_symbolic_create_matching(&ref2
, g_repo
, "HEAD", other_refname
, 1, NULL
, refname
));
74 cl_git_fail_with(GIT_EMODIFIED
, git_reference_symbolic_set_target(&ref3
, ref
, other_refname
, NULL
));
76 git_reference_free(ref
);
77 git_reference_free(ref2
);
78 git_reference_free(ref3
);
81 void test_refs_races__delete(void)
83 git_reference
*ref
, *ref2
;
86 git_oid_fromstr(&id
, commit_id
);
87 git_oid_fromstr(&other_id
, other_commit_id
);
89 /* We can delete a value that matches */
90 cl_git_pass(git_reference_lookup(&ref
, g_repo
, refname
));
91 cl_git_pass(git_reference_delete(ref
));
92 git_reference_free(ref
);
94 /* We cannot delete a symbolic value that doesn't match */
95 cl_git_pass(git_reference_lookup(&ref
, g_repo
, "refs/symref"));
96 cl_git_pass(git_reference_symbolic_create_matching(&ref2
, g_repo
, "refs/symref", other_refname
, 1, NULL
, refname
));
97 cl_git_fail_with(GIT_EMODIFIED
, git_reference_delete(ref
));
99 git_reference_free(ref
);
100 git_reference_free(ref2
);
102 cl_git_pass(git_reference_create(&ref
, g_repo
, refname
, &id
, 1, NULL
));
103 git_reference_free(ref
);
105 /* We cannot delete an oid value that doesn't match */
106 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 cl_git_fail_with(GIT_EMODIFIED
, git_reference_delete(ref
));
110 git_reference_free(ref
);
111 git_reference_free(ref2
);
114 void test_refs_races__switch_oid_to_symbolic(void)
116 git_reference
*ref
, *ref2
, *ref3
;
117 git_oid id
, other_id
;
119 git_oid_fromstr(&id
, commit_id
);
120 git_oid_fromstr(&other_id
, other_commit_id
);
122 /* Removing a direct ref when it's currently symbolic should fail */
123 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 cl_git_fail_with(GIT_EMODIFIED
, git_reference_delete(ref
));
127 git_reference_free(ref
);
128 git_reference_free(ref2
);
130 cl_git_pass(git_reference_create(&ref
, g_repo
, refname
, &id
, 1, NULL
));
131 git_reference_free(ref
);
133 /* Updating a direct ref when it's currently symbolic should fail */
134 cl_git_pass(git_reference_lookup(&ref
, g_repo
, refname
));
135 cl_git_pass(git_reference_symbolic_create(&ref2
, g_repo
, refname
, other_refname
, 1, NULL
));
136 cl_git_fail_with(GIT_EMODIFIED
, git_reference_set_target(&ref3
, ref
, &other_id
, NULL
));
138 git_reference_free(ref
);
139 git_reference_free(ref2
);
140 git_reference_free(ref3
);
143 void test_refs_races__switch_symbolic_to_oid(void)
145 git_reference
*ref
, *ref2
, *ref3
;
146 git_oid id
, other_id
;
148 git_oid_fromstr(&id
, commit_id
);
149 git_oid_fromstr(&other_id
, other_commit_id
);
151 /* Removing a symbolic ref when it's currently direct should fail */
152 cl_git_pass(git_reference_lookup(&ref
, g_repo
, "refs/symref"));
153 cl_git_pass(git_reference_create(&ref2
, g_repo
, "refs/symref", &id
, 1, NULL
));
154 cl_git_fail_with(GIT_EMODIFIED
, git_reference_delete(ref
));
156 git_reference_free(ref
);
157 git_reference_free(ref2
);
159 cl_git_pass(git_reference_symbolic_create(&ref
, g_repo
, "refs/symref", refname
, 1, NULL
));
160 git_reference_free(ref
);
162 /* Updating a symbolic ref when it's currently direct should fail */
163 cl_git_pass(git_reference_lookup(&ref
, g_repo
, "refs/symref"));
164 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
));
167 git_reference_free(ref
);
168 git_reference_free(ref2
);
169 git_reference_free(ref3
);