Add filesystem iterator variant This adds a new variant iterator that is a raw filesystem iterator for scanning directories from a root. There is still more work to do to blend this with the working directory iterator.
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
diff --git a/src/iterator.c b/src/iterator.c
index 5b5ed95..dd8a613 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -833,6 +833,307 @@ int git_iterator_for_index(
}
+typedef struct fs_iterator_frame fs_iterator_frame;
+struct fs_iterator_frame {
+ fs_iterator_frame *next;
+ git_vector entries;
+ size_t index;
+};
+
+typedef struct {
+ git_iterator base;
+ git_iterator_callbacks cb;
+ fs_iterator_frame *stack;
+ git_index_entry entry;
+ git_buf path;
+ size_t root_len;
+ int depth;
+} fs_iterator;
+
+#define FS_MAX_DEPTH 100
+
+static fs_iterator_frame *fs_iterator__alloc_frame(fs_iterator *fi)
+{
+ fs_iterator_frame *ff = git__calloc(1, sizeof(fs_iterator_frame));
+ git_vector_cmp entry_compare = CASESELECT(
+ iterator__ignore_case(fi),
+ git_path_with_stat_cmp_icase, git_path_with_stat_cmp);
+
+ if (ff && git_vector_init(&ff->entries, 0, entry_compare) < 0) {
+ git__free(ff);
+ ff = NULL;
+ }
+
+ return ff;
+}
+
+static void fs_iterator__free_frame(fs_iterator_frame *ff)
+{
+ size_t i;
+ git_path_with_stat *path;
+
+ git_vector_foreach(&ff->entries, i, path)
+ git__free(path);
+ git_vector_free(&ff->entries);
+ git__free(ff);
+}
+
+static int fs_iterator__update_entry(fs_iterator *fi);
+
+static int fs_iterator__entry_cmp(const void *i, const void *item)
+{
+ const fs_iterator *fi = (const fs_iterator *)i;
+ const git_path_with_stat *ps = item;
+ return fi->base.prefixcomp(fi->base.start, ps->path);
+}
+
+static void fs_iterator__seek_frame_start(
+ fs_iterator *fi, fs_iterator_frame *ff)
+{
+ if (!ff)
+ return;
+
+ if (fi->base.start)
+ git_vector_bsearch2(
+ &ff->index, &ff->entries, fs_iterator__entry_cmp, fi);
+ else
+ ff->index = 0;
+}
+
+static int fs_iterator__expand_dir(fs_iterator *fi)
+{
+ int error;
+ fs_iterator_frame *ff;
+
+ ff = fs_iterator__alloc_frame(fi);
+ GITERR_CHECK_ALLOC(ff);
+
+ error = git_path_dirload_with_stat(
+ fi->path.ptr, fi->root_len, iterator__ignore_case(fi),
+ fi->base.start, fi->base.end, &ff->entries);
+
+ if (error < 0 || ff->entries.length == 0) {
+ fs_iterator__free_frame(ff);
+ return GIT_ENOTFOUND;
+ }
+
+ if (++(fi->depth) > FS_MAX_DEPTH) {
+ giterr_set(GITERR_REPOSITORY,
+ "Directory nesting is too deep (%d)", fi->depth);
+ fs_iterator__free_frame(ff);
+ return -1;
+ }
+
+ fs_iterator__seek_frame_start(fi, ff);
+
+ ff->next = fi->stack;
+ fi->stack = ff;
+
+ return fs_iterator__update_entry(fi);
+}
+
+static int fs_iterator__current(
+ const git_index_entry **entry, git_iterator *self)
+{
+ fs_iterator *fi = (fs_iterator *)self;
+ if (entry)
+ *entry = (fi->entry.path == NULL) ? NULL : &fi->entry;
+ return 0;
+}
+
+static int fs_iterator__at_end(git_iterator *self)
+{
+ return (((fs_iterator *)self)->entry.path == NULL);
+}
+
+static int fs_iterator__advance_into(
+ const git_index_entry **entry, git_iterator *iter)
+{
+ int error = 0;
+ fs_iterator *fi = (fs_iterator *)iter;
+
+ iterator__clear_entry(entry);
+
+ /* Allow you to explicitly advance into a commit/submodule (as well as a
+ * tree) to avoid cases where an entry is mislabeled as a submodule in
+ * the working directory. The fs iterator will never have COMMMIT
+ * entries on it's own, but a wrapper might add them.
+ */
+ if (fi->entry.path != NULL &&
+ (fi->entry.mode == GIT_FILEMODE_TREE ||
+ fi->entry.mode == GIT_FILEMODE_COMMIT))
+ /* returns GIT_ENOTFOUND if the directory is empty */
+ error = fs_iterator__expand_dir(fi);
+
+ if (!error && entry)
+ error = fs_iterator__current(entry, iter);
+
+ return error;
+}
+
+static int fs_iterator__advance(
+ const git_index_entry **entry, git_iterator *self)
+{
+ int error = 0;
+ fs_iterator *fi = (fs_iterator *)self;
+ fs_iterator_frame *ff;
+ git_path_with_stat *next;
+
+ /* given include_trees & autoexpand, we might have to go into a tree */
+ if (iterator__do_autoexpand(fi) &&
+ fi->entry.path != NULL &&
+ fi->entry.mode == GIT_FILEMODE_TREE)
+ {
+ error = fs_iterator__advance_into(entry, self);
+
+ /* continue silently past empty directories if autoexpanding */
+ if (error != GIT_ENOTFOUND)
+ return error;
+ giterr_clear();
+ error = 0;
+ }
+
+ if (entry != NULL)
+ *entry = NULL;
+
+ while (fi->entry.path != NULL) {
+ ff = fi->stack;
+ next = git_vector_get(&ff->entries, ++ff->index);
+
+ if (next != NULL)
+ break;
+
+ /* pop stack if anything is left to pop */
+ if (!ff->next) {
+ memset(&fi->entry, 0, sizeof(fi->entry));
+ return 0;
+ }
+
+ fi->stack = ff->next;
+ fi->depth--;
+ fs_iterator__free_frame(ff);
+ }
+
+ error = fs_iterator__update_entry(fi);
+
+ if (!error && entry != NULL)
+ error = fs_iterator__current(entry, self);
+
+ return error;
+}
+
+static int fs_iterator__seek(git_iterator *self, const char *prefix)
+{
+ GIT_UNUSED(self);
+ GIT_UNUSED(prefix);
+ /* pop stack until matching prefix */
+ /* find prefix item in current frame */
+ /* push subdirectories as deep as possible while matching */
+ return 0;
+}
+
+static int fs_iterator__reset(
+ git_iterator *self, const char *start, const char *end)
+{
+ fs_iterator *fi = (fs_iterator *)self;
+
+ while (fi->stack != NULL && fi->stack->next != NULL) {
+ fs_iterator_frame *ff = fi->stack;
+ fi->stack = ff->next;
+ fs_iterator__free_frame(ff);
+ }
+ fi->depth = 0;
+
+ if (iterator__reset_range(self, start, end) < 0)
+ return -1;
+
+ fs_iterator__seek_frame_start(fi, fi->stack);
+
+ return fs_iterator__update_entry(fi);
+}
+
+static void fs_iterator__free(git_iterator *self)
+{
+ fs_iterator *fi = (fs_iterator *)self;
+
+ while (fi->stack != NULL) {
+ fs_iterator_frame *ff = fi->stack;
+ fi->stack = ff->next;
+ fs_iterator__free_frame(ff);
+ }
+
+ git_buf_free(&fi->path);
+}
+
+static int fs_iterator__update_entry(fs_iterator *fi)
+{
+ git_path_with_stat *ps =
+ git_vector_get(&fi->stack->entries, fi->stack->index);
+
+ git_buf_truncate(&fi->path, fi->root_len);
+ memset(&fi->entry, 0, sizeof(fi->entry));
+
+ if (!ps)
+ return 0;
+
+ if (git_buf_put(&fi->path, ps->path, ps->path_len) < 0)
+ return -1;
+
+ if (iterator__past_end(fi, fi->path.ptr + fi->root_len))
+ return 0;
+
+ fi->entry.path = ps->path;
+ git_index_entry__init_from_stat(&fi->entry, &ps->st);
+
+ /* need different mode here to keep directories during iteration */
+ fi->entry.mode = git_futils_canonical_mode(ps->st.st_mode);
+
+ /* if this isn't a tree, then we're done */
+ if (fi->entry.mode != GIT_FILEMODE_TREE)
+ return 0;
+
+ if (iterator__include_trees(fi))
+ return 0;
+
+ return fs_iterator__advance(NULL, (git_iterator *)fi);
+}
+
+int git_iterator_for_filesystem(
+ git_iterator **out,
+ const char *root,
+ git_iterator_flag_t flags,
+ const char *start,
+ const char *end)
+{
+ int error;
+ fs_iterator *fi;
+
+ ITERATOR_BASE_INIT(fi, fs, FS, NULL);
+
+ if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
+ fi->base.flags |= GIT_ITERATOR_IGNORE_CASE;
+
+ if (git_buf_sets(&fi->path, root) < 0 || git_path_to_dir(&fi->path) < 0) {
+ git__free(fi);
+ return -1;
+ }
+ fi->root_len = fi->path.size;
+
+ if ((error = fs_iterator__expand_dir(fi)) < 0) {
+ if (error != GIT_ENOTFOUND)
+ goto fail;
+ giterr_clear();
+ }
+
+ *out = (git_iterator *)fi;
+ return 0;
+
+fail:
+ git_iterator_free((git_iterator *)fi);
+ return error;
+}
+
+
#define WORKDIR_MAX_DEPTH 100
typedef struct workdir_iterator_frame workdir_iterator_frame;
diff --git a/src/iterator.h b/src/iterator.h
index 4a4e6a9..7998f7c 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -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
diff --git a/tests-clar/repo/iterator.c b/tests-clar/repo/iterator.c
index 00c46d6..ef9bfc3 100644
--- a/tests-clar/repo/iterator.c
+++ b/tests-clar/repo/iterator.c
@@ -808,3 +808,47 @@ 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_subdir[] = {
+ "current_file",
+ "modified_file",
+ "new_file",
+ NULL,
+ };
+
+ g_repo = cl_git_sandbox_init("status");
+
+ cl_git_pass(git_iterator_for_filesystem(
+ &i, "status/subdir", 0, NULL, NULL));
+ expect_iterator_items(i, 3, expect_subdir, 3, expect_subdir);
+ git_iterator_free(i);
+}
+
+void test_repo_iterator__fs2(void)
+{
+ git_iterator *i;
+ static const char *expect_subdir[] = {
+ "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_subdir, 11, expect_subdir);
+ git_iterator_free(i);
+}