2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
7 #ifndef INCLUDE_iterator_h__
8 #define INCLUDE_iterator_h__
12 #include "git2/index.h"
17 typedef struct git_iterator git_iterator
;
20 GIT_ITERATOR_EMPTY
= 0,
21 GIT_ITERATOR_TREE
= 1,
22 GIT_ITERATOR_INDEX
= 2,
23 GIT_ITERATOR_WORKDIR
= 3,
28 /** ignore case for entry sort order */
29 GIT_ITERATOR_IGNORE_CASE
= (1u << 0),
30 /** force case sensitivity for entry sort order */
31 GIT_ITERATOR_DONT_IGNORE_CASE
= (1u << 1),
32 /** return tree items in addition to blob items */
33 GIT_ITERATOR_INCLUDE_TREES
= (1u << 2),
34 /** don't flatten trees, requiring advance_into (implies INCLUDE_TREES) */
35 GIT_ITERATOR_DONT_AUTOEXPAND
= (1u << 3),
36 /** convert precomposed unicode to decomposed unicode */
37 GIT_ITERATOR_PRECOMPOSE_UNICODE
= (1u << 4),
38 /** never convert precomposed unicode to decomposed unicode */
39 GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE
= (1u << 5),
40 /** include conflicts */
41 GIT_ITERATOR_INCLUDE_CONFLICTS
= (1u << 6),
42 /** descend into symlinked directories */
43 GIT_ITERATOR_DESCEND_SYMLINKS
= (1u << 7),
44 /** hash files in workdir or filesystem iterators */
45 GIT_ITERATOR_INCLUDE_HASH
= (1u << 8),
46 } git_iterator_flag_t
;
49 GIT_ITERATOR_STATUS_NORMAL
= 0,
50 GIT_ITERATOR_STATUS_IGNORED
= 1,
51 GIT_ITERATOR_STATUS_EMPTY
= 2,
52 GIT_ITERATOR_STATUS_FILTERED
= 3
53 } git_iterator_status_t
;
59 /* paths to include in the iterator (literal). if set, any paths not
60 * listed here will be excluded from iteration.
62 git_strarray pathlist
;
64 /* flags, from above */
66 } git_iterator_options
;
68 #define GIT_ITERATOR_OPTIONS_INIT {0}
71 int (*current
)(const git_index_entry
**, git_iterator
*);
72 int (*advance
)(const git_index_entry
**, git_iterator
*);
73 int (*advance_into
)(const git_index_entry
**, git_iterator
*);
75 const git_index_entry
**, git_iterator_status_t
*, git_iterator
*);
76 int (*reset
)(git_iterator
*);
77 void (*free
)(git_iterator
*);
78 } git_iterator_callbacks
;
82 git_iterator_callbacks
*cb
;
96 size_t pathlist_walk_idx
;
97 int (*strcomp
)(const char *a
, const char *b
);
98 int (*strncomp
)(const char *a
, const char *b
, size_t n
);
99 int (*prefixcomp
)(const char *str
, const char *prefix
);
100 int (*entry_srch
)(const void *key
, const void *array_member
);
105 extern int git_iterator_for_nothing(
107 git_iterator_options
*options
);
109 /* tree iterators will match the ignore_case value from the index of the
110 * repository, unless you override with a non-zero flag value
112 extern int git_iterator_for_tree(
115 git_iterator_options
*options
);
117 /* index iterators will take the ignore_case value from the index; the
118 * ignore_case flags are not used
120 extern int git_iterator_for_index(
122 git_repository
*repo
,
124 git_iterator_options
*options
);
126 extern int git_iterator_for_workdir_ext(
128 git_repository
*repo
,
129 const char *repo_workdir
,
132 git_iterator_options
*options
);
134 /* workdir iterators will match the ignore_case value from the index of the
135 * repository, unless you override with a non-zero flag value
137 GIT_INLINE(int) git_iterator_for_workdir(
139 git_repository
*repo
,
142 git_iterator_options
*options
)
144 return git_iterator_for_workdir_ext(out
, repo
, NULL
, index
, tree
, options
);
147 /* for filesystem iterators, you have to explicitly pass in the ignore_case
148 * behavior that you desire
150 extern int git_iterator_for_filesystem(
153 git_iterator_options
*options
);
155 extern void git_iterator_free(git_iterator
*iter
);
157 /* Return a git_index_entry structure for the current value the iterator
158 * is looking at or NULL if the iterator is at the end.
160 * The entry may noy be fully populated. Tree iterators will only have a
161 * value mode, OID, and path. Workdir iterators will not have an OID (but
162 * you can use `git_iterator_current_oid()` to calculate it on demand).
164 * You do not need to free the entry. It is still "owned" by the iterator.
165 * Once you call `git_iterator_advance()` then the old entry is no longer
166 * guaranteed to be valid - it may be freed or just overwritten in place.
168 GIT_INLINE(int) git_iterator_current(
169 const git_index_entry
**entry
, git_iterator
*iter
)
171 return iter
->cb
->current(entry
, iter
);
175 * Advance to the next item for the iterator.
177 * If GIT_ITERATOR_INCLUDE_TREES is set, this may be a tree item. If
178 * GIT_ITERATOR_DONT_AUTOEXPAND is set, calling this again when on a tree
179 * item will skip over all the items under that tree.
181 GIT_INLINE(int) git_iterator_advance(
182 const git_index_entry
**entry
, git_iterator
*iter
)
184 return iter
->cb
->advance(entry
, iter
);
188 * Iterate into a tree item (when GIT_ITERATOR_DONT_AUTOEXPAND is set).
190 * git_iterator_advance() steps through all items being iterated over
191 * (either with or without trees, depending on GIT_ITERATOR_INCLUDE_TREES),
192 * but if GIT_ITERATOR_DONT_AUTOEXPAND is set, it will skip to the next
193 * sibling of a tree instead of going to the first child of the tree. In
194 * that case, use this function to advance to the first child of the tree.
196 * If the current item is not a tree, this is a no-op.
198 * For filesystem and working directory iterators, a tree (i.e. directory)
199 * can be empty. In that case, this function returns GIT_ENOTFOUND and
200 * does not advance. That can't happen for tree and index iterators.
202 GIT_INLINE(int) git_iterator_advance_into(
203 const git_index_entry
**entry
, git_iterator
*iter
)
205 return iter
->cb
->advance_into(entry
, iter
);
208 /* Advance over a directory and check if it contains no files or just
211 * In a tree or the index, all directories will contain files, but in the
212 * working directory it is possible to have an empty directory tree or a
213 * tree that only contains ignored files. Many Git operations treat these
214 * cases specially. This advances over a directory (presumably an
215 * untracked directory) but checks during the scan if there are any files
216 * and any non-ignored files.
218 GIT_INLINE(int) git_iterator_advance_over(
219 const git_index_entry
**entry
,
220 git_iterator_status_t
*status
,
223 return iter
->cb
->advance_over(entry
, status
, iter
);
227 * Go back to the start of the iteration.
229 GIT_INLINE(int) git_iterator_reset(git_iterator
*iter
)
231 return iter
->cb
->reset(iter
);
235 * Go back to the start of the iteration after updating the `start` and
236 * `end` pathname boundaries of the iteration.
238 extern int git_iterator_reset_range(
239 git_iterator
*iter
, const char *start
, const char *end
);
241 GIT_INLINE(git_iterator_t
) git_iterator_type(git_iterator
*iter
)
246 GIT_INLINE(git_repository
*) git_iterator_owner(git_iterator
*iter
)
251 GIT_INLINE(git_index
*) git_iterator_index(git_iterator
*iter
)
256 GIT_INLINE(git_iterator_flag_t
) git_iterator_flags(git_iterator
*iter
)
261 GIT_INLINE(bool) git_iterator_ignore_case(git_iterator
*iter
)
263 return ((iter
->flags
& GIT_ITERATOR_IGNORE_CASE
) != 0);
266 extern int git_iterator_set_ignore_case(
267 git_iterator
*iter
, bool ignore_case
);
269 extern int git_iterator_current_tree_entry(
270 const git_tree_entry
**entry_out
, git_iterator
*iter
);
272 extern int git_iterator_current_parent_tree(
273 const git_tree
**tree_out
, git_iterator
*iter
, size_t depth
);
275 extern bool git_iterator_current_is_ignored(git_iterator
*iter
);
277 extern bool git_iterator_current_tree_is_ignored(git_iterator
*iter
);
280 * Get full path of the current item from a workdir iterator. This will
281 * return NULL for a non-workdir iterator. The git_buf is still owned by
282 * the iterator; this is exposed just for efficiency.
284 extern int git_iterator_current_workdir_path(
285 git_buf
**path
, git_iterator
*iter
);
288 * Retrieve the index stored in the iterator.
290 * Only implemented for the workdir and index iterators.
292 extern git_index
*git_iterator_index(git_iterator
*iter
);
294 typedef int (*git_iterator_foreach_cb
)(
295 const git_index_entry
*entry
,
299 * Walk the given iterator and invoke the callback for each path
300 * contained in the iterator.
302 extern int git_iterator_foreach(
303 git_iterator
*iterator
,
304 git_iterator_foreach_cb cb
,
307 typedef int (*git_iterator_walk_cb
)(
308 const git_index_entry
**entries
,
312 * Walk the given iterators in lock-step. The given callback will be
313 * called for each unique path, with the index entry in each iterator
314 * (or NULL if the given iterator does not contain that path).
316 extern int git_iterator_walk(
317 git_iterator
**iterators
,
319 git_iterator_walk_cb cb
,