aboutsummaryrefslogtreecommitdiffstats
path: root/dir-iterator.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2025-03-12 16:56:15 +0100
committerJunio C Hamano <gitster@pobox.com>2025-03-12 11:31:18 -0700
commitcec2b6f55a805c010d2acc81abf4cbc41b712130 (patch)
tree5139aa10a4a9ad793ff4926eddebeb877d318415 /dir-iterator.c
parentrefs: stop re-verifying common prefixes for availability (diff)
downloadgit-cec2b6f55a805c010d2acc81abf4cbc41b712130.tar.gz
git-cec2b6f55a805c010d2acc81abf4cbc41b712130.zip
refs/iterator: separate lifecycle from iteration
The ref and reflog iterators have their lifecycle attached to iteration: once the iterator reaches its end, it is automatically released and the caller doesn't have to care about that anymore. When the iterator should be released before it has been exhausted, callers must explicitly abort the iterator via `ref_iterator_abort()`. This lifecycle is somewhat unusual in the Git codebase and creates two problems: - Callsites need to be very careful about when exactly they call `ref_iterator_abort()`, as calling the function is only valid when the iterator itself still is. This leads to somewhat awkward calling patterns in some situations. - It is impossible to reuse iterators and re-seek them to a different prefix. This feature isn't supported by any iterator implementation except for the reftable iterators anyway, but if it was implemented it would allow us to optimize cases where we need to search for specific references repeatedly by reusing internal state. Detangle the lifecycle from iteration so that we don't deallocate the iterator anymore once it is exhausted. Instead, callers are now expected to always call a newly introduce `ref_iterator_free()` function that deallocates the iterator and its internal state. Note that the `dir_iterator` is somewhat special because it does not implement the `ref_iterator` interface, but is only used to implement other iterators. Consequently, we have to provide `dir_iterator_free()` instead of `dir_iterator_release()` as the allocated structure itself is managed by the `dir_iterator` interfaces, as well, and not freed by `ref_iterator_free()` like in all the other cases. While at it, drop the return value of `ref_iterator_abort()`, which wasn't really required by any of the iterator implementations anyway. Furthermore, stop calling `base_ref_iterator_free()` in any of the backends, but instead call it in `ref_iterator_free()`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'dir-iterator.c')
-rw-r--r--dir-iterator.c24
1 files changed, 11 insertions, 13 deletions
diff --git a/dir-iterator.c b/dir-iterator.c
index de619846f2..857e1d9bda 100644
--- a/dir-iterator.c
+++ b/dir-iterator.c
@@ -193,9 +193,9 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)
if (S_ISDIR(iter->base.st.st_mode) && push_level(iter)) {
if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC)
- goto error_out;
+ return ITER_ERROR;
if (iter->levels_nr == 0)
- goto error_out;
+ return ITER_ERROR;
}
/* Loop until we find an entry that we can give back to the caller. */
@@ -211,11 +211,11 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)
int ret = next_directory_entry(level->dir, iter->base.path.buf, &de);
if (ret < 0) {
if (iter->flags & DIR_ITERATOR_PEDANTIC)
- goto error_out;
+ return ITER_ERROR;
continue;
} else if (ret > 0) {
if (pop_level(iter) == 0)
- return dir_iterator_abort(dir_iterator);
+ return ITER_DONE;
continue;
}
@@ -223,7 +223,7 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)
} else {
if (level->entries_idx >= level->entries.nr) {
if (pop_level(iter) == 0)
- return dir_iterator_abort(dir_iterator);
+ return ITER_DONE;
continue;
}
@@ -232,22 +232,21 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)
if (prepare_next_entry_data(iter, name)) {
if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC)
- goto error_out;
+ return ITER_ERROR;
continue;
}
return ITER_OK;
}
-
-error_out:
- dir_iterator_abort(dir_iterator);
- return ITER_ERROR;
}
-int dir_iterator_abort(struct dir_iterator *dir_iterator)
+void dir_iterator_free(struct dir_iterator *dir_iterator)
{
struct dir_iterator_int *iter = (struct dir_iterator_int *)dir_iterator;
+ if (!iter)
+ return;
+
for (; iter->levels_nr; iter->levels_nr--) {
struct dir_iterator_level *level =
&iter->levels[iter->levels_nr - 1];
@@ -266,7 +265,6 @@ int dir_iterator_abort(struct dir_iterator *dir_iterator)
free(iter->levels);
strbuf_release(&iter->base.path);
free(iter);
- return ITER_DONE;
}
struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags)
@@ -301,7 +299,7 @@ struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags)
return dir_iterator;
error_out:
- dir_iterator_abort(dir_iterator);
+ dir_iterator_free(dir_iterator);
errno = saved_errno;
return NULL;
}