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.
13 #define GIT_ITERATOR_FIRST_ACCESS (1 << 15)
14 #define GIT_ITERATOR_HONOR_IGNORES (1 << 16)
15 #define GIT_ITERATOR_IGNORE_DOT_GIT (1 << 17)
17 #define iterator__flag(I,F) ((((git_iterator *)(I))->flags & GIT_ITERATOR_ ## F) != 0)
18 #define iterator__ignore_case(I) iterator__flag(I,IGNORE_CASE)
19 #define iterator__include_trees(I) iterator__flag(I,INCLUDE_TREES)
20 #define iterator__dont_autoexpand(I) iterator__flag(I,DONT_AUTOEXPAND)
21 #define iterator__do_autoexpand(I) !iterator__flag(I,DONT_AUTOEXPAND)
22 #define iterator__include_conflicts(I) iterator__flag(I,INCLUDE_CONFLICTS)
23 #define iterator__has_been_accessed(I) iterator__flag(I,FIRST_ACCESS)
24 #define iterator__honor_ignores(I) iterator__flag(I,HONOR_IGNORES)
25 #define iterator__ignore_dot_git(I) iterator__flag(I,IGNORE_DOT_GIT)
26 #define iterator__descend_symlinks(I) iterator__flag(I,DESCEND_SYMLINKS)
29 static void iterator_set_ignore_case(git_iterator
*iter
, bool ignore_case
)
32 iter
->flags
|= GIT_ITERATOR_IGNORE_CASE
;
34 iter
->flags
&= ~GIT_ITERATOR_IGNORE_CASE
;
36 iter
->strcomp
= ignore_case
? git__strcasecmp
: git__strcmp
;
37 iter
->strncomp
= ignore_case
? git__strncasecmp
: git__strncmp
;
38 iter
->prefixcomp
= ignore_case
? git__prefixcmp_icase
: git__prefixcmp
;
39 iter
->entry_srch
= ignore_case
? git_index_entry_isrch
: git_index_entry_srch
;
41 git_vector_set_cmp(&iter
->pathlist
, (git_vector_cmp
)iter
->strcomp
);
44 static int iterator_range_init(
45 git_iterator
*iter
, const char *start
, const char *end
)
47 if (start
&& *start
) {
48 iter
->start
= git__strdup(start
);
49 GIT_ERROR_CHECK_ALLOC(iter
->start
);
51 iter
->start_len
= strlen(iter
->start
);
55 iter
->end
= git__strdup(end
);
56 GIT_ERROR_CHECK_ALLOC(iter
->end
);
58 iter
->end_len
= strlen(iter
->end
);
61 iter
->started
= (iter
->start
== NULL
);
67 static void iterator_range_free(git_iterator
*iter
)
70 git__free(iter
->start
);
82 static int iterator_reset_range(
83 git_iterator
*iter
, const char *start
, const char *end
)
85 iterator_range_free(iter
);
86 return iterator_range_init(iter
, start
, end
);
89 static int iterator_pathlist_init(git_iterator
*iter
, git_strarray
*pathlist
)
93 if (git_vector_init(&iter
->pathlist
, pathlist
->count
, NULL
) < 0)
96 for (i
= 0; i
< pathlist
->count
; i
++) {
97 if (!pathlist
->strings
[i
])
100 if (git_vector_insert(&iter
->pathlist
, pathlist
->strings
[i
]) < 0)
107 static int iterator_init_common(
109 git_repository
*repo
,
111 git_iterator_options
*given_opts
)
113 static git_iterator_options default_opts
= GIT_ITERATOR_OPTIONS_INIT
;
114 git_iterator_options
*options
= given_opts
? given_opts
: &default_opts
;
121 iter
->flags
= options
->flags
;
123 if ((iter
->flags
& GIT_ITERATOR_IGNORE_CASE
) != 0) {
125 } else if ((iter
->flags
& GIT_ITERATOR_DONT_IGNORE_CASE
) != 0) {
130 if ((error
= git_repository_index__weakptr(&index
, iter
->repo
)) < 0)
133 ignore_case
= !!index
->ignore_case
;
135 if (ignore_case
== 1)
136 iter
->flags
|= GIT_ITERATOR_IGNORE_CASE
;
138 iter
->flags
|= GIT_ITERATOR_DONT_IGNORE_CASE
;
143 /* try to look up precompose and set flag if appropriate */
145 (iter
->flags
& GIT_ITERATOR_PRECOMPOSE_UNICODE
) == 0 &&
146 (iter
->flags
& GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE
) == 0) {
148 if (git_repository__configmap_lookup(&precompose
, repo
, GIT_CONFIGMAP_PRECOMPOSE
) < 0)
151 iter
->flags
|= GIT_ITERATOR_PRECOMPOSE_UNICODE
;
154 if ((iter
->flags
& GIT_ITERATOR_DONT_AUTOEXPAND
))
155 iter
->flags
|= GIT_ITERATOR_INCLUDE_TREES
;
157 if ((error
= iterator_range_init(iter
, options
->start
, options
->end
)) < 0 ||
158 (error
= iterator_pathlist_init(iter
, &options
->pathlist
)) < 0)
161 iterator_set_ignore_case(iter
, ignore_case
);
165 static void iterator_clear(git_iterator
*iter
)
167 iter
->started
= false;
169 iter
->stat_calls
= 0;
170 iter
->pathlist_walk_idx
= 0;
171 iter
->flags
&= ~GIT_ITERATOR_FIRST_ACCESS
;
174 GIT_INLINE(bool) iterator_has_started(
175 git_iterator
*iter
, const char *path
, bool is_submodule
)
179 if (iter
->start
== NULL
|| iter
->started
== true)
182 /* the starting path is generally a prefix - we have started once we
183 * are prefixed by this path
185 iter
->started
= (iter
->prefixcomp(path
, iter
->start
) >= 0);
190 path_len
= strlen(path
);
192 /* if, however, we are a submodule, then we support `start` being
193 * suffixed with a `/` for crazy legacy reasons. match `submod`
194 * with a start path of `submod/`.
196 if (is_submodule
&& iter
->start_len
&& path_len
== iter
->start_len
- 1 &&
197 iter
->start
[iter
->start_len
-1] == '/')
200 /* if, however, our current path is a directory, and our starting path
201 * is _beneath_ that directory, then recurse into the directory (even
202 * though we have not yet "started")
204 if (path_len
> 0 && path
[path_len
-1] == '/' &&
205 iter
->strncomp(path
, iter
->start
, path_len
) == 0)
211 GIT_INLINE(bool) iterator_has_ended(git_iterator
*iter
, const char *path
)
213 if (iter
->end
== NULL
)
215 else if (iter
->ended
)
218 iter
->ended
= (iter
->prefixcomp(path
, iter
->end
) > 0);
222 /* walker for the index and tree iterator that allows it to walk the sorted
223 * pathlist entries alongside sorted iterator entries.
225 static bool iterator_pathlist_next_is(git_iterator
*iter
, const char *path
)
228 size_t path_len
, p_len
, cmp_len
, i
;
231 if (iter
->pathlist
.length
== 0)
234 git_vector_sort(&iter
->pathlist
);
236 path_len
= strlen(path
);
238 /* for comparison, drop the trailing slash on the current '/' */
239 if (path_len
&& path
[path_len
-1] == '/')
242 for (i
= iter
->pathlist_walk_idx
; i
< iter
->pathlist
.length
; i
++) {
243 p
= iter
->pathlist
.contents
[i
];
246 if (p_len
&& p
[p_len
-1] == '/')
249 cmp_len
= min(path_len
, p_len
);
251 /* see if the pathlist entry is a prefix of this path */
252 cmp
= iter
->strncomp(p
, path
, cmp_len
);
254 /* prefix match - see if there's an exact match, or if we were
255 * given a path that matches the directory
258 /* if this pathlist entry is not suffixed with a '/' then
259 * it matches a path that is a file or a directory.
260 * (eg, pathlist = "foo" and path is "foo" or "foo/" or
263 if (p
[cmp_len
] == '\0' &&
264 (path
[cmp_len
] == '\0' || path
[cmp_len
] == '/'))
267 /* if this pathlist entry _is_ suffixed with a '/' then
268 * it matches only paths that are directories.
269 * (eg, pathlist = "foo/" and path is "foo/" or "foo/something")
271 if (p
[cmp_len
] == '/' && path
[cmp_len
] == '/')
275 /* this pathlist entry sorts before the given path, try the next */
277 iter
->pathlist_walk_idx
++;
281 /* this pathlist sorts after the given path, no match. */
291 ITERATOR_PATHLIST_NONE
= 0,
292 ITERATOR_PATHLIST_IS_FILE
= 1,
293 ITERATOR_PATHLIST_IS_DIR
= 2,
294 ITERATOR_PATHLIST_IS_PARENT
= 3,
295 ITERATOR_PATHLIST_FULL
= 4,
296 } iterator_pathlist_search_t
;
298 static iterator_pathlist_search_t
iterator_pathlist_search(
299 git_iterator
*iter
, const char *path
, size_t path_len
)
305 if (iter
->pathlist
.length
== 0)
306 return ITERATOR_PATHLIST_FULL
;
308 git_vector_sort(&iter
->pathlist
);
310 error
= git_vector_bsearch2(&idx
, &iter
->pathlist
,
311 (git_vector_cmp
)iter
->strcomp
, path
);
313 /* the given path was found in the pathlist. since the pathlist only
314 * matches directories when they're suffixed with a '/', analyze the
315 * path string to determine whether it's a directory or not.
318 if (path_len
&& path
[path_len
-1] == '/')
319 return ITERATOR_PATHLIST_IS_DIR
;
321 return ITERATOR_PATHLIST_IS_FILE
;
324 /* at this point, the path we're examining may be a directory (though we
325 * don't know that yet, since we're avoiding a stat unless it's necessary)
326 * so walk the pathlist looking for the given path with a '/' after it,
328 while ((p
= git_vector_get(&iter
->pathlist
, idx
)) != NULL
) {
329 if (iter
->prefixcomp(p
, path
) != 0)
332 /* an exact match would have been matched by the bsearch above */
333 GIT_ASSERT_WITH_RETVAL(p
[path_len
], ITERATOR_PATHLIST_NONE
);
335 /* is this a literal directory entry (eg `foo/`) or a file beneath */
336 if (p
[path_len
] == '/') {
337 return (p
[path_len
+1] == '\0') ?
338 ITERATOR_PATHLIST_IS_DIR
:
339 ITERATOR_PATHLIST_IS_PARENT
;
342 if (p
[path_len
] > '/')
348 return ITERATOR_PATHLIST_NONE
;
353 static int empty_iterator_noop(const git_index_entry
**e
, git_iterator
*i
)
363 static int empty_iterator_advance_over(
364 const git_index_entry
**e
,
365 git_iterator_status_t
*s
,
368 *s
= GIT_ITERATOR_STATUS_EMPTY
;
369 return empty_iterator_noop(e
, i
);
372 static int empty_iterator_reset(git_iterator
*i
)
378 static void empty_iterator_free(git_iterator
*i
)
385 git_iterator_callbacks cb
;
388 int git_iterator_for_nothing(
390 git_iterator_options
*options
)
392 empty_iterator
*iter
;
394 static git_iterator_callbacks callbacks
= {
398 empty_iterator_advance_over
,
399 empty_iterator_reset
,
405 iter
= git__calloc(1, sizeof(empty_iterator
));
406 GIT_ERROR_CHECK_ALLOC(iter
);
408 iter
->base
.type
= GIT_ITERATOR_EMPTY
;
409 iter
->base
.cb
= &callbacks
;
410 iter
->base
.flags
= options
->flags
;
419 git_tree_entry
*tree_entry
;
420 const char *parent_path
;
421 } tree_iterator_entry
;
426 /* path to this particular frame (folder) */
429 /* a sorted list of the entries for this frame (folder), these are
430 * actually pointers to the iterator's entry pool.
433 tree_iterator_entry
*current
;
437 /* on case insensitive iterations, we also have an array of other
438 * paths that were case insensitively equal to this one, and their
439 * tree objects. we have coalesced the tree entries into this frame.
440 * a child `tree_iterator_entry` will contain a pointer to its actual
443 git_vector similar_trees
;
444 git_array_t(git_buf
) similar_paths
;
445 } tree_iterator_frame
;
450 git_array_t(tree_iterator_frame
) frames
;
452 git_index_entry entry
;
455 /* a pool of entries to reduce the number of allocations */
459 GIT_INLINE(tree_iterator_frame
*) tree_iterator_parent_frame(
462 return iter
->frames
.size
> 1 ?
463 &iter
->frames
.ptr
[iter
->frames
.size
-2] : NULL
;
466 GIT_INLINE(tree_iterator_frame
*) tree_iterator_current_frame(
469 return iter
->frames
.size
? &iter
->frames
.ptr
[iter
->frames
.size
-1] : NULL
;
472 GIT_INLINE(int) tree_entry_cmp(
473 const git_tree_entry
*a
, const git_tree_entry
*b
, bool icase
)
476 a
->filename
, a
->filename_len
, a
->attr
== GIT_FILEMODE_TREE
,
477 b
->filename
, b
->filename_len
, b
->attr
== GIT_FILEMODE_TREE
,
478 icase
? git__strncasecmp
: git__strncmp
);
481 GIT_INLINE(int) tree_iterator_entry_cmp_icase(
482 const void *ptr_a
, const void *ptr_b
)
484 const tree_iterator_entry
*a
= (const tree_iterator_entry
*)ptr_a
;
485 const tree_iterator_entry
*b
= (const tree_iterator_entry
*)ptr_b
;
487 return tree_entry_cmp(a
->tree_entry
, b
->tree_entry
, true);
490 static int tree_iterator_entry_sort_icase(const void *ptr_a
, const void *ptr_b
)
492 const tree_iterator_entry
*a
= (const tree_iterator_entry
*)ptr_a
;
493 const tree_iterator_entry
*b
= (const tree_iterator_entry
*)ptr_b
;
495 int c
= tree_entry_cmp(a
->tree_entry
, b
->tree_entry
, true);
497 /* stabilize the sort order for filenames that are (case insensitively)
498 * the same by examining the parent path (case sensitively) before
499 * falling back to a case sensitive sort of the filename.
501 if (!c
&& a
->parent_path
!= b
->parent_path
)
502 c
= git__strcmp(a
->parent_path
, b
->parent_path
);
505 c
= tree_entry_cmp(a
->tree_entry
, b
->tree_entry
, false);
510 static int tree_iterator_compute_path(
512 tree_iterator_entry
*entry
)
516 if (entry
->parent_path
)
517 git_buf_joinpath(out
, entry
->parent_path
, entry
->tree_entry
->filename
);
519 git_buf_puts(out
, entry
->tree_entry
->filename
);
521 if (git_tree_entry__is_tree(entry
->tree_entry
))
522 git_buf_putc(out
, '/');
524 if (git_buf_oom(out
))
530 static int tree_iterator_frame_init(
533 tree_iterator_entry
*frame_entry
)
535 tree_iterator_frame
*new_frame
= NULL
;
536 tree_iterator_entry
*new_entry
;
537 git_tree
*dup
= NULL
;
538 git_tree_entry
*tree_entry
;
543 new_frame
= git_array_alloc(iter
->frames
);
544 GIT_ERROR_CHECK_ALLOC(new_frame
);
546 if ((error
= git_tree_dup(&dup
, tree
)) < 0)
549 memset(new_frame
, 0x0, sizeof(tree_iterator_frame
));
550 new_frame
->tree
= dup
;
553 (error
= tree_iterator_compute_path(&new_frame
->path
, frame_entry
)) < 0)
556 cmp
= iterator__ignore_case(&iter
->base
) ?
557 tree_iterator_entry_sort_icase
: NULL
;
559 if ((error
= git_vector_init(&new_frame
->entries
,
560 dup
->entries
.size
, cmp
)) < 0)
563 git_array_foreach(dup
->entries
, i
, tree_entry
) {
564 if ((new_entry
= git_pool_malloc(&iter
->entry_pool
, 1)) == NULL
) {
570 new_entry
->tree_entry
= tree_entry
;
571 new_entry
->parent_path
= new_frame
->path
.ptr
;
573 if ((error
= git_vector_insert(&new_frame
->entries
, new_entry
)) < 0)
577 git_vector_set_sorted(&new_frame
->entries
,
578 !iterator__ignore_case(&iter
->base
));
583 git_array_pop(iter
->frames
);
589 GIT_INLINE(tree_iterator_entry
*) tree_iterator_current_entry(
590 tree_iterator_frame
*frame
)
592 return frame
->current
;
595 GIT_INLINE(int) tree_iterator_frame_push_neighbors(
597 tree_iterator_frame
*parent_frame
,
598 tree_iterator_frame
*frame
,
599 const char *filename
)
601 tree_iterator_entry
*entry
, *new_entry
;
602 git_tree
*tree
= NULL
;
603 git_tree_entry
*tree_entry
;
608 while (parent_frame
->next_idx
< parent_frame
->entries
.length
) {
609 entry
= parent_frame
->entries
.contents
[parent_frame
->next_idx
];
611 if (strcasecmp(filename
, entry
->tree_entry
->filename
) != 0)
614 if ((error
= git_tree_lookup(&tree
,
615 iter
->base
.repo
, entry
->tree_entry
->oid
)) < 0)
618 if (git_vector_insert(&parent_frame
->similar_trees
, tree
) < 0)
621 path
= git_array_alloc(parent_frame
->similar_paths
);
622 GIT_ERROR_CHECK_ALLOC(path
);
624 memset(path
, 0, sizeof(git_buf
));
626 if ((error
= tree_iterator_compute_path(path
, entry
)) < 0)
629 GIT_ERROR_CHECK_ALLOC_ADD(&new_size
,
630 frame
->entries
.length
, tree
->entries
.size
);
631 git_vector_size_hint(&frame
->entries
, new_size
);
633 git_array_foreach(tree
->entries
, i
, tree_entry
) {
634 new_entry
= git_pool_malloc(&iter
->entry_pool
, 1);
635 GIT_ERROR_CHECK_ALLOC(new_entry
);
637 new_entry
->tree_entry
= tree_entry
;
638 new_entry
->parent_path
= path
->ptr
;
640 if ((error
= git_vector_insert(&frame
->entries
, new_entry
)) < 0)
647 parent_frame
->next_idx
++;
653 GIT_INLINE(int) tree_iterator_frame_push(
654 tree_iterator
*iter
, tree_iterator_entry
*entry
)
656 tree_iterator_frame
*parent_frame
, *frame
;
657 git_tree
*tree
= NULL
;
660 if ((error
= git_tree_lookup(&tree
,
661 iter
->base
.repo
, entry
->tree_entry
->oid
)) < 0 ||
662 (error
= tree_iterator_frame_init(iter
, tree
, entry
)) < 0)
665 parent_frame
= tree_iterator_parent_frame(iter
);
666 frame
= tree_iterator_current_frame(iter
);
668 /* if we're case insensitive, then we may have another directory that
669 * is (case insensitively) equal to this one. coalesce those children
672 if (iterator__ignore_case(&iter
->base
))
673 error
= tree_iterator_frame_push_neighbors(iter
,
674 parent_frame
, frame
, entry
->tree_entry
->filename
);
681 static int tree_iterator_frame_pop(tree_iterator
*iter
)
683 tree_iterator_frame
*frame
;
688 GIT_ASSERT(iter
->frames
.size
);
690 frame
= git_array_pop(iter
->frames
);
692 git_vector_free(&frame
->entries
);
693 git_tree_free(frame
->tree
);
696 buf
= git_array_pop(frame
->similar_paths
);
697 git_buf_dispose(buf
);
698 } while (buf
!= NULL
);
700 git_array_clear(frame
->similar_paths
);
702 git_vector_foreach(&frame
->similar_trees
, i
, tree
)
705 git_vector_free(&frame
->similar_trees
);
707 git_buf_dispose(&frame
->path
);
712 static int tree_iterator_current(
713 const git_index_entry
**out
, git_iterator
*i
)
715 tree_iterator
*iter
= (tree_iterator
*)i
;
717 if (!iterator__has_been_accessed(i
))
718 return iter
->base
.cb
->advance(out
, i
);
720 if (!iter
->frames
.size
) {
729 static void tree_iterator_set_current(
731 tree_iterator_frame
*frame
,
732 tree_iterator_entry
*entry
)
734 git_tree_entry
*tree_entry
= entry
->tree_entry
;
736 frame
->current
= entry
;
738 memset(&iter
->entry
, 0x0, sizeof(git_index_entry
));
740 iter
->entry
.mode
= tree_entry
->attr
;
741 iter
->entry
.path
= iter
->entry_path
.ptr
;
742 git_oid_cpy(&iter
->entry
.id
, tree_entry
->oid
);
745 static int tree_iterator_advance(const git_index_entry
**out
, git_iterator
*i
)
747 tree_iterator
*iter
= (tree_iterator
*)i
;
750 iter
->base
.flags
|= GIT_ITERATOR_FIRST_ACCESS
;
752 /* examine tree entries until we find the next one to return */
754 tree_iterator_entry
*prev_entry
, *entry
;
755 tree_iterator_frame
*frame
;
758 if ((frame
= tree_iterator_current_frame(iter
)) == NULL
) {
759 error
= GIT_ITEROVER
;
763 /* no more entries in this frame. pop the frame out */
764 if (frame
->next_idx
== frame
->entries
.length
) {
765 if ((error
= tree_iterator_frame_pop(iter
)) < 0)
771 /* we may have coalesced the contents of case-insensitively same-named
772 * directories, so do the sort now.
774 if (frame
->next_idx
== 0 && !git_vector_is_sorted(&frame
->entries
))
775 git_vector_sort(&frame
->entries
);
777 /* we have more entries in the current frame, that's our next entry */
778 prev_entry
= tree_iterator_current_entry(frame
);
779 entry
= frame
->entries
.contents
[frame
->next_idx
];
782 /* we can have collisions when iterating case insensitively. (eg,
783 * 'A/a' and 'a/A'). squash this one if it's already been seen.
785 if (iterator__ignore_case(&iter
->base
) &&
787 tree_iterator_entry_cmp_icase(prev_entry
, entry
) == 0)
790 if ((error
= tree_iterator_compute_path(&iter
->entry_path
, entry
)) < 0)
793 /* if this path is before our start, advance over this entry */
794 if (!iterator_has_started(&iter
->base
, iter
->entry_path
.ptr
, false))
797 /* if this path is after our end, stop */
798 if (iterator_has_ended(&iter
->base
, iter
->entry_path
.ptr
)) {
799 error
= GIT_ITEROVER
;
803 /* if we have a list of paths we're interested in, examine it */
804 if (!iterator_pathlist_next_is(&iter
->base
, iter
->entry_path
.ptr
))
807 is_tree
= git_tree_entry__is_tree(entry
->tree_entry
);
809 /* if we are *not* including trees then advance over this entry */
810 if (is_tree
&& !iterator__include_trees(iter
)) {
812 /* if we've found a tree (and are not returning it to the caller)
813 * and we are autoexpanding, then we want to return the first
814 * child. push the new directory and advance.
816 if (iterator__do_autoexpand(iter
)) {
817 if ((error
= tree_iterator_frame_push(iter
, entry
)) < 0)
824 tree_iterator_set_current(iter
, frame
, entry
);
826 /* if we are autoexpanding, then push this as a new frame, so that
827 * the next call to `advance` will dive into this directory.
829 if (is_tree
&& iterator__do_autoexpand(iter
))
830 error
= tree_iterator_frame_push(iter
, entry
);
836 *out
= (error
== 0) ? &iter
->entry
: NULL
;
841 static int tree_iterator_advance_into(
842 const git_index_entry
**out
, git_iterator
*i
)
844 tree_iterator
*iter
= (tree_iterator
*)i
;
845 tree_iterator_frame
*frame
;
846 tree_iterator_entry
*prev_entry
;
852 if ((frame
= tree_iterator_current_frame(iter
)) == NULL
)
855 /* get the last seen entry */
856 prev_entry
= tree_iterator_current_entry(frame
);
858 /* it's legal to call advance_into when auto-expand is on. in this case,
859 * we will have pushed a new (empty) frame on to the stack for this
860 * new directory. since it's empty, its current_entry should be null.
862 GIT_ASSERT(iterator__do_autoexpand(i
) ^ (prev_entry
!= NULL
));
865 if (!git_tree_entry__is_tree(prev_entry
->tree_entry
))
868 if ((error
= tree_iterator_frame_push(iter
, prev_entry
)) < 0)
872 /* we've advanced into the directory in question, let advance
873 * find the first entry
875 return tree_iterator_advance(out
, i
);
878 static int tree_iterator_advance_over(
879 const git_index_entry
**out
,
880 git_iterator_status_t
*status
,
883 *status
= GIT_ITERATOR_STATUS_NORMAL
;
884 return git_iterator_advance(out
, i
);
887 static void tree_iterator_clear(tree_iterator
*iter
)
889 while (iter
->frames
.size
)
890 tree_iterator_frame_pop(iter
);
892 git_array_clear(iter
->frames
);
894 git_pool_clear(&iter
->entry_pool
);
895 git_buf_clear(&iter
->entry_path
);
897 iterator_clear(&iter
->base
);
900 static int tree_iterator_init(tree_iterator
*iter
)
904 if ((error
= git_pool_init(&iter
->entry_pool
, sizeof(tree_iterator_entry
))) < 0 ||
905 (error
= tree_iterator_frame_init(iter
, iter
->root
, NULL
)) < 0)
908 iter
->base
.flags
&= ~GIT_ITERATOR_FIRST_ACCESS
;
913 static int tree_iterator_reset(git_iterator
*i
)
915 tree_iterator
*iter
= (tree_iterator
*)i
;
917 tree_iterator_clear(iter
);
918 return tree_iterator_init(iter
);
921 static void tree_iterator_free(git_iterator
*i
)
923 tree_iterator
*iter
= (tree_iterator
*)i
;
925 tree_iterator_clear(iter
);
927 git_tree_free(iter
->root
);
928 git_buf_dispose(&iter
->entry_path
);
931 int git_iterator_for_tree(
934 git_iterator_options
*options
)
939 static git_iterator_callbacks callbacks
= {
940 tree_iterator_current
,
941 tree_iterator_advance
,
942 tree_iterator_advance_into
,
943 tree_iterator_advance_over
,
951 return git_iterator_for_nothing(out
, options
);
953 iter
= git__calloc(1, sizeof(tree_iterator
));
954 GIT_ERROR_CHECK_ALLOC(iter
);
956 iter
->base
.type
= GIT_ITERATOR_TREE
;
957 iter
->base
.cb
= &callbacks
;
959 if ((error
= iterator_init_common(&iter
->base
,
960 git_tree_owner(tree
), NULL
, options
)) < 0 ||
961 (error
= git_tree_dup(&iter
->root
, tree
)) < 0 ||
962 (error
= tree_iterator_init(iter
)) < 0)
969 git_iterator_free(&iter
->base
);
973 int git_iterator_current_tree_entry(
974 const git_tree_entry
**tree_entry
, git_iterator
*i
)
977 tree_iterator_frame
*frame
;
978 tree_iterator_entry
*entry
;
980 GIT_ASSERT(i
->type
== GIT_ITERATOR_TREE
);
982 iter
= (tree_iterator
*)i
;
984 frame
= tree_iterator_current_frame(iter
);
985 entry
= tree_iterator_current_entry(frame
);
987 *tree_entry
= entry
->tree_entry
;
991 int git_iterator_current_parent_tree(
992 const git_tree
**parent_tree
, git_iterator
*i
, size_t depth
)
995 tree_iterator_frame
*frame
;
997 GIT_ASSERT(i
->type
== GIT_ITERATOR_TREE
);
999 iter
= (tree_iterator
*)i
;
1001 GIT_ASSERT(depth
< iter
->frames
.size
);
1002 frame
= &iter
->frames
.ptr
[iter
->frames
.size
-depth
-1];
1004 *parent_tree
= frame
->tree
;
1008 /* Filesystem iterator */
1013 iterator_pathlist_search_t match
;
1015 char path
[GIT_FLEX_ARRAY
];
1016 } filesystem_iterator_entry
;
1020 git_pool entry_pool
;
1025 } filesystem_iterator_frame
;
1032 unsigned int dirload_flags
;
1036 git_vector index_snapshot
;
1038 git_array_t(filesystem_iterator_frame
) frames
;
1039 git_ignores ignores
;
1041 /* info about the current entry */
1042 git_index_entry entry
;
1043 git_buf current_path
;
1044 int current_is_ignored
;
1046 /* temporary buffer for advance_over */
1048 } filesystem_iterator
;
1051 GIT_INLINE(filesystem_iterator_frame
*) filesystem_iterator_parent_frame(
1052 filesystem_iterator
*iter
)
1054 return iter
->frames
.size
> 1 ?
1055 &iter
->frames
.ptr
[iter
->frames
.size
-2] : NULL
;
1058 GIT_INLINE(filesystem_iterator_frame
*) filesystem_iterator_current_frame(
1059 filesystem_iterator
*iter
)
1061 return iter
->frames
.size
? &iter
->frames
.ptr
[iter
->frames
.size
-1] : NULL
;
1064 GIT_INLINE(filesystem_iterator_entry
*) filesystem_iterator_current_entry(
1065 filesystem_iterator_frame
*frame
)
1067 return frame
->next_idx
== 0 ?
1068 NULL
: frame
->entries
.contents
[frame
->next_idx
-1];
1071 static int filesystem_iterator_entry_cmp(const void *_a
, const void *_b
)
1073 const filesystem_iterator_entry
*a
= (const filesystem_iterator_entry
*)_a
;
1074 const filesystem_iterator_entry
*b
= (const filesystem_iterator_entry
*)_b
;
1076 return git__strcmp(a
->path
, b
->path
);
1079 static int filesystem_iterator_entry_cmp_icase(const void *_a
, const void *_b
)
1081 const filesystem_iterator_entry
*a
= (const filesystem_iterator_entry
*)_a
;
1082 const filesystem_iterator_entry
*b
= (const filesystem_iterator_entry
*)_b
;
1084 return git__strcasecmp(a
->path
, b
->path
);
1087 #define FILESYSTEM_MAX_DEPTH 100
1090 * Figure out if an entry is a submodule.
1092 * We consider it a submodule if the path is listed as a submodule in
1093 * either the tree or the index.
1095 static int filesystem_iterator_is_submodule(
1096 bool *out
, filesystem_iterator
*iter
, const char *path
, size_t path_len
)
1098 bool is_submodule
= false;
1103 /* first see if this path is a submodule in HEAD */
1105 git_tree_entry
*entry
;
1107 error
= git_tree_entry_bypath(&entry
, iter
->tree
, path
);
1109 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1113 is_submodule
= (entry
->attr
== GIT_FILEMODE_COMMIT
);
1114 git_tree_entry_free(entry
);
1118 if (!is_submodule
&& iter
->base
.index
) {
1121 error
= git_index_snapshot_find(&pos
,
1122 &iter
->index_snapshot
, iter
->base
.entry_srch
, path
, path_len
, 0);
1124 if (error
< 0 && error
!= GIT_ENOTFOUND
)
1128 git_index_entry
*e
= git_vector_get(&iter
->index_snapshot
, pos
);
1129 is_submodule
= (e
->mode
== GIT_FILEMODE_COMMIT
);
1133 *out
= is_submodule
;
1137 static void filesystem_iterator_frame_push_ignores(
1138 filesystem_iterator
*iter
,
1139 filesystem_iterator_entry
*frame_entry
,
1140 filesystem_iterator_frame
*new_frame
)
1142 filesystem_iterator_frame
*previous_frame
;
1143 const char *path
= frame_entry
? frame_entry
->path
: "";
1145 if (!iterator__honor_ignores(&iter
->base
))
1148 if (git_ignore__lookup(&new_frame
->is_ignored
,
1149 &iter
->ignores
, path
, GIT_DIR_FLAG_TRUE
) < 0) {
1151 new_frame
->is_ignored
= GIT_IGNORE_NOTFOUND
;
1154 /* if this is not the top level directory... */
1156 const char *relative_path
;
1158 previous_frame
= filesystem_iterator_parent_frame(iter
);
1160 /* push new ignores for files in this directory */
1161 relative_path
= frame_entry
->path
+ previous_frame
->path_len
;
1163 /* inherit ignored from parent if no rule specified */
1164 if (new_frame
->is_ignored
<= GIT_IGNORE_NOTFOUND
)
1165 new_frame
->is_ignored
= previous_frame
->is_ignored
;
1167 git_ignore__push_dir(&iter
->ignores
, relative_path
);
1171 static void filesystem_iterator_frame_pop_ignores(
1172 filesystem_iterator
*iter
)
1174 if (iterator__honor_ignores(&iter
->base
))
1175 git_ignore__pop_dir(&iter
->ignores
);
1178 GIT_INLINE(bool) filesystem_iterator_examine_path(
1180 iterator_pathlist_search_t
*match_out
,
1181 filesystem_iterator
*iter
,
1182 filesystem_iterator_entry
*frame_entry
,
1187 iterator_pathlist_search_t match
= ITERATOR_PATHLIST_FULL
;
1189 *is_dir_out
= false;
1190 *match_out
= ITERATOR_PATHLIST_NONE
;
1192 if (iter
->base
.start_len
) {
1193 int cmp
= iter
->base
.strncomp(path
, iter
->base
.start
, path_len
);
1195 /* we haven't stat'ed `path` yet, so we don't yet know if it's a
1196 * directory or not. special case if the current path may be a
1197 * directory that matches the start prefix.
1200 if (iter
->base
.start
[path_len
] == '/')
1203 else if (iter
->base
.start
[path_len
] != '\0')
1211 if (iter
->base
.end_len
) {
1212 int cmp
= iter
->base
.strncomp(path
, iter
->base
.end
, iter
->base
.end_len
);
1218 /* if we have a pathlist that we're limiting to, examine this path now
1219 * to avoid a `stat` if we're not interested in the path.
1221 if (iter
->base
.pathlist
.length
) {
1222 /* if our parent was explicitly included, so too are we */
1223 if (frame_entry
&& frame_entry
->match
!= ITERATOR_PATHLIST_IS_PARENT
)
1224 match
= ITERATOR_PATHLIST_FULL
;
1226 match
= iterator_pathlist_search(&iter
->base
, path
, path_len
);
1228 if (match
== ITERATOR_PATHLIST_NONE
)
1231 /* Ensure that the pathlist entry lines up with what we expected */
1232 if (match
== ITERATOR_PATHLIST_IS_DIR
||
1233 match
== ITERATOR_PATHLIST_IS_PARENT
)
1237 *is_dir_out
= is_dir
;
1242 GIT_INLINE(bool) filesystem_iterator_is_dot_git(
1243 filesystem_iterator
*iter
, const char *path
, size_t path_len
)
1247 if (!iterator__ignore_dot_git(&iter
->base
))
1250 if ((len
= path_len
) < 4)
1253 if (path
[len
- 1] == '/')
1256 if (git__tolower(path
[len
- 1]) != 't' ||
1257 git__tolower(path
[len
- 2]) != 'i' ||
1258 git__tolower(path
[len
- 3]) != 'g' ||
1259 git__tolower(path
[len
- 4]) != '.')
1262 return (len
== 4 || path
[len
- 5] == '/');
1265 static int filesystem_iterator_entry_hash(
1266 filesystem_iterator
*iter
,
1267 filesystem_iterator_entry
*entry
)
1269 git_buf fullpath
= GIT_BUF_INIT
;
1272 if (S_ISDIR(entry
->st
.st_mode
)) {
1273 memset(&entry
->id
, 0, GIT_OID_RAWSZ
);
1277 if (iter
->base
.type
== GIT_ITERATOR_WORKDIR
)
1278 return git_repository_hashfile(&entry
->id
,
1279 iter
->base
.repo
, entry
->path
, GIT_OBJECT_BLOB
, NULL
);
1281 if (!(error
= git_buf_joinpath(&fullpath
, iter
->root
, entry
->path
)) &&
1282 !(error
= git_path_validate_workdir_buf(iter
->base
.repo
, &fullpath
)))
1283 error
= git_odb_hashfile(&entry
->id
, fullpath
.ptr
, GIT_OBJECT_BLOB
);
1285 git_buf_dispose(&fullpath
);
1289 static int filesystem_iterator_entry_init(
1290 filesystem_iterator_entry
**out
,
1291 filesystem_iterator
*iter
,
1292 filesystem_iterator_frame
*frame
,
1295 struct stat
*statbuf
,
1296 iterator_pathlist_search_t pathlist_match
)
1298 filesystem_iterator_entry
*entry
;
1304 /* Make sure to append two bytes, one for the path's null
1305 * termination, one for a possible trailing '/' for folders.
1307 GIT_ERROR_CHECK_ALLOC_ADD(&entry_size
,
1308 sizeof(filesystem_iterator_entry
), path_len
);
1309 GIT_ERROR_CHECK_ALLOC_ADD(&entry_size
, entry_size
, 2);
1311 entry
= git_pool_malloc(&frame
->entry_pool
, entry_size
);
1312 GIT_ERROR_CHECK_ALLOC(entry
);
1314 entry
->path_len
= path_len
;
1315 entry
->match
= pathlist_match
;
1316 memcpy(entry
->path
, path
, path_len
);
1317 memcpy(&entry
->st
, statbuf
, sizeof(struct stat
));
1319 /* Suffix directory paths with a '/' */
1320 if (S_ISDIR(entry
->st
.st_mode
))
1321 entry
->path
[entry
->path_len
++] = '/';
1323 entry
->path
[entry
->path_len
] = '\0';
1325 if (iter
->base
.flags
& GIT_ITERATOR_INCLUDE_HASH
)
1326 error
= filesystem_iterator_entry_hash(iter
, entry
);
1334 static int filesystem_iterator_frame_push(
1335 filesystem_iterator
*iter
,
1336 filesystem_iterator_entry
*frame_entry
)
1338 filesystem_iterator_frame
*new_frame
= NULL
;
1339 git_path_diriter diriter
= GIT_PATH_DIRITER_INIT
;
1340 git_buf root
= GIT_BUF_INIT
;
1342 filesystem_iterator_entry
*entry
;
1343 struct stat statbuf
;
1347 if (iter
->frames
.size
== FILESYSTEM_MAX_DEPTH
) {
1348 git_error_set(GIT_ERROR_REPOSITORY
,
1349 "directory nesting too deep (%"PRIuZ
")", iter
->frames
.size
);
1353 new_frame
= git_array_alloc(iter
->frames
);
1354 GIT_ERROR_CHECK_ALLOC(new_frame
);
1356 memset(new_frame
, 0, sizeof(filesystem_iterator_frame
));
1359 git_buf_joinpath(&root
, iter
->root
, frame_entry
->path
);
1361 git_buf_puts(&root
, iter
->root
);
1363 if (git_buf_oom(&root
) ||
1364 git_path_validate_workdir_buf(iter
->base
.repo
, &root
) < 0) {
1369 new_frame
->path_len
= frame_entry
? frame_entry
->path_len
: 0;
1371 /* Any error here is equivalent to the dir not existing, skip over it */
1372 if ((error
= git_path_diriter_init(
1373 &diriter
, root
.ptr
, iter
->dirload_flags
)) < 0) {
1374 error
= GIT_ENOTFOUND
;
1378 if ((error
= git_vector_init(&new_frame
->entries
, 64,
1379 iterator__ignore_case(&iter
->base
) ?
1380 filesystem_iterator_entry_cmp_icase
:
1381 filesystem_iterator_entry_cmp
)) < 0)
1384 if ((error
= git_pool_init(&new_frame
->entry_pool
, 1)) < 0)
1387 /* check if this directory is ignored */
1388 filesystem_iterator_frame_push_ignores(iter
, frame_entry
, new_frame
);
1390 while ((error
= git_path_diriter_next(&diriter
)) == 0) {
1391 iterator_pathlist_search_t pathlist_match
= ITERATOR_PATHLIST_FULL
;
1392 bool dir_expected
= false;
1394 if ((error
= git_path_diriter_fullpath(&path
, &path_len
, &diriter
)) < 0 ||
1395 (error
= git_path_validate_workdir_with_len(iter
->base
.repo
, path
, path_len
)) < 0)
1398 GIT_ASSERT(path_len
> iter
->root_len
);
1400 /* remove the prefix if requested */
1401 path
+= iter
->root_len
;
1402 path_len
-= iter
->root_len
;
1404 /* examine start / end and the pathlist to see if this path is in it.
1405 * note that since we haven't yet stat'ed the path, we cannot know
1406 * whether it's a directory yet or not, so this can give us an
1407 * expected type (S_IFDIR or S_IFREG) that we should examine)
1409 if (!filesystem_iterator_examine_path(&dir_expected
, &pathlist_match
,
1410 iter
, frame_entry
, path
, path_len
))
1413 /* TODO: don't need to stat if assume unchanged for this path and
1414 * we have an index, we can just copy the data out of it.
1417 if ((error
= git_path_diriter_stat(&statbuf
, &diriter
)) < 0) {
1418 /* file was removed between readdir and lstat */
1419 if (error
== GIT_ENOTFOUND
)
1422 /* treat the file as unreadable */
1423 memset(&statbuf
, 0, sizeof(statbuf
));
1424 statbuf
.st_mode
= GIT_FILEMODE_UNREADABLE
;
1429 iter
->base
.stat_calls
++;
1431 /* Ignore wacky things in the filesystem */
1432 if (!S_ISDIR(statbuf
.st_mode
) &&
1433 !S_ISREG(statbuf
.st_mode
) &&
1434 !S_ISLNK(statbuf
.st_mode
) &&
1435 statbuf
.st_mode
!= GIT_FILEMODE_UNREADABLE
)
1438 if (filesystem_iterator_is_dot_git(iter
, path
, path_len
))
1441 /* convert submodules to GITLINK and remove trailing slashes */
1442 if (S_ISDIR(statbuf
.st_mode
)) {
1443 bool submodule
= false;
1445 if ((error
= filesystem_iterator_is_submodule(&submodule
,
1446 iter
, path
, path_len
)) < 0)
1450 statbuf
.st_mode
= GIT_FILEMODE_COMMIT
;
1453 /* Ensure that the pathlist entry lines up with what we expected */
1454 else if (dir_expected
)
1457 if ((error
= filesystem_iterator_entry_init(&entry
,
1458 iter
, new_frame
, path
, path_len
, &statbuf
, pathlist_match
)) < 0)
1461 git_vector_insert(&new_frame
->entries
, entry
);
1464 if (error
== GIT_ITEROVER
)
1467 /* sort now that directory suffix is added */
1468 git_vector_sort(&new_frame
->entries
);
1472 git_array_pop(iter
->frames
);
1474 git_buf_dispose(&root
);
1475 git_path_diriter_free(&diriter
);
1479 GIT_INLINE(int) filesystem_iterator_frame_pop(filesystem_iterator
*iter
)
1481 filesystem_iterator_frame
*frame
;
1483 GIT_ASSERT(iter
->frames
.size
);
1485 frame
= git_array_pop(iter
->frames
);
1486 filesystem_iterator_frame_pop_ignores(iter
);
1488 git_pool_clear(&frame
->entry_pool
);
1489 git_vector_free(&frame
->entries
);
1494 static void filesystem_iterator_set_current(
1495 filesystem_iterator
*iter
,
1496 filesystem_iterator_entry
*entry
)
1499 * Index entries are limited to 32 bit timestamps. We can safely
1500 * cast this since workdir times are only used in the cache; any
1501 * mismatch will cause a hash recomputation which is unfortunate
1502 * but affects only people who set their filetimes to 2038.
1503 * (Same with the file size.)
1505 iter
->entry
.ctime
.seconds
= (int32_t)entry
->st
.st_ctime
;
1506 iter
->entry
.mtime
.seconds
= (int32_t)entry
->st
.st_mtime
;
1508 #if defined(GIT_USE_NSEC)
1509 iter
->entry
.ctime
.nanoseconds
= entry
->st
.st_ctime_nsec
;
1510 iter
->entry
.mtime
.nanoseconds
= entry
->st
.st_mtime_nsec
;
1512 iter
->entry
.ctime
.nanoseconds
= 0;
1513 iter
->entry
.mtime
.nanoseconds
= 0;
1516 iter
->entry
.dev
= entry
->st
.st_dev
;
1517 iter
->entry
.ino
= entry
->st
.st_ino
;
1518 iter
->entry
.mode
= git_futils_canonical_mode(entry
->st
.st_mode
);
1519 iter
->entry
.uid
= entry
->st
.st_uid
;
1520 iter
->entry
.gid
= entry
->st
.st_gid
;
1521 iter
->entry
.file_size
= (uint32_t)entry
->st
.st_size
;
1523 if (iter
->base
.flags
& GIT_ITERATOR_INCLUDE_HASH
)
1524 git_oid_cpy(&iter
->entry
.id
, &entry
->id
);
1526 iter
->entry
.path
= entry
->path
;
1528 iter
->current_is_ignored
= GIT_IGNORE_UNCHECKED
;
1531 static int filesystem_iterator_current(
1532 const git_index_entry
**out
, git_iterator
*i
)
1534 filesystem_iterator
*iter
= GIT_CONTAINER_OF(i
, filesystem_iterator
, base
);
1536 if (!iterator__has_been_accessed(i
))
1537 return iter
->base
.cb
->advance(out
, i
);
1539 if (!iter
->frames
.size
) {
1541 return GIT_ITEROVER
;
1544 *out
= &iter
->entry
;
1548 static int filesystem_iterator_is_dir(
1550 const filesystem_iterator
*iter
,
1551 const filesystem_iterator_entry
*entry
)
1554 git_buf fullpath
= GIT_BUF_INIT
;
1557 if (S_ISDIR(entry
->st
.st_mode
)) {
1562 if (!iterator__descend_symlinks(iter
) || !S_ISLNK(entry
->st
.st_mode
)) {
1567 if ((error
= git_buf_joinpath(&fullpath
, iter
->root
, entry
->path
)) < 0 ||
1568 (error
= git_path_validate_workdir_buf(iter
->base
.repo
, &fullpath
)) < 0 ||
1569 (error
= p_stat(fullpath
.ptr
, &st
)) < 0)
1572 *is_dir
= S_ISDIR(st
.st_mode
);
1575 git_buf_dispose(&fullpath
);
1579 static int filesystem_iterator_advance(
1580 const git_index_entry
**out
, git_iterator
*i
)
1582 filesystem_iterator
*iter
= GIT_CONTAINER_OF(i
, filesystem_iterator
, base
);
1586 iter
->base
.flags
|= GIT_ITERATOR_FIRST_ACCESS
;
1588 /* examine filesystem entries until we find the next one to return */
1590 filesystem_iterator_frame
*frame
;
1591 filesystem_iterator_entry
*entry
;
1593 if ((frame
= filesystem_iterator_current_frame(iter
)) == NULL
) {
1594 error
= GIT_ITEROVER
;
1598 /* no more entries in this frame. pop the frame out */
1599 if (frame
->next_idx
== frame
->entries
.length
) {
1600 filesystem_iterator_frame_pop(iter
);
1604 /* we have more entries in the current frame, that's our next entry */
1605 entry
= frame
->entries
.contents
[frame
->next_idx
];
1608 if ((error
= filesystem_iterator_is_dir(&is_dir
, iter
, entry
)) < 0)
1612 if (iterator__do_autoexpand(iter
)) {
1613 error
= filesystem_iterator_frame_push(iter
, entry
);
1615 /* may get GIT_ENOTFOUND due to races or permission problems
1616 * that we want to quietly swallow
1618 if (error
== GIT_ENOTFOUND
)
1624 if (!iterator__include_trees(iter
))
1628 filesystem_iterator_set_current(iter
, entry
);
1633 *out
= (error
== 0) ? &iter
->entry
: NULL
;
1638 static int filesystem_iterator_advance_into(
1639 const git_index_entry
**out
, git_iterator
*i
)
1641 filesystem_iterator
*iter
= GIT_CONTAINER_OF(i
, filesystem_iterator
, base
);
1642 filesystem_iterator_frame
*frame
;
1643 filesystem_iterator_entry
*prev_entry
;
1649 if ((frame
= filesystem_iterator_current_frame(iter
)) == NULL
)
1650 return GIT_ITEROVER
;
1652 /* get the last seen entry */
1653 prev_entry
= filesystem_iterator_current_entry(frame
);
1655 /* it's legal to call advance_into when auto-expand is on. in this case,
1656 * we will have pushed a new (empty) frame on to the stack for this
1657 * new directory. since it's empty, its current_entry should be null.
1659 GIT_ASSERT(iterator__do_autoexpand(i
) ^ (prev_entry
!= NULL
));
1662 if (prev_entry
->st
.st_mode
!= GIT_FILEMODE_COMMIT
&&
1663 !S_ISDIR(prev_entry
->st
.st_mode
))
1666 if ((error
= filesystem_iterator_frame_push(iter
, prev_entry
)) < 0)
1670 /* we've advanced into the directory in question, let advance
1671 * find the first entry
1673 return filesystem_iterator_advance(out
, i
);
1676 int git_iterator_current_workdir_path(git_buf
**out
, git_iterator
*i
)
1678 filesystem_iterator
*iter
= GIT_CONTAINER_OF(i
, filesystem_iterator
, base
);
1679 const git_index_entry
*entry
;
1681 if (i
->type
!= GIT_ITERATOR_FS
&&
1682 i
->type
!= GIT_ITERATOR_WORKDIR
) {
1687 git_buf_truncate(&iter
->current_path
, iter
->root_len
);
1689 if (git_iterator_current(&entry
, i
) < 0 ||
1690 git_buf_puts(&iter
->current_path
, entry
->path
) < 0)
1693 *out
= &iter
->current_path
;
1697 GIT_INLINE(git_dir_flag
) entry_dir_flag(git_index_entry
*entry
)
1699 #if defined(GIT_WIN32) && !defined(__MINGW32__)
1700 return (entry
&& entry
->mode
) ?
1701 (S_ISDIR(entry
->mode
) ? GIT_DIR_FLAG_TRUE
: GIT_DIR_FLAG_FALSE
) :
1702 GIT_DIR_FLAG_UNKNOWN
;
1705 return GIT_DIR_FLAG_UNKNOWN
;
1709 static void filesystem_iterator_update_ignored(filesystem_iterator
*iter
)
1711 filesystem_iterator_frame
*frame
;
1712 git_dir_flag dir_flag
= entry_dir_flag(&iter
->entry
);
1714 if (git_ignore__lookup(&iter
->current_is_ignored
,
1715 &iter
->ignores
, iter
->entry
.path
, dir_flag
) < 0) {
1717 iter
->current_is_ignored
= GIT_IGNORE_NOTFOUND
;
1720 /* use ignore from containing frame stack */
1721 if (iter
->current_is_ignored
<= GIT_IGNORE_NOTFOUND
) {
1722 frame
= filesystem_iterator_current_frame(iter
);
1723 iter
->current_is_ignored
= frame
->is_ignored
;
1727 GIT_INLINE(bool) filesystem_iterator_current_is_ignored(
1728 filesystem_iterator
*iter
)
1730 if (iter
->current_is_ignored
== GIT_IGNORE_UNCHECKED
)
1731 filesystem_iterator_update_ignored(iter
);
1733 return (iter
->current_is_ignored
== GIT_IGNORE_TRUE
);
1736 bool git_iterator_current_is_ignored(git_iterator
*i
)
1738 filesystem_iterator
*iter
= NULL
;
1740 if (i
->type
!= GIT_ITERATOR_WORKDIR
)
1743 iter
= GIT_CONTAINER_OF(i
, filesystem_iterator
, base
);
1745 return filesystem_iterator_current_is_ignored(iter
);
1748 bool git_iterator_current_tree_is_ignored(git_iterator
*i
)
1750 filesystem_iterator
*iter
= GIT_CONTAINER_OF(i
, filesystem_iterator
, base
);
1751 filesystem_iterator_frame
*frame
;
1753 if (i
->type
!= GIT_ITERATOR_WORKDIR
)
1756 frame
= filesystem_iterator_current_frame(iter
);
1757 return (frame
->is_ignored
== GIT_IGNORE_TRUE
);
1760 static int filesystem_iterator_advance_over(
1761 const git_index_entry
**out
,
1762 git_iterator_status_t
*status
,
1765 filesystem_iterator
*iter
= GIT_CONTAINER_OF(i
, filesystem_iterator
, base
);
1766 filesystem_iterator_frame
*current_frame
;
1767 filesystem_iterator_entry
*current_entry
;
1768 const git_index_entry
*entry
= NULL
;
1773 *status
= GIT_ITERATOR_STATUS_NORMAL
;
1775 GIT_ASSERT(iterator__has_been_accessed(i
));
1777 current_frame
= filesystem_iterator_current_frame(iter
);
1778 GIT_ASSERT(current_frame
);
1780 current_entry
= filesystem_iterator_current_entry(current_frame
);
1781 GIT_ASSERT(current_entry
);
1783 if ((error
= git_iterator_current(&entry
, i
)) < 0)
1786 if (!S_ISDIR(entry
->mode
)) {
1787 if (filesystem_iterator_current_is_ignored(iter
))
1788 *status
= GIT_ITERATOR_STATUS_IGNORED
;
1790 return filesystem_iterator_advance(out
, i
);
1793 git_buf_clear(&iter
->tmp_buf
);
1794 if ((error
= git_buf_puts(&iter
->tmp_buf
, entry
->path
)) < 0)
1797 base
= iter
->tmp_buf
.ptr
;
1799 /* scan inside the directory looking for files. if we find nothing,
1800 * we will remain EMPTY. if we find any ignored item, upgrade EMPTY to
1801 * IGNORED. if we find a real actual item, upgrade all the way to NORMAL
1804 * however, if we're here looking for a pathlist item (but are not
1805 * actually in the pathlist ourselves) then start at FILTERED instead of
1806 * EMPTY. callers then know that this path was not something they asked
1809 *status
= current_entry
->match
== ITERATOR_PATHLIST_IS_PARENT
?
1810 GIT_ITERATOR_STATUS_FILTERED
: GIT_ITERATOR_STATUS_EMPTY
;
1812 while (entry
&& !iter
->base
.prefixcomp(entry
->path
, base
)) {
1813 if (filesystem_iterator_current_is_ignored(iter
)) {
1814 /* if we found an explicitly ignored item, then update from
1817 *status
= GIT_ITERATOR_STATUS_IGNORED
;
1818 } else if (S_ISDIR(entry
->mode
)) {
1819 error
= filesystem_iterator_advance_into(&entry
, i
);
1824 /* this directory disappeared, ignore it */
1825 else if (error
== GIT_ENOTFOUND
)
1828 /* a real error occurred */
1832 /* we found a non-ignored item, treat parent as untracked */
1833 *status
= GIT_ITERATOR_STATUS_NORMAL
;
1837 if ((error
= git_iterator_advance(&entry
, i
)) < 0)
1841 /* wrap up scan back to base directory */
1842 while (entry
&& !iter
->base
.prefixcomp(entry
->path
, base
)) {
1843 if ((error
= git_iterator_advance(&entry
, i
)) < 0)
1853 static void filesystem_iterator_clear(filesystem_iterator
*iter
)
1855 while (iter
->frames
.size
)
1856 filesystem_iterator_frame_pop(iter
);
1858 git_array_clear(iter
->frames
);
1859 git_ignore__free(&iter
->ignores
);
1861 git_buf_dispose(&iter
->tmp_buf
);
1863 iterator_clear(&iter
->base
);
1866 static int filesystem_iterator_init(filesystem_iterator
*iter
)
1870 if (iterator__honor_ignores(&iter
->base
) &&
1871 (error
= git_ignore__for_path(iter
->base
.repo
,
1872 ".gitignore", &iter
->ignores
)) < 0)
1875 if ((error
= filesystem_iterator_frame_push(iter
, NULL
)) < 0)
1878 iter
->base
.flags
&= ~GIT_ITERATOR_FIRST_ACCESS
;
1883 static int filesystem_iterator_reset(git_iterator
*i
)
1885 filesystem_iterator
*iter
= GIT_CONTAINER_OF(i
, filesystem_iterator
, base
);
1887 filesystem_iterator_clear(iter
);
1888 return filesystem_iterator_init(iter
);
1891 static void filesystem_iterator_free(git_iterator
*i
)
1893 filesystem_iterator
*iter
= GIT_CONTAINER_OF(i
, filesystem_iterator
, base
);
1894 git__free(iter
->root
);
1895 git_buf_dispose(&iter
->current_path
);
1896 git_tree_free(iter
->tree
);
1898 git_index_snapshot_release(&iter
->index_snapshot
, iter
->index
);
1899 filesystem_iterator_clear(iter
);
1902 static int iterator_for_filesystem(
1904 git_repository
*repo
,
1908 git_iterator_t type
,
1909 git_iterator_options
*options
)
1911 filesystem_iterator
*iter
;
1915 static git_iterator_callbacks callbacks
= {
1916 filesystem_iterator_current
,
1917 filesystem_iterator_advance
,
1918 filesystem_iterator_advance_into
,
1919 filesystem_iterator_advance_over
,
1920 filesystem_iterator_reset
,
1921 filesystem_iterator_free
1927 return git_iterator_for_nothing(out
, options
);
1929 iter
= git__calloc(1, sizeof(filesystem_iterator
));
1930 GIT_ERROR_CHECK_ALLOC(iter
);
1932 iter
->base
.type
= type
;
1933 iter
->base
.cb
= &callbacks
;
1935 root_len
= strlen(root
);
1937 iter
->root
= git__malloc(root_len
+2);
1938 GIT_ERROR_CHECK_ALLOC(iter
->root
);
1940 memcpy(iter
->root
, root
, root_len
);
1942 if (root_len
== 0 || root
[root_len
-1] != '/') {
1943 iter
->root
[root_len
] = '/';
1946 iter
->root
[root_len
] = '\0';
1947 iter
->root_len
= root_len
;
1949 if ((error
= git_buf_puts(&iter
->current_path
, iter
->root
)) < 0)
1952 if ((error
= iterator_init_common(&iter
->base
, repo
, index
, options
)) < 0)
1955 if (tree
&& (error
= git_tree_dup(&iter
->tree
, tree
)) < 0)
1959 (error
= git_index_snapshot_new(&iter
->index_snapshot
, index
)) < 0)
1962 iter
->index
= index
;
1963 iter
->dirload_flags
=
1964 (iterator__ignore_case(&iter
->base
) ? GIT_PATH_DIR_IGNORE_CASE
: 0) |
1965 (iterator__flag(&iter
->base
, PRECOMPOSE_UNICODE
) ?
1966 GIT_PATH_DIR_PRECOMPOSE_UNICODE
: 0);
1968 if ((error
= filesystem_iterator_init(iter
)) < 0)
1975 git_iterator_free(&iter
->base
);
1979 int git_iterator_for_filesystem(
1982 git_iterator_options
*options
)
1984 return iterator_for_filesystem(out
,
1985 NULL
, root
, NULL
, NULL
, GIT_ITERATOR_FS
, options
);
1988 int git_iterator_for_workdir_ext(
1990 git_repository
*repo
,
1991 const char *repo_workdir
,
1994 git_iterator_options
*given_opts
)
1996 git_iterator_options options
= GIT_ITERATOR_OPTIONS_INIT
;
1998 if (!repo_workdir
) {
1999 if (git_repository__ensure_not_bare(repo
, "scan working directory") < 0)
2000 return GIT_EBAREREPO
;
2002 repo_workdir
= git_repository_workdir(repo
);
2005 /* upgrade to a workdir iterator, adding necessary internal flags */
2007 memcpy(&options
, given_opts
, sizeof(git_iterator_options
));
2009 options
.flags
|= GIT_ITERATOR_HONOR_IGNORES
|
2010 GIT_ITERATOR_IGNORE_DOT_GIT
;
2012 return iterator_for_filesystem(out
,
2013 repo
, repo_workdir
, index
, tree
, GIT_ITERATOR_WORKDIR
, &options
);
2017 /* Index iterator */
2025 /* the pseudotree entry */
2026 git_index_entry tree_entry
;
2030 const git_index_entry
*entry
;
2033 static int index_iterator_current(
2034 const git_index_entry
**out
, git_iterator
*i
)
2036 index_iterator
*iter
= (index_iterator
*)i
;
2038 if (!iterator__has_been_accessed(i
))
2039 return iter
->base
.cb
->advance(out
, i
);
2041 if (iter
->entry
== NULL
) {
2043 return GIT_ITEROVER
;
2050 static bool index_iterator_create_pseudotree(
2051 const git_index_entry
**out
,
2052 index_iterator
*iter
,
2055 const char *prev_path
, *relative_path
, *dirsep
;
2058 prev_path
= iter
->entry
? iter
->entry
->path
: "";
2060 /* determine if the new path is in a different directory from the old */
2061 common_len
= git_path_common_dirlen(prev_path
, path
);
2062 relative_path
= path
+ common_len
;
2064 if ((dirsep
= strchr(relative_path
, '/')) == NULL
)
2067 git_buf_clear(&iter
->tree_buf
);
2068 git_buf_put(&iter
->tree_buf
, path
, (dirsep
- path
) + 1);
2070 iter
->tree_entry
.mode
= GIT_FILEMODE_TREE
;
2071 iter
->tree_entry
.path
= iter
->tree_buf
.ptr
;
2073 *out
= &iter
->tree_entry
;
2077 static int index_iterator_skip_pseudotree(index_iterator
*iter
)
2079 GIT_ASSERT(iterator__has_been_accessed(&iter
->base
));
2080 GIT_ASSERT(S_ISDIR(iter
->entry
->mode
));
2083 const git_index_entry
*next_entry
= NULL
;
2085 if (++iter
->next_idx
>= iter
->entries
.length
)
2086 return GIT_ITEROVER
;
2088 next_entry
= iter
->entries
.contents
[iter
->next_idx
];
2090 if (iter
->base
.strncomp(iter
->tree_buf
.ptr
, next_entry
->path
,
2091 iter
->tree_buf
.size
) != 0)
2095 iter
->skip_tree
= false;
2099 static int index_iterator_advance(
2100 const git_index_entry
**out
, git_iterator
*i
)
2102 index_iterator
*iter
= GIT_CONTAINER_OF(i
, index_iterator
, base
);
2103 const git_index_entry
*entry
= NULL
;
2107 iter
->base
.flags
|= GIT_ITERATOR_FIRST_ACCESS
;
2110 if (iter
->next_idx
>= iter
->entries
.length
) {
2111 error
= GIT_ITEROVER
;
2115 /* we were not asked to expand this pseudotree. advance over it. */
2116 if (iter
->skip_tree
) {
2117 index_iterator_skip_pseudotree(iter
);
2121 entry
= iter
->entries
.contents
[iter
->next_idx
];
2122 is_submodule
= S_ISGITLINK(entry
->mode
);
2124 if (!iterator_has_started(&iter
->base
, entry
->path
, is_submodule
)) {
2129 if (iterator_has_ended(&iter
->base
, entry
->path
)) {
2130 error
= GIT_ITEROVER
;
2134 /* if we have a list of paths we're interested in, examine it */
2135 if (!iterator_pathlist_next_is(&iter
->base
, entry
->path
)) {
2140 /* if this is a conflict, skip it unless we're including conflicts */
2141 if (git_index_entry_is_conflict(entry
) &&
2142 !iterator__include_conflicts(&iter
->base
)) {
2147 /* we've found what will be our next _file_ entry. but if we are
2148 * returning trees entries, we may need to return a pseudotree
2149 * entry that will contain this. don't advance over this entry,
2150 * though, we still need to return it on the next `advance`.
2152 if (iterator__include_trees(&iter
->base
) &&
2153 index_iterator_create_pseudotree(&entry
, iter
, entry
->path
)) {
2155 /* Note whether this pseudo tree should be expanded or not */
2156 iter
->skip_tree
= iterator__dont_autoexpand(&iter
->base
);
2164 iter
->entry
= (error
== 0) ? entry
: NULL
;
2172 static int index_iterator_advance_into(
2173 const git_index_entry
**out
, git_iterator
*i
)
2175 index_iterator
*iter
= GIT_CONTAINER_OF(i
, index_iterator
, base
);
2177 if (! S_ISDIR(iter
->tree_entry
.mode
)) {
2184 iter
->skip_tree
= false;
2185 return index_iterator_advance(out
, i
);
2188 static int index_iterator_advance_over(
2189 const git_index_entry
**out
,
2190 git_iterator_status_t
*status
,
2193 index_iterator
*iter
= GIT_CONTAINER_OF(i
, index_iterator
, base
);
2194 const git_index_entry
*entry
;
2197 if ((error
= index_iterator_current(&entry
, i
)) < 0)
2200 if (S_ISDIR(entry
->mode
))
2201 index_iterator_skip_pseudotree(iter
);
2203 *status
= GIT_ITERATOR_STATUS_NORMAL
;
2204 return index_iterator_advance(out
, i
);
2207 static void index_iterator_clear(index_iterator
*iter
)
2209 iterator_clear(&iter
->base
);
2212 static int index_iterator_init(index_iterator
*iter
)
2214 iter
->base
.flags
&= ~GIT_ITERATOR_FIRST_ACCESS
;
2216 iter
->skip_tree
= false;
2220 static int index_iterator_reset(git_iterator
*i
)
2222 index_iterator
*iter
= GIT_CONTAINER_OF(i
, index_iterator
, base
);
2224 index_iterator_clear(iter
);
2225 return index_iterator_init(iter
);
2228 static void index_iterator_free(git_iterator
*i
)
2230 index_iterator
*iter
= GIT_CONTAINER_OF(i
, index_iterator
, base
);
2232 git_index_snapshot_release(&iter
->entries
, iter
->base
.index
);
2233 git_buf_dispose(&iter
->tree_buf
);
2236 int git_iterator_for_index(
2238 git_repository
*repo
,
2240 git_iterator_options
*options
)
2242 index_iterator
*iter
;
2245 static git_iterator_callbacks callbacks
= {
2246 index_iterator_current
,
2247 index_iterator_advance
,
2248 index_iterator_advance_into
,
2249 index_iterator_advance_over
,
2250 index_iterator_reset
,
2257 return git_iterator_for_nothing(out
, options
);
2259 iter
= git__calloc(1, sizeof(index_iterator
));
2260 GIT_ERROR_CHECK_ALLOC(iter
);
2262 iter
->base
.type
= GIT_ITERATOR_INDEX
;
2263 iter
->base
.cb
= &callbacks
;
2265 if ((error
= iterator_init_common(&iter
->base
, repo
, index
, options
)) < 0 ||
2266 (error
= git_index_snapshot_new(&iter
->entries
, index
)) < 0 ||
2267 (error
= index_iterator_init(iter
)) < 0)
2270 git_vector_set_cmp(&iter
->entries
, iterator__ignore_case(&iter
->base
) ?
2271 git_index_entry_icmp
: git_index_entry_cmp
);
2272 git_vector_sort(&iter
->entries
);
2278 git_iterator_free(&iter
->base
);
2285 int git_iterator_reset_range(
2286 git_iterator
*i
, const char *start
, const char *end
)
2288 if (iterator_reset_range(i
, start
, end
) < 0)
2291 return i
->cb
->reset(i
);
2294 int git_iterator_set_ignore_case(git_iterator
*i
, bool ignore_case
)
2296 GIT_ASSERT(!iterator__has_been_accessed(i
));
2297 iterator_set_ignore_case(i
, ignore_case
);
2301 void git_iterator_free(git_iterator
*iter
)
2306 iter
->cb
->free(iter
);
2308 git_vector_free(&iter
->pathlist
);
2309 git__free(iter
->start
);
2310 git__free(iter
->end
);
2312 memset(iter
, 0, sizeof(*iter
));
2317 int git_iterator_foreach(
2318 git_iterator
*iterator
,
2319 git_iterator_foreach_cb cb
,
2322 const git_index_entry
*iterator_item
;
2325 if ((error
= git_iterator_current(&iterator_item
, iterator
)) < 0)
2328 if ((error
= cb(iterator_item
, data
)) != 0)
2332 if ((error
= git_iterator_advance(&iterator_item
, iterator
)) < 0)
2335 if ((error
= cb(iterator_item
, data
)) != 0)
2340 if (error
== GIT_ITEROVER
)
2346 int git_iterator_walk(
2347 git_iterator
**iterators
,
2349 git_iterator_walk_cb cb
,
2352 const git_index_entry
**iterator_item
; /* next in each iterator */
2353 const git_index_entry
**cur_items
; /* current path in each iter */
2354 const git_index_entry
*first_match
;
2358 iterator_item
= git__calloc(cnt
, sizeof(git_index_entry
*));
2359 cur_items
= git__calloc(cnt
, sizeof(git_index_entry
*));
2361 GIT_ERROR_CHECK_ALLOC(iterator_item
);
2362 GIT_ERROR_CHECK_ALLOC(cur_items
);
2364 /* Set up the iterators */
2365 for (i
= 0; i
< cnt
; i
++) {
2366 error
= git_iterator_current(&iterator_item
[i
], iterators
[i
]);
2368 if (error
< 0 && error
!= GIT_ITEROVER
)
2373 for (i
= 0; i
< cnt
; i
++)
2374 cur_items
[i
] = NULL
;
2378 /* Find the next path(s) to consume from each iterator */
2379 for (i
= 0; i
< cnt
; i
++) {
2380 if (iterator_item
[i
] == NULL
)
2383 if (first_match
== NULL
) {
2384 first_match
= iterator_item
[i
];
2385 cur_items
[i
] = iterator_item
[i
];
2387 int path_diff
= git_index_entry_cmp(iterator_item
[i
], first_match
);
2389 if (path_diff
< 0) {
2390 /* Found an index entry that sorts before the one we're
2391 * looking at. Forget that we've seen the other and
2392 * look at the other iterators for this path.
2394 for (j
= 0; j
< i
; j
++)
2395 cur_items
[j
] = NULL
;
2397 first_match
= iterator_item
[i
];
2398 cur_items
[i
] = iterator_item
[i
];
2399 } else if (path_diff
== 0) {
2400 cur_items
[i
] = iterator_item
[i
];
2405 if (first_match
== NULL
)
2408 if ((error
= cb(cur_items
, data
)) != 0)
2411 /* Advance each iterator that participated */
2412 for (i
= 0; i
< cnt
; i
++) {
2413 if (cur_items
[i
] == NULL
)
2416 error
= git_iterator_advance(&iterator_item
[i
], iterators
[i
]);
2418 if (error
< 0 && error
!= GIT_ITEROVER
)
2424 git__free((git_index_entry
**)iterator_item
);
2425 git__free((git_index_entry
**)cur_items
);
2427 if (error
== GIT_ITEROVER
)