]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/refs/races.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / refs / races.c
CommitLineData
d6236cf6
CMN
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
8static const char *commit_id = "099fabac3a9ea935598528c27f866e34089c2eff";
9static const char *refname = "refs/heads/master";
878fb66f 10static const char *other_refname = "refs/heads/foo";
d6236cf6
CMN
11static const char *other_commit_id = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
12
13static git_repository *g_repo;
14
15void test_refs_races__initialize(void)
16{
17 g_repo = cl_git_sandbox_init("testrepo");
18}
19
20void test_refs_races__cleanup(void)
21{
22 cl_git_sandbox_cleanup();
23}
24
c25aa7cd
PP
25void 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
d6236cf6
CMN
43void test_refs_races__create_matching(void)
44{
d6236cf6
CMN
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
659cf202 51 cl_git_fail_with(GIT_EMODIFIED, git_reference_create_matching(&ref, g_repo, refname, &other_id, 1, &other_id, NULL));
d6236cf6
CMN
52
53 cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
659cf202
CMN
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));
d6236cf6
CMN
56
57 git_reference_free(ref);
58 git_reference_free(ref2);
59 git_reference_free(ref3);
60}
878fb66f
CMN
61
62void 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
659cf202 70 cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_create_matching(&ref, g_repo, "HEAD", other_refname, 1, other_refname, NULL));
878fb66f
CMN
71
72 cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
659cf202
CMN
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));
878fb66f
CMN
75
76 git_reference_free(ref);
77 git_reference_free(ref2);
78 git_reference_free(ref3);
79}
f44fd59e
CMN
80
81void 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 */
22a2d3d5
UG
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));
f44fd59e
CMN
97 cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
98
99 git_reference_free(ref);
100 git_reference_free(ref2);
101
659cf202 102 cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL));
f44fd59e
CMN
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));
659cf202 107 cl_git_pass(git_reference_create_matching(&ref2, g_repo, refname, &other_id, 1, &id, NULL));
f44fd59e
CMN
108 cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
109
110 git_reference_free(ref);
111 git_reference_free(ref2);
112}
b7ae71ec
CMN
113
114void 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));
659cf202 124 cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, refname, other_refname, 1, NULL));
b7ae71ec
CMN
125 cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
126
127 git_reference_free(ref);
128 git_reference_free(ref2);
129
659cf202 130 cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL));
b7ae71ec
CMN
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));
659cf202
CMN
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));
b7ae71ec
CMN
137
138 git_reference_free(ref);
139 git_reference_free(ref2);
140 git_reference_free(ref3);
141}
142
143void 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 */
22a2d3d5
UG
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));
b7ae71ec
CMN
154 cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
155
156 git_reference_free(ref);
157 git_reference_free(ref2);
158
22a2d3d5 159 cl_git_pass(git_reference_symbolic_create(&ref, g_repo, "refs/symref", refname, 1, NULL));
b7ae71ec
CMN
160 git_reference_free(ref);
161
162 /* Updating a symbolic ref when it's currently direct should fail */
22a2d3d5
UG
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));
659cf202 165 cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_set_target(&ref3, ref, other_refname, NULL));
b7ae71ec
CMN
166
167 git_reference_free(ref);
168 git_reference_free(ref2);
169 git_reference_free(ref3);
170}