1 #include "clar_libgit2.h"
5 #include "status_helpers.h"
6 #include "../submodule/submodule_helpers.h"
8 static git_repository
*g_repo
= NULL
;
10 void test_status_submodules__initialize(void)
14 void test_status_submodules__cleanup(void)
18 void test_status_submodules__api(void)
22 g_repo
= setup_fixture_submodules();
24 cl_assert(git_submodule_lookup(NULL
, g_repo
, "nonexistent") == GIT_ENOTFOUND
);
26 cl_assert(git_submodule_lookup(NULL
, g_repo
, "modified") == GIT_ENOTFOUND
);
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 git_submodule_free(sm
);
35 void test_status_submodules__0(void)
39 g_repo
= setup_fixture_submodules();
41 cl_assert(git_path_isdir("submodules/.git"));
42 cl_assert(git_path_isdir("submodules/testrepo/.git"));
43 cl_assert(git_path_isfile("submodules/.gitmodules"));
46 git_status_foreach(g_repo
, cb_status__count
, &counts
)
49 cl_assert_equal_i(6, counts
);
52 static const char *expected_files
[] = {
61 static unsigned int expected_status
[] = {
62 GIT_STATUS_WT_MODIFIED
,
64 GIT_STATUS_INDEX_DELETED
,
66 GIT_STATUS_WT_MODIFIED
,
70 static int cb_status__match(const char *p
, unsigned int s
, void *payload
)
72 status_entry_counts
*counts
= payload
;
73 int idx
= counts
->entry_count
++;
75 cl_assert_equal_s(counts
->expected_paths
[idx
], p
);
76 cl_assert(counts
->expected_statuses
[idx
] == s
);
81 void test_status_submodules__1(void)
83 status_entry_counts counts
;
85 g_repo
= setup_fixture_submodules();
87 cl_assert(git_path_isdir("submodules/.git"));
88 cl_assert(git_path_isdir("submodules/testrepo/.git"));
89 cl_assert(git_path_isfile("submodules/.gitmodules"));
91 memset(&counts
, 0, sizeof(counts
));
92 counts
.expected_paths
= expected_files
;
93 counts
.expected_statuses
= expected_status
;
96 git_status_foreach(g_repo
, cb_status__match
, &counts
)
99 cl_assert_equal_i(6, counts
.entry_count
);
102 void test_status_submodules__single_file(void)
104 unsigned int status
= 0;
105 g_repo
= setup_fixture_submodules();
106 cl_git_pass( git_status_file(&status
, g_repo
, "testrepo") );
110 void test_status_submodules__moved_head(void)
113 git_repository
*smrepo
;
115 git_status_options opts
= GIT_STATUS_OPTIONS_INIT
;
116 status_entry_counts counts
;
117 static const char *expected_files_with_sub
[] = {
126 static unsigned int expected_status_with_sub
[] = {
127 GIT_STATUS_WT_MODIFIED
,
128 GIT_STATUS_INDEX_NEW
,
129 GIT_STATUS_INDEX_DELETED
,
131 GIT_STATUS_WT_MODIFIED
,
132 GIT_STATUS_WT_MODIFIED
,
136 g_repo
= setup_fixture_submodules();
138 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "testrepo"));
139 cl_git_pass(git_submodule_open(&smrepo
, sm
));
140 git_submodule_free(sm
);
142 /* move submodule HEAD to c47800c7266a2be04c571c04d5a6614691ea99bd */
144 git_oid_fromstr(&oid
, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
145 cl_git_pass(git_repository_set_head_detached(smrepo
, &oid
, NULL
, NULL
));
147 /* first do a normal status, which should now include the submodule */
149 memset(&counts
, 0, sizeof(counts
));
150 counts
.expected_paths
= expected_files_with_sub
;
151 counts
.expected_statuses
= expected_status_with_sub
;
153 opts
.flags
= GIT_STATUS_OPT_DEFAULTS
;
156 git_status_foreach_ext(g_repo
, &opts
, cb_status__match
, &counts
));
157 cl_assert_equal_i(7, counts
.entry_count
);
159 /* try again with EXCLUDE_SUBMODULES which should skip it */
161 memset(&counts
, 0, sizeof(counts
));
162 counts
.expected_paths
= expected_files
;
163 counts
.expected_statuses
= expected_status
;
165 opts
.flags
= GIT_STATUS_OPT_DEFAULTS
| GIT_STATUS_OPT_EXCLUDE_SUBMODULES
;
168 git_status_foreach_ext(g_repo
, &opts
, cb_status__match
, &counts
));
169 cl_assert_equal_i(6, counts
.entry_count
);
171 git_repository_free(smrepo
);
174 void test_status_submodules__dirty_workdir_only(void)
176 git_status_options opts
= GIT_STATUS_OPTIONS_INIT
;
177 status_entry_counts counts
;
178 static const char *expected_files_with_sub
[] = {
187 static unsigned int expected_status_with_sub
[] = {
188 GIT_STATUS_WT_MODIFIED
,
189 GIT_STATUS_INDEX_NEW
,
190 GIT_STATUS_INDEX_DELETED
,
192 GIT_STATUS_WT_MODIFIED
,
193 GIT_STATUS_WT_MODIFIED
,
197 g_repo
= setup_fixture_submodules();
199 cl_git_rewritefile("submodules/testrepo/README", "heyheyhey");
200 cl_git_mkfile("submodules/testrepo/all_new.txt", "never seen before");
202 /* first do a normal status, which should now include the submodule */
204 memset(&counts
, 0, sizeof(counts
));
205 counts
.expected_paths
= expected_files_with_sub
;
206 counts
.expected_statuses
= expected_status_with_sub
;
208 opts
.flags
= GIT_STATUS_OPT_DEFAULTS
;
211 git_status_foreach_ext(g_repo
, &opts
, cb_status__match
, &counts
));
212 cl_assert_equal_i(7, counts
.entry_count
);
214 /* try again with EXCLUDE_SUBMODULES which should skip it */
216 memset(&counts
, 0, sizeof(counts
));
217 counts
.expected_paths
= expected_files
;
218 counts
.expected_statuses
= expected_status
;
220 opts
.flags
= GIT_STATUS_OPT_DEFAULTS
| GIT_STATUS_OPT_EXCLUDE_SUBMODULES
;
223 git_status_foreach_ext(g_repo
, &opts
, cb_status__match
, &counts
));
224 cl_assert_equal_i(6, counts
.entry_count
);
227 void test_status_submodules__uninitialized(void)
229 git_repository
*cloned_repo
;
230 git_status_list
*statuslist
;
232 g_repo
= cl_git_sandbox_init("submod2");
234 cl_git_pass(git_clone(&cloned_repo
, "submod2", "submod2-clone", NULL
));
236 cl_git_pass(git_status_list_new(&statuslist
, cloned_repo
, NULL
));
237 cl_assert_equal_i(0, git_status_list_entrycount(statuslist
));
239 git_status_list_free(statuslist
);
240 git_repository_free(cloned_repo
);
241 cl_git_sandbox_cleanup();