Commit 9d8f97c9 by Russell Belfer

Merge pull request #1479 from arrbee/iterator-for-directory

Add filesystem iterator variant
parents 743048f1 1af80a67
......@@ -377,7 +377,7 @@ int git_branch_remote_name(char *buffer, size_t buffer_len, git_repository *repo
if (buffer)
git_buf_copy_cstr(buffer, buffer_len, &buf);
ret = git_buf_len(&buf) + 1;
ret = (int)git_buf_len(&buf) + 1;
git_buf_free(&buf);
return ret;
......
......@@ -1345,7 +1345,7 @@ static size_t read_extension(git_index *index, const char *buffer, size_t buffer
static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
{
unsigned int i;
struct index_header header;
struct index_header header = { 0 };
git_oid checksum_calculated, checksum_expected;
#define seek_forward(_increase) { \
......
......@@ -19,6 +19,7 @@ typedef enum {
GIT_ITERATOR_TYPE_TREE = 1,
GIT_ITERATOR_TYPE_INDEX = 2,
GIT_ITERATOR_TYPE_WORKDIR = 3,
GIT_ITERATOR_TYPE_FS = 4,
} git_iterator_type_t;
typedef enum {
......@@ -88,6 +89,16 @@ extern int git_iterator_for_workdir(
const char *start,
const char *end);
/* for filesystem iterators, you have to explicitly pass in the ignore_case
* behavior that you desire
*/
extern int git_iterator_for_filesystem(
git_iterator **out,
const char *root,
git_iterator_flag_t flags,
const char *start,
const char *end);
extern void git_iterator_free(git_iterator *iter);
/* Return a git_index_entry structure for the current value the iterator
......
......@@ -16,7 +16,7 @@
static int disambiguate_refname(git_reference **out, git_repository *repo, const char *refname)
{
int error, i;
int error = 0, i;
bool fallbackmode = true;
git_reference *ref;
git_buf refnamebuf = GIT_BUF_INIT, name = GIT_BUF_INIT;
......
......@@ -160,7 +160,7 @@ void test_refdb_inmemory__foreach(void)
cl_git_pass(git_reference_create(&write3, repo, GIT_REFS_HEADS_DIR "test3", &oid3, 0));
cl_git_pass(git_reference_foreach(repo, GIT_REF_LISTALL, foreach_test, &i));
cl_assert_equal_i(i, 3);
cl_assert_equal_i(3, (int)i);
git_reference_free(write1);
git_reference_free(write2);
......@@ -207,7 +207,7 @@ void test_refdb_inmemory__delete(void)
git_reference_free(write3);
cl_git_pass(git_reference_foreach(repo, GIT_REF_LISTALL, delete_test, &i));
cl_assert_equal_i(i, 1);
cl_assert_equal_i(1, (int)i);
git_reference_free(write2);
}
......@@ -422,7 +422,7 @@ static void build_test_tree(
git_treebuilder *builder;
const char *scan = fmt, *next;
char type, delimiter;
git_filemode_t mode;
git_filemode_t mode = GIT_FILEMODE_BLOB;
git_buf name = GIT_BUF_INIT;
va_list arglist;
......@@ -755,47 +755,52 @@ void test_repo_iterator__workdir_icase(void)
git_iterator_free(i);
}
void test_repo_iterator__workdir_depth(void)
static void build_workdir_tree(const char *root, int dirs, int subs)
{
int i, j;
git_iterator *iter;
char buf[64];
g_repo = cl_git_sandbox_init("icase");
for (i = 0; i < 10; ++i) {
p_snprintf(buf, sizeof(buf), "icase/dir%02d", i);
cl_git_pass(git_futils_mkdir(buf, NULL, 0775, GIT_MKDIR_PATH));
char buf[64], sub[64];
for (i = 0; i < dirs; ++i) {
if (i % 2 == 0) {
p_snprintf(buf, sizeof(buf), "icase/dir%02d/file", i);
p_snprintf(buf, sizeof(buf), "%s/dir%02d", root, i);
cl_git_pass(git_futils_mkdir(buf, NULL, 0775, GIT_MKDIR_PATH));
p_snprintf(buf, sizeof(buf), "%s/dir%02d/file", root, i);
cl_git_mkfile(buf, buf);
buf[strlen(buf) - 5] = '\0';
} else {
p_snprintf(buf, sizeof(buf), "%s/DIR%02d", root, i);
cl_git_pass(git_futils_mkdir(buf, NULL, 0775, GIT_MKDIR_PATH));
}
for (j = 0; j < 10; ++j) {
p_snprintf(buf, sizeof(buf), "icase/dir%02d/sub%02d", i, j);
cl_git_pass(git_futils_mkdir(buf, NULL, 0775, GIT_MKDIR_PATH));
for (j = 0; j < subs; ++j) {
switch (j % 4) {
case 0: p_snprintf(sub, sizeof(sub), "%s/sub%02d", buf, j); break;
case 1: p_snprintf(sub, sizeof(sub), "%s/sUB%02d", buf, j); break;
case 2: p_snprintf(sub, sizeof(sub), "%s/Sub%02d", buf, j); break;
case 3: p_snprintf(sub, sizeof(sub), "%s/SUB%02d", buf, j); break;
}
cl_git_pass(git_futils_mkdir(sub, NULL, 0775, GIT_MKDIR_PATH));
if (j % 2 == 0) {
p_snprintf(
buf, sizeof(buf), "icase/dir%02d/sub%02d/file", i, j);
cl_git_mkfile(buf, buf);
size_t sublen = strlen(sub);
memcpy(&sub[sublen], "/file", sizeof("/file"));
cl_git_mkfile(sub, sub);
sub[sublen] = '\0';
}
}
}
}
for (i = 1; i < 3; ++i) {
for (j = 0; j < 50; ++j) {
p_snprintf(buf, sizeof(buf), "icase/dir%02d/sub01/moar%02d", i, j);
cl_git_pass(git_futils_mkdir(buf, NULL, 0775, GIT_MKDIR_PATH));
void test_repo_iterator__workdir_depth(void)
{
git_iterator *iter;
if (j % 2 == 0) {
p_snprintf(buf, sizeof(buf),
"icase/dir%02d/sub01/moar%02d/file", i, j);
cl_git_mkfile(buf, buf);
}
}
}
g_repo = cl_git_sandbox_init("icase");
build_workdir_tree("icase", 10, 10);
build_workdir_tree("icase/DIR01/sUB01", 50, 0);
build_workdir_tree("icase/dir02/sUB01", 50, 0);
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_workdir(&iter, g_repo, 0, NULL, NULL));
......@@ -808,3 +813,114 @@ void test_repo_iterator__workdir_depth(void)
expect_iterator_items(iter, 337, NULL, 337, NULL);
git_iterator_free(iter);
}
void test_repo_iterator__fs(void)
{
git_iterator *i;
static const char *expect_base[] = {
"DIR01/Sub02/file",
"DIR01/sub00/file",
"current_file",
"dir00/Sub02/file",
"dir00/file",
"dir00/sub00/file",
"modified_file",
"new_file",
NULL,
};
static const char *expect_trees[] = {
"DIR01/",
"DIR01/SUB03/",
"DIR01/Sub02/",
"DIR01/Sub02/file",
"DIR01/sUB01/",
"DIR01/sub00/",
"DIR01/sub00/file",
"current_file",
"dir00/",
"dir00/SUB03/",
"dir00/Sub02/",
"dir00/Sub02/file",
"dir00/file",
"dir00/sUB01/",
"dir00/sub00/",
"dir00/sub00/file",
"modified_file",
"new_file",
NULL,
};
static const char *expect_noauto[] = {
"DIR01/",
"current_file",
"dir00/",
"modified_file",
"new_file",
NULL,
};
g_repo = cl_git_sandbox_init("status");
build_workdir_tree("status/subdir", 2, 4);
cl_git_pass(git_iterator_for_filesystem(
&i, "status/subdir", 0, NULL, NULL));
expect_iterator_items(i, 8, expect_base, 8, expect_base);
git_iterator_free(i);
cl_git_pass(git_iterator_for_filesystem(
&i, "status/subdir", GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
expect_iterator_items(i, 18, expect_trees, 18, expect_trees);
git_iterator_free(i);
cl_git_pass(git_iterator_for_filesystem(
&i, "status/subdir", GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
expect_iterator_items(i, 5, expect_noauto, 18, expect_trees);
git_iterator_free(i);
git__tsort((void **)expect_base, 8, (git__tsort_cmp)git__strcasecmp);
git__tsort((void **)expect_trees, 18, (git__tsort_cmp)git__strcasecmp);
git__tsort((void **)expect_noauto, 5, (git__tsort_cmp)git__strcasecmp);
cl_git_pass(git_iterator_for_filesystem(
&i, "status/subdir", GIT_ITERATOR_IGNORE_CASE, NULL, NULL));
expect_iterator_items(i, 8, expect_base, 8, expect_base);
git_iterator_free(i);
cl_git_pass(git_iterator_for_filesystem(
&i, "status/subdir", GIT_ITERATOR_IGNORE_CASE |
GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
expect_iterator_items(i, 18, expect_trees, 18, expect_trees);
git_iterator_free(i);
cl_git_pass(git_iterator_for_filesystem(
&i, "status/subdir", GIT_ITERATOR_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
expect_iterator_items(i, 5, expect_noauto, 18, expect_trees);
git_iterator_free(i);
}
void test_repo_iterator__fs2(void)
{
git_iterator *i;
static const char *expect_base[] = {
"heads/br2",
"heads/dir",
"heads/master",
"heads/packed-test",
"heads/subtrees",
"heads/test",
"tags/e90810b",
"tags/foo/bar",
"tags/foo/foo/bar",
"tags/point_to_blob",
"tags/test",
NULL,
};
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_iterator_for_filesystem(
&i, "testrepo/.git/refs", 0, NULL, NULL));
expect_iterator_items(i, 11, expect_base, 11, expect_base);
git_iterator_free(i);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment