]> git.proxmox.com Git - libgit2.git/blob - tests/refs/races.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / refs / races.c
1 #include "clar_libgit2.h"
2
3 #include "repository.h"
4 #include "git2/reflog.h"
5 #include "reflog.h"
6 #include "ref_helpers.h"
7
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";
12
13 static git_repository *g_repo;
14
15 void test_refs_races__initialize(void)
16 {
17 g_repo = cl_git_sandbox_init("testrepo");
18 }
19
20 void test_refs_races__cleanup(void)
21 {
22 cl_git_sandbox_cleanup();
23 }
24
25 void test_refs_races__create_matching_zero_old(void)
26 {
27 git_reference *ref;
28 git_oid id, zero_id;
29
30 git_oid_fromstr(&id, commit_id);
31 git_oid_fromstr(&zero_id, "0000000000000000000000000000000000000000");
32
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 git_reference_free(ref);
41 }
42
43 void test_refs_races__create_matching(void)
44 {
45 git_reference *ref, *ref2, *ref3;
46 git_oid id, other_id;
47
48 git_oid_fromstr(&id, commit_id);
49 git_oid_fromstr(&other_id, other_commit_id);
50
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 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));
56
57 git_reference_free(ref);
58 git_reference_free(ref2);
59 git_reference_free(ref3);
60 }
61
62 void test_refs_races__symbolic_create_matching(void)
63 {
64 git_reference *ref, *ref2, *ref3;
65 git_oid id, other_id;
66
67 git_oid_fromstr(&id, commit_id);
68 git_oid_fromstr(&other_id, other_commit_id);
69
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 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));
75
76 git_reference_free(ref);
77 git_reference_free(ref2);
78 git_reference_free(ref3);
79 }
80
81 void test_refs_races__delete(void)
82 {
83 git_reference *ref, *ref2;
84 git_oid id, other_id;
85
86 git_oid_fromstr(&id, commit_id);
87 git_oid_fromstr(&other_id, other_commit_id);
88
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);
93
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));
98
99 git_reference_free(ref);
100 git_reference_free(ref2);
101
102 cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL));
103 git_reference_free(ref);
104
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));
109
110 git_reference_free(ref);
111 git_reference_free(ref2);
112 }
113
114 void test_refs_races__switch_oid_to_symbolic(void)
115 {
116 git_reference *ref, *ref2, *ref3;
117 git_oid id, other_id;
118
119 git_oid_fromstr(&id, commit_id);
120 git_oid_fromstr(&other_id, other_commit_id);
121
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));
126
127 git_reference_free(ref);
128 git_reference_free(ref2);
129
130 cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL));
131 git_reference_free(ref);
132
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));
137
138 git_reference_free(ref);
139 git_reference_free(ref2);
140 git_reference_free(ref3);
141 }
142
143 void test_refs_races__switch_symbolic_to_oid(void)
144 {
145 git_reference *ref, *ref2, *ref3;
146 git_oid id, other_id;
147
148 git_oid_fromstr(&id, commit_id);
149 git_oid_fromstr(&other_id, other_commit_id);
150
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));
155
156 git_reference_free(ref);
157 git_reference_free(ref2);
158
159 cl_git_pass(git_reference_symbolic_create(&ref, g_repo, "refs/symref", refname, 1, NULL));
160 git_reference_free(ref);
161
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));
166
167 git_reference_free(ref);
168 git_reference_free(ref2);
169 git_reference_free(ref3);
170 }