]>
git.proxmox.com Git - libgit2.git/blob - tests/refs/transactions.c
50c102ad093b7821213905d733e14fbfe174db13
1 #include "clar_libgit2.h"
2 #include "git2/transaction.h"
4 static git_repository
*g_repo
;
5 static git_transaction
*g_tx
;
7 void test_refs_transactions__initialize(void)
9 g_repo
= cl_git_sandbox_init("testrepo");
10 cl_git_pass(git_transaction_new(&g_tx
, g_repo
));
13 void test_refs_transactions__cleanup(void)
15 git_transaction_free(g_tx
);
16 cl_git_sandbox_cleanup();
19 void test_refs_transactions__single_ref_oid(void)
24 git_oid_fromstr(&id
, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
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
));
30 cl_git_pass(git_reference_lookup(&ref
, g_repo
, "refs/heads/master"));
32 cl_assert(!git_oid_cmp(&id
, git_reference_target(ref
)));
33 git_reference_free(ref
);
36 void test_refs_transactions__single_ref_symbolic(void)
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
));
44 cl_git_pass(git_reference_lookup(&ref
, g_repo
, "HEAD"));
46 cl_assert_equal_s("refs/heads/foo", git_reference_symbolic_target(ref
));
47 git_reference_free(ref
);
50 void test_refs_transactions__single_ref_mix_types(void)
55 git_oid_fromstr(&id
, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
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
));
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
);
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
);
72 void test_refs_transactions__single_ref_delete(void)
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
));
80 cl_git_fail_with(GIT_ENOTFOUND
, git_reference_lookup(&ref
, g_repo
, "refs/heads/master"));
83 void test_refs_transactions__single_create(void)
86 const char *name
= "refs/heads/new-branch";
89 cl_git_fail_with(GIT_ENOTFOUND
, git_reference_lookup(&ref
, g_repo
, name
));
91 cl_git_pass(git_transaction_lock_ref(g_tx
, name
));
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
));
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
);
102 void test_refs_transactions__unlocked_set(void)
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
));
112 void test_refs_transactions__error_on_locking_locked_ref(void)
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
);
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"));
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
));
129 git_transaction_free(g_tx_with_lock
);
130 git_repository_free(g_repo_with_locking_tx
);
133 void test_refs_transactions__commit_unlocks_unmodified_ref(void)
135 git_transaction
*second_tx
;
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
));
141 /* a transaction must now be able to get the lock */
142 cl_git_pass(git_transaction_lock_ref(g_tx
, "refs/heads/master"));
144 git_transaction_free(second_tx
);
147 void test_refs_transactions__free_unlocks_unmodified_ref(void)
149 git_transaction
*second_tx
;
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
);
155 /* a transaction must now be able to get the lock */
156 cl_git_pass(git_transaction_lock_ref(g_tx
, "refs/heads/master"));