]> git.proxmox.com Git - libgit2.git/blob - tests/status/submodules.c
Merge pull request #1997 from mgbowen/merge-options-init-fix
[libgit2.git] / tests / status / submodules.c
1 #include "clar_libgit2.h"
2 #include "buffer.h"
3 #include "path.h"
4 #include "posix.h"
5 #include "status_helpers.h"
6 #include "../submodule/submodule_helpers.h"
7
8 static git_repository *g_repo = NULL;
9
10 void test_status_submodules__initialize(void)
11 {
12 }
13
14 void test_status_submodules__cleanup(void)
15 {
16 }
17
18 void test_status_submodules__api(void)
19 {
20 git_submodule *sm;
21
22 g_repo = setup_fixture_submodules();
23
24 cl_assert(git_submodule_lookup(NULL, g_repo, "nonexistent") == GIT_ENOTFOUND);
25
26 cl_assert(git_submodule_lookup(NULL, g_repo, "modified") == GIT_ENOTFOUND);
27
28 cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
29 cl_assert(sm != NULL);
30 cl_assert_equal_s("testrepo", git_submodule_name(sm));
31 cl_assert_equal_s("testrepo", git_submodule_path(sm));
32 }
33
34 void test_status_submodules__0(void)
35 {
36 int counts = 0;
37
38 g_repo = setup_fixture_submodules();
39
40 cl_assert(git_path_isdir("submodules/.git"));
41 cl_assert(git_path_isdir("submodules/testrepo/.git"));
42 cl_assert(git_path_isfile("submodules/.gitmodules"));
43
44 cl_git_pass(
45 git_status_foreach(g_repo, cb_status__count, &counts)
46 );
47
48 cl_assert_equal_i(6, counts);
49 }
50
51 static const char *expected_files[] = {
52 ".gitmodules",
53 "added",
54 "deleted",
55 "ignored",
56 "modified",
57 "untracked"
58 };
59
60 static unsigned int expected_status[] = {
61 GIT_STATUS_WT_MODIFIED,
62 GIT_STATUS_INDEX_NEW,
63 GIT_STATUS_INDEX_DELETED,
64 GIT_STATUS_IGNORED,
65 GIT_STATUS_WT_MODIFIED,
66 GIT_STATUS_WT_NEW
67 };
68
69 static int cb_status__match(const char *p, unsigned int s, void *payload)
70 {
71 status_entry_counts *counts = payload;
72 int idx = counts->entry_count++;
73
74 cl_assert_equal_s(counts->expected_paths[idx], p);
75 cl_assert(counts->expected_statuses[idx] == s);
76
77 return 0;
78 }
79
80 void test_status_submodules__1(void)
81 {
82 status_entry_counts counts;
83
84 g_repo = setup_fixture_submodules();
85
86 cl_assert(git_path_isdir("submodules/.git"));
87 cl_assert(git_path_isdir("submodules/testrepo/.git"));
88 cl_assert(git_path_isfile("submodules/.gitmodules"));
89
90 memset(&counts, 0, sizeof(counts));
91 counts.expected_paths = expected_files;
92 counts.expected_statuses = expected_status;
93
94 cl_git_pass(
95 git_status_foreach(g_repo, cb_status__match, &counts)
96 );
97
98 cl_assert_equal_i(6, counts.entry_count);
99 }
100
101 void test_status_submodules__single_file(void)
102 {
103 unsigned int status = 0;
104 g_repo = setup_fixture_submodules();
105 cl_git_pass( git_status_file(&status, g_repo, "testrepo") );
106 cl_assert(!status);
107 }
108
109 void test_status_submodules__moved_head(void)
110 {
111 git_submodule *sm;
112 git_repository *smrepo;
113 git_oid oid;
114 git_status_options opts = GIT_STATUS_OPTIONS_INIT;
115 status_entry_counts counts;
116 static const char *expected_files_with_sub[] = {
117 ".gitmodules",
118 "added",
119 "deleted",
120 "ignored",
121 "modified",
122 "testrepo",
123 "untracked"
124 };
125 static unsigned int expected_status_with_sub[] = {
126 GIT_STATUS_WT_MODIFIED,
127 GIT_STATUS_INDEX_NEW,
128 GIT_STATUS_INDEX_DELETED,
129 GIT_STATUS_IGNORED,
130 GIT_STATUS_WT_MODIFIED,
131 GIT_STATUS_WT_MODIFIED,
132 GIT_STATUS_WT_NEW
133 };
134
135 g_repo = setup_fixture_submodules();
136
137 cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
138 cl_git_pass(git_submodule_open(&smrepo, sm));
139
140 /* move submodule HEAD to c47800c7266a2be04c571c04d5a6614691ea99bd */
141 cl_git_pass(
142 git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
143 cl_git_pass(git_repository_set_head_detached(smrepo, &oid, NULL, NULL));
144
145 /* first do a normal status, which should now include the submodule */
146
147 memset(&counts, 0, sizeof(counts));
148 counts.expected_paths = expected_files_with_sub;
149 counts.expected_statuses = expected_status_with_sub;
150
151 opts.flags = GIT_STATUS_OPT_DEFAULTS;
152
153 cl_git_pass(
154 git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
155 cl_assert_equal_i(7, counts.entry_count);
156
157 /* try again with EXCLUDE_SUBMODULES which should skip it */
158
159 memset(&counts, 0, sizeof(counts));
160 counts.expected_paths = expected_files;
161 counts.expected_statuses = expected_status;
162
163 opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
164
165 cl_git_pass(
166 git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
167 cl_assert_equal_i(6, counts.entry_count);
168
169 git_repository_free(smrepo);
170 }
171
172 void test_status_submodules__dirty_workdir_only(void)
173 {
174 git_status_options opts = GIT_STATUS_OPTIONS_INIT;
175 status_entry_counts counts;
176 static const char *expected_files_with_sub[] = {
177 ".gitmodules",
178 "added",
179 "deleted",
180 "ignored",
181 "modified",
182 "testrepo",
183 "untracked"
184 };
185 static unsigned int expected_status_with_sub[] = {
186 GIT_STATUS_WT_MODIFIED,
187 GIT_STATUS_INDEX_NEW,
188 GIT_STATUS_INDEX_DELETED,
189 GIT_STATUS_IGNORED,
190 GIT_STATUS_WT_MODIFIED,
191 GIT_STATUS_WT_MODIFIED,
192 GIT_STATUS_WT_NEW
193 };
194
195 g_repo = setup_fixture_submodules();
196
197 cl_git_rewritefile("submodules/testrepo/README", "heyheyhey");
198 cl_git_mkfile("submodules/testrepo/all_new.txt", "never seen before");
199
200 /* first do a normal status, which should now include the submodule */
201
202 memset(&counts, 0, sizeof(counts));
203 counts.expected_paths = expected_files_with_sub;
204 counts.expected_statuses = expected_status_with_sub;
205
206 opts.flags = GIT_STATUS_OPT_DEFAULTS;
207
208 cl_git_pass(
209 git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
210 cl_assert_equal_i(7, counts.entry_count);
211
212 /* try again with EXCLUDE_SUBMODULES which should skip it */
213
214 memset(&counts, 0, sizeof(counts));
215 counts.expected_paths = expected_files;
216 counts.expected_statuses = expected_status;
217
218 opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
219
220 cl_git_pass(
221 git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
222 cl_assert_equal_i(6, counts.entry_count);
223 }
224
225 void test_status_submodules__uninitialized(void)
226 {
227 git_repository *cloned_repo;
228 git_status_list *statuslist;
229
230 g_repo = cl_git_sandbox_init("submod2");
231
232 cl_git_pass(git_clone(&cloned_repo, "submod2", "submod2-clone", NULL));
233
234 cl_git_pass(git_status_list_new(&statuslist, cloned_repo, NULL));
235 cl_assert_equal_i(0, git_status_list_entrycount(statuslist));
236
237 git_status_list_free(statuslist);
238 git_repository_free(cloned_repo);
239 cl_git_sandbox_cleanup();
240 }