]> git.proxmox.com Git - libgit2.git/blob - tests/refs/branches/iterator.c
e086681e554f9841216a9fe12b4959e5815f16f1
[libgit2.git] / tests / refs / branches / iterator.c
1 #include "clar_libgit2.h"
2 #include "refs.h"
3
4 static git_repository *repo;
5 static git_reference *fake_remote;
6
7 void test_refs_branches_iterator__initialize(void)
8 {
9 git_oid id;
10
11 cl_fixture_sandbox("testrepo.git");
12 cl_git_pass(git_repository_open(&repo, "testrepo.git"));
13
14 cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
15 cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0, NULL));
16 }
17
18 void test_refs_branches_iterator__cleanup(void)
19 {
20 git_reference_free(fake_remote);
21 fake_remote = NULL;
22
23 git_repository_free(repo);
24 repo = NULL;
25
26 cl_fixture_cleanup("testrepo.git");
27
28 cl_git_sandbox_cleanup();
29 }
30
31 static void assert_retrieval(unsigned int flags, unsigned int expected_count)
32 {
33 git_branch_iterator *iter;
34 git_reference *ref;
35 int count = 0, error;
36 git_branch_t type;
37
38 cl_git_pass(git_branch_iterator_new(&iter, repo, flags));
39 while ((error = git_branch_next(&ref, &type, iter)) == 0) {
40 count++;
41 git_reference_free(ref);
42 }
43
44 git_branch_iterator_free(iter);
45 cl_assert_equal_i(error, GIT_ITEROVER);
46 cl_assert_equal_i(expected_count, count);
47 }
48
49 void test_refs_branches_iterator__retrieve_all_branches(void)
50 {
51 assert_retrieval(GIT_BRANCH_ALL, 15);
52 }
53
54 void test_refs_branches_iterator__retrieve_remote_branches(void)
55 {
56 assert_retrieval(GIT_BRANCH_REMOTE, 2);
57 }
58
59 void test_refs_branches_iterator__retrieve_local_branches(void)
60 {
61 assert_retrieval(GIT_BRANCH_LOCAL, 13);
62 }
63
64 struct expectations {
65 const char *branch_name;
66 int encounters;
67 };
68
69 static void assert_branch_has_been_found(struct expectations *findings, const char* expected_branch_name)
70 {
71 int pos = 0;
72
73 for (pos = 0; findings[pos].branch_name; ++pos) {
74 if (strcmp(expected_branch_name, findings[pos].branch_name) == 0) {
75 cl_assert_equal_i(1, findings[pos].encounters);
76 return;
77 }
78 }
79
80 cl_fail("expected branch not found in list.");
81 }
82
83 static void contains_branches(struct expectations exp[], git_branch_iterator *iter)
84 {
85 git_reference *ref;
86 git_branch_t type;
87 int error, pos = 0;
88
89 while ((error = git_branch_next(&ref, &type, iter)) == 0) {
90 for (pos = 0; exp[pos].branch_name; ++pos) {
91 if (strcmp(git_reference_shorthand(ref), exp[pos].branch_name) == 0)
92 exp[pos].encounters++;
93 }
94
95 git_reference_free(ref);
96 }
97
98 cl_assert_equal_i(error, GIT_ITEROVER);
99 }
100
101 /*
102 * $ git branch -r
103 * nulltoken/HEAD -> nulltoken/master
104 * nulltoken/master
105 */
106 void test_refs_branches_iterator__retrieve_remote_symbolic_HEAD_when_present(void)
107 {
108 git_branch_iterator *iter;
109 struct expectations exp[] = {
110 { "nulltoken/HEAD", 0 },
111 { "nulltoken/master", 0 },
112 { NULL, 0 }
113 };
114
115 git_reference_free(fake_remote);
116 cl_git_pass(git_reference_symbolic_create(&fake_remote, repo, "refs/remotes/nulltoken/HEAD", "refs/remotes/nulltoken/master", 0, NULL));
117
118 assert_retrieval(GIT_BRANCH_REMOTE, 3);
119
120 cl_git_pass(git_branch_iterator_new(&iter, repo, GIT_BRANCH_REMOTE));
121 contains_branches(exp, iter);
122 git_branch_iterator_free(iter);
123
124 assert_branch_has_been_found(exp, "nulltoken/HEAD");
125 assert_branch_has_been_found(exp, "nulltoken/master");
126 }
127
128 void test_refs_branches_iterator__mix_of_packed_and_loose(void)
129 {
130 git_branch_iterator *iter;
131 struct expectations exp[] = {
132 { "master", 0 },
133 { "origin/HEAD", 0 },
134 { "origin/master", 0 },
135 { "origin/packed", 0 },
136 { NULL, 0 }
137 };
138 git_repository *r2;
139
140 r2 = cl_git_sandbox_init("testrepo2");
141
142 cl_git_pass(git_branch_iterator_new(&iter, r2, GIT_BRANCH_ALL));
143 contains_branches(exp, iter);
144
145 git_branch_iterator_free(iter);
146
147 assert_branch_has_been_found(exp, "master");
148 assert_branch_has_been_found(exp, "origin/HEAD");
149 assert_branch_has_been_found(exp, "origin/master");
150 assert_branch_has_been_found(exp, "origin/packed");
151 }