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
));
34 void test_status_submodules__0(void)
38 g_repo
= setup_fixture_submodules();
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"));
45 git_status_foreach(g_repo
, cb_status__count
, &counts
)
48 cl_assert_equal_i(6, counts
);
51 static const char *expected_files
[] = {
60 static unsigned int expected_status
[] = {
61 GIT_STATUS_WT_MODIFIED
,
63 GIT_STATUS_INDEX_DELETED
,
65 GIT_STATUS_WT_MODIFIED
,
69 static int cb_status__match(const char *p
, unsigned int s
, void *payload
)
71 status_entry_counts
*counts
= payload
;
72 int idx
= counts
->entry_count
++;
74 cl_assert_equal_s(counts
->expected_paths
[idx
], p
);
75 cl_assert(counts
->expected_statuses
[idx
] == s
);
80 void test_status_submodules__1(void)
82 status_entry_counts counts
;
84 g_repo
= setup_fixture_submodules();
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"));
90 memset(&counts
, 0, sizeof(counts
));
91 counts
.expected_paths
= expected_files
;
92 counts
.expected_statuses
= expected_status
;
95 git_status_foreach(g_repo
, cb_status__match
, &counts
)
98 cl_assert_equal_i(6, counts
.entry_count
);
101 void test_status_submodules__single_file(void)
103 unsigned int status
= 0;
104 g_repo
= setup_fixture_submodules();
105 cl_git_pass( git_status_file(&status
, g_repo
, "testrepo") );
109 void test_status_submodules__moved_head(void)
112 git_repository
*smrepo
;
114 git_status_options opts
= GIT_STATUS_OPTIONS_INIT
;
115 status_entry_counts counts
;
116 static const char *expected_files_with_sub
[] = {
125 static unsigned int expected_status_with_sub
[] = {
126 GIT_STATUS_WT_MODIFIED
,
127 GIT_STATUS_INDEX_NEW
,
128 GIT_STATUS_INDEX_DELETED
,
130 GIT_STATUS_WT_MODIFIED
,
131 GIT_STATUS_WT_MODIFIED
,
135 g_repo
= setup_fixture_submodules();
137 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "testrepo"));
138 cl_git_pass(git_submodule_open(&smrepo
, sm
));
140 /* move submodule HEAD to c47800c7266a2be04c571c04d5a6614691ea99bd */
142 git_oid_fromstr(&oid
, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
143 cl_git_pass(git_repository_set_head_detached(smrepo
, &oid
, NULL
, NULL
));
145 /* first do a normal status, which should now include the submodule */
147 memset(&counts
, 0, sizeof(counts
));
148 counts
.expected_paths
= expected_files_with_sub
;
149 counts
.expected_statuses
= expected_status_with_sub
;
151 opts
.flags
= GIT_STATUS_OPT_DEFAULTS
;
154 git_status_foreach_ext(g_repo
, &opts
, cb_status__match
, &counts
));
155 cl_assert_equal_i(7, counts
.entry_count
);
157 /* try again with EXCLUDE_SUBMODULES which should skip it */
159 memset(&counts
, 0, sizeof(counts
));
160 counts
.expected_paths
= expected_files
;
161 counts
.expected_statuses
= expected_status
;
163 opts
.flags
= GIT_STATUS_OPT_DEFAULTS
| GIT_STATUS_OPT_EXCLUDE_SUBMODULES
;
166 git_status_foreach_ext(g_repo
, &opts
, cb_status__match
, &counts
));
167 cl_assert_equal_i(6, counts
.entry_count
);
169 git_repository_free(smrepo
);
172 void test_status_submodules__dirty_workdir_only(void)
174 git_status_options opts
= GIT_STATUS_OPTIONS_INIT
;
175 status_entry_counts counts
;
176 static const char *expected_files_with_sub
[] = {
185 static unsigned int expected_status_with_sub
[] = {
186 GIT_STATUS_WT_MODIFIED
,
187 GIT_STATUS_INDEX_NEW
,
188 GIT_STATUS_INDEX_DELETED
,
190 GIT_STATUS_WT_MODIFIED
,
191 GIT_STATUS_WT_MODIFIED
,
195 g_repo
= setup_fixture_submodules();
197 cl_git_rewritefile("submodules/testrepo/README", "heyheyhey");
198 cl_git_mkfile("submodules/testrepo/all_new.txt", "never seen before");
200 /* first do a normal status, which should now include the submodule */
202 memset(&counts
, 0, sizeof(counts
));
203 counts
.expected_paths
= expected_files_with_sub
;
204 counts
.expected_statuses
= expected_status_with_sub
;
206 opts
.flags
= GIT_STATUS_OPT_DEFAULTS
;
209 git_status_foreach_ext(g_repo
, &opts
, cb_status__match
, &counts
));
210 cl_assert_equal_i(7, counts
.entry_count
);
212 /* try again with EXCLUDE_SUBMODULES which should skip it */
214 memset(&counts
, 0, sizeof(counts
));
215 counts
.expected_paths
= expected_files
;
216 counts
.expected_statuses
= expected_status
;
218 opts
.flags
= GIT_STATUS_OPT_DEFAULTS
| GIT_STATUS_OPT_EXCLUDE_SUBMODULES
;
221 git_status_foreach_ext(g_repo
, &opts
, cb_status__match
, &counts
));
222 cl_assert_equal_i(6, counts
.entry_count
);
225 void test_status_submodules__uninitialized(void)
227 git_repository
*cloned_repo
;
228 git_status_list
*statuslist
;
230 g_repo
= cl_git_sandbox_init("submod2");
232 cl_git_pass(git_clone(&cloned_repo
, "submod2", "submod2-clone", NULL
));
234 cl_git_pass(git_status_list_new(&statuslist
, cloned_repo
, NULL
));
235 cl_assert_equal_i(0, git_status_list_entrycount(statuslist
));
237 git_status_list_free(statuslist
);
238 git_repository_free(cloned_repo
);
239 cl_git_sandbox_cleanup();