Skip to content

Commit e02f567

Browse files
author
Edward Thomson
committed
repo::open: remove dead code, free buffers
1 parent 378d12a commit e02f567

File tree

2 files changed

+277
-256
lines changed

2 files changed

+277
-256
lines changed

tests/repo/env.c

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
#include "clar_libgit2.h"
2+
#include "fileops.h"
3+
#include "sysdir.h"
4+
#include <ctype.h>
5+
6+
static void clear_git_env(void)
7+
{
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);
16+
}
17+
18+
void test_repo_env__initialize(void)
19+
{
20+
clear_git_env();
21+
}
22+
23+
void test_repo_env__cleanup(void)
24+
{
25+
cl_git_sandbox_cleanup();
26+
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);
33+
34+
clear_git_env();
35+
}
36+
37+
static int GIT_FORMAT_PRINTF(2, 3) cl_setenv_printf(const char *name, const char *fmt, ...)
38+
{
39+
int ret;
40+
va_list args;
41+
git_buf buf = GIT_BUF_INIT;
42+
43+
va_start(args, fmt);
44+
cl_git_pass(git_buf_vprintf(&buf, fmt, args));
45+
va_end(args);
46+
47+
ret = cl_setenv(name, git_buf_cstr(&buf));
48+
git_buf_free(&buf);
49+
return ret;
50+
}
51+
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. */
55+
56+
static void env_pass_(const char *path, const char *file, int line)
57+
{
58+
git_repository *repo;
59+
cl_git_pass_(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
60+
cl_git_pass_(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
61+
cl_assert_at_line(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0, file, line);
62+
cl_assert_at_line(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0, file, line);
63+
cl_assert_at_line(!git_repository_is_bare(repo), file, line);
64+
git_repository_free(repo);
65+
}
66+
#define env_pass(path) env_pass_((path), __FILE__, __LINE__)
67+
68+
#define cl_git_fail_at_line(expr, file, line) clar__assert((expr) < 0, file, line, "Expected function call to fail: " #expr, NULL, 1)
69+
70+
static void env_fail_(const char *path, const char *file, int line)
71+
{
72+
git_repository *repo;
73+
cl_git_fail_at_line(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
74+
cl_git_fail_at_line(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
75+
}
76+
#define env_fail(path) env_fail_((path), __FILE__, __LINE__)
77+
78+
static void env_cd_(
79+
const char *path,
80+
void (*passfail_)(const char *, const char *, int),
81+
const char *file, int line)
82+
{
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, line);
87+
cl_must_pass(p_chdir(git_buf_cstr(&cwd_buf)));
88+
git_buf_free(&cwd_buf);
89+
}
90+
#define env_cd_pass(path) env_cd_((path), env_pass_, __FILE__, __LINE__)
91+
#define env_cd_fail(path) env_cd_((path), env_fail_, __FILE__, __LINE__)
92+
93+
static void env_check_objects_(bool a, bool t, bool p, const char *file, int line)
94+
{
95+
git_repository *repo;
96+
git_oid oid_a, oid_t, oid_p;
97+
git_object *object;
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_pass_(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
102+
103+
if (a) {
104+
cl_git_pass_(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), file, line);
105+
git_object_free(object);
106+
} else {
107+
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), file, line);
108+
}
109+
110+
if (t) {
111+
cl_git_pass_(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), file, line);
112+
git_object_free(object);
113+
} else {
114+
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), file, line);
115+
}
116+
117+
if (p) {
118+
cl_git_pass_(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), file, line);
119+
git_object_free(object);
120+
} else {
121+
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), file, line);
122+
}
123+
124+
git_repository_free(repo);
125+
}
126+
#define env_check_objects(a, t, t2) env_check_objects_((a), (t), (t2), __FILE__, __LINE__)
127+
128+
void test_repo_env__open(void)
129+
{
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";
136+
137+
clear_git_env();
138+
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"));
143+
144+
cl_git_pass(git_path_prettify_dir(&repo_dir_buf, "attr", NULL));
145+
repo_dir = git_buf_cstr(&repo_dir_buf);
146+
147+
/* GIT_DIR that doesn't exist */
148+
cl_setenv("GIT_DIR", "does-not-exist");
149+
env_fail(NULL);
150+
/* Explicit start_path overrides GIT_DIR */
151+
env_pass("attr");
152+
env_pass("attr/.git");
153+
env_pass("attr/sub");
154+
env_pass("attr/sub/sub");
155+
156+
/* GIT_DIR with relative paths */
157+
cl_setenv("GIT_DIR", "attr/.git");
158+
env_pass(NULL);
159+
cl_setenv("GIT_DIR", "attr");
160+
env_fail(NULL);
161+
cl_setenv("GIT_DIR", "attr/sub");
162+
env_fail(NULL);
163+
cl_setenv("GIT_DIR", "attr/sub/sub");
164+
env_fail(NULL);
165+
166+
/* GIT_DIR with absolute paths */
167+
cl_setenv_printf("GIT_DIR", "%s/.git", repo_dir);
168+
env_pass(NULL);
169+
cl_setenv("GIT_DIR", repo_dir);
170+
env_fail(NULL);
171+
cl_setenv_printf("GIT_DIR", "%s/sub", repo_dir);
172+
env_fail(NULL);
173+
cl_setenv_printf("GIT_DIR", "%s/sub/sub", repo_dir);
174+
env_fail(NULL);
175+
cl_setenv("GIT_DIR", NULL);
176+
177+
/* Searching from the current directory */
178+
env_cd_pass("attr");
179+
env_cd_pass("attr/.git");
180+
env_cd_pass("attr/sub");
181+
env_cd_pass("attr/sub/sub");
182+
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);
186+
env_cd_pass("attr");
187+
env_cd_fail("attr/sub");
188+
env_cd_fail("attr/sub/sub");
189+
190+
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub", repo_dir);
191+
env_cd_pass("attr");
192+
env_cd_pass("attr/sub");
193+
env_cd_fail("attr/sub/sub");
194+
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);
198+
env_cd_pass("attr");
199+
env_cd_pass("attr/sub");
200+
env_cd_fail("attr/sub/sub");
201+
202+
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub",
203+
repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
204+
env_cd_pass("attr");
205+
env_cd_fail("attr/sub");
206+
env_cd_fail("attr/sub/sub");
207+
208+
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub%c%s",
209+
repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
210+
env_cd_pass("attr");
211+
env_cd_fail("attr/sub");
212+
env_cd_fail("attr/sub/sub");
213+
214+
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub/sub",
215+
repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
216+
env_cd_pass("attr");
217+
env_cd_fail("attr/sub");
218+
env_cd_fail("attr/sub/sub");
219+
220+
cl_setenv("GIT_CEILING_DIRECTORIES", NULL);
221+
222+
/* Index files */
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);
231+
232+
/* Namespaces */
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);
238+
239+
/* Object directories and alternates */
240+
env_check_objects(true, false, false);
241+
242+
cl_setenv("GIT_OBJECT_DIRECTORY", t_obj);
243+
env_check_objects(false, true, false);
244+
cl_setenv("GIT_OBJECT_DIRECTORY", NULL);
245+
246+
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", t_obj);
247+
env_check_objects(true, true, false);
248+
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
249+
250+
cl_setenv("GIT_OBJECT_DIRECTORY", p_obj);
251+
env_check_objects(false, false, true);
252+
cl_setenv("GIT_OBJECT_DIRECTORY", NULL);
253+
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);
259+
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);
264+
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);
269+
270+
cl_fixture_cleanup("peeled.git");
271+
cl_fixture_cleanup("testrepo.git");
272+
cl_fixture_cleanup("attr");
273+
274+
git_buf_free(&repo_dir_buf);
275+
276+
clear_git_env();
277+
}

0 commit comments

Comments
 (0)