env.c 9.21 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "futils.h"
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include "sysdir.h"
#include <ctype.h>

static void clear_git_env(void)
{
	cl_setenv("GIT_DIR", NULL);
	cl_setenv("GIT_CEILING_DIRECTORIES", NULL);
	cl_setenv("GIT_INDEX_FILE", NULL);
	cl_setenv("GIT_NAMESPACE", NULL);
	cl_setenv("GIT_OBJECT_DIRECTORY", NULL);
	cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
	cl_setenv("GIT_WORK_TREE", NULL);
	cl_setenv("GIT_COMMON_DIR", NULL);
}

void test_repo_env__initialize(void)
{
	clear_git_env();
}

void test_repo_env__cleanup(void)
{
	cl_git_sandbox_cleanup();

27
	if (git_fs_path_isdir("attr"))
28
		git_futils_rmdir_r("attr", NULL, GIT_RMDIR_REMOVE_FILES);
29
	if (git_fs_path_isdir("testrepo.git"))
30
		git_futils_rmdir_r("testrepo.git", NULL, GIT_RMDIR_REMOVE_FILES);
31
	if (git_fs_path_isdir("peeled.git"))
32 33 34 35 36 37 38 39 40
		git_futils_rmdir_r("peeled.git", NULL, GIT_RMDIR_REMOVE_FILES);

	clear_git_env();
}

static int GIT_FORMAT_PRINTF(2, 3) cl_setenv_printf(const char *name, const char *fmt, ...)
{
	int ret;
	va_list args;
41
	git_str buf = GIT_STR_INIT;
42 43

	va_start(args, fmt);
44
	cl_git_pass(git_str_vprintf(&buf, fmt, args));
45 46
	va_end(args);

47 48
	ret = cl_setenv(name, git_str_cstr(&buf));
	git_str_dispose(&buf);
49 50 51 52 53 54 55
	return ret;
}

/* Helper functions for test_repo_open__env, passing through the file and line
 * from the caller rather than those of the helper. The expression strings
 * distinguish between the possible failures within the helper. */

56
static void env_pass_(const char *path, const char *file, const char *func, int line)
57 58
{
	git_repository *repo;
59 60 61 62 63
	cl_git_expect(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), 0, file, func, line);
	cl_git_expect(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), 0, file, func, line);
	cl_assert_at_line(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0, file, func, line);
	cl_assert_at_line(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0, file, func, line);
	cl_assert_at_line(!git_repository_is_bare(repo), file, func, line);
64 65
	git_repository_free(repo);
}
66
#define env_pass(path) env_pass_((path), __FILE__, __func__, __LINE__)
67

68
#define cl_git_fail_at_line(expr, file, func, line) clar__assert((expr) < 0, file, func, line, "Expected function call to fail: " #expr, NULL, 1)
69

70
static void env_fail_(const char *path, const char *file, const char *func, int line)
71 72
{
	git_repository *repo;
73 74
	cl_git_fail_at_line(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, func, line);
	cl_git_fail_at_line(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, func, line);
75
}
76
#define env_fail(path) env_fail_((path), __FILE__, __func__, __LINE__)
77 78 79

static void env_cd_(
	const char *path,
80 81
	void (*passfail_)(const char *, const char *, const char *, int),
	const char *file, const char *func, int line)
82
{
83
	git_str cwd_buf = GIT_STR_INIT;
84
	cl_git_pass(git_fs_path_prettify_dir(&cwd_buf, ".", NULL));
85
	cl_must_pass(p_chdir(path));
86
	passfail_(NULL, file, func, line);
87 88
	cl_must_pass(p_chdir(git_str_cstr(&cwd_buf)));
	git_str_dispose(&cwd_buf);
89
}
90 91
#define env_cd_pass(path) env_cd_((path), env_pass_, __FILE__, __func__, __LINE__)
#define env_cd_fail(path) env_cd_((path), env_fail_, __FILE__, __func__, __LINE__)
92

93
static void env_check_objects_(bool a, bool t, bool p, const char *file, const char *func, int line)
94 95 96 97
{
	git_repository *repo;
	git_oid oid_a, oid_t, oid_p;
	git_object *object;
98 99 100
	cl_git_pass(git_oid__fromstr(&oid_a, "45141a79a77842c59a63229403220a4e4be74e3d", GIT_OID_SHA1));
	cl_git_pass(git_oid__fromstr(&oid_t, "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OID_SHA1));
	cl_git_pass(git_oid__fromstr(&oid_p, "0df1a5865c8abfc09f1f2182e6a31be550e99f07", GIT_OID_SHA1));
101
	cl_git_expect(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL), 0, file, func, line);
102 103

	if (a) {
104
		cl_git_expect(git_object_lookup(&object, repo, &oid_a, GIT_OBJECT_BLOB), 0, file, func, line);
105 106
		git_object_free(object);
	} else {
107
		cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_a, GIT_OBJECT_BLOB), file, func, line);
108 109 110
	}

	if (t) {
111
		cl_git_expect(git_object_lookup(&object, repo, &oid_t, GIT_OBJECT_BLOB), 0, file, func, line);
112 113
		git_object_free(object);
	} else {
114
		cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_t, GIT_OBJECT_BLOB), file, func, line);
115 116 117
	}

	if (p) {
118
		cl_git_expect(git_object_lookup(&object, repo, &oid_p, GIT_OBJECT_COMMIT), 0, file, func, line);
119 120
		git_object_free(object);
	} else {
121
		cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_p, GIT_OBJECT_COMMIT), file, func, line);
122 123 124 125
	}

	git_repository_free(repo);
}
126
#define env_check_objects(a, t, t2) env_check_objects_((a), (t), (t2), __FILE__, __func__, __LINE__)
127 128 129 130

void test_repo_env__open(void)
{
	git_repository *repo = NULL;
131
	git_str repo_dir_buf = GIT_STR_INIT;
132 133 134 135 136 137 138 139 140 141 142 143
	const char *repo_dir = NULL;
	git_index *index = NULL;
	const char *t_obj = "testrepo.git/objects";
	const char *p_obj = "peeled.git/objects";

	clear_git_env();

	cl_fixture_sandbox("attr");
	cl_fixture_sandbox("testrepo.git");
	cl_fixture_sandbox("peeled.git");
	cl_git_pass(p_rename("attr/.gitted", "attr/.git"));

144
	cl_git_pass(git_fs_path_prettify_dir(&repo_dir_buf, "attr", NULL));
145
	repo_dir = git_str_cstr(&repo_dir_buf);
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

	/* GIT_DIR that doesn't exist */
	cl_setenv("GIT_DIR", "does-not-exist");
	env_fail(NULL);
	/* Explicit start_path overrides GIT_DIR */
	env_pass("attr");
	env_pass("attr/.git");
	env_pass("attr/sub");
	env_pass("attr/sub/sub");

	/* GIT_DIR with relative paths */
	cl_setenv("GIT_DIR", "attr/.git");
	env_pass(NULL);
	cl_setenv("GIT_DIR", "attr");
	env_fail(NULL);
	cl_setenv("GIT_DIR", "attr/sub");
	env_fail(NULL);
	cl_setenv("GIT_DIR", "attr/sub/sub");
	env_fail(NULL);

	/* GIT_DIR with absolute paths */
	cl_setenv_printf("GIT_DIR", "%s/.git", repo_dir);
	env_pass(NULL);
	cl_setenv("GIT_DIR", repo_dir);
	env_fail(NULL);
	cl_setenv_printf("GIT_DIR", "%s/sub", repo_dir);
	env_fail(NULL);
	cl_setenv_printf("GIT_DIR", "%s/sub/sub", repo_dir);
	env_fail(NULL);
	cl_setenv("GIT_DIR", NULL);

	/* Searching from the current directory */
	env_cd_pass("attr");
	env_cd_pass("attr/.git");
	env_cd_pass("attr/sub");
	env_cd_pass("attr/sub/sub");

	/* A ceiling directory blocks searches from ascending into that
	 * directory, but doesn't block the start_path itself. */
	cl_setenv("GIT_CEILING_DIRECTORIES", repo_dir);
	env_cd_pass("attr");
	env_cd_fail("attr/sub");
	env_cd_fail("attr/sub/sub");

	cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub", repo_dir);
	env_cd_pass("attr");
	env_cd_pass("attr/sub");
	env_cd_fail("attr/sub/sub");

	/* Multiple ceiling directories */
	cl_setenv_printf("GIT_CEILING_DIRECTORIES", "123%c%s/sub%cabc",
		GIT_PATH_LIST_SEPARATOR, repo_dir, GIT_PATH_LIST_SEPARATOR);
	env_cd_pass("attr");
	env_cd_pass("attr/sub");
	env_cd_fail("attr/sub/sub");

	cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub",
		repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
	env_cd_pass("attr");
	env_cd_fail("attr/sub");
	env_cd_fail("attr/sub/sub");

	cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub%c%s",
		repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
	env_cd_pass("attr");
	env_cd_fail("attr/sub");
	env_cd_fail("attr/sub/sub");

	cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub/sub",
		repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
	env_cd_pass("attr");
	env_cd_fail("attr/sub");
	env_cd_fail("attr/sub/sub");

	cl_setenv("GIT_CEILING_DIRECTORIES", NULL);

	/* Index files */
	cl_setenv("GIT_INDEX_FILE", cl_fixture("gitgit.index"));
	cl_git_pass(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL));
	cl_git_pass(git_repository_index(&index, repo));
	cl_assert_equal_s(git_index_path(index), cl_fixture("gitgit.index"));
	cl_assert_equal_i(git_index_entrycount(index), 1437);
	git_index_free(index);
	git_repository_free(repo);
	cl_setenv("GIT_INDEX_FILE", NULL);

	/* Namespaces */
	cl_setenv("GIT_NAMESPACE", "some-namespace");
	cl_git_pass(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL));
	cl_assert_equal_s(git_repository_get_namespace(repo), "some-namespace");
	git_repository_free(repo);
	cl_setenv("GIT_NAMESPACE", NULL);

	/* Object directories and alternates */
	env_check_objects(true, false, false);

	cl_setenv("GIT_OBJECT_DIRECTORY", t_obj);
	env_check_objects(false, true, false);
	cl_setenv("GIT_OBJECT_DIRECTORY", NULL);

	cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", t_obj);
	env_check_objects(true, true, false);
	cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);

	cl_setenv("GIT_OBJECT_DIRECTORY", p_obj);
	env_check_objects(false, false, true);
	cl_setenv("GIT_OBJECT_DIRECTORY", NULL);

	cl_setenv("GIT_OBJECT_DIRECTORY", t_obj);
	cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", p_obj);
	env_check_objects(false, true, true);
	cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
	cl_setenv("GIT_OBJECT_DIRECTORY", NULL);

	cl_setenv_printf("GIT_ALTERNATE_OBJECT_DIRECTORIES",
			"%s%c%s", t_obj, GIT_PATH_LIST_SEPARATOR, p_obj);
	env_check_objects(true, true, true);
	cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);

	cl_setenv_printf("GIT_ALTERNATE_OBJECT_DIRECTORIES",
			"%s%c%s", p_obj, GIT_PATH_LIST_SEPARATOR, t_obj);
	env_check_objects(true, true, true);
	cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);

	cl_fixture_cleanup("peeled.git");
	cl_fixture_cleanup("testrepo.git");
	cl_fixture_cleanup("attr");

274
	git_str_dispose(&repo_dir_buf);
275 276 277

	clear_git_env();
}