1 #include "clar_libgit2.h"
6 static void clear_git_env(void)
8 cl_setenv("GIT_DIR", NULL
);
9 cl_setenv("GIT_CEILING_DIRECTORIES", NULL
);
10 cl_setenv("GIT_INDEX_FILE", NULL
);
11 cl_setenv("GIT_NAMESPACE", NULL
);
12 cl_setenv("GIT_OBJECT_DIRECTORY", NULL
);
13 cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL
);
14 cl_setenv("GIT_WORK_TREE", NULL
);
15 cl_setenv("GIT_COMMON_DIR", NULL
);
18 void test_repo_env__initialize(void)
23 void test_repo_env__cleanup(void)
25 cl_git_sandbox_cleanup();
27 if (git_path_isdir("attr"))
28 git_futils_rmdir_r("attr", NULL
, GIT_RMDIR_REMOVE_FILES
);
29 if (git_path_isdir("testrepo.git"))
30 git_futils_rmdir_r("testrepo.git", NULL
, GIT_RMDIR_REMOVE_FILES
);
31 if (git_path_isdir("peeled.git"))
32 git_futils_rmdir_r("peeled.git", NULL
, GIT_RMDIR_REMOVE_FILES
);
37 static int GIT_FORMAT_PRINTF(2, 3) cl_setenv_printf(const char *name
, const char *fmt
, ...)
41 git_buf buf
= GIT_BUF_INIT
;
44 cl_git_pass(git_buf_vprintf(&buf
, fmt
, args
));
47 ret
= cl_setenv(name
, git_buf_cstr(&buf
));
48 git_buf_dispose(&buf
);
52 /* Helper functions for test_repo_open__env, passing through the file and line
53 * from the caller rather than those of the helper. The expression strings
54 * distinguish between the possible failures within the helper. */
56 static void env_pass_(const char *path
, const char *file
, const char *func
, int line
)
59 cl_git_expect(git_repository_open_ext(NULL
, path
, GIT_REPOSITORY_OPEN_FROM_ENV
, NULL
), 0, file
, func
, line
);
60 cl_git_expect(git_repository_open_ext(&repo
, path
, GIT_REPOSITORY_OPEN_FROM_ENV
, NULL
), 0, file
, func
, line
);
61 cl_assert_at_line(git__suffixcmp(git_repository_path(repo
), "attr/.git/") == 0, file
, func
, line
);
62 cl_assert_at_line(git__suffixcmp(git_repository_workdir(repo
), "attr/") == 0, file
, func
, line
);
63 cl_assert_at_line(!git_repository_is_bare(repo
), file
, func
, line
);
64 git_repository_free(repo
);
66 #define env_pass(path) env_pass_((path), __FILE__, __func__, __LINE__)
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)
70 static void env_fail_(const char *path
, const char *file
, const char *func
, int line
)
73 cl_git_fail_at_line(git_repository_open_ext(NULL
, path
, GIT_REPOSITORY_OPEN_FROM_ENV
, NULL
), file
, func
, line
);
74 cl_git_fail_at_line(git_repository_open_ext(&repo
, path
, GIT_REPOSITORY_OPEN_FROM_ENV
, NULL
), file
, func
, line
);
76 #define env_fail(path) env_fail_((path), __FILE__, __func__, __LINE__)
80 void (*passfail_
)(const char *, const char *, const char *, int),
81 const char *file
, const char *func
, int line
)
83 git_buf cwd_buf
= GIT_BUF_INIT
;
84 cl_git_pass(git_path_prettify_dir(&cwd_buf
, ".", NULL
));
85 cl_must_pass(p_chdir(path
));
86 passfail_(NULL
, file
, func
, line
);
87 cl_must_pass(p_chdir(git_buf_cstr(&cwd_buf
)));
88 git_buf_dispose(&cwd_buf
);
90 #define env_cd_pass(path) env_cd_((path), env_pass_, __FILE__, __func__, __LINE__)
91 #define env_cd_fail(path) env_cd_((path), env_fail_, __FILE__, __func__, __LINE__)
93 static void env_check_objects_(bool a
, bool t
, bool p
, const char *file
, const char *func
, int line
)
96 git_oid oid_a
, oid_t
, oid_p
;
98 cl_git_pass(git_oid_fromstr(&oid_a
, "45141a79a77842c59a63229403220a4e4be74e3d"));
99 cl_git_pass(git_oid_fromstr(&oid_t
, "1385f264afb75a56a5bec74243be9b367ba4ca08"));
100 cl_git_pass(git_oid_fromstr(&oid_p
, "0df1a5865c8abfc09f1f2182e6a31be550e99f07"));
101 cl_git_expect(git_repository_open_ext(&repo
, "attr", GIT_REPOSITORY_OPEN_FROM_ENV
, NULL
), 0, file
, func
, line
);
104 cl_git_expect(git_object_lookup(&object
, repo
, &oid_a
, GIT_OBJECT_BLOB
), 0, file
, func
, line
);
105 git_object_free(object
);
107 cl_git_fail_at_line(git_object_lookup(&object
, repo
, &oid_a
, GIT_OBJECT_BLOB
), file
, func
, line
);
111 cl_git_expect(git_object_lookup(&object
, repo
, &oid_t
, GIT_OBJECT_BLOB
), 0, file
, func
, line
);
112 git_object_free(object
);
114 cl_git_fail_at_line(git_object_lookup(&object
, repo
, &oid_t
, GIT_OBJECT_BLOB
), file
, func
, line
);
118 cl_git_expect(git_object_lookup(&object
, repo
, &oid_p
, GIT_OBJECT_COMMIT
), 0, file
, func
, line
);
119 git_object_free(object
);
121 cl_git_fail_at_line(git_object_lookup(&object
, repo
, &oid_p
, GIT_OBJECT_COMMIT
), file
, func
, line
);
124 git_repository_free(repo
);
126 #define env_check_objects(a, t, t2) env_check_objects_((a), (t), (t2), __FILE__, __func__, __LINE__)
128 void test_repo_env__open(void)
130 git_repository
*repo
= NULL
;
131 git_buf repo_dir_buf
= GIT_BUF_INIT
;
132 const char *repo_dir
= NULL
;
133 git_index
*index
= NULL
;
134 const char *t_obj
= "testrepo.git/objects";
135 const char *p_obj
= "peeled.git/objects";
139 cl_fixture_sandbox("attr");
140 cl_fixture_sandbox("testrepo.git");
141 cl_fixture_sandbox("peeled.git");
142 cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
144 cl_git_pass(git_path_prettify_dir(&repo_dir_buf
, "attr", NULL
));
145 repo_dir
= git_buf_cstr(&repo_dir_buf
);
147 /* GIT_DIR that doesn't exist */
148 cl_setenv("GIT_DIR", "does-not-exist");
150 /* Explicit start_path overrides GIT_DIR */
152 env_pass("attr/.git");
153 env_pass("attr/sub");
154 env_pass("attr/sub/sub");
156 /* GIT_DIR with relative paths */
157 cl_setenv("GIT_DIR", "attr/.git");
159 cl_setenv("GIT_DIR", "attr");
161 cl_setenv("GIT_DIR", "attr/sub");
163 cl_setenv("GIT_DIR", "attr/sub/sub");
166 /* GIT_DIR with absolute paths */
167 cl_setenv_printf("GIT_DIR", "%s/.git", repo_dir
);
169 cl_setenv("GIT_DIR", repo_dir
);
171 cl_setenv_printf("GIT_DIR", "%s/sub", repo_dir
);
173 cl_setenv_printf("GIT_DIR", "%s/sub/sub", repo_dir
);
175 cl_setenv("GIT_DIR", NULL
);
177 /* Searching from the current directory */
179 env_cd_pass("attr/.git");
180 env_cd_pass("attr/sub");
181 env_cd_pass("attr/sub/sub");
183 /* A ceiling directory blocks searches from ascending into that
184 * directory, but doesn't block the start_path itself. */
185 cl_setenv("GIT_CEILING_DIRECTORIES", repo_dir
);
187 env_cd_fail("attr/sub");
188 env_cd_fail("attr/sub/sub");
190 cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub", repo_dir
);
192 env_cd_pass("attr/sub");
193 env_cd_fail("attr/sub/sub");
195 /* Multiple ceiling directories */
196 cl_setenv_printf("GIT_CEILING_DIRECTORIES", "123%c%s/sub%cabc",
197 GIT_PATH_LIST_SEPARATOR
, repo_dir
, GIT_PATH_LIST_SEPARATOR
);
199 env_cd_pass("attr/sub");
200 env_cd_fail("attr/sub/sub");
202 cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub",
203 repo_dir
, GIT_PATH_LIST_SEPARATOR
, repo_dir
);
205 env_cd_fail("attr/sub");
206 env_cd_fail("attr/sub/sub");
208 cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub%c%s",
209 repo_dir
, GIT_PATH_LIST_SEPARATOR
, repo_dir
);
211 env_cd_fail("attr/sub");
212 env_cd_fail("attr/sub/sub");
214 cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub/sub",
215 repo_dir
, GIT_PATH_LIST_SEPARATOR
, repo_dir
);
217 env_cd_fail("attr/sub");
218 env_cd_fail("attr/sub/sub");
220 cl_setenv("GIT_CEILING_DIRECTORIES", NULL
);
223 cl_setenv("GIT_INDEX_FILE", cl_fixture("gitgit.index"));
224 cl_git_pass(git_repository_open_ext(&repo
, "attr", GIT_REPOSITORY_OPEN_FROM_ENV
, NULL
));
225 cl_git_pass(git_repository_index(&index
, repo
));
226 cl_assert_equal_s(git_index_path(index
), cl_fixture("gitgit.index"));
227 cl_assert_equal_i(git_index_entrycount(index
), 1437);
228 git_index_free(index
);
229 git_repository_free(repo
);
230 cl_setenv("GIT_INDEX_FILE", NULL
);
233 cl_setenv("GIT_NAMESPACE", "some-namespace");
234 cl_git_pass(git_repository_open_ext(&repo
, "attr", GIT_REPOSITORY_OPEN_FROM_ENV
, NULL
));
235 cl_assert_equal_s(git_repository_get_namespace(repo
), "some-namespace");
236 git_repository_free(repo
);
237 cl_setenv("GIT_NAMESPACE", NULL
);
239 /* Object directories and alternates */
240 env_check_objects(true, false, false);
242 cl_setenv("GIT_OBJECT_DIRECTORY", t_obj
);
243 env_check_objects(false, true, false);
244 cl_setenv("GIT_OBJECT_DIRECTORY", NULL
);
246 cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", t_obj
);
247 env_check_objects(true, true, false);
248 cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL
);
250 cl_setenv("GIT_OBJECT_DIRECTORY", p_obj
);
251 env_check_objects(false, false, true);
252 cl_setenv("GIT_OBJECT_DIRECTORY", NULL
);
254 cl_setenv("GIT_OBJECT_DIRECTORY", t_obj
);
255 cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", p_obj
);
256 env_check_objects(false, true, true);
257 cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL
);
258 cl_setenv("GIT_OBJECT_DIRECTORY", NULL
);
260 cl_setenv_printf("GIT_ALTERNATE_OBJECT_DIRECTORIES",
261 "%s%c%s", t_obj
, GIT_PATH_LIST_SEPARATOR
, p_obj
);
262 env_check_objects(true, true, true);
263 cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL
);
265 cl_setenv_printf("GIT_ALTERNATE_OBJECT_DIRECTORIES",
266 "%s%c%s", p_obj
, GIT_PATH_LIST_SEPARATOR
, t_obj
);
267 env_check_objects(true, true, true);
268 cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL
);
270 cl_fixture_cleanup("peeled.git");
271 cl_fixture_cleanup("testrepo.git");
272 cl_fixture_cleanup("attr");
274 git_buf_dispose(&repo_dir_buf
);