]>
git.proxmox.com Git - libgit2.git/blob - tests/refs/races.c
fbecf4a75eea562c0303de042465789329ab589a
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(void)
27 git_reference
*ref
, *ref2
, *ref3
;
30 git_oid_fromstr(&id
, commit_id
);
31 git_oid_fromstr(&other_id
, other_commit_id
);
33 cl_git_fail_with(GIT_EMODIFIED
, git_reference_create_matching(&ref
, g_repo
, refname
, &other_id
, 1, &other_id
, NULL
));
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
));
39 git_reference_free(ref
);
40 git_reference_free(ref2
);
41 git_reference_free(ref3
);
44 void test_refs_races__symbolic_create_matching(void)
46 git_reference
*ref
, *ref2
, *ref3
;
49 git_oid_fromstr(&id
, commit_id
);
50 git_oid_fromstr(&other_id
, other_commit_id
);
52 cl_git_fail_with(GIT_EMODIFIED
, git_reference_symbolic_create_matching(&ref
, g_repo
, "HEAD", other_refname
, 1, other_refname
, NULL
));
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
));
58 git_reference_free(ref
);
59 git_reference_free(ref2
);
60 git_reference_free(ref3
);
63 void test_refs_races__delete(void)
65 git_reference
*ref
, *ref2
;
68 git_oid_fromstr(&id
, commit_id
);
69 git_oid_fromstr(&other_id
, other_commit_id
);
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
);
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
));
81 git_reference_free(ref
);
82 git_reference_free(ref2
);
84 cl_git_pass(git_reference_create(&ref
, g_repo
, refname
, &id
, 1, NULL
));
85 git_reference_free(ref
);
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
));
92 git_reference_free(ref
);
93 git_reference_free(ref2
);
96 void test_refs_races__switch_oid_to_symbolic(void)
98 git_reference
*ref
, *ref2
, *ref3
;
101 git_oid_fromstr(&id
, commit_id
);
102 git_oid_fromstr(&other_id
, other_commit_id
);
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
));
109 git_reference_free(ref
);
110 git_reference_free(ref2
);
112 cl_git_pass(git_reference_create(&ref
, g_repo
, refname
, &id
, 1, NULL
));
113 git_reference_free(ref
);
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
));
120 git_reference_free(ref
);
121 git_reference_free(ref2
);
122 git_reference_free(ref3
);
125 void test_refs_races__switch_symbolic_to_oid(void)
127 git_reference
*ref
, *ref2
, *ref3
;
128 git_oid id
, other_id
;
130 git_oid_fromstr(&id
, commit_id
);
131 git_oid_fromstr(&other_id
, other_commit_id
);
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
));
138 git_reference_free(ref
);
139 git_reference_free(ref2
);
141 cl_git_pass(git_reference_symbolic_create(&ref
, g_repo
, "HEAD", refname
, 1, NULL
));
142 git_reference_free(ref
);
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
));
149 git_reference_free(ref
);
150 git_reference_free(ref2
);
151 git_reference_free(ref3
);