]> git.proxmox.com Git - libgit2.git/blob - tests/refs/setter.c
b34c71eb529433d4b4f1d3e85397c44c58d7ad47
[libgit2.git] / tests / refs / setter.c
1 #include "clar_libgit2.h"
2
3 #include "repository.h"
4 #include "git2/reflog.h"
5 #include "reflog.h"
6 #include "git2/refs.h"
7
8 static const char *ref_name = "refs/heads/other";
9 static const char *ref_master_name = "refs/heads/master";
10 static const char *ref_test_name = "refs/heads/test";
11
12 static git_repository *g_repo;
13
14 void test_refs_setter__initialize(void)
15 {
16 g_repo = cl_git_sandbox_init("testrepo");
17 }
18
19 void test_refs_setter__cleanup(void)
20 {
21 cl_git_sandbox_cleanup();
22 }
23
24 void test_refs_setter__update_direct(void)
25 {
26 git_reference *ref, *test_ref, *new_ref;
27 git_oid id;
28
29 cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
30 cl_assert(git_reference_type(ref) == GIT_REFERENCE_DIRECT);
31 git_oid_cpy(&id, git_reference_target(ref));
32 git_reference_free(ref);
33
34 cl_git_pass(git_reference_lookup(&test_ref, g_repo, ref_test_name));
35 cl_assert(git_reference_type(test_ref) == GIT_REFERENCE_DIRECT);
36
37 cl_git_pass(git_reference_set_target(&new_ref, test_ref, &id, NULL));
38
39 git_reference_free(test_ref);
40 git_reference_free(new_ref);
41
42 cl_git_pass(git_reference_lookup(&test_ref, g_repo, ref_test_name));
43 cl_assert(git_reference_type(test_ref) == GIT_REFERENCE_DIRECT);
44 cl_assert_equal_oid(&id, git_reference_target(test_ref));
45 git_reference_free(test_ref);
46 }
47
48 void test_refs_setter__update_symbolic(void)
49 {
50 git_reference *head, *new_head;
51
52 cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
53 cl_assert(git_reference_type(head) == GIT_REFERENCE_SYMBOLIC);
54 cl_assert(strcmp(git_reference_symbolic_target(head), ref_master_name) == 0);
55
56 cl_git_pass(git_reference_symbolic_set_target(&new_head, head, ref_test_name, NULL));
57 git_reference_free(new_head);
58 git_reference_free(head);
59
60 cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
61 cl_assert(git_reference_type(head) == GIT_REFERENCE_SYMBOLIC);
62 cl_assert(strcmp(git_reference_symbolic_target(head), ref_test_name) == 0);
63 git_reference_free(head);
64 }
65
66 void test_refs_setter__cant_update_direct_with_symbolic(void)
67 {
68 /* Overwrite an existing object id reference with a symbolic one */
69 git_reference *ref, *new;
70 git_oid id;
71
72 cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
73 cl_assert(git_reference_type(ref) == GIT_REFERENCE_DIRECT);
74 git_oid_cpy(&id, git_reference_target(ref));
75
76 cl_git_fail(git_reference_symbolic_set_target(&new, ref, ref_name, NULL));
77
78 git_reference_free(ref);
79 }
80
81 void test_refs_setter__cant_update_symbolic_with_direct(void)
82 {
83 /* Overwrite an existing symbolic reference with an object id one */
84 git_reference *ref, *new;
85 git_oid id;
86
87 cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
88 cl_assert(git_reference_type(ref) == GIT_REFERENCE_DIRECT);
89 git_oid_cpy(&id, git_reference_target(ref));
90 git_reference_free(ref);
91
92 /* Create the symbolic ref */
93 cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 0, NULL));
94
95 /* Can't set an OID on a direct ref */
96 cl_git_fail(git_reference_set_target(&new, ref, &id, NULL));
97
98 git_reference_free(ref);
99 }