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