Merge pull request #4455 from libgit2/ethomson/branch_symlinks refs: traverse symlinked directories
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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
diff --git a/src/iterator.c b/src/iterator.c
index 9600312..132b2c7 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -23,6 +23,7 @@
#define iterator__has_been_accessed(I) iterator__flag(I,FIRST_ACCESS)
#define iterator__honor_ignores(I) iterator__flag(I,HONOR_IGNORES)
#define iterator__ignore_dot_git(I) iterator__flag(I,IGNORE_DOT_GIT)
+#define iterator__descend_symlinks(I) iterator__flag(I,DESCEND_SYMLINKS)
static void iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
@@ -1491,10 +1492,41 @@ static int filesystem_iterator_current(
return 0;
}
+static int filesystem_iterator_is_dir(
+ bool *is_dir,
+ const filesystem_iterator *iter,
+ const filesystem_iterator_entry *entry)
+{
+ struct stat st;
+ git_buf fullpath = GIT_BUF_INIT;
+ int error = 0;
+
+ if (S_ISDIR(entry->st.st_mode)) {
+ *is_dir = 1;
+ goto done;
+ }
+
+ if (!iterator__descend_symlinks(iter) || !S_ISLNK(entry->st.st_mode)) {
+ *is_dir = 0;
+ goto done;
+ }
+
+ if ((error = git_buf_joinpath(&fullpath, iter->root, entry->path)) < 0 ||
+ (error = p_stat(fullpath.ptr, &st)) < 0)
+ goto done;
+
+ *is_dir = S_ISDIR(st.st_mode);
+
+done:
+ git_buf_free(&fullpath);
+ return error;
+}
+
static int filesystem_iterator_advance(
const git_index_entry **out, git_iterator *i)
{
filesystem_iterator *iter = (filesystem_iterator *)i;
+ bool is_dir;
int error = 0;
iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
@@ -1519,7 +1551,10 @@ static int filesystem_iterator_advance(
entry = frame->entries.contents[frame->next_idx];
frame->next_idx++;
- if (S_ISDIR(entry->st.st_mode)) {
+ if ((error = filesystem_iterator_is_dir(&is_dir, iter, entry)) < 0)
+ break;
+
+ if (is_dir) {
if (iterator__do_autoexpand(iter)) {
error = filesystem_iterator_frame_push(iter, entry);
diff --git a/src/iterator.h b/src/iterator.h
index 0bcb128..a6497d8 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -39,6 +39,8 @@ typedef enum {
GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE = (1u << 5),
/** include conflicts */
GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 6),
+ /** descend into symlinked directories */
+ GIT_ITERATOR_DESCEND_SYMLINKS = (1u << 7),
} git_iterator_flag_t;
typedef enum {
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index ade734c..140879d 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -2035,6 +2035,7 @@ int git_refdb_backend_fs(
if ((!git_repository__cvar(&t, backend->repo, GIT_CVAR_FSYNCOBJECTFILES) && t) ||
git_repository__fsync_gitdir)
backend->fsync = 1;
+ backend->iterator_flags |= GIT_ITERATOR_DESCEND_SYMLINKS;
backend->parent.exists = &refdb_fs_backend__exists;
backend->parent.lookup = &refdb_fs_backend__lookup;
diff --git a/tests/refs/iterator.c b/tests/refs/iterator.c
index c774513..56f6ce5 100644
--- a/tests/refs/iterator.c
+++ b/tests/refs/iterator.c
@@ -6,12 +6,12 @@ static git_repository *repo;
void test_refs_iterator__initialize(void)
{
- cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
+ repo = cl_git_sandbox_init("testrepo.git");
}
void test_refs_iterator__cleanup(void)
{
- git_repository_free(repo);
+ cl_git_sandbox_cleanup();
}
static const char *refnames[] = {
@@ -36,6 +36,36 @@ static const char *refnames[] = {
"refs/tags/taggerless",
"refs/tags/test",
"refs/tags/wrapped_tag",
+ NULL
+};
+
+static const char *refnames_with_symlink[] = {
+ "refs/heads/br2",
+ "refs/heads/cannot-fetch",
+ "refs/heads/chomped",
+ "refs/heads/haacked",
+ "refs/heads/link/a",
+ "refs/heads/link/b",
+ "refs/heads/link/c",
+ "refs/heads/link/d",
+ "refs/heads/master",
+ "refs/heads/not-good",
+ "refs/heads/packed",
+ "refs/heads/packed-test",
+ "refs/heads/subtrees",
+ "refs/heads/test",
+ "refs/heads/track-local",
+ "refs/heads/trailing",
+ "refs/notes/fanout",
+ "refs/remotes/test/master",
+ "refs/tags/annotated_tag_to_blob",
+ "refs/tags/e90810b",
+ "refs/tags/hard_tag",
+ "refs/tags/point_to_blob",
+ "refs/tags/taggerless",
+ "refs/tags/test",
+ "refs/tags/wrapped_tag",
+ NULL
};
static int refcmp_cb(const void *a, const void *b)
@@ -46,21 +76,21 @@ static int refcmp_cb(const void *a, const void *b)
return strcmp(refa->name, refb->name);
}
-static void assert_all_refnames_match(git_vector *output)
+static void assert_all_refnames_match(const char **expected, git_vector *names)
{
size_t i;
git_reference *ref;
- cl_assert_equal_sz(output->length, ARRAY_SIZE(refnames));
-
- git_vector_sort(output);
+ git_vector_sort(names);
- git_vector_foreach(output, i, ref) {
- cl_assert_equal_s(ref->name, refnames[i]);
+ git_vector_foreach(names, i, ref) {
+ cl_assert(expected[i] != NULL);
+ cl_assert_equal_s(expected[i], ref->name);
git_reference_free(ref);
}
+ cl_assert(expected[i] == NULL);
- git_vector_free(output);
+ git_vector_free(names);
}
void test_refs_iterator__list(void)
@@ -82,7 +112,7 @@ void test_refs_iterator__list(void)
git_reference_iterator_free(iter);
- assert_all_refnames_match(&output);
+ assert_all_refnames_match(refnames, &output);
}
void test_refs_iterator__empty(void)
@@ -115,7 +145,29 @@ void test_refs_iterator__foreach(void)
git_vector output;
cl_git_pass(git_vector_init(&output, 32, &refcmp_cb));
cl_git_pass(git_reference_foreach(repo, refs_foreach_cb, &output));
- assert_all_refnames_match(&output);
+ assert_all_refnames_match(refnames, &output);
+}
+
+void test_refs_iterator__foreach_through_symlink(void)
+{
+ git_vector output;
+
+#ifdef GIT_WIN32
+ cl_skip();
+#endif
+
+ cl_git_pass(git_vector_init(&output, 32, &refcmp_cb));
+
+ cl_git_pass(p_mkdir("refs", 0777));
+ cl_git_mkfile("refs/a", "1234567890123456789012345678901234567890");
+ cl_git_mkfile("refs/b", "1234567890123456789012345678901234567890");
+ cl_git_mkfile("refs/c", "1234567890123456789012345678901234567890");
+ cl_git_mkfile("refs/d", "1234567890123456789012345678901234567890");
+
+ cl_git_pass(p_symlink("../../../refs", "testrepo.git/refs/heads/link"));
+
+ cl_git_pass(git_reference_foreach(repo, refs_foreach_cb, &output));
+ assert_all_refnames_match(refnames_with_symlink, &output);
}
static int refs_foreach_cancel_cb(git_reference *reference, void *payload)
@@ -156,12 +208,11 @@ void test_refs_iterator__foreach_name(void)
cl_git_pass(
git_reference_foreach_name(repo, refs_foreach_name_cb, &output));
- cl_assert_equal_sz(output.length, ARRAY_SIZE(refnames));
git_vector_sort(&output);
git_vector_foreach(&output, i, name) {
- cl_assert_equal_s(name, refnames[i]);
- git__free(name);
+ cl_assert(refnames[i] != NULL);
+ cl_assert_equal_s(refnames[i], name);
}
git_vector_free(&output);
@@ -194,7 +245,7 @@ void test_refs_iterator__concurrent_delete(void)
const char *name;
int error;
- git_repository_free(repo);
+ cl_git_sandbox_cleanup();
repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_reference_iterator_new(&iter, repo));
@@ -215,7 +266,4 @@ void test_refs_iterator__concurrent_delete(void)
cl_assert_equal_i(GIT_ITEROVER, error);
cl_assert_equal_i(full_count, concurrent_count);
-
- cl_git_sandbox_cleanup();
- repo = NULL;
}