]> git.proxmox.com Git - libgit2.git/blob - tests/refs/races.c
fbecf4a75eea562c0303de042465789329ab589a
[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(void)
26 {
27 git_reference *ref, *ref2, *ref3;
28 git_oid id, other_id;
29
30 git_oid_fromstr(&id, commit_id);
31 git_oid_fromstr(&other_id, other_commit_id);
32
33 cl_git_fail_with(GIT_EMODIFIED, git_reference_create_matching(&ref, g_repo, refname, &other_id, 1, &other_id, NULL));
34
35 cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
36 cl_git_pass(git_reference_create_matching(&ref2, g_repo, refname, &other_id, 1, &id, NULL));
37 cl_git_fail_with(GIT_EMODIFIED, git_reference_set_target(&ref3, ref, &other_id, NULL));
38
39 git_reference_free(ref);
40 git_reference_free(ref2);
41 git_reference_free(ref3);
42 }
43
44 void test_refs_races__symbolic_create_matching(void)
45 {
46 git_reference *ref, *ref2, *ref3;
47 git_oid id, other_id;
48
49 git_oid_fromstr(&id, commit_id);
50 git_oid_fromstr(&other_id, other_commit_id);
51
52 cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_create_matching(&ref, g_repo, "HEAD", other_refname, 1, other_refname, NULL));
53
54 cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
55 cl_git_pass(git_reference_symbolic_create_matching(&ref2, g_repo, "HEAD", other_refname, 1, NULL, refname));
56 cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_set_target(&ref3, ref, other_refname, NULL));
57
58 git_reference_free(ref);
59 git_reference_free(ref2);
60 git_reference_free(ref3);
61 }
62
63 void test_refs_races__delete(void)
64 {
65 git_reference *ref, *ref2;
66 git_oid id, other_id;
67
68 git_oid_fromstr(&id, commit_id);
69 git_oid_fromstr(&other_id, other_commit_id);
70
71 /* We can delete a value that matches */
72 cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
73 cl_git_pass(git_reference_delete(ref));
74 git_reference_free(ref);
75
76 /* We cannot delete a symbolic value that doesn't match */
77 cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
78 cl_git_pass(git_reference_symbolic_create_matching(&ref2, g_repo, "HEAD", other_refname, 1, NULL, refname));
79 cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
80
81 git_reference_free(ref);
82 git_reference_free(ref2);
83
84 cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL));
85 git_reference_free(ref);
86
87 /* We cannot delete an oid value that doesn't match */
88 cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
89 cl_git_pass(git_reference_create_matching(&ref2, g_repo, refname, &other_id, 1, &id, NULL));
90 cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
91
92 git_reference_free(ref);
93 git_reference_free(ref2);
94 }
95
96 void test_refs_races__switch_oid_to_symbolic(void)
97 {
98 git_reference *ref, *ref2, *ref3;
99 git_oid id, other_id;
100
101 git_oid_fromstr(&id, commit_id);
102 git_oid_fromstr(&other_id, other_commit_id);
103
104 /* Removing a direct ref when it's currently symbolic should fail */
105 cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
106 cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, refname, other_refname, 1, NULL));
107 cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
108
109 git_reference_free(ref);
110 git_reference_free(ref2);
111
112 cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL));
113 git_reference_free(ref);
114
115 /* Updating a direct ref when it's currently symbolic should fail */
116 cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
117 cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, refname, other_refname, 1, NULL));
118 cl_git_fail_with(GIT_EMODIFIED, git_reference_set_target(&ref3, ref, &other_id, NULL));
119
120 git_reference_free(ref);
121 git_reference_free(ref2);
122 git_reference_free(ref3);
123 }
124
125 void test_refs_races__switch_symbolic_to_oid(void)
126 {
127 git_reference *ref, *ref2, *ref3;
128 git_oid id, other_id;
129
130 git_oid_fromstr(&id, commit_id);
131 git_oid_fromstr(&other_id, other_commit_id);
132
133 /* Removing a symbolic ref when it's currently direct should fail */
134 cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
135 cl_git_pass(git_reference_create(&ref2, g_repo, "HEAD", &id, 1, NULL));
136 cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
137
138 git_reference_free(ref);
139 git_reference_free(ref2);
140
141 cl_git_pass(git_reference_symbolic_create(&ref, g_repo, "HEAD", refname, 1, NULL));
142 git_reference_free(ref);
143
144 /* Updating a symbolic ref when it's currently direct should fail */
145 cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
146 cl_git_pass(git_reference_create(&ref2, g_repo, "HEAD", &id, 1, NULL));
147 cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_set_target(&ref3, ref, other_refname, NULL));
148
149 git_reference_free(ref);
150 git_reference_free(ref2);
151 git_reference_free(ref3);
152 }