submodules.c 5.47 KB
Newer Older
Russell Belfer committed
1 2 3 4
#include "clar_libgit2.h"
#include "buffer.h"
#include "path.h"
#include "posix.h"
5
#include "status_helpers.h"
Russell Belfer committed
6
#include "../submodule/submodule_helpers.h"
Russell Belfer committed
7 8 9 10 11 12 13 14 15 16 17

static git_repository *g_repo = NULL;

void test_status_submodules__initialize(void)
{
}

void test_status_submodules__cleanup(void)
{
}

18 19 20 21
void test_status_submodules__api(void)
{
	git_submodule *sm;

22 23
	g_repo = setup_fixture_submodules();

24 25 26 27 28 29
	cl_assert(git_submodule_lookup(NULL, g_repo, "nonexistent") == GIT_ENOTFOUND);

	cl_assert(git_submodule_lookup(NULL, g_repo, "modified") == GIT_ENOTFOUND);

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
	cl_assert(sm != NULL);
Russell Belfer committed
30 31
	cl_assert_equal_s("testrepo", git_submodule_name(sm));
	cl_assert_equal_s("testrepo", git_submodule_path(sm));
32 33
}

Russell Belfer committed
34 35 36 37
void test_status_submodules__0(void)
{
	int counts = 0;

38 39
	g_repo = setup_fixture_submodules();

Russell Belfer committed
40 41 42 43 44
	cl_assert(git_path_isdir("submodules/.git"));
	cl_assert(git_path_isdir("submodules/testrepo/.git"));
	cl_assert(git_path_isfile("submodules/.gitmodules"));

	cl_git_pass(
45
		git_status_foreach(g_repo, cb_status__count, &counts)
Russell Belfer committed
46 47
	);

48
	cl_assert_equal_i(6, counts);
Russell Belfer committed
49 50 51 52 53 54 55 56 57 58 59 60
}

static const char *expected_files[] = {
	".gitmodules",
	"added",
	"deleted",
	"ignored",
	"modified",
	"untracked"
};

static unsigned int expected_status[] = {
61
	GIT_STATUS_WT_MODIFIED,
Russell Belfer committed
62 63 64 65 66 67 68
	GIT_STATUS_INDEX_NEW,
	GIT_STATUS_INDEX_DELETED,
	GIT_STATUS_IGNORED,
	GIT_STATUS_WT_MODIFIED,
	GIT_STATUS_WT_NEW
};

69
static int cb_status__match(const char *p, unsigned int s, void *payload)
Russell Belfer committed
70
{
71 72
	status_entry_counts *counts = payload;
	int idx = counts->entry_count++;
Russell Belfer committed
73

74 75
	cl_assert_equal_s(counts->expected_paths[idx], p);
	cl_assert(counts->expected_statuses[idx] == s);
Russell Belfer committed
76 77 78 79 80 81

	return 0;
}

void test_status_submodules__1(void)
{
82
	status_entry_counts counts;
Russell Belfer committed
83

84 85
	g_repo = setup_fixture_submodules();

Russell Belfer committed
86 87 88 89
	cl_assert(git_path_isdir("submodules/.git"));
	cl_assert(git_path_isdir("submodules/testrepo/.git"));
	cl_assert(git_path_isfile("submodules/.gitmodules"));

90 91 92 93
	memset(&counts, 0, sizeof(counts));
	counts.expected_paths = expected_files;
	counts.expected_statuses = expected_status;

Russell Belfer committed
94
	cl_git_pass(
95
		git_status_foreach(g_repo, cb_status__match, &counts)
Russell Belfer committed
96 97
	);

98
	cl_assert_equal_i(6, counts.entry_count);
Russell Belfer committed
99
}
100 101 102

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") );
106
	cl_assert(!status);
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

void test_status_submodules__moved_head(void)
{
	git_submodule *sm;
	git_repository *smrepo;
	git_oid oid;
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	status_entry_counts counts;
	static const char *expected_files_with_sub[] = {
		".gitmodules",
		"added",
		"deleted",
		"ignored",
		"modified",
		"testrepo",
		"untracked"
	};
	static unsigned int expected_status_with_sub[] = {
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_INDEX_NEW,
		GIT_STATUS_INDEX_DELETED,
		GIT_STATUS_IGNORED,
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_WT_NEW
	};

135 136
	g_repo = setup_fixture_submodules();

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
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
	cl_git_pass(git_submodule_open(&smrepo, sm));

	/* move submodule HEAD to c47800c7266a2be04c571c04d5a6614691ea99bd */
	cl_git_pass(
		git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
	cl_git_pass(git_repository_set_head_detached(smrepo, &oid));

	/* first do a normal status, which should now include the submodule */

	memset(&counts, 0, sizeof(counts));
	counts.expected_paths = expected_files_with_sub;
	counts.expected_statuses = expected_status_with_sub;

	opts.flags = GIT_STATUS_OPT_DEFAULTS;

	cl_git_pass(
		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(7, counts.entry_count);

	/* try again with EXCLUDE_SUBMODULES which should skip it */

	memset(&counts, 0, sizeof(counts));
	counts.expected_paths = expected_files;
	counts.expected_statuses = expected_status;

	opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;

	cl_git_pass(
		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(6, counts.entry_count);
168

Edward Thomson committed
169
	git_repository_free(smrepo);
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

void test_status_submodules__dirty_workdir_only(void)
{
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	status_entry_counts counts;
	static const char *expected_files_with_sub[] = {
		".gitmodules",
		"added",
		"deleted",
		"ignored",
		"modified",
		"testrepo",
		"untracked"
	};
	static unsigned int expected_status_with_sub[] = {
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_INDEX_NEW,
		GIT_STATUS_INDEX_DELETED,
		GIT_STATUS_IGNORED,
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_WT_MODIFIED,
		GIT_STATUS_WT_NEW
	};

195 196
	g_repo = setup_fixture_submodules();

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
	cl_git_rewritefile("submodules/testrepo/README", "heyheyhey");
	cl_git_mkfile("submodules/testrepo/all_new.txt", "never seen before");

	/* first do a normal status, which should now include the submodule */

	memset(&counts, 0, sizeof(counts));
	counts.expected_paths = expected_files_with_sub;
	counts.expected_statuses = expected_status_with_sub;

	opts.flags = GIT_STATUS_OPT_DEFAULTS;

	cl_git_pass(
		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(7, counts.entry_count);

	/* try again with EXCLUDE_SUBMODULES which should skip it */

	memset(&counts, 0, sizeof(counts));
	counts.expected_paths = expected_files;
	counts.expected_statuses = expected_status;

	opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;

	cl_git_pass(
		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
	cl_assert_equal_i(6, counts.entry_count);
}