Fix tree iterator path for tree issue + cleanups This fixes an off by one error for generating full paths for tree entries in tree iterators when INCLUDE_TREES is set. Also, contains a bunch of small code cleanups with a couple of small utility functions and macro changes to eliminate redundant code.
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
diff --git a/src/iterator.c b/src/iterator.c
index 53ec6f6..273f073 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -25,12 +25,13 @@
#define ITERATOR_CASE_FLAGS \
(GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_IGNORE_CASE)
-#define ITERATOR_BASE_INIT(P,NAME_LC,NAME_UC) do { \
+#define ITERATOR_BASE_INIT(P,NAME_LC,NAME_UC,REPO) do { \
(P) = git__calloc(1, sizeof(NAME_LC ## _iterator)); \
GITERR_CHECK_ALLOC(P); \
(P)->base.type = GIT_ITERATOR_TYPE_ ## NAME_UC; \
- (P)->base.cb = &(P)->cb; \
+ (P)->base.cb = &(P)->cb; \
ITERATOR_SET_CB(P,NAME_LC); \
+ (P)->base.repo = (REPO); \
(P)->base.start = start ? git__strdup(start) : NULL; \
(P)->base.end = end ? git__strdup(end) : NULL; \
if ((start && !(P)->base.start) || (end && !(P)->base.end)) { \
@@ -72,7 +73,7 @@ static int iterator__reset_range(
return 0;
}
-static int iterator_update_ignore_case(
+static int iterator__update_ignore_case(
git_iterator *iter,
git_iterator_flag_t flags)
{
@@ -100,37 +101,40 @@ static int iterator_update_ignore_case(
return error;
}
-
-static int empty_iterator__noop(
- const git_index_entry **entry, git_iterator *iter)
+GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry)
{
- GIT_UNUSED(iter);
if (entry) *entry = NULL;
+}
+
+
+static int empty_iterator__noop(const git_index_entry **e, git_iterator *i)
+{
+ GIT_UNUSED(i);
+ iterator__clear_entry(e);
return 0;
}
-static int empty_iterator__seek(git_iterator *iter, const char *prefix)
+static int empty_iterator__seek(git_iterator *i, const char *p)
{
- GIT_UNUSED(iter); GIT_UNUSED(prefix);
+ GIT_UNUSED(i); GIT_UNUSED(p);
return -1;
}
-static int empty_iterator__reset(
- git_iterator *iter, const char *start, const char *end)
+static int empty_iterator__reset(git_iterator *i, const char *s, const char *e)
{
- GIT_UNUSED(iter); GIT_UNUSED(start); GIT_UNUSED(end);
+ GIT_UNUSED(i); GIT_UNUSED(s); GIT_UNUSED(e);
return 0;
}
-static int empty_iterator__at_end(git_iterator *iter)
+static int empty_iterator__at_end(git_iterator *i)
{
- GIT_UNUSED(iter);
+ GIT_UNUSED(i);
return 1;
}
-static void empty_iterator__free(git_iterator *iter)
+static void empty_iterator__free(git_iterator *i)
{
- GIT_UNUSED(iter);
+ GIT_UNUSED(i);
}
typedef struct {
@@ -151,18 +155,16 @@ int git_iterator_for_nothing(
#define empty_iterator__advance empty_iterator__noop
#define empty_iterator__advance_into empty_iterator__noop
- ITERATOR_BASE_INIT(i, empty, EMPTY);
+ ITERATOR_BASE_INIT(i, empty, EMPTY, NULL);
if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
i->base.flags |= GIT_ITERATOR_IGNORE_CASE;
*iter = (git_iterator *)i;
-
return 0;
}
-
typedef struct {
size_t parent_entry_index; /* index in parent entries array */
size_t parent_tree_index; /* index in parent entry tree */
@@ -236,6 +238,9 @@ static void tree_iterator__rewrite_filename(tree_iterator *ti)
ssize_t strpos = ti->path.size;
const git_tree_entry *te;
+ if (strpos && ti->path.ptr[strpos - 1] == '/')
+ strpos--;
+
while (scan && scan->parent) {
tree_iterator_entry *entry = &scan->entries[current];
@@ -386,18 +391,23 @@ static int tree_iterator__push_frame(tree_iterator *ti)
return 0;
}
+GIT_INLINE(void) tree_iterator__free_tree(tree_iterator_entry *entry)
+{
+ if (entry->tree) {
+ git_tree_free(entry->tree);
+ entry->tree = NULL;
+ }
+}
+
static bool tree_iterator__move_to_next(
tree_iterator *ti, tree_iterator_frame *tf)
{
if (tf->next > tf->current + 1)
ti->path_ambiguities--;
- while (tf->current < tf->next) {
- if (tf->parent && tf->entries[tf->current].tree) {
- git_tree_free(tf->entries[tf->current].tree);
- tf->entries[tf->current].tree = NULL;
- }
- tf->current++;
+ for (; tf->current < tf->next; tf->current++) {
+ if (tf->parent)
+ tree_iterator__free_tree(&tf->entries[tf->current]);
}
return (tf->current < tf->n_entries);
@@ -428,8 +438,7 @@ static int tree_iterator__current(
tree_iterator_frame *tf = ti->head;
const git_tree_entry *te;
- if (entry)
- *entry = NULL;
+ iterator__clear_entry(entry);
if (!(te = tree_iterator__get_tree_entry(tf, NULL, tf->current)))
return 0;
@@ -462,8 +471,7 @@ static int tree_iterator__advance_into(
int error = 0;
tree_iterator *ti = (tree_iterator *)self;
- if (entry)
- *entry = NULL;
+ iterator__clear_entry(entry);
if (tree_iterator__at_tree(ti) &&
!(error = tree_iterator__push_frame(ti)))
@@ -479,8 +487,7 @@ static int tree_iterator__advance(
tree_iterator *ti = (tree_iterator *)self;
tree_iterator_frame *tf = ti->head;
- if (entry)
- *entry = NULL;
+ iterator__clear_entry(entry);
if (tf->current > tf->n_entries)
return 0;
@@ -511,8 +518,7 @@ static int tree_iterator__advance(
static int tree_iterator__seek(git_iterator *self, const char *prefix)
{
- GIT_UNUSED(self);
- GIT_UNUSED(prefix);
+ GIT_UNUSED(self); GIT_UNUSED(prefix);
return -1;
}
@@ -544,10 +550,8 @@ static void tree_iterator__free(git_iterator *self)
while (tree_iterator__pop_frame(ti)) /* pop to top */;
- /* free base frame */
if (ti->head) {
- git_tree_free(ti->head->entries[0].tree);
- ti->head->entries[0].tree = NULL;
+ tree_iterator__free_tree(&ti->head->entries[0]);
git__free(ti->head);
}
ti->head = ti->top = NULL;
@@ -588,10 +592,9 @@ int git_iterator_for_tree(
if ((error = git_tree__dup(&tree, tree)) < 0)
return error;
- ITERATOR_BASE_INIT(ti, tree, TREE);
- ti->base.repo = git_tree_owner(tree);
+ ITERATOR_BASE_INIT(ti, tree, TREE, git_tree_owner(tree));
- if ((error = iterator_update_ignore_case((git_iterator *)ti, flags)) < 0)
+ if ((error = iterator__update_ignore_case((git_iterator *)ti, flags)) < 0)
goto fail;
if (iterator__ignore_case(ti)) {
@@ -678,7 +681,6 @@ static int index_iterator__first_prefix_tree(index_iterator *ii)
return 0;
/* find longest common prefix with prior index entry */
-
for (scan = slash = ie->path, prior = ii->partial.ptr;
*scan && *scan == *prior; ++scan, ++prior)
if (*scan == '/')
@@ -731,9 +733,7 @@ static int index_iterator__advance(
ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
index_iterator__next_prefix_tree(ii);
} else {
- /* advance to sibling tree (i.e. until we find entry that does
- * not share this prefix)
- */
+ /* advance to sibling tree (i.e. find entry with new prefix) */
while (ii->current < entrycount) {
ii->current++;
@@ -773,9 +773,7 @@ static int index_iterator__advance_into(
static int index_iterator__seek(git_iterator *self, const char *prefix)
{
- GIT_UNUSED(self);
- GIT_UNUSED(prefix);
- /* find last item before prefix */
+ GIT_UNUSED(self); GIT_UNUSED(prefix);
return -1;
}
@@ -829,9 +827,7 @@ int git_iterator_for_index(
{
index_iterator *ii;
- ITERATOR_BASE_INIT(ii, index, INDEX);
-
- ii->base.repo = git_index_owner(index);
+ ITERATOR_BASE_INIT(ii, index, INDEX, git_index_owner(index));
if (index->ignore_case) {
ii->base.flags |= GIT_ITERATOR_IGNORE_CASE;
@@ -969,8 +965,7 @@ static int workdir_iterator__expand_dir(workdir_iterator *wi)
GITERR_CHECK_ALLOC(wf);
error = git_path_dirload_with_stat(
- wi->path.ptr, wi->root_len,
- iterator__ignore_case(wi),
+ wi->path.ptr, wi->root_len, iterator__ignore_case(wi),
wi->base.start, wi->base.end, &wf->entries);
if (error < 0 || wf->entries.length == 0) {
@@ -1012,8 +1007,7 @@ static int workdir_iterator__advance_into(
int error = 0;
workdir_iterator *wi = (workdir_iterator *)iter;
- if (entry)
- *entry = NULL;
+ iterator__clear_entry(entry);
/* workdir iterator will allow you to explicitly advance into a
* commit/submodule (as well as a tree) to avoid some cases where an
@@ -1206,13 +1200,12 @@ int git_iterator_for_workdir(
assert(iter && repo);
if ((error = git_repository__ensure_not_bare(
- repo, "scan working directory")) < 0)
+ repo, "scan working directory")) < 0)
return error;
- ITERATOR_BASE_INIT(wi, workdir, WORKDIR);
- wi->base.repo = repo;
+ ITERATOR_BASE_INIT(wi, workdir, WORKDIR, repo);
- if ((error = iterator_update_ignore_case((git_iterator *)wi, flags)) < 0)
+ if ((error = iterator__update_ignore_case((git_iterator *)wi, flags)) < 0)
goto fail;
if (git_buf_sets(&wi->path, git_repository_workdir(repo)) < 0 ||
@@ -1282,7 +1275,6 @@ git_index *git_iterator_get_index(git_iterator *iter)
{
if (iter->type == GIT_ITERATOR_TYPE_INDEX)
return ((index_iterator *)iter)->index;
-
return NULL;
}
@@ -1354,8 +1346,7 @@ int git_iterator_cmp(git_iterator *iter, const char *path_prefix)
const git_index_entry *entry;
/* a "done" iterator is after every prefix */
- if (git_iterator_current(&entry, iter) < 0 ||
- entry == NULL)
+ if (git_iterator_current(&entry, iter) < 0 || entry == NULL)
return 1;
/* a NULL prefix is after any valid iterator */
diff --git a/tests-clar/repo/iterator.c b/tests-clar/repo/iterator.c
index 804bc83..e7498f6 100644
--- a/tests-clar/repo/iterator.c
+++ b/tests-clar/repo/iterator.c
@@ -457,6 +457,10 @@ void test_repo_iterator__tree_case_conflicts(void)
"A/1.file", "A/3.file", "a/2.file", "a/4.file" };
const char *expect_ci[] = {
"A/1.file", "a/2.file", "A/3.file", "a/4.file" };
+ const char *expect_cs_trees[] = {
+ "A/", "A/1.file", "A/3.file", "a/", "a/2.file", "a/4.file" };
+ const char *expect_ci_trees[] = {
+ "A/", "A/1.file", "a/2.file", "A/3.file", "a/4.file" };
g_repo = cl_git_sandbox_init("icase");
@@ -482,6 +486,18 @@ void test_repo_iterator__tree_case_conflicts(void)
expect_iterator_items(i, 4, expect_ci, 4, expect_ci);
git_iterator_free(i);
+ cl_git_pass(git_iterator_for_tree(
+ &i, tree, GIT_ITERATOR_DONT_IGNORE_CASE |
+ GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
+ expect_iterator_items(i, 6, expect_cs_trees, 6, expect_cs_trees);
+ git_iterator_free(i);
+
+ cl_git_pass(git_iterator_for_tree(
+ &i, tree, GIT_ITERATOR_IGNORE_CASE |
+ GIT_ITERATOR_INCLUDE_TREES, NULL, NULL));
+ expect_iterator_items(i, 5, expect_ci_trees, 5, expect_ci_trees);
+ git_iterator_free(i);
+
git_tree_free(tree);
}