]> git.proxmox.com Git - libgit2.git/blob - tests/refs/transactions.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / refs / transactions.c
1 #include "clar_libgit2.h"
2 #include "git2/transaction.h"
3
4 static git_repository *g_repo;
5 static git_transaction *g_tx;
6
7 void test_refs_transactions__initialize(void)
8 {
9 g_repo = cl_git_sandbox_init("testrepo");
10 cl_git_pass(git_transaction_new(&g_tx, g_repo));
11 }
12
13 void test_refs_transactions__cleanup(void)
14 {
15 git_transaction_free(g_tx);
16 cl_git_sandbox_cleanup();
17 }
18
19 void test_refs_transactions__single_ref_oid(void)
20 {
21 git_reference *ref;
22 git_oid id;
23
24 git_oid_fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
25
26 cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
27 cl_git_pass(git_transaction_set_target(g_tx, "refs/heads/master", &id, NULL, NULL));
28 cl_git_pass(git_transaction_commit(g_tx));
29
30 cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/master"));
31
32 cl_assert(!git_oid_cmp(&id, git_reference_target(ref)));
33 git_reference_free(ref);
34 }
35
36 void test_refs_transactions__single_ref_symbolic(void)
37 {
38 git_reference *ref;
39
40 cl_git_pass(git_transaction_lock_ref(g_tx, "HEAD"));
41 cl_git_pass(git_transaction_set_symbolic_target(g_tx, "HEAD", "refs/heads/foo", NULL, NULL));
42 cl_git_pass(git_transaction_commit(g_tx));
43
44 cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
45
46 cl_assert_equal_s("refs/heads/foo", git_reference_symbolic_target(ref));
47 git_reference_free(ref);
48 }
49
50 void test_refs_transactions__single_ref_mix_types(void)
51 {
52 git_reference *ref;
53 git_oid id;
54
55 git_oid_fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
56
57 cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
58 cl_git_pass(git_transaction_lock_ref(g_tx, "HEAD"));
59 cl_git_pass(git_transaction_set_symbolic_target(g_tx, "refs/heads/master", "refs/heads/foo", NULL, NULL));
60 cl_git_pass(git_transaction_set_target(g_tx, "HEAD", &id, NULL, NULL));
61 cl_git_pass(git_transaction_commit(g_tx));
62
63 cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/master"));
64 cl_assert_equal_s("refs/heads/foo", git_reference_symbolic_target(ref));
65 git_reference_free(ref);
66
67 cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
68 cl_assert(!git_oid_cmp(&id, git_reference_target(ref)));
69 git_reference_free(ref);
70 }
71
72 void test_refs_transactions__single_ref_delete(void)
73 {
74 git_reference *ref;
75
76 cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
77 cl_git_pass(git_transaction_remove(g_tx, "refs/heads/master"));
78 cl_git_pass(git_transaction_commit(g_tx));
79
80 cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo, "refs/heads/master"));
81 }
82
83 void test_refs_transactions__single_create(void)
84 {
85 git_reference *ref;
86 const char *name = "refs/heads/new-branch";
87 git_oid id;
88
89 cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo, name));
90
91 cl_git_pass(git_transaction_lock_ref(g_tx, name));
92
93 git_oid_fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
94 cl_git_pass(git_transaction_set_target(g_tx, name, &id, NULL, NULL));
95 cl_git_pass(git_transaction_commit(g_tx));
96
97 cl_git_pass(git_reference_lookup(&ref, g_repo, name));
98 cl_assert(!git_oid_cmp(&id, git_reference_target(ref)));
99 git_reference_free(ref);
100 }
101
102 void test_refs_transactions__unlocked_set(void)
103 {
104 git_oid id;
105
106 cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
107 git_oid_fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
108 cl_git_fail_with(GIT_ENOTFOUND, git_transaction_set_target(g_tx, "refs/heads/foo", &id, NULL, NULL));
109 cl_git_pass(git_transaction_commit(g_tx));
110 }
111
112 void test_refs_transactions__error_on_locking_locked_ref(void)
113 {
114 git_oid id;
115 git_transaction *g_tx_with_lock;
116 git_repository *g_repo_with_locking_tx;
117 const char *g_repo_path = git_repository_path(g_repo);
118
119 /* prepare a separate transaction in another instance of testrepo and lock master */
120 cl_git_pass(git_repository_open(&g_repo_with_locking_tx, g_repo_path));
121 cl_git_pass(git_transaction_new(&g_tx_with_lock, g_repo_with_locking_tx));
122 cl_git_pass(git_transaction_lock_ref(g_tx_with_lock, "refs/heads/master"));
123
124 /* lock reference for set_target */
125 cl_git_pass(git_oid_fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
126 cl_git_fail_with(GIT_ELOCKED, git_transaction_lock_ref(g_tx, "refs/heads/master"));
127 cl_git_fail_with(GIT_ENOTFOUND, git_transaction_set_target(g_tx, "refs/heads/master", &id, NULL, NULL));
128
129 git_transaction_free(g_tx_with_lock);
130 git_repository_free(g_repo_with_locking_tx);
131 }
132
133 void test_refs_transactions__commit_unlocks_unmodified_ref(void)
134 {
135 git_transaction *second_tx;
136
137 cl_git_pass(git_transaction_new(&second_tx, g_repo));
138 cl_git_pass(git_transaction_lock_ref(second_tx, "refs/heads/master"));
139 cl_git_pass(git_transaction_commit(second_tx));
140
141 /* a transaction must now be able to get the lock */
142 cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
143
144 git_transaction_free(second_tx);
145 }
146
147 void test_refs_transactions__free_unlocks_unmodified_ref(void)
148 {
149 git_transaction *second_tx;
150
151 cl_git_pass(git_transaction_new(&second_tx, g_repo));
152 cl_git_pass(git_transaction_lock_ref(second_tx, "refs/heads/master"));
153 git_transaction_free(second_tx);
154
155 /* a transaction must now be able to get the lock */
156 cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
157 }