summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRussell Belfer <arrbee@arrbee.com>2011-11-30 11:27:15 -0800
committerRussell Belfer <arrbee@arrbee.com>2011-12-07 23:08:15 -0800
commit97769280ba9938ae27f6e06cbd0d5e8a768a86b9 (patch)
tree4fe43e99acb55f904f6b586bd7c5158610f9512f /tests
parenta22b14d32dd8d5f06f121aa154d45bac3b10a305 (diff)
downloadlibgit2-97769280ba9938ae27f6e06cbd0d5e8a768a86b9.tar.gz
Use git_buf for path storage instead of stack-based buffers
This converts virtually all of the places that allocate GIT_PATH_MAX buffers on the stack for manipulating paths to use git_buf objects instead. The patch is pretty careful not to touch the public API for libgit2, so there are a few places that still use GIT_PATH_MAX. This extends and changes some details of the git_buf implementation to add a couple of extra functions and to make error handling easier. This includes serious alterations to all the path.c functions, and several of the fileops.c ones, too. Also, there are a number of new functions that parallel existing ones except that use a git_buf instead of a stack-based buffer (such as git_config_find_global_r that exists alongsize git_config_find_global). This also modifies the win32 version of p_realpath to allocate whatever buffer size is needed to accommodate the realpath instead of hardcoding a GIT_PATH_MAX limit, but that change needs to be tested still.
Diffstat (limited to 'tests')
-rw-r--r--tests/t00-core.c144
-rw-r--r--tests/t10-refs.c80
-rw-r--r--tests/t12-repo.c43
-rw-r--r--tests/t18-status.c7
-rw-r--r--tests/test_helpers.c92
-rw-r--r--tests/test_helpers.h2
6 files changed, 212 insertions, 156 deletions
diff --git a/tests/t00-core.c b/tests/t00-core.c
index 16a5c6f9..708a889d 100644
--- a/tests/t00-core.c
+++ b/tests/t00-core.c
@@ -105,14 +105,15 @@ END_TEST
BEGIN_TEST(path0, "get the dirname of a path")
- char dir[64], *dir2;
+ git_buf dir = GIT_BUF_INIT;
+ char *dir2;
#define DIRNAME_TEST(A, B) { \
- must_be_true(git_path_dirname_r(dir, sizeof(dir), A) >= 0); \
- must_be_true(strcmp(dir, B) == 0); \
+ must_be_true(git_path_dirname_r(&dir, A) >= 0); \
+ must_be_true(strcmp(B, dir.ptr) == 0); \
must_be_true((dir2 = git_path_dirname(A)) != NULL); \
must_be_true(strcmp(dir2, B) == 0); \
- git__free(dir2); \
+ git__free(dir2); \
}
DIRNAME_TEST(NULL, ".");
@@ -131,16 +132,18 @@ BEGIN_TEST(path0, "get the dirname of a path")
#undef DIRNAME_TEST
+ git_buf_free(&dir);
END_TEST
BEGIN_TEST(path1, "get the base name of a path")
- char base[64], *base2;
+ git_buf base = GIT_BUF_INIT;
+ char *base2;
#define BASENAME_TEST(A, B) { \
- must_be_true(git_path_basename_r(base, sizeof(base), A) >= 0); \
- must_be_true(strcmp(base, B) == 0); \
+ must_be_true(git_path_basename_r(&base, A) >= 0); \
+ must_be_true(strcmp(B, base.ptr) == 0); \
must_be_true((base2 = git_path_basename(A)) != NULL); \
- must_be_true(strcmp(base2, B) == 0); \
+ must_be_true(strcmp(base2, B) == 0); \
git__free(base2); \
}
@@ -156,6 +159,7 @@ BEGIN_TEST(path1, "get the base name of a path")
#undef BASENAME_TEST
+ git_buf_free(&base);
END_TEST
BEGIN_TEST(path2, "get the latest component in a path")
@@ -184,9 +188,13 @@ END_TEST
static int ensure_joinpath(const char *path_a, const char *path_b, const char *expected_path)
{
- char joined_path[GIT_PATH_MAX];
- git_path_join(joined_path, path_a, path_b);
- return strcmp(joined_path, expected_path) == 0 ? GIT_SUCCESS : GIT_ERROR;
+ int error = GIT_SUCCESS;
+ git_buf joined_path = GIT_BUF_INIT;
+ if (!(error = git_buf_joinpath(&joined_path, path_a, path_b)))
+ error = strcmp(joined_path.ptr, expected_path) == 0 ?
+ GIT_SUCCESS : GIT_ERROR;
+ git_buf_free(&joined_path);
+ return error;
}
BEGIN_TEST(path5, "properly join path components")
@@ -206,9 +214,14 @@ END_TEST
static int ensure_joinpath_n(const char *path_a, const char *path_b, const char *path_c, const char *path_d, const char *expected_path)
{
- char joined_path[GIT_PATH_MAX];
- git_path_join_n(joined_path, 4, path_a, path_b, path_c, path_d);
- return strcmp(joined_path, expected_path) == 0 ? GIT_SUCCESS : GIT_ERROR;
+ int error = GIT_SUCCESS;
+ git_buf joined_path = GIT_BUF_INIT;
+ if (!(error = git_buf_join_n(&joined_path, '/', 4,
+ path_a, path_b, path_c, path_d)))
+ error = strcmp(joined_path.ptr, expected_path) == 0 ?
+ GIT_SUCCESS : GIT_ERROR;
+ git_buf_free(&joined_path);
+ return error;
}
BEGIN_TEST(path6, "properly join path components for more than one path")
@@ -228,10 +241,10 @@ typedef struct name_data {
typedef struct walk_data {
char *sub; /* sub-directory name */
name_data *names; /* name state data */
+ git_buf path; /* buffer to store path */
} walk_data;
-static char path_buffer[GIT_PATH_MAX];
static char *top_dir = "dir-walk";
static walk_data *state_loc;
@@ -260,7 +273,9 @@ static int setup(walk_data *d)
if (p_mkdir(d->sub, 0777) < 0)
return error("can't mkdir(\"%s\")", d->sub);
- strcpy(path_buffer, d->sub);
+ if (git_buf_sets(&d->path, d->sub) < 0)
+ return error("can't allocate space for \"%s\"", d->sub);
+
state_loc = d;
for (n = d->names; n->name; n++) {
@@ -278,6 +293,8 @@ static int knockdown(walk_data *d)
{
name_data *n;
+ git_buf_free(&d->path);
+
for (n = d->names; n->name; n++) {
if (p_unlink(n->name) < 0)
return error("can't unlink(\"%s\")", n->name);
@@ -308,7 +325,7 @@ static int check_counts(walk_data *d)
return ret;
}
-static int one_entry(void *state, char *path)
+static int one_entry(void *state, git_buf *path)
{
walk_data *d = (walk_data *) state;
name_data *n;
@@ -316,11 +333,11 @@ static int one_entry(void *state, char *path)
if (state != state_loc)
return GIT_ERROR;
- if (path != path_buffer)
+ if (path != &d->path)
return GIT_ERROR;
for (n = d->names; n->name; n++) {
- if (!strcmp(n->name, path)) {
+ if (!strcmp(n->name, path->ptr)) {
n->count++;
return 0;
}
@@ -338,15 +355,14 @@ static name_data dot_names[] = {
};
static walk_data dot = {
".",
- dot_names
+ dot_names,
+ GIT_BUF_INIT
};
BEGIN_TEST(dirent0, "make sure that the '.' folder is not traversed")
-
must_pass(setup(&dot));
- must_pass(git_futils_direach(path_buffer,
- sizeof(path_buffer),
+ must_pass(git_futils_direach(&dot.path,
one_entry,
&dot));
@@ -363,15 +379,15 @@ static name_data sub_names[] = {
};
static walk_data sub = {
"sub",
- sub_names
+ sub_names,
+ GIT_BUF_INIT
};
BEGIN_TEST(dirent1, "traverse a subfolder")
must_pass(setup(&sub));
- must_pass(git_futils_direach(path_buffer,
- sizeof(path_buffer),
+ must_pass(git_futils_direach(&sub.path,
one_entry,
&sub));
@@ -382,15 +398,15 @@ END_TEST
static walk_data sub_slash = {
"sub/",
- sub_names
+ sub_names,
+ GIT_BUF_INIT
};
BEGIN_TEST(dirent2, "traverse a slash-terminated subfolder")
must_pass(setup(&sub_slash));
- must_pass(git_futils_direach(path_buffer,
- sizeof(path_buffer),
+ must_pass(git_futils_direach(&sub_slash.path,
one_entry,
&sub_slash));
@@ -404,10 +420,11 @@ static name_data empty_names[] = {
};
static walk_data empty = {
"empty",
- empty_names
+ empty_names,
+ GIT_BUF_INIT
};
-static int dont_call_me(void *GIT_UNUSED(state), char *GIT_UNUSED(path))
+static int dont_call_me(void *GIT_UNUSED(state), git_buf *GIT_UNUSED(path))
{
GIT_UNUSED_ARG(state)
GIT_UNUSED_ARG(path)
@@ -418,16 +435,14 @@ BEGIN_TEST(dirent3, "make sure that empty folders are not traversed")
must_pass(setup(&empty));
- must_pass(git_futils_direach(path_buffer,
- sizeof(path_buffer),
+ must_pass(git_futils_direach(&empty.path,
one_entry,
&empty));
must_pass(check_counts(&empty));
/* make sure callback not called */
- must_pass(git_futils_direach(path_buffer,
- sizeof(path_buffer),
+ must_pass(git_futils_direach(&empty.path,
dont_call_me,
&empty));
@@ -444,15 +459,15 @@ static name_data odd_names[] = {
};
static walk_data odd = {
"odd",
- odd_names
+ odd_names,
+ GIT_BUF_INIT
};
BEGIN_TEST(dirent4, "make sure that strange looking filenames ('..c') are traversed")
must_pass(setup(&odd));
- must_pass(git_futils_direach(path_buffer,
- sizeof(path_buffer),
+ must_pass(git_futils_direach(&odd.path,
one_entry,
&odd));
@@ -508,32 +523,24 @@ static char *empty_tmp_dir = "test_gitfo_rmdir_recurs_test";
static int setup_empty_tmp_dir(void)
{
- char path[GIT_PATH_MAX];
-
- if (p_mkdir(empty_tmp_dir, 0777))
- return -1;
-
- git_path_join(path, empty_tmp_dir, "/one");
- if (p_mkdir(path, 0777))
- return -1;
-
- git_path_join(path, empty_tmp_dir, "/one/two_one");
- if (p_mkdir(path, 0777))
- return -1;
-
- git_path_join(path, empty_tmp_dir, "/one/two_two");
- if (p_mkdir(path, 0777))
- return -1;
-
- git_path_join(path, empty_tmp_dir, "/one/two_two/three");
- if (p_mkdir(path, 0777))
- return -1;
-
- git_path_join(path, empty_tmp_dir, "/two");
- if (p_mkdir(path, 0777))
- return -1;
-
- return 0;
+ git_buf path = GIT_BUF_INIT;
+
+ int error =
+ p_mkdir(empty_tmp_dir, 0777) ||
+ git_buf_joinpath(&path, empty_tmp_dir, "/one") ||
+ p_mkdir(path.ptr, 0777) ||
+ git_buf_joinpath(&path, empty_tmp_dir, "/one/two_one") ||
+ p_mkdir(path.ptr, 0777) ||
+ git_buf_joinpath(&path, empty_tmp_dir, "/one/two_two") ||
+ p_mkdir(path.ptr, 0777) ||
+ git_buf_joinpath(&path, empty_tmp_dir, "/one/two_two/three") ||
+ p_mkdir(path.ptr, 0777) ||
+ git_buf_joinpath(&path, empty_tmp_dir, "/two") ||
+ p_mkdir(path.ptr, 0777);
+
+ git_buf_free(&path);
+
+ return error ? -1 : 0;
}
BEGIN_TEST(rmdir0, "make sure empty dir can be deleted recusively")
@@ -542,17 +549,18 @@ BEGIN_TEST(rmdir0, "make sure empty dir can be deleted recusively")
END_TEST
BEGIN_TEST(rmdir1, "make sure non-empty dir cannot be deleted recusively")
- char file[GIT_PATH_MAX];
+ git_buf file = GIT_BUF_INIT;
int fd;
must_pass(setup_empty_tmp_dir());
- git_path_join(file, empty_tmp_dir, "/two/file.txt");
- fd = p_creat(file, 0777);
+ must_pass(git_buf_joinpath(&file, empty_tmp_dir, "/two/file.txt"));
+ fd = p_creat(file.ptr, 0777);
must_pass(fd);
must_pass(p_close(fd));
must_fail(git_futils_rmdir_r(empty_tmp_dir, 0));
- must_pass(p_unlink(file));
+ must_pass(p_unlink(file.ptr));
must_pass(git_futils_rmdir_r(empty_tmp_dir, 0));
+ git_buf_free(&file);
END_TEST
BEGIN_TEST(strtol0, "parsing out 32 integers from a string")
diff --git a/tests/t10-refs.c b/tests/t10-refs.c
index 3cfba582..e8c7b7e0 100644
--- a/tests/t10-refs.c
+++ b/tests/t10-refs.c
@@ -37,7 +37,7 @@ BEGIN_TEST(readtag0, "lookup a loose tag reference")
git_repository *repo;
git_reference *reference;
git_object *object;
- char ref_name_from_tag_name[GIT_REFNAME_MAX];
+ git_buf ref_name_from_tag_name = GIT_BUF_INIT;
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
@@ -51,8 +51,9 @@ BEGIN_TEST(readtag0, "lookup a loose tag reference")
must_be_true(git_object_type(object) == GIT_OBJ_TAG);
/* Ensure the name of the tag matches the name of the reference */
- git_path_join(ref_name_from_tag_name, GIT_REFS_TAGS_DIR, git_tag_name((git_tag *)object));
- must_be_true(strcmp(ref_name_from_tag_name, loose_tag_ref_name) == 0);
+ must_pass(git_buf_joinpath(&ref_name_from_tag_name, GIT_REFS_TAGS_DIR, git_tag_name((git_tag *)object)));
+ must_be_true(strcmp(ref_name_from_tag_name.ptr, loose_tag_ref_name) == 0);
+ git_buf_free(&ref_name_from_tag_name);
git_object_free(object);
git_repository_free(repo);
@@ -227,7 +228,7 @@ BEGIN_TEST(create0, "create a new symbolic reference")
git_reference *new_reference, *looked_up_ref, *resolved_ref;
git_repository *repo, *repo2;
git_oid id;
- char ref_path[GIT_PATH_MAX];
+ git_buf ref_path = GIT_BUF_INIT;
const char *new_head_tracker = "another-head-tracker";
@@ -236,7 +237,8 @@ BEGIN_TEST(create0, "create a new symbolic reference")
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
/* Retrieve the physical path to the symbolic ref for further cleaning */
- git_path_join(ref_path, repo->path_repository, new_head_tracker);
+ must_pass(git_buf_joinpath(&ref_path, repo->path_repository, new_head_tracker));
+ git_buf_free(&ref_path);
/* Create and write the new symbolic reference */
must_pass(git_reference_create_symbolic(&new_reference, repo, new_head_tracker, current_head_target, 0));
@@ -276,7 +278,7 @@ BEGIN_TEST(create1, "create a deep symbolic reference")
git_reference *new_reference, *looked_up_ref, *resolved_ref;
git_repository *repo;
git_oid id;
- char ref_path[GIT_PATH_MAX];
+ git_buf ref_path = GIT_BUF_INIT;
const char *new_head_tracker = "deep/rooted/tracker";
@@ -284,7 +286,7 @@ BEGIN_TEST(create1, "create a deep symbolic reference")
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
- git_path_join(ref_path, repo->path_repository, new_head_tracker);
+ must_pass(git_buf_joinpath(&ref_path, repo->path_repository, new_head_tracker));
must_pass(git_reference_create_symbolic(&new_reference, repo, new_head_tracker, current_head_target, 0));
must_pass(git_reference_lookup(&looked_up_ref, repo, new_head_tracker));
must_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
@@ -295,13 +297,14 @@ BEGIN_TEST(create1, "create a deep symbolic reference")
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
git_reference_free(resolved_ref);
+ git_buf_free(&ref_path);
END_TEST
BEGIN_TEST(create2, "create a new OID reference")
git_reference *new_reference, *looked_up_ref;
git_repository *repo, *repo2;
git_oid id;
- char ref_path[GIT_PATH_MAX];
+ git_buf ref_path = GIT_BUF_INIT;
const char *new_head = "refs/heads/new-head";
@@ -310,7 +313,7 @@ BEGIN_TEST(create2, "create a new OID reference")
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
/* Retrieve the physical path to the symbolic ref for further cleaning */
- git_path_join(ref_path, repo->path_repository, new_head);
+ must_pass(git_buf_joinpath(&ref_path, repo->path_repository, new_head));
/* Create and write the new object id reference */
must_pass(git_reference_create_oid(&new_reference, repo, new_head, &id, 0));
@@ -337,6 +340,7 @@ BEGIN_TEST(create2, "create a new OID reference")
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
+ git_buf_free(&ref_path);
END_TEST
BEGIN_TEST(create3, "Can not create a new OID reference which targets at an unknown id")
@@ -491,12 +495,13 @@ END_TEST
BEGIN_TEST(pack0, "create a packfile for an empty folder")
git_repository *repo;
- char temp_path[GIT_PATH_MAX];
+ git_buf temp_path = GIT_BUF_INIT;
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
- git_path_join_n(temp_path, 3, repo->path_repository, GIT_REFS_HEADS_DIR, "empty_dir");
- must_pass(git_futils_mkdir_r(temp_path, GIT_REFS_DIR_MODE));
+ must_pass(git_buf_join_n(&temp_path, '/', 3, repo->path_repository, GIT_REFS_HEADS_DIR, "empty_dir"));
+ must_pass(git_futils_mkdir_r(temp_path.ptr, NULL, GIT_REFS_DIR_MODE));
+ git_buf_free(&temp_path);
must_pass(git_reference_packall(repo));
@@ -506,7 +511,7 @@ END_TEST
BEGIN_TEST(pack1, "create a packfile from all the loose rn a repo")
git_repository *repo;
git_reference *reference;
- char temp_path[GIT_PATH_MAX];
+ git_buf temp_path = GIT_BUF_INIT;
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
@@ -524,8 +529,8 @@ BEGIN_TEST(pack1, "create a packfile from all the loose rn a repo")
must_pass(git_reference_packall(repo));
/* Ensure the packed-refs file exists */
- git_path_join(temp_path, repo->path_repository, GIT_PACKEDREFS_FILE);
- must_pass(git_futils_exists(temp_path));
+ must_pass(git_buf_joinpath(&temp_path, repo->path_repository, GIT_PACKEDREFS_FILE));
+ must_pass(git_futils_exists(temp_path.ptr));
/* Ensure the known ref can still be looked up but is now packed */
must_pass(git_reference_lookup(&reference, repo, loose_tag_ref_name));
@@ -533,25 +538,26 @@ BEGIN_TEST(pack1, "create a packfile from all the loose rn a repo")
must_be_true(strcmp(reference->name, loose_tag_ref_name) == 0);
/* Ensure the known ref has been removed from the loose folder structure */
- git_path_join(temp_path, repo->path_repository, loose_tag_ref_name);
- must_pass(!git_futils_exists(temp_path));
+ must_pass(git_buf_joinpath(&temp_path, repo->path_repository, loose_tag_ref_name));
+ must_pass(!git_futils_exists(temp_path.ptr));
close_temp_repo(repo);
git_reference_free(reference);
+ git_buf_free(&temp_path);
END_TEST
BEGIN_TEST(rename0, "rename a loose reference")
git_reference *looked_up_ref, *another_looked_up_ref;
git_repository *repo;
- char temp_path[GIT_PATH_MAX];
+ git_buf temp_path = GIT_BUF_INIT;
const char *new_name = "refs/tags/Nemo/knows/refs.kung-fu";
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
/* Ensure the ref doesn't exist on the file system */
- git_path_join(temp_path, repo->path_repository, new_name);
- must_pass(!git_futils_exists(temp_path));
+ must_pass(git_buf_joinpath(&temp_path, repo->path_repository, new_name));
+ must_pass(!git_futils_exists(temp_path.ptr));
/* Retrieval of the reference to rename */
must_pass(git_reference_lookup(&looked_up_ref, repo, loose_tag_ref_name));
@@ -575,26 +581,27 @@ BEGIN_TEST(rename0, "rename a loose reference")
must_be_true(git_reference_is_packed(looked_up_ref) == 0);
/* ...and the ref can be found in the file system */
- git_path_join(temp_path, repo->path_repository, new_name);
- must_pass(git_futils_exists(temp_path));
+ must_pass(git_buf_joinpath(&temp_path, repo->path_repository, new_name));
+ must_pass(git_futils_exists(temp_path.ptr));
close_temp_repo(repo);
git_reference_free(looked_up_ref);
git_reference_free(another_looked_up_ref);
+ git_buf_free(&temp_path);
END_TEST
BEGIN_TEST(rename1, "rename a packed reference (should make it loose)")
git_reference *looked_up_ref, *another_looked_up_ref;
git_repository *repo;
- char temp_path[GIT_PATH_MAX];
+ git_buf temp_path = GIT_BUF_INIT;
const char *brand_new_name = "refs/heads/brand_new_name";
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
/* Ensure the ref doesn't exist on the file system */
- git_path_join(temp_path, repo->path_repository, packed_head_name);
- must_pass(!git_futils_exists(temp_path));
+ must_pass(git_buf_joinpath(&temp_path, repo->path_repository, packed_head_name));
+ must_pass(!git_futils_exists(temp_path.ptr));
/* The reference can however be looked-up... */
must_pass(git_reference_lookup(&looked_up_ref, repo, packed_head_name));
@@ -618,26 +625,27 @@ BEGIN_TEST(rename1, "rename a packed reference (should make it loose)")
must_be_true(git_reference_is_packed(looked_up_ref) == 0);
/* ...and the ref now happily lives in the file system */
- git_path_join(temp_path, repo->path_repository, brand_new_name);
- must_pass(git_futils_exists(temp_path));
+ must_pass(git_buf_joinpath(&temp_path, repo->path_repository, brand_new_name));
+ must_pass(git_futils_exists(temp_path.ptr));
close_temp_repo(repo);
git_reference_free(looked_up_ref);
git_reference_free(another_looked_up_ref);
+ git_buf_free(&temp_path);
END_TEST
BEGIN_TEST(rename2, "renaming a packed reference does not pack another reference which happens to be in both loose and pack state")
git_reference *looked_up_ref, *another_looked_up_ref;
git_repository *repo;
- char temp_path[GIT_PATH_MAX];
+ git_buf temp_path = GIT_BUF_INIT;
const char *brand_new_name = "refs/heads/brand_new_name";
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
/* Ensure the other reference exists on the file system */
- git_path_join(temp_path, repo->path_repository, packed_test_head_name);
- must_pass(git_futils_exists(temp_path));
+ must_pass(git_buf_joinpath(&temp_path, repo->path_repository, packed_test_head_name));
+ must_pass(git_futils_exists(temp_path.ptr));
/* Lookup the other reference */
must_pass(git_reference_lookup(&another_looked_up_ref, repo, packed_test_head_name));
@@ -662,12 +670,13 @@ BEGIN_TEST(rename2, "renaming a packed reference does not pack another reference
must_be_true(git_reference_is_packed(another_looked_up_ref) == 0);
/* Ensure the other ref still exists on the file system */
- must_pass(git_futils_exists(temp_path));
+ must_pass(git_futils_exists(temp_path.ptr));
close_temp_repo(repo);
git_reference_free(looked_up_ref);
git_reference_free(another_looked_up_ref);
+ git_buf_free(&temp_path);
END_TEST
BEGIN_TEST(rename3, "can not rename a reference with the name of an existing reference")
@@ -884,13 +893,13 @@ END_TEST
BEGIN_TEST(delete0, "deleting a ref which is both packed and loose should remove both tracks in the filesystem")
git_reference *looked_up_ref, *another_looked_up_ref;
git_repository *repo;
- char temp_path[GIT_PATH_MAX];
+ git_buf temp_path = GIT_BUF_INIT;
must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
/* Ensure the loose reference exists on the file system */
- git_path_join(temp_path, repo->path_repository, packed_test_head_name);
- must_pass(git_futils_exists(temp_path));
+ must_pass(git_buf_joinpath(&temp_path, repo->path_repository, packed_test_head_name));
+ must_pass(git_futils_exists(temp_path.ptr));
/* Lookup the reference */
must_pass(git_reference_lookup(&looked_up_ref, repo, packed_test_head_name));
@@ -905,11 +914,12 @@ BEGIN_TEST(delete0, "deleting a ref which is both packed and loose should remove
must_fail(git_reference_lookup(&another_looked_up_ref, repo, packed_test_head_name));
/* Ensure the loose reference doesn't exist any longer on the file system */
- must_pass(!git_futils_exists(temp_path));
+ must_pass(!git_futils_exists(temp_path.ptr));
close_temp_repo(repo);
git_reference_free(another_looked_up_ref);
+ git_buf_free(&temp_path);
END_TEST
BEGIN_TEST(delete1, "can delete a just packed reference")
diff --git a/tests/t12-repo.c b/tests/t12-repo.c
index acf8b743..0b245656 100644
--- a/tests/t12-repo.c
+++ b/tests/t12-repo.c
@@ -87,31 +87,37 @@ static int write_file(const char *path, const char *content)
}
//no check is performed on ceiling_dirs length, so be sure it's long enough
-static int append_ceiling_dir(char *ceiling_dirs, const char *path)
+static int append_ceiling_dir(git_buf *ceiling_dirs, const char *path)
{
- int len = strlen(ceiling_dirs);
+ git_buf pretty_path = GIT_BUF_INIT;
int error;
+ char ceiling_separator[2] = { GIT_PATH_LIST_SEPARATOR, '\0' };
- error = git_path_prettify_dir(ceiling_dirs + len + (len ? 1 : 0), path, NULL);
+ error = git_path_prettify_dir(&pretty_path, path, NULL);
if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to append ceiling directory.");
- if (len)
- ceiling_dirs[len] = GIT_PATH_LIST_SEPARATOR;
+ if (ceiling_dirs->size > 0)
+ git_buf_puts(ceiling_dirs, ceiling_separator);
+ git_buf_puts(ceiling_dirs, pretty_path.ptr);
- return GIT_SUCCESS;
+ git_buf_free(&pretty_path);
+
+ return git_buf_lasterror(ceiling_dirs);
}
BEGIN_TEST(discover0, "test discover")
git_repository *repo;
- char ceiling_dirs[GIT_PATH_MAX * 2] = "";
+ git_buf ceiling_dirs_buf = GIT_BUF_INIT;
+ const char *ceiling_dirs;
char repository_path[GIT_PATH_MAX];
char sub_repository_path[GIT_PATH_MAX];
char found_path[GIT_PATH_MAX];
const mode_t mode = 0777;
- git_futils_mkdir_r(DISCOVER_FOLDER, mode);
- must_pass(append_ceiling_dir(ceiling_dirs, TEMP_REPO_FOLDER));
+ git_futils_mkdir_r(DISCOVER_FOLDER, NULL, mode);
+ must_pass(append_ceiling_dir(&ceiling_dirs_buf, TEMP_REPO_FOLDER));
+ ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf);
must_be_true(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs) == GIT_ENOTAREPO);
@@ -120,15 +126,15 @@ BEGIN_TEST(discover0, "test discover")
git_repository_free(repo);
must_pass(git_repository_init(&repo, SUB_REPOSITORY_FOLDER, 0));
- must_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, mode));
+ must_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode));
must_pass(git_repository_discover(sub_repository_path, sizeof(sub_repository_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
- must_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, mode));
+ must_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode));
must_pass(ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, sub_repository_path));
must_pass(ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path));
must_pass(ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, sub_repository_path));
- must_pass(git_futils_mkdir_r(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, mode));
+ must_pass(git_futils_mkdir_r(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, NULL, mode));
must_pass(write_file(REPOSITORY_ALTERNATE_FOLDER "/" DOT_GIT, "gitdir: ../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT));
must_pass(write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/" DOT_GIT, "gitdir: ../../../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT));
must_pass(write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB "/" DOT_GIT, "gitdir: ../../../../"));
@@ -137,20 +143,22 @@ BEGIN_TEST(discover0, "test discover")
must_pass(ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path));
must_pass(ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path));
- must_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER1, mode));
+ must_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER1, NULL, mode));
must_pass(write_file(ALTERNATE_MALFORMED_FOLDER1 "/" DOT_GIT, "Anything but not gitdir:"));
- must_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER2, mode));
+ must_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER2, NULL, mode));
must_pass(write_file(ALTERNATE_MALFORMED_FOLDER2 "/" DOT_GIT, "gitdir:"));
- must_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER3, mode));
+ must_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER3, NULL, mode));
must_pass(write_file(ALTERNATE_MALFORMED_FOLDER3 "/" DOT_GIT, "gitdir: \n\n\n"));
- must_pass(git_futils_mkdir_r(ALTERNATE_NOT_FOUND_FOLDER, mode));
+ must_pass(git_futils_mkdir_r(ALTERNATE_NOT_FOUND_FOLDER, NULL, mode));
must_pass(write_file(ALTERNATE_NOT_FOUND_FOLDER "/" DOT_GIT, "gitdir: a_repository_that_surely_does_not_exist"));
must_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs));
must_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs));
must_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs));
must_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs));
- must_pass(append_ceiling_dir(ceiling_dirs, SUB_REPOSITORY_FOLDER));
+ must_pass(append_ceiling_dir(&ceiling_dirs_buf, SUB_REPOSITORY_FOLDER));
+ ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf);
+
//this must pass as ceiling_directories cannot predent the current
//working directory to be checked
must_pass(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
@@ -166,6 +174,7 @@ BEGIN_TEST(discover0, "test discover")
must_pass(git_futils_rmdir_r(TEMP_REPO_FOLDER, 1));
git_repository_free(repo);
+ git_buf_free(&ceiling_dirs_buf);
END_TEST
BEGIN_SUITE(repository)
diff --git a/tests/t18-status.c b/tests/t18-status.c
index 73e328c2..3799dc2a 100644
--- a/tests/t18-status.c
+++ b/tests/t18-status.c
@@ -392,15 +392,15 @@ END_TEST
BEGIN_TEST(singlestatus3, "test retrieving status for a new file in an empty repository")
git_repository *repo;
unsigned int status_flags;
- char file_path[GIT_PATH_MAX];
+ git_buf file_path = GIT_BUF_INIT;
char filename[] = "new_file";
int fd;
must_pass(copydir_recurs(EMPTY_REPOSITORY_FOLDER, TEST_STD_REPO_FOLDER));
must_pass(remove_placeholders(TEST_STD_REPO_FOLDER, "dummy-marker.txt"));
- git_path_join(file_path, TEMP_REPO_FOLDER, filename);
- fd = p_creat(file_path, 0666);
+ must_pass(git_buf_joinpath(&file_path, TEMP_REPO_FOLDER, filename));
+ fd = p_creat(file_path.ptr, 0666);
must_pass(fd);
must_pass(p_write(fd, "new_file\n", 9));
must_pass(p_close(fd));
@@ -411,6 +411,7 @@ BEGIN_TEST(singlestatus3, "test retrieving status for a new file in an empty rep
must_be_true(status_flags == GIT_STATUS_WT_NEW);
git_repository_free(repo);
+ git_buf_free(&file_path);
git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
END_TEST
diff --git a/tests/test_helpers.c b/tests/test_helpers.c
index 31e38bf6..d4ed10d9 100644
--- a/tests/test_helpers.c
+++ b/tests/test_helpers.c
@@ -225,42 +225,49 @@ int cmp_files(const char *a, const char *b)
}
typedef struct {
- size_t src_len, dst_len;
- char *dst;
+ git_buf src;
+ size_t src_baselen;
+ git_buf dst;
+ size_t dst_baselen;
} copydir_data;
-static int copy_filesystem_element_recurs(void *_data, char *source)
+static int copy_filesystem_element_recurs(void *_data, git_buf *source)
{
copydir_data *data = (copydir_data *)_data;
- data->dst[data->dst_len] = 0;
- git_path_join(data->dst, data->dst, source + data->src_len);
+ git_buf_truncate(&data->dst, data->dst_baselen);
+ git_buf_joinpath(&data->dst, data->dst.ptr, source->ptr + data->src_baselen);
- if (git_futils_isdir(source) == GIT_SUCCESS)
- return git_futils_direach(source, GIT_PATH_MAX, copy_filesystem_element_recurs, _data);
-
- return copy_file(source, data->dst);
+ if (git_futils_isdir(source->ptr) == GIT_SUCCESS)
+ return git_futils_direach(source, copy_filesystem_element_recurs, _data);
+ else
+ return copy_file(source->ptr, data->dst.ptr);
}
-int copydir_recurs(const char *source_directory_path, const char *destination_directory_path)
+int copydir_recurs(
+ const char *source_directory_path,
+ const char *destination_directory_path)
{
- char source_buffer[GIT_PATH_MAX];
- char dest_buffer[GIT_PATH_MAX];
- copydir_data data;
+ int error;
+ copydir_data data = { GIT_BUF_INIT, 0, GIT_BUF_INIT, 0 };
/* Source has to exist, Destination hast to _not_ exist */
if (git_futils_isdir(source_directory_path) != GIT_SUCCESS ||
git_futils_isdir(destination_directory_path) == GIT_SUCCESS)
return GIT_EINVALIDPATH;
- git_path_join(source_buffer, source_directory_path, "");
- data.src_len = strlen(source_buffer);
+ git_buf_joinpath(&data.src, source_directory_path, "");
+ data.src_baselen = data.src.size;
+
+ git_buf_joinpath(&data.dst, destination_directory_path, "");
+ data.dst_baselen = data.dst.size;
+
+ error = copy_filesystem_element_recurs(&data, &data.src);
- git_path_join(dest_buffer, destination_directory_path, "");
- data.dst = dest_buffer;
- data.dst_len = strlen(dest_buffer);
+ git_buf_free(&data.src);
+ git_buf_free(&data.dst);
- return copy_filesystem_element_recurs(&data, source_buffer);
+ return error;
}
int open_temp_repo(git_repository **repo, const char *path)
@@ -281,30 +288,51 @@ void close_temp_repo(git_repository *repo)
}
}
-static int remove_placeholders_recurs(void *filename, char *path)
+typedef struct {
+ const char *filename;
+ size_t filename_len;
+} remove_data;
+
+static int remove_placeholders_recurs(void *_data, git_buf *path)
{
- char passed_filename[GIT_PATH_MAX];
- char *data = (char *)filename;
+ remove_data *data = (remove_data *)_data;
+ size_t pathlen;
- if (!git_futils_isdir(path))
- return git_futils_direach(path, GIT_PATH_MAX, remove_placeholders_recurs, data);
+ if (!git_futils_isdir(path->ptr))
+ return git_futils_direach(path, remove_placeholders_recurs, data);
- if (git_path_basename_r(passed_filename, sizeof(passed_filename), path) < GIT_SUCCESS)
- return GIT_EINVALIDPATH;
+ pathlen = path->size;
- if (!strcmp(data, passed_filename))
- return p_unlink(path);
+ if (pathlen < data->filename_len)
+ return GIT_SUCCESS;
+
+ /* if path ends in '/'+filename (or equals filename) */
+ if (!strcmp(data->filename, path->ptr + pathlen - data->filename_len) &&
+ (pathlen == data->filename_len ||
+ path->ptr[pathlen - data->filename_len - 1] == '/'))
+ return p_unlink(path->ptr);
return GIT_SUCCESS;
}
-int remove_placeholders(char *directory_path, char *filename)
+int remove_placeholders(const char *directory_path, const char *filename)
{
- char buffer[GIT_PATH_MAX];
+ int error;
+ remove_data data;
+ git_buf buffer = GIT_BUF_INIT;
if (git_futils_isdir(directory_path))
return GIT_EINVALIDPATH;
- strcpy(buffer, directory_path);
- return remove_placeholders_recurs(filename, buffer);
+ if ((error = git_buf_sets(&buffer, directory_path)) < GIT_SUCCESS)
+ return error;
+
+ data.filename = filename;
+ data.filename_len = strlen(filename);
+
+ error = remove_placeholders_recurs(&data, &buffer);
+
+ git_buf_free(&buffer);
+
+ return error;
}
diff --git a/tests/test_helpers.h b/tests/test_helpers.h
index 16a3a2ce..a475f66f 100644
--- a/tests/test_helpers.h
+++ b/tests/test_helpers.h
@@ -74,7 +74,7 @@ extern int cmp_files(const char *a, const char *b);
extern int copy_file(const char *source, const char *dest);
extern int rmdir_recurs(const char *directory_path);
extern int copydir_recurs(const char *source_directory_path, const char *destination_directory_path);
-extern int remove_placeholders(char *directory_path, char *filename);
+extern int remove_placeholders(const char *directory_path, const char *filename);
extern int open_temp_repo(git_repository **repo, const char *path);
extern void close_temp_repo(git_repository *repo);