summaryrefslogtreecommitdiff
path: root/tests/submodule/status.c
blob: 6efae35c65460e81a9957564f838de65069392e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "clar_libgit2.h"
#include "posix.h"
#include "path.h"
#include "submodule_helpers.h"
#include "fileops.h"
#include "iterator.h"

static git_repository *g_repo = NULL;

void test_submodule_status__initialize(void)
{
	g_repo = setup_fixture_submod2();
}

void test_submodule_status__cleanup(void)
{
}

void test_submodule_status__unchanged(void)
{
	unsigned int status = get_submodule_status(g_repo, "sm_unchanged");
	unsigned int expected =
		GIT_SUBMODULE_STATUS_IN_HEAD |
		GIT_SUBMODULE_STATUS_IN_INDEX |
		GIT_SUBMODULE_STATUS_IN_CONFIG |
		GIT_SUBMODULE_STATUS_IN_WD;

	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
	cl_assert(expected == status);
}

static void rm_submodule(const char *name)
{
	git_buf path = GIT_BUF_INIT;
	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), name));
	cl_git_pass(git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES));
	git_buf_free(&path);
}

static void add_submodule_to_index(const char *name)
{
	git_submodule *sm;
	cl_git_pass(git_submodule_lookup(&sm, g_repo, name));
	cl_git_pass(git_submodule_add_to_index(sm, true));
	git_submodule_free(sm);
}

static void rm_submodule_from_index(const char *name)
{
	git_index *index;
	size_t pos;

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_assert(!git_index_find(&pos, index, name));
	cl_git_pass(git_index_remove(index, name, 0));
	cl_git_pass(git_index_write(index));
	git_index_free(index);
}

/* 4 values of GIT_SUBMODULE_IGNORE to check */

void test_submodule_status__ignore_none(void)
{
	unsigned int status;

	rm_submodule("sm_unchanged");

	refute_submodule_exists(g_repo, "just_a_dir", GIT_ENOTFOUND);
	refute_submodule_exists(g_repo, "not-submodule", GIT_EEXISTS);
	refute_submodule_exists(g_repo, "not", GIT_EEXISTS);

	status = get_submodule_status(g_repo, "sm_changed_index");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) != 0);

	status = get_submodule_status(g_repo, "sm_changed_head");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);

	status = get_submodule_status(g_repo, "sm_changed_file");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) != 0);

	status = get_submodule_status(g_repo, "sm_changed_untracked_file");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNTRACKED) != 0);

	status = get_submodule_status(g_repo, "sm_missing_commits");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);

	status = get_submodule_status(g_repo, "sm_added_and_uncommited");
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0);

	/* removed sm_unchanged for deleted workdir */
	status = get_submodule_status(g_repo, "sm_unchanged");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0);

	/* now mkdir sm_unchanged to test uninitialized */
	cl_git_pass(git_futils_mkdir("sm_unchanged", "submod2", 0755, 0));
	status = get_submodule_status(g_repo, "sm_unchanged");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0);

	/* update sm_changed_head in index */
	add_submodule_to_index("sm_changed_head");
	status = get_submodule_status(g_repo, "sm_changed_head");
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0);

	/* remove sm_changed_head from index */
	rm_submodule_from_index("sm_changed_head");
	status = get_submodule_status(g_repo, "sm_changed_head");
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_DELETED) != 0);
}

static int set_sm_ignore(git_submodule *sm, const char *name, void *payload)
{
	git_submodule_ignore_t ignore = *(git_submodule_ignore_t *)payload;
	GIT_UNUSED(name);
	git_submodule_set_ignore(sm, ignore);
	return 0;
}

void test_submodule_status__ignore_untracked(void)
{
	unsigned int status;
	git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_UNTRACKED;

	rm_submodule("sm_unchanged");
	cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign));

	refute_submodule_exists(g_repo, "just_a_dir", GIT_ENOTFOUND);
	refute_submodule_exists(g_repo, "not-submodule", GIT_EEXISTS);
	refute_submodule_exists(g_repo, "not", GIT_EEXISTS);

	status = get_submodule_status(g_repo, "sm_changed_index");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) != 0);

	status = get_submodule_status(g_repo, "sm_changed_head");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);

	status = get_submodule_status(g_repo, "sm_changed_file");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) != 0);

	status = get_submodule_status(g_repo, "sm_changed_untracked_file");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	status = get_submodule_status(g_repo, "sm_missing_commits");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);

	status = get_submodule_status(g_repo, "sm_added_and_uncommited");
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0);

	/* removed sm_unchanged for deleted workdir */
	status = get_submodule_status(g_repo, "sm_unchanged");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0);

	/* now mkdir sm_unchanged to test uninitialized */
	cl_git_pass(git_futils_mkdir("sm_unchanged", "submod2", 0755, 0));
	status = get_submodule_status(g_repo, "sm_unchanged");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0);

	/* update sm_changed_head in index */
	add_submodule_to_index("sm_changed_head");
	status = get_submodule_status(g_repo, "sm_changed_head");
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0);
}

void test_submodule_status__ignore_dirty(void)
{
	unsigned int status;
	git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_DIRTY;

	rm_submodule("sm_unchanged");
	cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign));

	refute_submodule_exists(g_repo, "just_a_dir", GIT_ENOTFOUND);
	refute_submodule_exists(g_repo, "not-submodule", GIT_EEXISTS);
	refute_submodule_exists(g_repo, "not", GIT_EEXISTS);

	status = get_submodule_status(g_repo, "sm_changed_index");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	status = get_submodule_status(g_repo, "sm_changed_head");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);

	status = get_submodule_status(g_repo, "sm_changed_file");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	status = get_submodule_status(g_repo, "sm_changed_untracked_file");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	status = get_submodule_status(g_repo, "sm_missing_commits");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);

	status = get_submodule_status(g_repo, "sm_added_and_uncommited");
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0);

	/* removed sm_unchanged for deleted workdir */
	status = get_submodule_status(g_repo, "sm_unchanged");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0);

	/* now mkdir sm_unchanged to test uninitialized */
	cl_git_pass(git_futils_mkdir("sm_unchanged", "submod2", 0755, 0));
	status = get_submodule_status(g_repo, "sm_unchanged");
	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0);

	/* update sm_changed_head in index */
	add_submodule_to_index("sm_changed_head");
	status = get_submodule_status(g_repo, "sm_changed_head");
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0);
}

void test_submodule_status__ignore_all(void)
{
	unsigned int status;
	git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_ALL;

	rm_submodule("sm_unchanged");
	cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign));

	refute_submodule_exists(g_repo, "just_a_dir", GIT_ENOTFOUND);
	refute_submodule_exists(g_repo, "not-submodule", GIT_EEXISTS);
	refute_submodule_exists(g_repo, "not", GIT_EEXISTS);

	status = get_submodule_status(g_repo, "sm_changed_index");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	status = get_submodule_status(g_repo, "sm_changed_head");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	status = get_submodule_status(g_repo, "sm_changed_file");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	status = get_submodule_status(g_repo, "sm_changed_untracked_file");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	status = get_submodule_status(g_repo, "sm_missing_commits");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	status = get_submodule_status(g_repo, "sm_added_and_uncommited");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	/* removed sm_unchanged for deleted workdir */
	status = get_submodule_status(g_repo, "sm_unchanged");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	/* now mkdir sm_unchanged to test uninitialized */
	cl_git_pass(git_futils_mkdir("sm_unchanged", "submod2", 0755, 0));
	status = get_submodule_status(g_repo, "sm_unchanged");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	/* update sm_changed_head in index */
	add_submodule_to_index("sm_changed_head");
	status = get_submodule_status(g_repo, "sm_changed_head");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
}

typedef struct {
	size_t counter;
	const char **paths;
	int *statuses;
} submodule_expectations;

static int confirm_submodule_status(
	const char *path, unsigned int status_flags, void *payload)
{
	submodule_expectations *exp = payload;

	while (exp->statuses[exp->counter] < 0)
		exp->counter++;

	cl_assert_equal_i(exp->statuses[exp->counter], (int)status_flags);
	cl_assert_equal_s(exp->paths[exp->counter++], path);

	GIT_UNUSED(status_flags);

	return 0;
}

void test_submodule_status__iterator(void)
{
	git_iterator *iter;
	const git_index_entry *entry;
	size_t i;
	static const char *expected[] = {
		".gitmodules",
		"just_a_dir/",
		"just_a_dir/contents",
		"just_a_file",
		"not-submodule/",
		"not-submodule/README.txt",
		"not/",
		"not/README.txt",
		"README.txt",
		"sm_added_and_uncommited",
		"sm_changed_file",
		"sm_changed_head",
		"sm_changed_index",
		"sm_changed_untracked_file",
		"sm_missing_commits",
		"sm_unchanged",
		NULL
	};
	static int expected_flags[] = {
		GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED, /* ".gitmodules" */
		-1,					    /* "just_a_dir/" will be skipped */
		GIT_STATUS_CURRENT,     /* "just_a_dir/contents" */
		GIT_STATUS_CURRENT,	    /* "just_a_file" */
		GIT_STATUS_WT_NEW,      /* "not-submodule/" untracked item */
		-1,                     /* "not-submodule/README.txt" */
		GIT_STATUS_WT_NEW,      /* "not/" untracked item */
		-1,                     /* "not/README.txt" */
		GIT_STATUS_CURRENT,     /* "README.txt */
		GIT_STATUS_INDEX_NEW,   /* "sm_added_and_uncommited" */
		GIT_STATUS_WT_MODIFIED, /* "sm_changed_file" */
		GIT_STATUS_WT_MODIFIED, /* "sm_changed_head" */
		GIT_STATUS_WT_MODIFIED, /* "sm_changed_index" */
		GIT_STATUS_WT_MODIFIED, /* "sm_changed_untracked_file" */
		GIT_STATUS_WT_MODIFIED, /* "sm_missing_commits" */
		GIT_STATUS_CURRENT,     /* "sm_unchanged" */
		0
	};
	submodule_expectations exp = { 0, expected, expected_flags };
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	git_index *index;

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_iterator_for_workdir(&iter, g_repo, index, NULL,
		GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));

	for (i = 0; !git_iterator_advance(&entry, iter); ++i)
		cl_assert_equal_s(expected[i], entry->path);

	git_iterator_free(iter);
	git_index_free(index);

	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
		GIT_STATUS_OPT_INCLUDE_IGNORED |
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
		GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY;

	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, confirm_submodule_status, &exp));
}

void test_submodule_status__untracked_dirs_containing_ignored_files(void)
{
	unsigned int status, expected;

	cl_git_append2file(
		"submod2/.git/modules/sm_unchanged/info/exclude", "\n*.ignored\n");

	cl_git_pass(
		git_futils_mkdir("sm_unchanged/directory", "submod2", 0755, 0));
	cl_git_mkfile(
		"submod2/sm_unchanged/directory/i_am.ignored",
		"ignore this file, please\n");

	status = get_submodule_status(g_repo, "sm_unchanged");
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

	expected = GIT_SUBMODULE_STATUS_IN_HEAD |
		GIT_SUBMODULE_STATUS_IN_INDEX |
		GIT_SUBMODULE_STATUS_IN_CONFIG |
		GIT_SUBMODULE_STATUS_IN_WD;
	cl_assert(status == expected);
}