status.c 11.8 KB
Newer Older
Russell Belfer committed
1 2 3 4
#include "clar_libgit2.h"
#include "posix.h"
#include "path.h"
#include "submodule_helpers.h"
5
#include "fileops.h"
6
#include "iterator.h"
Russell Belfer committed
7 8 9 10 11

static git_repository *g_repo = NULL;

void test_submodule_status__initialize(void)
{
12
	g_repo = setup_fixture_submod2();
Russell Belfer committed
13 14 15 16 17 18 19 20
}

void test_submodule_status__cleanup(void)
{
}

void test_submodule_status__unchanged(void)
{
21 22 23
	unsigned int status = get_submodule_status(g_repo, "sm_unchanged");
	unsigned int expected =
		GIT_SUBMODULE_STATUS_IN_HEAD |
24 25 26 27
		GIT_SUBMODULE_STATUS_IN_INDEX |
		GIT_SUBMODULE_STATUS_IN_CONFIG |
		GIT_SUBMODULE_STATUS_IN_WD;

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
	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);
Russell Belfer committed
58 59
}

60 61 62
/* 4 values of GIT_SUBMODULE_IGNORE to check */

void test_submodule_status__ignore_none(void)
Russell Belfer committed
63
{
64 65
	unsigned int status;

66
	rm_submodule("sm_unchanged");
67

68 69 70
	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);
71

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

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

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

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

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

87
	status = get_submodule_status(g_repo, "sm_added_and_uncommited");
88 89 90
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0);

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

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

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

	/* remove sm_changed_head from index */
105 106
	rm_submodule_from_index("sm_changed_head");
	status = get_submodule_status(g_repo, "sm_changed_head");
107
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_DELETED) != 0);
Russell Belfer committed
108 109
}

110 111 112 113 114 115 116 117 118 119 120 121 122
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;

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

126 127 128
	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);
129

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

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

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

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

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

145
	status = get_submodule_status(g_repo, "sm_added_and_uncommited");
146 147 148
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0);

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

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

	/* update sm_changed_head in index */
158 159
	add_submodule_to_index("sm_changed_head");
	status = get_submodule_status(g_repo, "sm_changed_head");
160 161 162 163 164 165 166 167
	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;

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

171 172 173
	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);
174

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

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

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

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

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

190
	status = get_submodule_status(g_repo, "sm_added_and_uncommited");
191 192 193
	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0);

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

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

	/* update sm_changed_head in index */
203 204
	add_submodule_to_index("sm_changed_head");
	status = get_submodule_status(g_repo, "sm_changed_head");
205 206 207 208 209 210 211 212
	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;

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

216 217 218
	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);
219

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

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

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

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

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

235
	status = get_submodule_status(g_repo, "sm_added_and_uncommited");
236 237 238
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));

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

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

	/* update sm_changed_head in index */
248 249
	add_submodule_to_index("sm_changed_head");
	status = get_submodule_status(g_repo, "sm_changed_head");
250 251
	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
}
252 253 254 255

typedef struct {
	size_t counter;
	const char **paths;
256
	int *statuses;
257 258 259 260 261 262 263
} submodule_expectations;

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

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

267
	cl_assert_equal_i(exp->statuses[exp->counter], (int)status_flags);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	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",
285 286 287 288
		"not-submodule/",
		"not-submodule/README.txt",
		"not/",
		"not/README.txt",
289 290 291 292 293 294 295 296 297 298
		"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
	};
299 300
	static int expected_flags[] = {
		GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED, /* ".gitmodules" */
301
		-1,					    /* "just_a_dir/" will be skipped */
302 303
		GIT_STATUS_CURRENT,     /* "just_a_dir/contents" */
		GIT_STATUS_CURRENT,	    /* "just_a_file" */
304 305 306 307
		GIT_STATUS_WT_NEW,      /* "not-submodule/" untracked item */
		-1,                     /* "not-submodule/README.txt" */
		GIT_STATUS_WT_NEW,      /* "not/" untracked item */
		-1,                     /* "not/README.txt" */
308 309 310 311 312 313 314 315 316 317 318
		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 };
319 320 321 322 323
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;

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

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

	git_iterator_free(iter);

329 330
	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
		GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
331
		GIT_STATUS_OPT_INCLUDE_IGNORED |
332 333
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
		GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY;
334

335 336
	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, confirm_submodule_status, &exp));
337
}
338 339 340 341 342

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

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

346 347 348 349 350
	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");
351

352
	status = get_submodule_status(g_repo, "sm_unchanged");
353 354 355 356 357 358 359 360
	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);
}