deps/libgit2/src/libgit2/iterator.c

filter: Statementstmt 74.0% 804 / 1086 Branchbran 59.0% 410 / 694 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.2% 1214 / 1780 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/reflog.c ↑ Summary const-c-error.inc »
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7  
8 #include "iterator.h"
9  
10 #include "tree.h"
11 #include "index.h"
12 #include "path.h"
13  
14 #define GIT_ITERATOR_FIRST_ACCESS (1 << 15)
15 #define GIT_ITERATOR_HONOR_IGNORES (1 << 16)
16 #define GIT_ITERATOR_IGNORE_DOT_GIT (1 << 17)
17  
18 #define iterator__flag(I,F) ((((git_iterator *)(I))->flags & GIT_ITERATOR_ ## F) != 0)
19 #define iterator__ignore_case(I) iterator__flag(I,IGNORE_CASE)
20 #define iterator__include_trees(I) iterator__flag(I,INCLUDE_TREES)
21 #define iterator__dont_autoexpand(I) iterator__flag(I,DONT_AUTOEXPAND)
22 #define iterator__do_autoexpand(I) !iterator__flag(I,DONT_AUTOEXPAND)
23 #define iterator__include_conflicts(I) iterator__flag(I,INCLUDE_CONFLICTS)
24 #define iterator__has_been_accessed(I) iterator__flag(I,FIRST_ACCESS)
25 #define iterator__honor_ignores(I) iterator__flag(I,HONOR_IGNORES)
26 #define iterator__ignore_dot_git(I) iterator__flag(I,IGNORE_DOT_GIT)
27 #define iterator__descend_symlinks(I) iterator__flag(I,DESCEND_SYMLINKS)
28  
29  
30 752 static void iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
31 {
32 752 ▶ if (ignore_case)
Branch: 1 branch
BranchTrueFalseTotal
if (ignore_case) 3 749 752
33 3 iter->flags |= GIT_ITERATOR_IGNORE_CASE;
34 else
35 749 iter->flags &= ~GIT_ITERATOR_IGNORE_CASE;
36  
37 752 ▶ iter->strcomp = ignore_case ? git__strcasecmp : git__strcmp;
Branch: 1 branch
BranchTrueFalseTotal
iter->strcomp = ignore_case ? git__strcasecmp : git__strcmp; 3 749 752
38 752 ▶ iter->strncomp = ignore_case ? git__strncasecmp : git__strncmp;
Branch: 1 branch
BranchTrueFalseTotal
iter->strncomp = ignore_case ? git__strncasecmp : git__strncmp; 3 749 752
39 752 ▶ iter->prefixcomp = ignore_case ? git__prefixcmp_icase : git__prefixcmp;
Branch: 1 branch
BranchTrueFalseTotal
iter->prefixcomp = ignore_case ? git__prefixcmp_icase : git__prefixcmp; 3 749 752
40 752 ▶ iter->entry_srch = ignore_case ? git_index_entry_isrch : git_index_entry_srch;
Branch: 1 branch
BranchTrueFalseTotal
iter->entry_srch = ignore_case ? git_index_entry_isrch : git_index_entry_srch; 3 749 752
41  
42 752 git_vector_set_cmp(&iter->pathlist, (git_vector_cmp)iter->strcomp);
43 752 }
44  
45 816 static int iterator_range_init(
46 git_iterator *iter, const char *start, const char *end)
47 {
48 816 ▶ if (start && *start) {
Branch: 2 branches
BranchTrueFalseTotal
if (start && *start) { 33 783 816
if (start && *start) { 33 0 33
49 33 iter->start = git__strdup(start);
50 33 ▶ GIT_ERROR_CHECK_ALLOC(iter->start);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iter->start); 0 33 33
51  
52 33 iter->start_len = strlen(iter->start);
53 }
54  
55 816 ▶ if (end && *end) {
Branch: 2 branches
BranchTrueFalseTotal
if (end && *end) { 33 783 816
if (end && *end) { 33 0 33
56 33 iter->end = git__strdup(end);
57 33 ▶ GIT_ERROR_CHECK_ALLOC(iter->end);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iter->end); 0 33 33
58  
59 33 iter->end_len = strlen(iter->end);
60 }
61  
62 816 iter->started = (iter->start == NULL);
63 816 iter->ended = false;
64  
65 816 return 0;
66 }
67  
68 64 static void iterator_range_free(git_iterator *iter)
69 {
70 64 ▶ if (iter->start) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->start) { 0 64 64
71 0 git__free(iter->start);
72 0 iter->start = NULL;
73 0 iter->start_len = 0;
74 }
75  
76 64 ▶ if (iter->end) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->end) { 0 64 64
77 0 git__free(iter->end);
78 0 iter->end = NULL;
79 0 iter->end_len = 0;
80 }
81 64 }
82  
83 64 static int iterator_reset_range(
84 git_iterator *iter, const char *start, const char *end)
85 {
86 64 iterator_range_free(iter);
87 64 return iterator_range_init(iter, start, end);
88 }
89  
90 752 static int iterator_pathlist_init(git_iterator *iter, git_strarray *pathlist)
91 {
92 size_t i;
93  
94 752 ▶ if (git_vector_init(&iter->pathlist, pathlist->count, NULL) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_vector_init(&iter->pathlist, pathlist->count, NULL) < 0) 0 752 752
95 0 return -1;
96  
97 850 ▶ for (i = 0; i < pathlist->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pathlist->count; i++) { 98 752 850
98 98 ▶ if (!pathlist->strings[i])
Branch: 1 branch
BranchTrueFalseTotal
if (!pathlist->strings[i]) 0 98 98
99 0 continue;
100  
101 98 ▶ if (git_vector_insert(&iter->pathlist, pathlist->strings[i]) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_vector_insert(&iter->pathlist, pathlist->strings[i]) < 0) 0 98 98
102 0 return -1;
103 }
104  
105 752 return 0;
106 }
107  
108 752 static int iterator_init_common(
109 git_iterator *iter,
110 git_repository *repo,
111 git_index *index,
112 git_iterator_options *given_opts)
113 {
114 static git_iterator_options default_opts = GIT_ITERATOR_OPTIONS_INIT;
115 752 ▶ git_iterator_options *options = given_opts ? given_opts : &default_opts;
Branch: 1 branch
BranchTrueFalseTotal
git_iterator_options *options = given_opts ? given_opts : &default_opts; 752 0 752
116 bool ignore_case;
117 int precompose;
118 int error;
119  
120 752 iter->repo = repo;
121 752 iter->index = index;
122 752 iter->flags = options->flags;
123  
124 752 ▶ if ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0) { 3 749 752
125 3 ignore_case = true;
126 749 ▶ } else if ((iter->flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ((iter->flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0) { 421 328 749
127 421 ignore_case = false;
128 328 ▶ } else if (repo) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (repo) { 297 31 328
129 git_index *index;
130  
131 297 ▶ if ((error = git_repository_index__weakptr(&index, iter->repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository_index__weakptr(&index, iter->repo)) < 0) 0 297 297
132 0 return error;
133  
134 297 ignore_case = !!index->ignore_case;
135  
136 297 ▶ if (ignore_case == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ignore_case == 1) 0 297 297
137 0 iter->flags |= GIT_ITERATOR_IGNORE_CASE;
138 else
139 297 iter->flags |= GIT_ITERATOR_DONT_IGNORE_CASE;
140 } else {
141 31 ignore_case = false;
142 }
143  
144 /* try to look up precompose and set flag if appropriate */
145 752 ▶ if (repo &&
Branch: 1 branch
BranchTrueFalseTotal
if (repo && 719 33 752
146 719 ▶ (iter->flags & GIT_ITERATOR_PRECOMPOSE_UNICODE) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
(iter->flags & GIT_ITERATOR_PRECOMPOSE_UNICODE) == 0 && 719 0 719
147 719 ▶ (iter->flags & GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
(iter->flags & GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE) == 0) { 719 0 719
148  
149 719 ▶ if (git_repository__configmap_lookup(&precompose, repo, GIT_CONFIGMAP_PRECOMPOSE) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository__configmap_lookup(&precompose, repo, GIT_CONFIGMAP_PRECOMPOSE) < 0) 0 719 719
150 0 git_error_clear();
151 719 ▶ else if (precompose)
Branch: 1 branch
BranchTrueFalseTotal
else if (precompose) 0 719 719
152 0 iter->flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
153 }
154  
155 752 ▶ if ((iter->flags & GIT_ITERATOR_DONT_AUTOEXPAND))
Branch: 1 branch
BranchTrueFalseTotal
if ((iter->flags & GIT_ITERATOR_DONT_AUTOEXPAND)) 171 581 752
156 171 iter->flags |= GIT_ITERATOR_INCLUDE_TREES;
157  
158 1504 if ((error = iterator_range_init(iter, options->start, options->end)) < 0 ||
159 752 (error = iterator_pathlist_init(iter, &options->pathlist)) < 0)
160 0 return error;
161  
162 752 iterator_set_ignore_case(iter, ignore_case);
163 752 return 0;
164 }
165  
166 556 static void iterator_clear(git_iterator *iter)
167 {
168 556 iter->started = false;
169 556 iter->ended = false;
170 556 iter->stat_calls = 0;
171 556 iter->pathlist_walk_idx = 0;
172 556 iter->flags &= ~GIT_ITERATOR_FIRST_ACCESS;
173 556 }
174  
175 1480 GIT_INLINE(bool) iterator_has_started(
176 git_iterator *iter, const char *path, bool is_submodule)
177 {
178 size_t path_len;
179  
180 1480 ▶ if (iter->start == NULL || iter->started == true)
Branch: 2 branches
BranchTrueFalseTotal
if (iter->start == NULL || iter->started == true) 26 1454 1480
if (iter->start == NULL || iter->started == true) 14 12 26
181 1468 return true;
182  
183 /* the starting path is generally a prefix - we have started once we
184 * are prefixed by this path
185 */
186 12 iter->started = (iter->prefixcomp(path, iter->start) >= 0);
187  
188 12 ▶ if (iter->started)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->started) 12 0 12
189 12 return true;
190  
191 0 path_len = strlen(path);
192  
193 /* if, however, we are a submodule, then we support `start` being
194 * suffixed with a `/` for crazy legacy reasons. match `submod`
195 * with a start path of `submod/`.
196 */
197 0 ▶ if (is_submodule && iter->start_len && path_len == iter->start_len - 1 &&
Branch: 3 branches
BranchTrueFalseTotal
if (is_submodule && iter->start_len && path_len == iter->start_len - 1 && 0 0 0
if (is_submodule && iter->start_len && path_len == iter->start_len - 1 && 0 0 0
if (is_submodule && iter->start_len && path_len == iter->start_len - 1 && 0 0 0
198 0 ▶ iter->start[iter->start_len-1] == '/')
Branch: 1 branch
BranchTrueFalseTotal
iter->start[iter->start_len-1] == '/') 0 0 0
199 0 return true;
200  
201 /* if, however, our current path is a directory, and our starting path
202 * is _beneath_ that directory, then recurse into the directory (even
203 * though we have not yet "started")
204 */
205 0 ▶ if (path_len > 0 && path[path_len-1] == '/' &&
Branch: 1 branch
BranchTrueFalseTotal
if (path_len > 0 && path[path_len-1] == '/' && 0 0 0
206 0 iter->strncomp(path, iter->start, path_len) == 0)
207 0 return true;
208  
209 0 return false;
210 }
211  
212 1480 GIT_INLINE(bool) iterator_has_ended(git_iterator *iter, const char *path)
213 {
214 1480 ▶ if (iter->end == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->end == NULL) 1454 26 1480
215 1454 return false;
216 26 ▶ else if (iter->ended)
Branch: 1 branch
BranchTrueFalseTotal
else if (iter->ended) 0 26 26
217 0 return true;
218  
219 26 iter->ended = (iter->prefixcomp(path, iter->end) > 0);
220 26 return iter->ended;
221 }
222  
223 /* walker for the index and tree iterator that allows it to walk the sorted
224 * pathlist entries alongside sorted iterator entries.
225 */
226 1474 static bool iterator_pathlist_next_is(git_iterator *iter, const char *path)
227 {
228 char *p;
229 size_t path_len, p_len, cmp_len, i;
230 int cmp;
231  
232 1474 ▶ if (iter->pathlist.length == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->pathlist.length == 0) 1339 135 1474
233 1339 return true;
234  
235 135 git_vector_sort(&iter->pathlist);
236  
237 135 path_len = strlen(path);
238  
239 /* for comparison, drop the trailing slash on the current '/' */
240 135 ▶ if (path_len && path[path_len-1] == '/')
Branch: 2 branches
BranchTrueFalseTotal
if (path_len && path[path_len-1] == '/') 135 0 135
if (path_len && path[path_len-1] == '/') 6 129 135
241 6 path_len--;
242  
243 196 ▶ for (i = iter->pathlist_walk_idx; i < iter->pathlist.length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = iter->pathlist_walk_idx; i < iter->pathlist.length; i++) { 115 81 196
244 115 p = iter->pathlist.contents[i];
245 115 p_len = strlen(p);
246  
247 115 ▶ if (p_len && p[p_len-1] == '/')
Branch: 2 branches
BranchTrueFalseTotal
if (p_len && p[p_len-1] == '/') 115 0 115
if (p_len && p[p_len-1] == '/') 0 115 115
248 0 p_len--;
249  
250 115 cmp_len = min(path_len, p_len);
251  
252 /* see if the pathlist entry is a prefix of this path */
253 115 cmp = iter->strncomp(p, path, cmp_len);
254  
255 /* prefix match - see if there's an exact match, or if we were
256 * given a path that matches the directory
257 */
258 115 ▶ if (cmp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmp == 0) { 80 35 115
259 /* if this pathlist entry is not suffixed with a '/' then
260 * it matches a path that is a file or a directory.
261 * (eg, pathlist = "foo" and path is "foo" or "foo/" or
262 * "foo/something")
263 */
264 80 ▶ if (p[cmp_len] == '\0' &&
Branch: 1 branch
BranchTrueFalseTotal
if (p[cmp_len] == '\0' && 74 6 80
265 74 ▶ (path[cmp_len] == '\0' || path[cmp_len] == '/'))
Branch: 2 branches
BranchTrueFalseTotal
(path[cmp_len] == '\0' || path[cmp_len] == '/')) 24 50 74
(path[cmp_len] == '\0' || path[cmp_len] == '/')) 0 24 24
266 50 return true;
267  
268 /* if this pathlist entry _is_ suffixed with a '/' then
269 * it matches only paths that are directories.
270 * (eg, pathlist = "foo/" and path is "foo/" or "foo/something")
271 */
272 30 ▶ if (p[cmp_len] == '/' && path[cmp_len] == '/')
Branch: 2 branches
BranchTrueFalseTotal
if (p[cmp_len] == '/' && path[cmp_len] == '/') 0 30 30
if (p[cmp_len] == '/' && path[cmp_len] == '/') 0 0 0
273 0 return true;
274 }
275  
276 /* this pathlist entry sorts before the given path, try the next */
277 35 ▶ else if (cmp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (cmp < 0) { 31 4 35
278 31 iter->pathlist_walk_idx++;
279 31 continue;
280 }
281  
282 /* this pathlist sorts after the given path, no match. */
283 4 ▶ else if (cmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (cmp > 0) { 4 0 4
284 4 break;
285 }
286 }
287  
288 85 return false;
289 }
290  
291 typedef enum {
292 ITERATOR_PATHLIST_NONE = 0,
293 ITERATOR_PATHLIST_IS_FILE = 1,
294 ITERATOR_PATHLIST_IS_DIR = 2,
295 ITERATOR_PATHLIST_IS_PARENT = 3,
296 ITERATOR_PATHLIST_FULL = 4
297 } iterator_pathlist_search_t;
298  
299 145 static iterator_pathlist_search_t iterator_pathlist_search(
300 git_iterator *iter, const char *path, size_t path_len)
301 {
302 const char *p;
303 size_t idx;
304 int error;
305  
306 145 ▶ if (iter->pathlist.length == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->pathlist.length == 0) 0 145 145
307 0 return ITERATOR_PATHLIST_FULL;
308  
309 145 git_vector_sort(&iter->pathlist);
310  
311 145 error = git_vector_bsearch2(&idx, &iter->pathlist,
312 145 (git_vector_cmp)iter->strcomp, path);
313  
314 /* the given path was found in the pathlist. since the pathlist only
315 * matches directories when they're suffixed with a '/', analyze the
316 * path string to determine whether it's a directory or not.
317 */
318 145 ▶ if (error == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == 0) { 38 107 145
319 38 ▶ if (path_len && path[path_len-1] == '/')
Branch: 2 branches
BranchTrueFalseTotal
if (path_len && path[path_len-1] == '/') 38 0 38
if (path_len && path[path_len-1] == '/') 0 38 38
320 0 return ITERATOR_PATHLIST_IS_DIR;
321  
322 38 return ITERATOR_PATHLIST_IS_FILE;
323 }
324  
325 /* at this point, the path we're examining may be a directory (though we
326 * don't know that yet, since we're avoiding a stat unless it's necessary)
327 * so walk the pathlist looking for the given path with a '/' after it,
328 */
329 107 ▶ while ((p = git_vector_get(&iter->pathlist, idx)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((p = git_vector_get(&iter->pathlist, idx)) != NULL) { 64 43 107
330 64 ▶ if (iter->prefixcomp(p, path) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->prefixcomp(p, path) != 0) 47 17 64
331 47 break;
332  
333 /* an exact match would have been matched by the bsearch above */
334 17 ▶ GIT_ASSERT_WITH_RETVAL(p[path_len], ITERATOR_PATHLIST_NONE);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_WITH_RETVAL(p[path_len], ITERATOR_PATHLIST_NONE); 0 17 17
335  
336 /* is this a literal directory entry (eg `foo/`) or a file beneath */
337 17 ▶ if (p[path_len] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (p[path_len] == '/') { 8 9 17
338 8 return (p[path_len+1] == '\0') ?
339 8 ▶ ITERATOR_PATHLIST_IS_DIR :
Branch: 1 branch
BranchTrueFalseTotal
ITERATOR_PATHLIST_IS_DIR : 0 8 8
340 ITERATOR_PATHLIST_IS_PARENT;
341 }
342  
343 9 ▶ if (p[path_len] > '/')
Branch: 1 branch
BranchTrueFalseTotal
if (p[path_len] > '/') 9 0 9
344 9 break;
345  
346 0 idx++;
347 }
348  
349 99 return ITERATOR_PATHLIST_NONE;
350 }
351  
352 /* Empty iterator */
353  
354 17 static int empty_iterator_noop(const git_index_entry **e, git_iterator *i)
355 {
356 17 GIT_UNUSED(i);
357  
358 17 ▶ if (e)
Branch: 1 branch
BranchTrueFalseTotal
if (e) 17 0 17
359 17 *e = NULL;
360  
361 17 return GIT_ITEROVER;
362 }
363  
364 0 static int empty_iterator_advance_over(
365 const git_index_entry **e,
366 git_iterator_status_t *s,
367 git_iterator *i)
368 {
369 0 *s = GIT_ITERATOR_STATUS_EMPTY;
370 0 return empty_iterator_noop(e, i);
371 }
372  
373 0 static int empty_iterator_reset(git_iterator *i)
374 {
375 0 GIT_UNUSED(i);
376 0 return 0;
377 }
378  
379 17 static void empty_iterator_free(git_iterator *i)
380 {
381 17 GIT_UNUSED(i);
382 17 }
383  
384 typedef struct {
385 git_iterator base;
386 git_iterator_callbacks cb;
387 } empty_iterator;
388  
389 17 int git_iterator_for_nothing(
390 git_iterator **out,
391 git_iterator_options *options)
392 {
393 empty_iterator *iter;
394  
395 static git_iterator_callbacks callbacks = {
396 empty_iterator_noop,
397 empty_iterator_noop,
398 empty_iterator_noop,
399 empty_iterator_advance_over,
400 empty_iterator_reset,
401 empty_iterator_free
402 };
403  
404 17 *out = NULL;
405  
406 17 iter = git__calloc(1, sizeof(empty_iterator));
407 17 ▶ GIT_ERROR_CHECK_ALLOC(iter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iter); 0 17 17
408  
409 17 iter->base.type = GIT_ITERATOR_EMPTY;
410 17 iter->base.cb = &callbacks;
411 17 iter->base.flags = options->flags;
412  
413 17 *out = &iter->base;
414 17 return 0;
415 }
416  
417 /* Tree iterator */
418  
419 typedef struct {
420 git_tree_entry *tree_entry;
421 const char *parent_path;
422 } tree_iterator_entry;
423  
424 typedef struct {
425 git_tree *tree;
426  
427 /* path to this particular frame (folder) */
428 git_str path;
429  
430 /* a sorted list of the entries for this frame (folder), these are
431 * actually pointers to the iterator's entry pool.
432 */
433 git_vector entries;
434 tree_iterator_entry *current;
435  
436 size_t next_idx;
437  
438 /* on case insensitive iterations, we also have an array of other
439 * paths that were case insensitively equal to this one, and their
440 * tree objects. we have coalesced the tree entries into this frame.
441 * a child `tree_iterator_entry` will contain a pointer to its actual
442 * parent path.
443 */
444 git_vector similar_trees;
445 git_array_t(git_str) similar_paths;
446 } tree_iterator_frame;
447  
448 typedef struct {
449 git_iterator base;
450 git_tree *root;
451 git_array_t(tree_iterator_frame) frames;
452  
453 git_index_entry entry;
454 git_str entry_path;
455  
456 /* a pool of entries to reduce the number of allocations */
457 git_pool entry_pool;
458 } tree_iterator;
459  
460 260 GIT_INLINE(tree_iterator_frame *) tree_iterator_parent_frame(
461 tree_iterator *iter)
462 {
463 260 return iter->frames.size > 1 ?
464 260 ▶ &iter->frames.ptr[iter->frames.size-2] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
&iter->frames.ptr[iter->frames.size-2] : NULL; 260 0 260
465 }
466  
467 1969 GIT_INLINE(tree_iterator_frame *) tree_iterator_current_frame(
468 tree_iterator *iter)
469 {
470 1969 ▶ return iter->frames.size ? &iter->frames.ptr[iter->frames.size-1] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return iter->frames.size ? &iter->frames.ptr[iter->frames.size-1] : NULL; 1691 278 1969
471 }
472  
473 8 GIT_INLINE(int) tree_entry_cmp(
474 const git_tree_entry *a, const git_tree_entry *b, bool icase)
475 {
476 8 ▶ return git_fs_path_cmp(
Branch: 1 branch
BranchTrueFalseTotal
return git_fs_path_cmp( 8 0 8
477 8 a->filename, a->filename_len, a->attr == GIT_FILEMODE_TREE,
478 8 b->filename, b->filename_len, b->attr == GIT_FILEMODE_TREE,
479 icase ? git__strncasecmp : git__strncmp);
480 }
481  
482 4 GIT_INLINE(int) tree_iterator_entry_cmp_icase(
483 const void *ptr_a, const void *ptr_b)
484 {
485 4 const tree_iterator_entry *a = (const tree_iterator_entry *)ptr_a;
486 4 const tree_iterator_entry *b = (const tree_iterator_entry *)ptr_b;
487  
488 4 return tree_entry_cmp(a->tree_entry, b->tree_entry, true);
489 }
490  
491 4 static int tree_iterator_entry_sort_icase(const void *ptr_a, const void *ptr_b)
492 {
493 4 const tree_iterator_entry *a = (const tree_iterator_entry *)ptr_a;
494 4 const tree_iterator_entry *b = (const tree_iterator_entry *)ptr_b;
495  
496 4 int c = tree_entry_cmp(a->tree_entry, b->tree_entry, true);
497  
498 /* stabilize the sort order for filenames that are (case insensitively)
499 * the same by examining the parent path (case sensitively) before
500 * falling back to a case sensitive sort of the filename.
501 */
502 4 ▶ if (!c && a->parent_path != b->parent_path)
Branch: 2 branches
BranchTrueFalseTotal
if (!c && a->parent_path != b->parent_path) 0 4 4
if (!c && a->parent_path != b->parent_path) 0 0 0
503 0 c = git__strcmp(a->parent_path, b->parent_path);
504  
505 4 ▶ if (!c)
Branch: 1 branch
BranchTrueFalseTotal
if (!c) 0 4 4
506 0 c = tree_entry_cmp(a->tree_entry, b->tree_entry, false);
507  
508 4 return c;
509 }
510  
511 1153 static int tree_iterator_compute_path(
512 git_str *out,
513 tree_iterator_entry *entry)
514 {
515 1153 git_str_clear(out);
516  
517 1153 ▶ if (entry->parent_path)
Branch: 1 branch
BranchTrueFalseTotal
if (entry->parent_path) 455 698 1153
518 455 git_str_joinpath(out, entry->parent_path, entry->tree_entry->filename);
519 else
520 698 git_str_puts(out, entry->tree_entry->filename);
521  
522 1153 ▶ if (git_tree_entry__is_tree(entry->tree_entry))
Branch: 1 branch
BranchTrueFalseTotal
if (git_tree_entry__is_tree(entry->tree_entry)) 526 627 1153
523 526 git_str_putc(out, '/');
524  
525 1153 ▶ if (git_str_oom(out))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(out)) 0 1153 1153
526 0 return -1;
527  
528 1153 return 0;
529 }
530  
531 584 static int tree_iterator_frame_init(
532 tree_iterator *iter,
533 git_tree *tree,
534 tree_iterator_entry *frame_entry)
535 {
536 584 tree_iterator_frame *new_frame = NULL;
537 tree_iterator_entry *new_entry;
538 584 git_tree *dup = NULL;
539 git_tree_entry *tree_entry;
540 git_vector_cmp cmp;
541 size_t i;
542 584 int error = 0;
543  
544 584 ▶ new_frame = git_array_alloc(iter->frames);
Branch: 2 branches
BranchTrueFalseTotal
new_frame = git_array_alloc(iter->frames); 324 260 584
new_frame = git_array_alloc(iter->frames); 324 0 324
545 584 ▶ GIT_ERROR_CHECK_ALLOC(new_frame);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(new_frame); 0 584 584
546  
547 584 ▶ if ((error = git_tree_dup(&dup, tree)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_tree_dup(&dup, tree)) < 0) 0 584 584
548 0 goto done;
549  
550 584 memset(new_frame, 0x0, sizeof(tree_iterator_frame));
551 584 new_frame->tree = dup;
552  
553 844 if (frame_entry &&
554 260 (error = tree_iterator_compute_path(&new_frame->path, frame_entry)) < 0)
555 0 goto done;
556  
557 1168 cmp = iterator__ignore_case(&iter->base) ?
558 584 ▶ tree_iterator_entry_sort_icase : NULL;
Branch: 1 branch
BranchTrueFalseTotal
tree_iterator_entry_sort_icase : NULL; 8 576 584
559  
560 584 ▶ if ((error = git_vector_init(&new_frame->entries,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_init(&new_frame->entries, 0 584 584
561 584 dup->entries.size, cmp)) < 0)
562 0 goto done;
563  
564 1570 ▶ git_array_foreach(dup->entries, i, tree_entry) {
Branch: 2 branches
BranchTrueFalseTotal
git_array_foreach(dup->entries, i, tree_entry) { 986 584 1570
git_array_foreach(dup->entries, i, tree_entry) { 986 0 986
565 986 ▶ if ((new_entry = git_pool_malloc(&iter->entry_pool, 1)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((new_entry = git_pool_malloc(&iter->entry_pool, 1)) == NULL) { 0 986 986
566 0 git_error_set_oom();
567 0 error = -1;
568 0 goto done;
569 }
570  
571 986 new_entry->tree_entry = tree_entry;
572 986 new_entry->parent_path = new_frame->path.ptr;
573  
574 986 ▶ if ((error = git_vector_insert(&new_frame->entries, new_entry)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_insert(&new_frame->entries, new_entry)) < 0) 0 986 986
575 0 goto done;
576 }
577  
578 584 ▶ git_vector_set_sorted(&new_frame->entries,
Branch: 1 branch
BranchTrueFalseTotal
git_vector_set_sorted(&new_frame->entries, 576 8 584
579 !iterator__ignore_case(&iter->base));
580  
581 584 done:
582 584 ▶ if (error < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) { 0 584 584
583 0 git_tree_free(dup);
584 0 ▶ git_array_pop(iter->frames);
Branch: 1 branch
BranchTrueFalseTotal
git_array_pop(iter->frames); 0 0 0
585 }
586  
587 584 return error;
588 }
589  
590 893 GIT_INLINE(tree_iterator_entry *) tree_iterator_current_entry(
591 tree_iterator_frame *frame)
592 {
593 893 return frame->current;
594 }
595  
596 4 GIT_INLINE(int) tree_iterator_frame_push_neighbors(
597 tree_iterator *iter,
598 tree_iterator_frame *parent_frame,
599 tree_iterator_frame *frame,
600 const char *filename)
601 {
602 tree_iterator_entry *entry, *new_entry;
603 4 git_tree *tree = NULL;
604 git_tree_entry *tree_entry;
605 git_str *path;
606 size_t new_size, i;
607 4 int error = 0;
608  
609 4 ▶ while (parent_frame->next_idx < parent_frame->entries.length) {
Branch: 1 branch
BranchTrueFalseTotal
while (parent_frame->next_idx < parent_frame->entries.length) { 0 4 4
610 0 entry = parent_frame->entries.contents[parent_frame->next_idx];
611  
612 0 ▶ if (strcasecmp(filename, entry->tree_entry->filename) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcasecmp(filename, entry->tree_entry->filename) != 0) 0 0 0
613 0 break;
614  
615 0 ▶ if ((error = git_tree_lookup(&tree,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_tree_lookup(&tree, 0 0 0
616 0 iter->base.repo, &entry->tree_entry->oid)) < 0)
617 0 break;
618  
619 0 ▶ if (git_vector_insert(&parent_frame->similar_trees, tree) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_vector_insert(&parent_frame->similar_trees, tree) < 0) 0 0 0
620 0 break;
621  
622 0 ▶ path = git_array_alloc(parent_frame->similar_paths);
Branch: 2 branches
BranchTrueFalseTotal
path = git_array_alloc(parent_frame->similar_paths); 0 0 0
path = git_array_alloc(parent_frame->similar_paths); 0 0 0
623 0 ▶ GIT_ERROR_CHECK_ALLOC(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(path); 0 0 0
624  
625 0 memset(path, 0, sizeof(git_str));
626  
627 0 ▶ if ((error = tree_iterator_compute_path(path, entry)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = tree_iterator_compute_path(path, entry)) < 0) 0 0 0
628 0 break;
629  
630 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&new_size,
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&new_size, 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&new_size, 0 0 0
631 frame->entries.length, tree->entries.size);
632 0 git_vector_size_hint(&frame->entries, new_size);
633  
634 0 ▶ git_array_foreach(tree->entries, i, tree_entry) {
Branch: 2 branches
BranchTrueFalseTotal
git_array_foreach(tree->entries, i, tree_entry) { 0 0 0
git_array_foreach(tree->entries, i, tree_entry) { 0 0 0
635 0 new_entry = git_pool_malloc(&iter->entry_pool, 1);
636 0 ▶ GIT_ERROR_CHECK_ALLOC(new_entry);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(new_entry); 0 0 0
637  
638 0 new_entry->tree_entry = tree_entry;
639 0 new_entry->parent_path = path->ptr;
640  
641 0 ▶ if ((error = git_vector_insert(&frame->entries, new_entry)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_insert(&frame->entries, new_entry)) < 0) 0 0 0
642 0 break;
643 }
644  
645 0 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 0 0 0
646 0 break;
647  
648 0 parent_frame->next_idx++;
649 }
650  
651 4 return error;
652 }
653  
654 260 GIT_INLINE(int) tree_iterator_frame_push(
655 tree_iterator *iter, tree_iterator_entry *entry)
656 {
657 tree_iterator_frame *parent_frame, *frame;
658 260 git_tree *tree = NULL;
659 int error;
660  
661 260 ▶ if ((error = git_tree_lookup(&tree,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_tree_lookup(&tree, 260 0 260
662 520 ▶ iter->base.repo, &entry->tree_entry->oid)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
iter->base.repo, &entry->tree_entry->oid)) < 0 || 0 260 260
663 260 (error = tree_iterator_frame_init(iter, tree, entry)) < 0)
664 0 goto done;
665  
666 260 parent_frame = tree_iterator_parent_frame(iter);
667 260 frame = tree_iterator_current_frame(iter);
668  
669 /* if we're case insensitive, then we may have another directory that
670 * is (case insensitively) equal to this one. coalesce those children
671 * into this tree.
672 */
673 260 ▶ if (iterator__ignore_case(&iter->base))
Branch: 1 branch
BranchTrueFalseTotal
if (iterator__ignore_case(&iter->base)) 256 4 260
674 4 error = tree_iterator_frame_push_neighbors(iter,
675 4 parent_frame, frame, entry->tree_entry->filename);
676  
677 256 done:
678 260 git_tree_free(tree);
679 260 return error;
680 }
681  
682 584 static int tree_iterator_frame_pop(tree_iterator *iter)
683 {
684 tree_iterator_frame *frame;
685 584 git_str *buf = NULL;
686 git_tree *tree;
687 size_t i;
688  
689 584 ▶ GIT_ASSERT(iter->frames.size);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(iter->frames.size); 0 584 584
690  
691 584 ▶ frame = git_array_pop(iter->frames);
Branch: 1 branch
BranchTrueFalseTotal
frame = git_array_pop(iter->frames); 584 0 584
692  
693 584 git_vector_free(&frame->entries);
694 584 git_tree_free(frame->tree);
695  
696 do {
697 584 ▶ buf = git_array_pop(frame->similar_paths);
Branch: 1 branch
BranchTrueFalseTotal
buf = git_array_pop(frame->similar_paths); 0 584 584
698 584 git_str_dispose(buf);
699 584 ▶ } while (buf != NULL);
Branch: 1 branch
BranchTrueFalseTotal
} while (buf != NULL); 0 584 584
700  
701 584 git_array_clear(frame->similar_paths);
702  
703 584 ▶ git_vector_foreach(&frame->similar_trees, i, tree)
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&frame->similar_trees, i, tree) 0 584 584
704 0 git_tree_free(tree);
705  
706 584 git_vector_free(&frame->similar_trees);
707  
708 584 git_str_dispose(&frame->path);
709  
710 584 return 0;
711 }
712  
713 280 static int tree_iterator_current(
714 const git_index_entry **out, git_iterator *i)
715 {
716 280 tree_iterator *iter = (tree_iterator *)i;
717  
718 280 ▶ if (!iterator__has_been_accessed(i))
Branch: 1 branch
BranchTrueFalseTotal
if (!iterator__has_been_accessed(i)) 280 0 280
719 280 return iter->base.cb->advance(out, i);
720  
721 0 ▶ if (!iter->frames.size) {
Branch: 1 branch
BranchTrueFalseTotal
if (!iter->frames.size) { 0 0 0
722 0 *out = NULL;
723 0 return GIT_ITEROVER;
724 }
725  
726 0 *out = &iter->entry;
727 0 return 0;
728 }
729  
730 607 static void tree_iterator_set_current(
731 tree_iterator *iter,
732 tree_iterator_frame *frame,
733 tree_iterator_entry *entry)
734 {
735 607 git_tree_entry *tree_entry = entry->tree_entry;
736  
737 607 frame->current = entry;
738  
739 607 memset(&iter->entry, 0x0, sizeof(git_index_entry));
740  
741 607 iter->entry.mode = tree_entry->attr;
742 607 iter->entry.path = iter->entry_path.ptr;
743 607 git_oid_cpy(&iter->entry.id, &tree_entry->oid);
744 607 }
745  
746 891 static int tree_iterator_advance(const git_index_entry **out, git_iterator *i)
747 {
748 891 tree_iterator *iter = (tree_iterator *)i;
749 891 int error = 0;
750  
751 891 iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
752  
753 /* examine tree entries until we find the next one to return */
754 818 while (true) {
755 tree_iterator_entry *prev_entry, *entry;
756 tree_iterator_frame *frame;
757 bool is_tree;
758  
759 1709 ▶ if ((frame = tree_iterator_current_frame(iter)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((frame = tree_iterator_current_frame(iter)) == NULL) { 278 1431 1709
760 278 error = GIT_ITEROVER;
761 278 break;
762 }
763  
764 /* no more entries in this frame. pop the frame out */
765 1431 ▶ if (frame->next_idx == frame->entries.length) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->next_idx == frame->entries.length) { 538 893 1431
766 538 ▶ if ((error = tree_iterator_frame_pop(iter)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = tree_iterator_frame_pop(iter)) < 0) 0 538 538
767 0 break;
768  
769 538 continue;
770 }
771  
772 /* we may have coalesced the contents of case-insensitively same-named
773 * directories, so do the sort now.
774 */
775 893 ▶ if (frame->next_idx == 0 && !git_vector_is_sorted(&frame->entries))
Branch: 2 branches
BranchTrueFalseTotal
if (frame->next_idx == 0 && !git_vector_is_sorted(&frame->entries)) 544 349 893
if (frame->next_idx == 0 && !git_vector_is_sorted(&frame->entries)) 6 538 544
776 6 git_vector_sort(&frame->entries);
777  
778 /* we have more entries in the current frame, that's our next entry */
779 893 prev_entry = tree_iterator_current_entry(frame);
780 893 entry = frame->entries.contents[frame->next_idx];
781 893 frame->next_idx++;
782  
783 /* we can have collisions when iterating case insensitively. (eg,
784 * 'A/a' and 'a/A'). squash this one if it's already been seen.
785 */
786 893 ▶ if (iterator__ignore_case(&iter->base) &&
Branch: 2 branches
BranchTrueFalseTotal
if (iterator__ignore_case(&iter->base) && 10 883 893
if (iterator__ignore_case(&iter->base) && 4 6 10
787 4 ▶ prev_entry &&
Branch: 1 branch
BranchTrueFalseTotal
prev_entry && 0 4 4
788 4 tree_iterator_entry_cmp_icase(prev_entry, entry) == 0)
789 0 continue;
790  
791 893 ▶ if ((error = tree_iterator_compute_path(&iter->entry_path, entry)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = tree_iterator_compute_path(&iter->entry_path, entry)) < 0) 0 893 893
792 0 break;
793  
794 /* if this path is before our start, advance over this entry */
795 893 ▶ if (!iterator_has_started(&iter->base, iter->entry_path.ptr, false))
Branch: 1 branch
BranchTrueFalseTotal
if (!iterator_has_started(&iter->base, iter->entry_path.ptr, false)) 0 893 893
796 0 continue;
797  
798 /* if this path is after our end, stop */
799 893 ▶ if (iterator_has_ended(&iter->base, iter->entry_path.ptr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (iterator_has_ended(&iter->base, iter->entry_path.ptr)) { 6 887 893
800 6 error = GIT_ITEROVER;
801 6 break;
802 }
803  
804 /* if we have a list of paths we're interested in, examine it */
805 887 ▶ if (!iterator_pathlist_next_is(&iter->base, iter->entry_path.ptr))
Branch: 1 branch
BranchTrueFalseTotal
if (!iterator_pathlist_next_is(&iter->base, iter->entry_path.ptr)) 20 867 887
806 20 continue;
807  
808 867 is_tree = git_tree_entry__is_tree(entry->tree_entry);
809  
810 /* if we are *not* including trees then advance over this entry */
811 867 ▶ if (is_tree && !iterator__include_trees(iter)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_tree && !iterator__include_trees(iter)) { 260 607 867
if (is_tree && !iterator__include_trees(iter)) { 260 0 260
812  
813 /* if we've found a tree (and are not returning it to the caller)
814 * and we are autoexpanding, then we want to return the first
815 * child. push the new directory and advance.
816 */
817 260 ▶ if (iterator__do_autoexpand(iter)) {
Branch: 1 branch
BranchTrueFalseTotal
if (iterator__do_autoexpand(iter)) { 260 0 260
818 260 ▶ if ((error = tree_iterator_frame_push(iter, entry)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = tree_iterator_frame_push(iter, entry)) < 0) 0 260 260
819 0 break;
820 }
821  
822 260 continue;
823 }
824  
825 607 tree_iterator_set_current(iter, frame, entry);
826  
827 /* if we are autoexpanding, then push this as a new frame, so that
828 * the next call to `advance` will dive into this directory.
829 */
830 607 ▶ if (is_tree && iterator__do_autoexpand(iter))
Branch: 2 branches
BranchTrueFalseTotal
if (is_tree && iterator__do_autoexpand(iter)) 0 607 607
if (is_tree && iterator__do_autoexpand(iter)) 0 0 0
831 0 error = tree_iterator_frame_push(iter, entry);
832  
833 607 break;
834 }
835  
836 891 ▶ if (out)
Branch: 1 branch
BranchTrueFalseTotal
if (out) 891 0 891
837 891 ▶ *out = (error == 0) ? &iter->entry : NULL;
Branch: 1 branch
BranchTrueFalseTotal
*out = (error == 0) ? &iter->entry : NULL; 607 284 891
838  
839 891 return error;
840 }
841  
842 0 static int tree_iterator_advance_into(
843 const git_index_entry **out, git_iterator *i)
844 {
845 0 tree_iterator *iter = (tree_iterator *)i;
846 tree_iterator_frame *frame;
847 tree_iterator_entry *prev_entry;
848 int error;
849  
850 0 ▶ if (out)
Branch: 1 branch
BranchTrueFalseTotal
if (out) 0 0 0
851 0 *out = NULL;
852  
853 0 ▶ if ((frame = tree_iterator_current_frame(iter)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((frame = tree_iterator_current_frame(iter)) == NULL) 0 0 0
854 0 return GIT_ITEROVER;
855  
856 /* get the last seen entry */
857 0 prev_entry = tree_iterator_current_entry(frame);
858  
859 /* it's legal to call advance_into when auto-expand is on. in this case,
860 * we will have pushed a new (empty) frame on to the stack for this
861 * new directory. since it's empty, its current_entry should be null.
862 */
863 0 ▶ GIT_ASSERT(iterator__do_autoexpand(i) ^ (prev_entry != NULL));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(iterator__do_autoexpand(i) ^ (prev_entry != NULL)); 0 0 0
864  
865 0 ▶ if (prev_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev_entry) { 0 0 0
866 0 ▶ if (!git_tree_entry__is_tree(prev_entry->tree_entry))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_tree_entry__is_tree(prev_entry->tree_entry)) 0 0 0
867 0 return 0;
868  
869 0 ▶ if ((error = tree_iterator_frame_push(iter, prev_entry)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = tree_iterator_frame_push(iter, prev_entry)) < 0) 0 0 0
870 0 return error;
871 }
872  
873 /* we've advanced into the directory in question, let advance
874 * find the first entry
875 */
876 0 return tree_iterator_advance(out, i);
877 }
878  
879 0 static int tree_iterator_advance_over(
880 const git_index_entry **out,
881 git_iterator_status_t *status,
882 git_iterator *i)
883 {
884 0 *status = GIT_ITERATOR_STATUS_NORMAL;
885 0 return git_iterator_advance(out, i);
886 }
887  
888 324 static void tree_iterator_clear(tree_iterator *iter)
889 {
890 370 ▶ while (iter->frames.size)
Branch: 1 branch
BranchTrueFalseTotal
while (iter->frames.size) 46 324 370
891 46 tree_iterator_frame_pop(iter);
892  
893 324 git_array_clear(iter->frames);
894  
895 324 git_pool_clear(&iter->entry_pool);
896 324 git_str_clear(&iter->entry_path);
897  
898 324 iterator_clear(&iter->base);
899 324 }
900  
901 324 static int tree_iterator_init(tree_iterator *iter)
902 {
903 int error;
904  
905 648 if ((error = git_pool_init(&iter->entry_pool, sizeof(tree_iterator_entry))) < 0 ||
906 324 (error = tree_iterator_frame_init(iter, iter->root, NULL)) < 0)
907 0 return error;
908  
909 324 iter->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
910  
911 324 return 0;
912 }
913  
914 40 static int tree_iterator_reset(git_iterator *i)
915 {
916 40 tree_iterator *iter = (tree_iterator *)i;
917  
918 40 tree_iterator_clear(iter);
919 40 return tree_iterator_init(iter);
920 }
921  
922 284 static void tree_iterator_free(git_iterator *i)
923 {
924 284 tree_iterator *iter = (tree_iterator *)i;
925  
926 284 tree_iterator_clear(iter);
927  
928 284 git_tree_free(iter->root);
929 284 git_str_dispose(&iter->entry_path);
930 284 }
931  
932 301 int git_iterator_for_tree(
933 git_iterator **out,
934 git_tree *tree,
935 git_iterator_options *options)
936 {
937 tree_iterator *iter;
938 int error;
939  
940 static git_iterator_callbacks callbacks = {
941 tree_iterator_current,
942 tree_iterator_advance,
943 tree_iterator_advance_into,
944 tree_iterator_advance_over,
945 tree_iterator_reset,
946 tree_iterator_free
947 };
948  
949 301 *out = NULL;
950  
951 301 ▶ if (tree == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (tree == NULL) 17 284 301
952 17 return git_iterator_for_nothing(out, options);
953  
954 284 iter = git__calloc(1, sizeof(tree_iterator));
955 284 ▶ GIT_ERROR_CHECK_ALLOC(iter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iter); 0 284 284
956  
957 284 iter->base.type = GIT_ITERATOR_TREE;
958 284 iter->base.cb = &callbacks;
959  
960 284 ▶ if ((error = iterator_init_common(&iter->base,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = iterator_init_common(&iter->base, 284 0 284
961 284 ▶ git_tree_owner(tree), NULL, options)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
git_tree_owner(tree), NULL, options)) < 0 || 284 0 284
962 568 ▶ (error = git_tree_dup(&iter->root, tree)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_tree_dup(&iter->root, tree)) < 0 || 0 284 284
963 284 (error = tree_iterator_init(iter)) < 0)
964 0 goto on_error;
965  
966 284 *out = &iter->base;
967 284 return 0;
968  
969 0 on_error:
970 0 git_iterator_free(&iter->base);
971 0 return error;
972 }
973  
974 0 int git_iterator_current_tree_entry(
975 const git_tree_entry **tree_entry, git_iterator *i)
976 {
977 tree_iterator *iter;
978 tree_iterator_frame *frame;
979 tree_iterator_entry *entry;
980  
981 0 ▶ GIT_ASSERT(i->type == GIT_ITERATOR_TREE);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(i->type == GIT_ITERATOR_TREE); 0 0 0
982  
983 0 iter = (tree_iterator *)i;
984  
985 0 frame = tree_iterator_current_frame(iter);
986 0 entry = tree_iterator_current_entry(frame);
987  
988 0 *tree_entry = entry->tree_entry;
989 0 return 0;
990 }
991  
992 0 int git_iterator_current_parent_tree(
993 const git_tree **parent_tree, git_iterator *i, size_t depth)
994 {
995 tree_iterator *iter;
996 tree_iterator_frame *frame;
997  
998 0 ▶ GIT_ASSERT(i->type == GIT_ITERATOR_TREE);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(i->type == GIT_ITERATOR_TREE); 0 0 0
999  
1000 0 iter = (tree_iterator *)i;
1001  
1002 0 ▶ GIT_ASSERT(depth < iter->frames.size);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(depth < iter->frames.size); 0 0 0
1003 0 frame = &iter->frames.ptr[iter->frames.size-depth-1];
1004  
1005 0 *parent_tree = frame->tree;
1006 0 return 0;
1007 }
1008  
1009 /* Filesystem iterator */
1010  
1011 typedef struct {
1012 struct stat st;
1013 size_t path_len;
1014 iterator_pathlist_search_t match;
1015 git_oid id;
1016 char path[GIT_FLEX_ARRAY];
1017 } filesystem_iterator_entry;
1018  
1019 typedef struct {
1020 git_vector entries;
1021 git_pool entry_pool;
1022 size_t next_idx;
1023  
1024 size_t path_len;
1025 int is_ignored;
1026 } filesystem_iterator_frame;
1027  
1028 typedef struct {
1029 git_iterator base;
1030 char *root;
1031 size_t root_len;
1032  
1033 unsigned int dirload_flags;
1034  
1035 git_tree *tree;
1036 git_index *index;
1037 git_vector index_snapshot;
1038  
1039 git_array_t(filesystem_iterator_frame) frames;
1040 git_ignores ignores;
1041  
1042 /* info about the current entry */
1043 git_index_entry entry;
1044 git_str current_path;
1045 int current_is_ignored;
1046  
1047 /* temporary buffer for advance_over */
1048 git_str tmp_buf;
1049 } filesystem_iterator;
1050  
1051  
1052 176 GIT_INLINE(filesystem_iterator_frame *) filesystem_iterator_parent_frame(
1053 filesystem_iterator *iter)
1054 {
1055 176 return iter->frames.size > 1 ?
1056 176 ▶ &iter->frames.ptr[iter->frames.size-2] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
&iter->frames.ptr[iter->frames.size-2] : NULL; 176 0 176
1057 }
1058  
1059 2234 GIT_INLINE(filesystem_iterator_frame *) filesystem_iterator_current_frame(
1060 filesystem_iterator *iter)
1061 {
1062 2234 ▶ return iter->frames.size ? &iter->frames.ptr[iter->frames.size-1] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return iter->frames.size ? &iter->frames.ptr[iter->frames.size-1] : NULL; 2028 206 2234
1063 }
1064  
1065 243 GIT_INLINE(filesystem_iterator_entry *) filesystem_iterator_current_entry(
1066 filesystem_iterator_frame *frame)
1067 {
1068 243 return frame->next_idx == 0 ?
1069 243 ▶ NULL : frame->entries.contents[frame->next_idx-1];
Branch: 1 branch
BranchTrueFalseTotal
NULL : frame->entries.contents[frame->next_idx-1]; 243 0 243
1070 }
1071  
1072 1352 static int filesystem_iterator_entry_cmp(const void *_a, const void *_b)
1073 {
1074 1352 const filesystem_iterator_entry *a = (const filesystem_iterator_entry *)_a;
1075 1352 const filesystem_iterator_entry *b = (const filesystem_iterator_entry *)_b;
1076  
1077 1352 return git__strcmp(a->path, b->path);
1078 }
1079  
1080 4 static int filesystem_iterator_entry_cmp_icase(const void *_a, const void *_b)
1081 {
1082 4 const filesystem_iterator_entry *a = (const filesystem_iterator_entry *)_a;
1083 4 const filesystem_iterator_entry *b = (const filesystem_iterator_entry *)_b;
1084  
1085 4 return git__strcasecmp(a->path, b->path);
1086 }
1087  
1088 #define FILESYSTEM_MAX_DEPTH 100
1089  
1090 /**
1091 * Figure out if an entry is a submodule.
1092 *
1093 * We consider it a submodule if the path is listed as a submodule in
1094 * either the tree or the index.
1095 */
1096 266 static int filesystem_iterator_is_submodule(
1097 bool *out, filesystem_iterator *iter, const char *path, size_t path_len)
1098 {
1099 266 bool is_submodule = false;
1100 int error;
1101  
1102 266 *out = false;
1103  
1104 /* first see if this path is a submodule in HEAD */
1105 266 ▶ if (iter->tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->tree) { 9 257 266
1106 git_tree_entry *entry;
1107  
1108 9 error = git_tree_entry_bypath(&entry, iter->tree, path);
1109  
1110 9 ▶ if (error < 0 && error != GIT_ENOTFOUND)
Branch: 2 branches
BranchTrueFalseTotal
if (error < 0 && error != GIT_ENOTFOUND) 1 8 9
if (error < 0 && error != GIT_ENOTFOUND) 0 1 1
1111 0 return error;
1112  
1113 9 ▶ if (!error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!error) { 8 1 9
1114 8 is_submodule = (entry->attr == GIT_FILEMODE_COMMIT);
1115 8 git_tree_entry_free(entry);
1116 }
1117 }
1118  
1119 266 ▶ if (!is_submodule && iter->base.index) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_submodule && iter->base.index) { 266 0 266
if (!is_submodule && iter->base.index) { 206 60 266
1120 size_t pos;
1121  
1122 206 error = git_index_snapshot_find(&pos,
1123 &iter->index_snapshot, iter->base.entry_srch, path, path_len, 0);
1124  
1125 206 ▶ if (error < 0 && error != GIT_ENOTFOUND)
Branch: 2 branches
BranchTrueFalseTotal
if (error < 0 && error != GIT_ENOTFOUND) 206 0 206
if (error < 0 && error != GIT_ENOTFOUND) 0 206 206
1126 0 return error;
1127  
1128 206 ▶ if (!error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!error) { 0 206 206
1129 0 git_index_entry *e = git_vector_get(&iter->index_snapshot, pos);
1130 0 is_submodule = (e->mode == GIT_FILEMODE_COMMIT);
1131 }
1132 }
1133  
1134 266 *out = is_submodule;
1135 266 return 0;
1136 }
1137  
1138 448 static void filesystem_iterator_frame_push_ignores(
1139 filesystem_iterator *iter,
1140 filesystem_iterator_entry *frame_entry,
1141 filesystem_iterator_frame *new_frame)
1142 {
1143 filesystem_iterator_frame *previous_frame;
1144 448 ▶ const char *path = frame_entry ? frame_entry->path : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *path = frame_entry ? frame_entry->path : ""; 236 212 448
1145  
1146 448 ▶ if (!iterator__honor_ignores(&iter->base))
Branch: 1 branch
BranchTrueFalseTotal
if (!iterator__honor_ignores(&iter->base)) 89 359 448
1147 89 return;
1148  
1149 359 ▶ if (git_ignore__lookup(&new_frame->is_ignored,
Branch: 1 branch
BranchTrueFalseTotal
if (git_ignore__lookup(&new_frame->is_ignored, 0 359 359
1150 &iter->ignores, path, GIT_DIR_FLAG_TRUE) < 0) {
1151 0 git_error_clear();
1152 0 new_frame->is_ignored = GIT_IGNORE_NOTFOUND;
1153 }
1154  
1155 /* if this is not the top level directory... */
1156 359 ▶ if (frame_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame_entry) { 176 183 359
1157 const char *relative_path;
1158  
1159 176 previous_frame = filesystem_iterator_parent_frame(iter);
1160  
1161 /* push new ignores for files in this directory */
1162 176 relative_path = frame_entry->path + previous_frame->path_len;
1163  
1164 /* inherit ignored from parent if no rule specified */
1165 176 ▶ if (new_frame->is_ignored <= GIT_IGNORE_NOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (new_frame->is_ignored <= GIT_IGNORE_NOTFOUND) 176 0 176
1166 176 new_frame->is_ignored = previous_frame->is_ignored;
1167  
1168 176 git_ignore__push_dir(&iter->ignores, relative_path);
1169 }
1170 }
1171  
1172 448 static void filesystem_iterator_frame_pop_ignores(
1173 filesystem_iterator *iter)
1174 {
1175 448 ▶ if (iterator__honor_ignores(&iter->base))
Branch: 1 branch
BranchTrueFalseTotal
if (iterator__honor_ignores(&iter->base)) 359 89 448
1176 359 git_ignore__pop_dir(&iter->ignores);
1177 448 }
1178  
1179 1229 GIT_INLINE(bool) filesystem_iterator_examine_path(
1180 bool *is_dir_out,
1181 iterator_pathlist_search_t *match_out,
1182 filesystem_iterator *iter,
1183 filesystem_iterator_entry *frame_entry,
1184 const char *path,
1185 size_t path_len)
1186 {
1187 1229 bool is_dir = 0;
1188 1229 iterator_pathlist_search_t match = ITERATOR_PATHLIST_FULL;
1189  
1190 1229 *is_dir_out = false;
1191 1229 *match_out = ITERATOR_PATHLIST_NONE;
1192  
1193 1229 ▶ if (iter->base.start_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->base.start_len) { 46 1183 1229
1194 46 int cmp = iter->base.strncomp(path, iter->base.start, path_len);
1195  
1196 /* we haven't stat'ed `path` yet, so we don't yet know if it's a
1197 * directory or not. special case if the current path may be a
1198 * directory that matches the start prefix.
1199 */
1200 46 ▶ if (cmp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmp == 0) { 6 40 46
1201 6 ▶ if (iter->base.start[path_len] == '/')
Branch: 1 branch
BranchTrueFalseTotal
if (iter->base.start[path_len] == '/') 1 5 6
1202 1 is_dir = true;
1203  
1204 5 ▶ else if (iter->base.start[path_len] != '\0')
Branch: 1 branch
BranchTrueFalseTotal
else if (iter->base.start[path_len] != '\0') 0 5 5
1205 0 cmp = -1;
1206 }
1207  
1208 46 ▶ if (cmp < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cmp < 0) 20 26 46
1209 20 return false;
1210 }
1211  
1212 1209 ▶ if (iter->base.end_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->base.end_len) { 26 1183 1209
1213 26 int cmp = iter->base.strncomp(path, iter->base.end, iter->base.end_len);
1214  
1215 26 ▶ if (cmp > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cmp > 0) 14 12 26
1216 14 return false;
1217 }
1218  
1219 /* if we have a pathlist that we're limiting to, examine this path now
1220 * to avoid a `stat` if we're not interested in the path.
1221 */
1222 1195 ▶ if (iter->base.pathlist.length) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->base.pathlist.length) { 145 1050 1195
1223 /* if our parent was explicitly included, so too are we */
1224 145 ▶ if (frame_entry && frame_entry->match != ITERATOR_PATHLIST_IS_PARENT)
Branch: 2 branches
BranchTrueFalseTotal
if (frame_entry && frame_entry->match != ITERATOR_PATHLIST_IS_PARENT) 8 137 145
if (frame_entry && frame_entry->match != ITERATOR_PATHLIST_IS_PARENT) 0 8 8
1225 0 match = ITERATOR_PATHLIST_FULL;
1226 else
1227 145 match = iterator_pathlist_search(&iter->base, path, path_len);
1228  
1229 145 ▶ if (match == ITERATOR_PATHLIST_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if (match == ITERATOR_PATHLIST_NONE) 99 46 145
1230 99 return false;
1231  
1232 /* Ensure that the pathlist entry lines up with what we expected */
1233 46 ▶ if (match == ITERATOR_PATHLIST_IS_DIR ||
Branch: 2 branches
BranchTrueFalseTotal
if (match == ITERATOR_PATHLIST_IS_DIR || 46 0 46
if (match == ITERATOR_PATHLIST_IS_DIR || 8 38 46
1234 match == ITERATOR_PATHLIST_IS_PARENT)
1235 8 is_dir = true;
1236 }
1237  
1238 1096 *is_dir_out = is_dir;
1239 1096 *match_out = match;
1240 1096 return true;
1241 }
1242  
1243 1096 GIT_INLINE(bool) filesystem_iterator_is_dot_git(
1244 filesystem_iterator *iter, const char *path, size_t path_len)
1245 {
1246 size_t len;
1247  
1248 1096 ▶ if (!iterator__ignore_dot_git(&iter->base))
Branch: 1 branch
BranchTrueFalseTotal
if (!iterator__ignore_dot_git(&iter->base)) 174 922 1096
1249 174 return false;
1250  
1251 922 ▶ if ((len = path_len) < 4)
Branch: 1 branch
BranchTrueFalseTotal
if ((len = path_len) < 4) 0 922 922
1252 0 return false;
1253  
1254 922 ▶ if (path[len - 1] == '/')
Branch: 1 branch
BranchTrueFalseTotal
if (path[len - 1] == '/') 0 922 922
1255 0 len--;
1256  
1257 922 ▶ if (git__tolower(path[len - 1]) != 't' ||
Branch: 1 branch
BranchTrueFalseTotal
if (git__tolower(path[len - 1]) != 't' || 305 617 922
1258 305 ▶ git__tolower(path[len - 2]) != 'i' ||
Branch: 1 branch
BranchTrueFalseTotal
git__tolower(path[len - 2]) != 'i' || 137 168 305
1259 137 ▶ git__tolower(path[len - 3]) != 'g' ||
Branch: 1 branch
BranchTrueFalseTotal
git__tolower(path[len - 3]) != 'g' || 137 0 137
1260 137 ▶ git__tolower(path[len - 4]) != '.')
Branch: 1 branch
BranchTrueFalseTotal
git__tolower(path[len - 4]) != '.') 0 137 137
1261 785 return false;
1262  
1263 137 ▶ return (len == 4 || path[len - 5] == '/');
Branch: 2 branches
BranchTrueFalseTotal
return (len == 4 || path[len - 5] == '/'); 0 137 137
return (len == 4 || path[len - 5] == '/'); 0 0 0
1264 }
1265  
1266 0 static int filesystem_iterator_entry_hash(
1267 filesystem_iterator *iter,
1268 filesystem_iterator_entry *entry)
1269 {
1270 0 git_str fullpath = GIT_STR_INIT;
1271 int error;
1272  
1273 0 ▶ if (S_ISDIR(entry->st.st_mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR(entry->st.st_mode)) { 0 0 0
1274 0 memset(&entry->id, 0, GIT_OID_RAWSZ);
1275 0 return 0;
1276 }
1277  
1278 0 ▶ if (iter->base.type == GIT_ITERATOR_WORKDIR)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->base.type == GIT_ITERATOR_WORKDIR) 0 0 0
1279 0 return git_repository_hashfile(&entry->id,
1280 0 iter->base.repo, entry->path, GIT_OBJECT_BLOB, NULL);
1281  
1282 0 if (!(error = git_str_joinpath(&fullpath, iter->root, entry->path)) &&
1283 0 !(error = git_path_validate_str_length(iter->base.repo, &fullpath)))
1284 0 error = git_odb_hashfile(&entry->id, fullpath.ptr, GIT_OBJECT_BLOB);
1285  
1286 0 git_str_dispose(&fullpath);
1287 0 return error;
1288 }
1289  
1290 959 static int filesystem_iterator_entry_init(
1291 filesystem_iterator_entry **out,
1292 filesystem_iterator *iter,
1293 filesystem_iterator_frame *frame,
1294 const char *path,
1295 size_t path_len,
1296 struct stat *statbuf,
1297 iterator_pathlist_search_t pathlist_match)
1298 {
1299 filesystem_iterator_entry *entry;
1300 size_t entry_size;
1301 959 int error = 0;
1302  
1303 959 *out = NULL;
1304  
1305 /* Make sure to append two bytes, one for the path's null
1306 * termination, one for a possible trailing '/' for folders.
1307 */
1308 959 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&entry_size,
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&entry_size, 0 959 959
GIT_ERROR_CHECK_ALLOC_ADD(&entry_size, 0 959 959
1309 sizeof(filesystem_iterator_entry), path_len);
1310 959 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&entry_size, entry_size, 2);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&entry_size, entry_size, 2); 0 959 959
GIT_ERROR_CHECK_ALLOC_ADD(&entry_size, entry_size, 2); 0 959 959
1311  
1312 959 entry = git_pool_malloc(&frame->entry_pool, entry_size);
1313 959 ▶ GIT_ERROR_CHECK_ALLOC(entry);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(entry); 0 959 959
1314  
1315 959 entry->path_len = path_len;
1316 959 entry->match = pathlist_match;
1317 959 memcpy(entry->path, path, path_len);
1318 959 memcpy(&entry->st, statbuf, sizeof(struct stat));
1319  
1320 /* Suffix directory paths with a '/' */
1321 959 ▶ if (S_ISDIR(entry->st.st_mode))
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR(entry->st.st_mode)) 266 693 959
1322 266 entry->path[entry->path_len++] = '/';
1323  
1324 959 entry->path[entry->path_len] = '\0';
1325  
1326 959 ▶ if (iter->base.flags & GIT_ITERATOR_INCLUDE_HASH)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->base.flags & GIT_ITERATOR_INCLUDE_HASH) 0 959 959
1327 0 error = filesystem_iterator_entry_hash(iter, entry);
1328  
1329 959 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 959 0 959
1330 959 *out = entry;
1331  
1332 959 return error;
1333 }
1334  
1335 450 static int filesystem_iterator_frame_push(
1336 filesystem_iterator *iter,
1337 filesystem_iterator_entry *frame_entry)
1338 {
1339 450 filesystem_iterator_frame *new_frame = NULL;
1340 450 git_fs_path_diriter diriter = GIT_FS_PATH_DIRITER_INIT;
1341 450 git_str root = GIT_STR_INIT;
1342 const char *path;
1343 filesystem_iterator_entry *entry;
1344 struct stat statbuf;
1345 size_t path_len;
1346 int error;
1347  
1348 450 ▶ if (iter->frames.size == FILESYSTEM_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->frames.size == FILESYSTEM_MAX_DEPTH) { 0 450 450
1349 0 git_error_set(GIT_ERROR_REPOSITORY,
1350 "directory nesting too deep (%"PRIuZ")", iter->frames.size);
1351 0 return -1;
1352 }
1353  
1354 450 ▶ new_frame = git_array_alloc(iter->frames);
Branch: 2 branches
BranchTrueFalseTotal
new_frame = git_array_alloc(iter->frames); 214 236 450
new_frame = git_array_alloc(iter->frames); 214 0 214
1355 450 ▶ GIT_ERROR_CHECK_ALLOC(new_frame);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(new_frame); 0 450 450
1356  
1357 450 memset(new_frame, 0, sizeof(filesystem_iterator_frame));
1358  
1359 450 ▶ if (frame_entry)
Branch: 1 branch
BranchTrueFalseTotal
if (frame_entry) 236 214 450
1360 236 git_str_joinpath(&root, iter->root, frame_entry->path);
1361 else
1362 214 git_str_puts(&root, iter->root);
1363  
1364 900 if (git_str_oom(&root) ||
1365 450 git_path_validate_str_length(iter->base.repo, &root) < 0) {
1366 0 error = -1;
1367 0 goto done;
1368 }
1369  
1370 450 ▶ new_frame->path_len = frame_entry ? frame_entry->path_len : 0;
Branch: 1 branch
BranchTrueFalseTotal
new_frame->path_len = frame_entry ? frame_entry->path_len : 0; 236 214 450
1371  
1372 /* Any error here is equivalent to the dir not existing, skip over it */
1373 450 ▶ if ((error = git_fs_path_diriter_init(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_fs_path_diriter_init( 2 448 450
1374 450 &diriter, root.ptr, iter->dirload_flags)) < 0) {
1375 2 error = GIT_ENOTFOUND;
1376 2 goto done;
1377 }
1378  
1379 448 ▶ if ((error = git_vector_init(&new_frame->entries, 64,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_init(&new_frame->entries, 64, 0 448 448
1380 448 ▶ iterator__ignore_case(&iter->base) ?
Branch: 1 branch
BranchTrueFalseTotal
iterator__ignore_case(&iter->base) ? 2 446 448
1381 filesystem_iterator_entry_cmp_icase :
1382 filesystem_iterator_entry_cmp)) < 0)
1383 0 goto done;
1384  
1385 448 ▶ if ((error = git_pool_init(&new_frame->entry_pool, 1)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_pool_init(&new_frame->entry_pool, 1)) < 0) 0 448 448
1386 0 goto done;
1387  
1388 /* check if this directory is ignored */
1389 448 filesystem_iterator_frame_push_ignores(iter, frame_entry, new_frame);
1390  
1391 1677 ▶ while ((error = git_fs_path_diriter_next(&diriter)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((error = git_fs_path_diriter_next(&diriter)) == 0) { 1229 448 1677
1392 1229 iterator_pathlist_search_t pathlist_match = ITERATOR_PATHLIST_FULL;
1393 1229 git_str path_str = GIT_STR_INIT;
1394 1229 bool dir_expected = false;
1395  
1396 1229 ▶ if ((error = git_fs_path_diriter_fullpath(&path, &path_len, &diriter)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_fs_path_diriter_fullpath(&path, &path_len, &diriter)) < 0) 0 1229 1229
1397 0 goto done;
1398  
1399 1229 path_str.ptr = (char *)path;
1400 1229 path_str.size = path_len;
1401  
1402 1229 ▶ if ((error = git_path_validate_str_length(iter->base.repo, &path_str)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_path_validate_str_length(iter->base.repo, &path_str)) < 0) 0 1229 1229
1403 0 goto done;
1404  
1405 1229 ▶ GIT_ASSERT(path_len > iter->root_len);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(path_len > iter->root_len); 0 1229 1229
1406  
1407 /* remove the prefix if requested */
1408 1229 path += iter->root_len;
1409 1229 path_len -= iter->root_len;
1410  
1411 /* examine start / end and the pathlist to see if this path is in it.
1412 * note that since we haven't yet stat'ed the path, we cannot know
1413 * whether it's a directory yet or not, so this can give us an
1414 * expected type (S_IFDIR or S_IFREG) that we should examine)
1415 */
1416 1229 ▶ if (!filesystem_iterator_examine_path(&dir_expected, &pathlist_match,
Branch: 1 branch
BranchTrueFalseTotal
if (!filesystem_iterator_examine_path(&dir_expected, &pathlist_match, 133 1096 1229
1417 iter, frame_entry, path, path_len))
1418 270 continue;
1419  
1420 /* TODO: don't need to stat if assume unchanged for this path and
1421 * we have an index, we can just copy the data out of it.
1422 */
1423  
1424 1096 ▶ if ((error = git_fs_path_diriter_stat(&statbuf, &diriter)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_fs_path_diriter_stat(&statbuf, &diriter)) < 0) { 0 1096 1096
1425 /* file was removed between readdir and lstat */
1426 0 ▶ if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) 0 0 0
1427 0 continue;
1428  
1429 /* treat the file as unreadable */
1430 0 memset(&statbuf, 0, sizeof(statbuf));
1431 0 statbuf.st_mode = GIT_FILEMODE_UNREADABLE;
1432  
1433 0 error = 0;
1434 }
1435  
1436 1096 iter->base.stat_calls++;
1437  
1438 /* Ignore wacky things in the filesystem */
1439 1096 ▶ if (!S_ISDIR(statbuf.st_mode) &&
Branch: 1 branch
BranchTrueFalseTotal
if (!S_ISDIR(statbuf.st_mode) && 695 401 1096
1440 695 ▶ !S_ISREG(statbuf.st_mode) &&
Branch: 1 branch
BranchTrueFalseTotal
!S_ISREG(statbuf.st_mode) && 0 695 695
1441 0 ▶ !S_ISLNK(statbuf.st_mode) &&
Branch: 1 branch
BranchTrueFalseTotal
!S_ISLNK(statbuf.st_mode) && 0 0 0
1442 0 ▶ statbuf.st_mode != GIT_FILEMODE_UNREADABLE)
Branch: 1 branch
BranchTrueFalseTotal
statbuf.st_mode != GIT_FILEMODE_UNREADABLE) 0 0 0
1443 0 continue;
1444  
1445 1096 ▶ if (filesystem_iterator_is_dot_git(iter, path, path_len))
Branch: 1 branch
BranchTrueFalseTotal
if (filesystem_iterator_is_dot_git(iter, path, path_len)) 137 959 1096
1446 137 continue;
1447  
1448 /* convert submodules to GITLINK and remove trailing slashes */
1449 959 ▶ if (S_ISDIR(statbuf.st_mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR(statbuf.st_mode)) { 266 693 959
1450 266 bool submodule = false;
1451  
1452 266 ▶ if ((error = filesystem_iterator_is_submodule(&submodule,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = filesystem_iterator_is_submodule(&submodule, 0 266 266
1453 iter, path, path_len)) < 0)
1454 0 goto done;
1455  
1456 266 ▶ if (submodule)
Branch: 1 branch
BranchTrueFalseTotal
if (submodule) 0 266 266
1457 0 statbuf.st_mode = GIT_FILEMODE_COMMIT;
1458 }
1459  
1460 /* Ensure that the pathlist entry lines up with what we expected */
1461 693 ▶ else if (dir_expected)
Branch: 1 branch
BranchTrueFalseTotal
else if (dir_expected) 0 693 693
1462 0 continue;
1463  
1464 959 ▶ if ((error = filesystem_iterator_entry_init(&entry,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = filesystem_iterator_entry_init(&entry, 0 959 959
1465 iter, new_frame, path, path_len, &statbuf, pathlist_match)) < 0)
1466 0 goto done;
1467  
1468 959 git_vector_insert(&new_frame->entries, entry);
1469 }
1470  
1471 448 ▶ if (error == GIT_ITEROVER)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ITEROVER) 448 0 448
1472 448 error = 0;
1473  
1474 /* sort now that directory suffix is added */
1475 448 git_vector_sort(&new_frame->entries);
1476  
1477 450 done:
1478 450 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 2 448 450
1479 2 ▶ git_array_pop(iter->frames);
Branch: 1 branch
BranchTrueFalseTotal
git_array_pop(iter->frames); 2 0 2
1480  
1481 450 git_str_dispose(&root);
1482 450 git_fs_path_diriter_free(&diriter);
1483 450 return error;
1484 }
1485  
1486 448 GIT_INLINE(int) filesystem_iterator_frame_pop(filesystem_iterator *iter)
1487 {
1488 filesystem_iterator_frame *frame;
1489  
1490 448 ▶ GIT_ASSERT(iter->frames.size);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(iter->frames.size); 0 448 448
1491  
1492 448 ▶ frame = git_array_pop(iter->frames);
Branch: 1 branch
BranchTrueFalseTotal
frame = git_array_pop(iter->frames); 448 0 448
1493 448 filesystem_iterator_frame_pop_ignores(iter);
1494  
1495 448 git_pool_clear(&frame->entry_pool);
1496 448 git_vector_free(&frame->entries);
1497  
1498 448 return 0;
1499 }
1500  
1501 881 static void filesystem_iterator_set_current(
1502 filesystem_iterator *iter,
1503 filesystem_iterator_entry *entry)
1504 {
1505 /*
1506 * Index entries are limited to 32 bit timestamps. We can safely
1507 * cast this since workdir times are only used in the cache; any
1508 * mismatch will cause a hash recomputation which is unfortunate
1509 * but affects only people who set their filetimes to 2038.
1510 * (Same with the file size.)
1511 */
1512 881 iter->entry.ctime.seconds = (int32_t)entry->st.st_ctime;
1513 881 iter->entry.mtime.seconds = (int32_t)entry->st.st_mtime;
1514  
1515 #if defined(GIT_USE_NSEC)
1516 iter->entry.ctime.nanoseconds = entry->st.st_ctime_nsec;
1517 iter->entry.mtime.nanoseconds = entry->st.st_mtime_nsec;
1518 #else
1519 881 iter->entry.ctime.nanoseconds = 0;
1520 881 iter->entry.mtime.nanoseconds = 0;
1521 #endif
1522  
1523 881 iter->entry.dev = entry->st.st_dev;
1524 881 iter->entry.ino = entry->st.st_ino;
1525 881 iter->entry.mode = git_futils_canonical_mode(entry->st.st_mode);
1526 881 iter->entry.uid = entry->st.st_uid;
1527 881 iter->entry.gid = entry->st.st_gid;
1528 881 iter->entry.file_size = (uint32_t)entry->st.st_size;
1529  
1530 881 ▶ if (iter->base.flags & GIT_ITERATOR_INCLUDE_HASH)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->base.flags & GIT_ITERATOR_INCLUDE_HASH) 0 881 881
1531 0 git_oid_cpy(&iter->entry.id, &entry->id);
1532  
1533 881 iter->entry.path = entry->path;
1534  
1535 881 iter->current_is_ignored = GIT_IGNORE_UNCHECKED;
1536 881 }
1537  
1538 244 static int filesystem_iterator_current(
1539 const git_index_entry **out, git_iterator *i)
1540 {
1541 244 filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
1542  
1543 244 ▶ if (!iterator__has_been_accessed(i))
Branch: 1 branch
BranchTrueFalseTotal
if (!iterator__has_been_accessed(i)) 171 73 244
1544 171 return iter->base.cb->advance(out, i);
1545  
1546 73 ▶ if (!iter->frames.size) {
Branch: 1 branch
BranchTrueFalseTotal
if (!iter->frames.size) { 0 73 73
1547 0 *out = NULL;
1548 0 return GIT_ITEROVER;
1549 }
1550  
1551 73 *out = &iter->entry;
1552 73 return 0;
1553 }
1554  
1555 941 static int filesystem_iterator_is_dir(
1556 bool *is_dir,
1557 const filesystem_iterator *iter,
1558 const filesystem_iterator_entry *entry)
1559 {
1560 struct stat st;
1561 941 git_str fullpath = GIT_STR_INIT;
1562 941 int error = 0;
1563  
1564 941 ▶ if (S_ISDIR(entry->st.st_mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR(entry->st.st_mode)) { 266 675 941
1565 266 *is_dir = 1;
1566 266 goto done;
1567 }
1568  
1569 675 ▶ if (!iterator__descend_symlinks(iter) || !S_ISLNK(entry->st.st_mode)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!iterator__descend_symlinks(iter) || !S_ISLNK(entry->st.st_mode)) { 114 561 675
if (!iterator__descend_symlinks(iter) || !S_ISLNK(entry->st.st_mode)) { 114 0 114
1570 675 *is_dir = 0;
1571 675 goto done;
1572 }
1573  
1574 0 if ((error = git_str_joinpath(&fullpath, iter->root, entry->path)) < 0 ||
1575 0 ▶ (error = git_path_validate_str_length(iter->base.repo, &fullpath)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_path_validate_str_length(iter->base.repo, &fullpath)) < 0 || 0 0 0
1576 0 (error = p_stat(fullpath.ptr, &st)) < 0)
1577 0 goto done;
1578  
1579 0 *is_dir = S_ISDIR(st.st_mode);
1580  
1581 941 done:
1582 941 git_str_dispose(&fullpath);
1583 941 return error;
1584 }
1585  
1586 1087 static int filesystem_iterator_advance(
1587 const git_index_entry **out, git_iterator *i)
1588 {
1589 1087 filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
1590 bool is_dir;
1591 1087 int error = 0;
1592  
1593 1087 iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
1594  
1595 /* examine filesystem entries until we find the next one to return */
1596 502 while (true) {
1597 filesystem_iterator_frame *frame;
1598 filesystem_iterator_entry *entry;
1599  
1600 1589 ▶ if ((frame = filesystem_iterator_current_frame(iter)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((frame = filesystem_iterator_current_frame(iter)) == NULL) { 206 1383 1589
1601 206 error = GIT_ITEROVER;
1602 206 break;
1603 }
1604  
1605 /* no more entries in this frame. pop the frame out */
1606 1383 ▶ if (frame->next_idx == frame->entries.length) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->next_idx == frame->entries.length) { 442 941 1383
1607 442 filesystem_iterator_frame_pop(iter);
1608 442 continue;
1609 }
1610  
1611 /* we have more entries in the current frame, that's our next entry */
1612 941 entry = frame->entries.contents[frame->next_idx];
1613 941 frame->next_idx++;
1614  
1615 941 ▶ if ((error = filesystem_iterator_is_dir(&is_dir, iter, entry)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = filesystem_iterator_is_dir(&is_dir, iter, entry)) < 0) 0 941 941
1616 0 break;
1617  
1618 941 ▶ if (is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_dir) { 266 675 941
1619 266 ▶ if (iterator__do_autoexpand(iter)) {
Branch: 1 branch
BranchTrueFalseTotal
if (iterator__do_autoexpand(iter)) { 60 206 266
1620 60 error = filesystem_iterator_frame_push(iter, entry);
1621  
1622 /* may get GIT_ENOTFOUND due to races or permission problems
1623 * that we want to quietly swallow
1624 */
1625 60 ▶ if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) 0 60 60
1626 0 continue;
1627 60 ▶ else if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (error < 0) 0 60 60
1628 0 break;
1629 }
1630  
1631 266 ▶ if (!iterator__include_trees(iter))
Branch: 1 branch
BranchTrueFalseTotal
if (!iterator__include_trees(iter)) 60 206 266
1632 60 continue;
1633 }
1634  
1635 881 filesystem_iterator_set_current(iter, entry);
1636 881 break;
1637 }
1638  
1639 1087 ▶ if (out)
Branch: 1 branch
BranchTrueFalseTotal
if (out) 1087 0 1087
1640 1087 ▶ *out = (error == 0) ? &iter->entry : NULL;
Branch: 1 branch
BranchTrueFalseTotal
*out = (error == 0) ? &iter->entry : NULL; 881 206 1087
1641  
1642 1087 return error;
1643 }
1644  
1645 176 static int filesystem_iterator_advance_into(
1646 const git_index_entry **out, git_iterator *i)
1647 {
1648 176 filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
1649 filesystem_iterator_frame *frame;
1650 filesystem_iterator_entry *prev_entry;
1651 int error;
1652  
1653 176 ▶ if (out)
Branch: 1 branch
BranchTrueFalseTotal
if (out) 176 0 176
1654 176 *out = NULL;
1655  
1656 176 ▶ if ((frame = filesystem_iterator_current_frame(iter)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((frame = filesystem_iterator_current_frame(iter)) == NULL) 0 176 176
1657 0 return GIT_ITEROVER;
1658  
1659 /* get the last seen entry */
1660 176 prev_entry = filesystem_iterator_current_entry(frame);
1661  
1662 /* it's legal to call advance_into when auto-expand is on. in this case,
1663 * we will have pushed a new (empty) frame on to the stack for this
1664 * new directory. since it's empty, its current_entry should be null.
1665 */
1666 176 ▶ GIT_ASSERT(iterator__do_autoexpand(i) ^ (prev_entry != NULL));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(iterator__do_autoexpand(i) ^ (prev_entry != NULL)); 0 176 176
1667  
1668 176 ▶ if (prev_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev_entry) { 176 0 176
1669 176 ▶ if (prev_entry->st.st_mode != GIT_FILEMODE_COMMIT &&
Branch: 1 branch
BranchTrueFalseTotal
if (prev_entry->st.st_mode != GIT_FILEMODE_COMMIT && 176 0 176
1670 176 ▶ !S_ISDIR(prev_entry->st.st_mode))
Branch: 1 branch
BranchTrueFalseTotal
!S_ISDIR(prev_entry->st.st_mode)) 0 176 176
1671 0 return 0;
1672  
1673 176 ▶ if ((error = filesystem_iterator_frame_push(iter, prev_entry)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = filesystem_iterator_frame_push(iter, prev_entry)) < 0) 0 176 176
1674 0 return error;
1675 }
1676  
1677 /* we've advanced into the directory in question, let advance
1678 * find the first entry
1679 */
1680 176 return filesystem_iterator_advance(out, i);
1681 }
1682  
1683 6 int git_iterator_current_workdir_path(git_str **out, git_iterator *i)
1684 {
1685 6 filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
1686 const git_index_entry *entry;
1687  
1688 6 ▶ if (i->type != GIT_ITERATOR_FS &&
Branch: 1 branch
BranchTrueFalseTotal
if (i->type != GIT_ITERATOR_FS && 6 0 6
1689 6 ▶ i->type != GIT_ITERATOR_WORKDIR) {
Branch: 1 branch
BranchTrueFalseTotal
i->type != GIT_ITERATOR_WORKDIR) { 0 6 6
1690 0 *out = NULL;
1691 0 return 0;
1692 }
1693  
1694 6 git_str_truncate(&iter->current_path, iter->root_len);
1695  
1696 12 if (git_iterator_current(&entry, i) < 0 ||
1697 6 git_str_puts(&iter->current_path, entry->path) < 0)
1698 0 return -1;
1699  
1700 6 *out = &iter->current_path;
1701 6 return 0;
1702 }
1703  
1704 405 GIT_INLINE(git_dir_flag) entry_dir_flag(git_index_entry *entry)
1705 {
1706 #if defined(GIT_WIN32) && !defined(__MINGW32__)
1707 return (entry && entry->mode) ?
1708 (S_ISDIR(entry->mode) ? GIT_DIR_FLAG_TRUE : GIT_DIR_FLAG_FALSE) :
1709 GIT_DIR_FLAG_UNKNOWN;
1710 #else
1711 405 GIT_UNUSED(entry);
1712 405 return GIT_DIR_FLAG_UNKNOWN;
1713 #endif
1714 }
1715  
1716 405 static void filesystem_iterator_update_ignored(filesystem_iterator *iter)
1717 {
1718 filesystem_iterator_frame *frame;
1719 405 git_dir_flag dir_flag = entry_dir_flag(&iter->entry);
1720  
1721 405 ▶ if (git_ignore__lookup(&iter->current_is_ignored,
Branch: 1 branch
BranchTrueFalseTotal
if (git_ignore__lookup(&iter->current_is_ignored, 0 405 405
1722 &iter->ignores, iter->entry.path, dir_flag) < 0) {
1723 0 git_error_clear();
1724 0 iter->current_is_ignored = GIT_IGNORE_NOTFOUND;
1725 }
1726  
1727 /* use ignore from containing frame stack */
1728 405 ▶ if (iter->current_is_ignored <= GIT_IGNORE_NOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->current_is_ignored <= GIT_IGNORE_NOTFOUND) { 395 10 405
1729 395 frame = filesystem_iterator_current_frame(iter);
1730 395 iter->current_is_ignored = frame->is_ignored;
1731 }
1732 405 }
1733  
1734 416 GIT_INLINE(bool) filesystem_iterator_current_is_ignored(
1735 filesystem_iterator *iter)
1736 {
1737 416 ▶ if (iter->current_is_ignored == GIT_IGNORE_UNCHECKED)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->current_is_ignored == GIT_IGNORE_UNCHECKED) 405 11 416
1738 405 filesystem_iterator_update_ignored(iter);
1739  
1740 416 return (iter->current_is_ignored == GIT_IGNORE_TRUE);
1741 }
1742  
1743 381 bool git_iterator_current_is_ignored(git_iterator *i)
1744 {
1745 381 filesystem_iterator *iter = NULL;
1746  
1747 381 ▶ if (i->type != GIT_ITERATOR_WORKDIR)
Branch: 1 branch
BranchTrueFalseTotal
if (i->type != GIT_ITERATOR_WORKDIR) 49 332 381
1748 49 return false;
1749  
1750 332 iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
1751  
1752 332 return filesystem_iterator_current_is_ignored(iter);
1753 }
1754  
1755 7 bool git_iterator_current_tree_is_ignored(git_iterator *i)
1756 {
1757 7 filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
1758 filesystem_iterator_frame *frame;
1759  
1760 7 ▶ if (i->type != GIT_ITERATOR_WORKDIR)
Branch: 1 branch
BranchTrueFalseTotal
if (i->type != GIT_ITERATOR_WORKDIR) 0 7 7
1761 0 return false;
1762  
1763 7 frame = filesystem_iterator_current_frame(iter);
1764 7 return (frame->is_ignored == GIT_IGNORE_TRUE);
1765 }
1766  
1767 67 static int filesystem_iterator_advance_over(
1768 const git_index_entry **out,
1769 git_iterator_status_t *status,
1770 git_iterator *i)
1771 {
1772 67 filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
1773 filesystem_iterator_frame *current_frame;
1774 filesystem_iterator_entry *current_entry;
1775 67 const git_index_entry *entry = NULL;
1776 const char *base;
1777 67 int error = 0;
1778  
1779 67 *out = NULL;
1780 67 *status = GIT_ITERATOR_STATUS_NORMAL;
1781  
1782 67 ▶ GIT_ASSERT(iterator__has_been_accessed(i));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(iterator__has_been_accessed(i)); 0 67 67
1783  
1784 67 current_frame = filesystem_iterator_current_frame(iter);
1785 67 ▶ GIT_ASSERT(current_frame);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(current_frame); 0 67 67
1786  
1787 67 current_entry = filesystem_iterator_current_entry(current_frame);
1788 67 ▶ GIT_ASSERT(current_entry);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(current_entry); 0 67 67
1789  
1790 67 ▶ if ((error = git_iterator_current(&entry, i)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_iterator_current(&entry, i)) < 0) 0 67 67
1791 0 return error;
1792  
1793 67 ▶ if (!S_ISDIR(entry->mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!S_ISDIR(entry->mode)) { 49 18 67
1794 49 ▶ if (filesystem_iterator_current_is_ignored(iter))
Branch: 1 branch
BranchTrueFalseTotal
if (filesystem_iterator_current_is_ignored(iter)) 1 48 49
1795 1 *status = GIT_ITERATOR_STATUS_IGNORED;
1796  
1797 49 return filesystem_iterator_advance(out, i);
1798 }
1799  
1800 18 git_str_clear(&iter->tmp_buf);
1801 18 ▶ if ((error = git_str_puts(&iter->tmp_buf, entry->path)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_puts(&iter->tmp_buf, entry->path)) < 0) 0 18 18
1802 0 return error;
1803  
1804 18 base = iter->tmp_buf.ptr;
1805  
1806 /* scan inside the directory looking for files. if we find nothing,
1807 * we will remain EMPTY. if we find any ignored item, upgrade EMPTY to
1808 * IGNORED. if we find a real actual item, upgrade all the way to NORMAL
1809 * and then stop.
1810 *
1811 * however, if we're here looking for a pathlist item (but are not
1812 * actually in the pathlist ourselves) then start at FILTERED instead of
1813 * EMPTY. callers then know that this path was not something they asked
1814 * about.
1815 */
1816 36 *status = current_entry->match == ITERATOR_PATHLIST_IS_PARENT ?
1817 18 ▶ GIT_ITERATOR_STATUS_FILTERED : GIT_ITERATOR_STATUS_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
GIT_ITERATOR_STATUS_FILTERED : GIT_ITERATOR_STATUS_EMPTY; 0 18 18
1818  
1819 36 ▶ while (entry && !iter->base.prefixcomp(entry->path, base)) {
Branch: 2 branches
BranchTrueFalseTotal
while (entry && !iter->base.prefixcomp(entry->path, base)) { 36 0 36
while (entry && !iter->base.prefixcomp(entry->path, base)) { 35 1 36
1820 35 ▶ if (filesystem_iterator_current_is_ignored(iter)) {
Branch: 1 branch
BranchTrueFalseTotal
if (filesystem_iterator_current_is_ignored(iter)) { 0 35 35
1821 /* if we found an explicitly ignored item, then update from
1822 * EMPTY to IGNORED
1823 */
1824 0 *status = GIT_ITERATOR_STATUS_IGNORED;
1825 35 ▶ } else if (S_ISDIR(entry->mode)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (S_ISDIR(entry->mode)) { 18 17 35
1826 18 error = filesystem_iterator_advance_into(&entry, i);
1827  
1828 18 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 18 0 18
1829 18 continue;
1830  
1831 /* this directory disappeared, ignore it */
1832 0 ▶ else if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
else if (error == GIT_ENOTFOUND) 0 0 0
1833 0 error = 0;
1834  
1835 /* a real error occurred */
1836 else
1837 0 break;
1838 } else {
1839 /* we found a non-ignored item, treat parent as untracked */
1840 17 *status = GIT_ITERATOR_STATUS_NORMAL;
1841 17 break;
1842 }
1843  
1844 0 ▶ if ((error = git_iterator_advance(&entry, i)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_iterator_advance(&entry, i)) < 0) 0 0 0
1845 0 break;
1846 }
1847  
1848 /* wrap up scan back to base directory */
1849 34 ▶ while (entry && !iter->base.prefixcomp(entry->path, base)) {
Branch: 2 branches
BranchTrueFalseTotal
while (entry && !iter->base.prefixcomp(entry->path, base)) { 34 0 34
while (entry && !iter->base.prefixcomp(entry->path, base)) { 17 17 34
1850 17 ▶ if ((error = git_iterator_advance(&entry, i)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_iterator_advance(&entry, i)) < 0) 1 16 17
1851 1 break;
1852 }
1853  
1854 18 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 17 1 18
1855 17 *out = entry;
1856  
1857 18 return error;
1858 }
1859  
1860 214 static void filesystem_iterator_clear(filesystem_iterator *iter)
1861 {
1862 220 ▶ while (iter->frames.size)
Branch: 1 branch
BranchTrueFalseTotal
while (iter->frames.size) 6 214 220
1863 6 filesystem_iterator_frame_pop(iter);
1864  
1865 214 git_array_clear(iter->frames);
1866 214 git_ignore__free(&iter->ignores);
1867  
1868 214 git_str_dispose(&iter->tmp_buf);
1869  
1870 214 iterator_clear(&iter->base);
1871 214 }
1872  
1873 214 static int filesystem_iterator_init(filesystem_iterator *iter)
1874 {
1875 int error;
1876  
1877 397 if (iterator__honor_ignores(&iter->base) &&
1878 183 (error = git_ignore__for_path(iter->base.repo,
1879 ".gitignore", &iter->ignores)) < 0)
1880 0 return error;
1881  
1882 214 ▶ if ((error = filesystem_iterator_frame_push(iter, NULL)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = filesystem_iterator_frame_push(iter, NULL)) < 0) 2 212 214
1883 2 return error;
1884  
1885 212 iter->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
1886  
1887 212 return 0;
1888 }
1889  
1890 6 static int filesystem_iterator_reset(git_iterator *i)
1891 {
1892 6 filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
1893  
1894 6 filesystem_iterator_clear(iter);
1895 6 return filesystem_iterator_init(iter);
1896 }
1897  
1898 208 static void filesystem_iterator_free(git_iterator *i)
1899 {
1900 208 filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
1901 208 git__free(iter->root);
1902 208 git_str_dispose(&iter->current_path);
1903 208 git_tree_free(iter->tree);
1904 208 ▶ if (iter->index)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->index) 171 37 208
1905 171 git_index_snapshot_release(&iter->index_snapshot, iter->index);
1906 208 filesystem_iterator_clear(iter);
1907 208 }
1908  
1909 208 static int iterator_for_filesystem(
1910 git_iterator **out,
1911 git_repository *repo,
1912 const char *root,
1913 git_index *index,
1914 git_tree *tree,
1915 git_iterator_t type,
1916 git_iterator_options *options)
1917 {
1918 filesystem_iterator *iter;
1919 size_t root_len;
1920 int error;
1921  
1922 static git_iterator_callbacks callbacks = {
1923 filesystem_iterator_current,
1924 filesystem_iterator_advance,
1925 filesystem_iterator_advance_into,
1926 filesystem_iterator_advance_over,
1927 filesystem_iterator_reset,
1928 filesystem_iterator_free
1929 };
1930  
1931 208 *out = NULL;
1932  
1933 208 ▶ if (root == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (root == NULL) 0 208 208
1934 0 return git_iterator_for_nothing(out, options);
1935  
1936 208 iter = git__calloc(1, sizeof(filesystem_iterator));
1937 208 ▶ GIT_ERROR_CHECK_ALLOC(iter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iter); 0 208 208
1938  
1939 208 iter->base.type = type;
1940 208 iter->base.cb = &callbacks;
1941  
1942 208 root_len = strlen(root);
1943  
1944 208 iter->root = git__malloc(root_len+2);
1945 208 ▶ GIT_ERROR_CHECK_ALLOC(iter->root);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iter->root); 0 208 208
1946  
1947 208 memcpy(iter->root, root, root_len);
1948  
1949 208 ▶ if (root_len == 0 || root[root_len-1] != '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (root_len == 0 || root[root_len-1] != '/') { 208 0 208
if (root_len == 0 || root[root_len-1] != '/') { 0 208 208
1950 0 iter->root[root_len] = '/';
1951 0 root_len++;
1952 }
1953 208 iter->root[root_len] = '\0';
1954 208 iter->root_len = root_len;
1955  
1956 208 ▶ if ((error = git_str_puts(&iter->current_path, iter->root)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_puts(&iter->current_path, iter->root)) < 0) 0 208 208
1957 0 goto on_error;
1958  
1959 208 ▶ if ((error = iterator_init_common(&iter->base, repo, index, options)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = iterator_init_common(&iter->base, repo, index, options)) < 0) 0 208 208
1960 0 goto on_error;
1961  
1962 208 ▶ if (tree && (error = git_tree_dup(&iter->tree, tree)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (tree && (error = git_tree_dup(&iter->tree, tree)) < 0) 2 206 208
if (tree && (error = git_tree_dup(&iter->tree, tree)) < 0) 0 2 2
1963 0 goto on_error;
1964  
1965 379 if (index &&
1966 171 (error = git_index_snapshot_new(&iter->index_snapshot, index)) < 0)
1967 0 goto on_error;
1968  
1969 208 iter->index = index;
1970 208 iter->dirload_flags =
1971 208 (iterator__ignore_case(&iter->base) ?
1972 208 GIT_FS_PATH_DIR_IGNORE_CASE : 0) |
1973 208 (iterator__flag(&iter->base, PRECOMPOSE_UNICODE) ?
1974 208 GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE : 0);
1975  
1976 208 ▶ if ((error = filesystem_iterator_init(iter)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = filesystem_iterator_init(iter)) < 0) 2 206 208
1977 2 goto on_error;
1978  
1979 206 *out = &iter->base;
1980 206 return 0;
1981  
1982 2 on_error:
1983 2 git_iterator_free(&iter->base);
1984 2 return error;
1985 }
1986  
1987 31 int git_iterator_for_filesystem(
1988 git_iterator **out,
1989 const char *root,
1990 git_iterator_options *options)
1991 {
1992 31 return iterator_for_filesystem(out,
1993 NULL, root, NULL, NULL, GIT_ITERATOR_FS, options);
1994 }
1995  
1996 177 int git_iterator_for_workdir_ext(
1997 git_iterator **out,
1998 git_repository *repo,
1999 const char *repo_workdir,
2000 git_index *index,
2001 git_tree *tree,
2002 git_iterator_options *given_opts)
2003 {
2004 177 git_iterator_options options = GIT_ITERATOR_OPTIONS_INIT;
2005  
2006 177 ▶ if (!repo_workdir) {
Branch: 1 branch
BranchTrueFalseTotal
if (!repo_workdir) { 124 53 177
2007 124 ▶ if (git_repository__ensure_not_bare(repo, "scan working directory") < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository__ensure_not_bare(repo, "scan working directory") < 0) 0 124 124
2008 0 return GIT_EBAREREPO;
2009  
2010 124 repo_workdir = git_repository_workdir(repo);
2011 }
2012  
2013 /* upgrade to a workdir iterator, adding necessary internal flags */
2014 177 ▶ if (given_opts)
Branch: 1 branch
BranchTrueFalseTotal
if (given_opts) 177 0 177
2015 177 memcpy(&options, given_opts, sizeof(git_iterator_options));
2016  
2017 177 options.flags |= GIT_ITERATOR_HONOR_IGNORES |
2018 GIT_ITERATOR_IGNORE_DOT_GIT;
2019  
2020 177 return iterator_for_filesystem(out,
2021 repo, repo_workdir, index, tree, GIT_ITERATOR_WORKDIR, &options);
2022 }
2023  
2024  
2025 /* Index iterator */
2026  
2027  
2028 typedef struct {
2029 git_iterator base;
2030 git_vector entries;
2031 size_t next_idx;
2032  
2033 /* the pseudotree entry */
2034 git_index_entry tree_entry;
2035 git_str tree_buf;
2036 bool skip_tree;
2037  
2038 const git_index_entry *entry;
2039 } index_iterator;
2040  
2041 259 static int index_iterator_current(
2042 const git_index_entry **out, git_iterator *i)
2043 {
2044 259 index_iterator *iter = (index_iterator *)i;
2045  
2046 259 ▶ if (!iterator__has_been_accessed(i))
Branch: 1 branch
BranchTrueFalseTotal
if (!iterator__has_been_accessed(i)) 259 0 259
2047 259 return iter->base.cb->advance(out, i);
2048  
2049 0 ▶ if (iter->entry == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->entry == NULL) { 0 0 0
2050 0 *out = NULL;
2051 0 return GIT_ITEROVER;
2052 }
2053  
2054 0 *out = iter->entry;
2055 0 return 0;
2056 }
2057  
2058 0 static bool index_iterator_create_pseudotree(
2059 const git_index_entry **out,
2060 index_iterator *iter,
2061 const char *path)
2062 {
2063 const char *prev_path, *relative_path, *dirsep;
2064 size_t common_len;
2065  
2066 0 ▶ prev_path = iter->entry ? iter->entry->path : "";
Branch: 1 branch
BranchTrueFalseTotal
prev_path = iter->entry ? iter->entry->path : ""; 0 0 0
2067  
2068 /* determine if the new path is in a different directory from the old */
2069 0 common_len = git_fs_path_common_dirlen(prev_path, path);
2070 0 relative_path = path + common_len;
2071  
2072 0 ▶ if ((dirsep = strchr(relative_path, '/')) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((dirsep = strchr(relative_path, '/')) == NULL) 0 0 0
2073 0 return false;
2074  
2075 0 git_str_clear(&iter->tree_buf);
2076 0 git_str_put(&iter->tree_buf, path, (dirsep - path) + 1);
2077  
2078 0 iter->tree_entry.mode = GIT_FILEMODE_TREE;
2079 0 iter->tree_entry.path = iter->tree_buf.ptr;
2080  
2081 0 *out = &iter->tree_entry;
2082 0 return true;
2083 }
2084  
2085 0 static int index_iterator_skip_pseudotree(index_iterator *iter)
2086 {
2087 0 ▶ GIT_ASSERT(iterator__has_been_accessed(&iter->base));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(iterator__has_been_accessed(&iter->base)); 0 0 0
2088 0 ▶ GIT_ASSERT(S_ISDIR(iter->entry->mode));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(S_ISDIR(iter->entry->mode)); 0 0 0
2089  
2090 0 while (true) {
2091 0 const git_index_entry *next_entry = NULL;
2092  
2093 0 ▶ if (++iter->next_idx >= iter->entries.length)
Branch: 1 branch
BranchTrueFalseTotal
if (++iter->next_idx >= iter->entries.length) 0 0 0
2094 0 return GIT_ITEROVER;
2095  
2096 0 next_entry = iter->entries.contents[iter->next_idx];
2097  
2098 0 ▶ if (iter->base.strncomp(iter->tree_buf.ptr, next_entry->path,
Branch: 1 branch
BranchTrueFalseTotal
if (iter->base.strncomp(iter->tree_buf.ptr, next_entry->path, 0 0 0
2099 iter->tree_buf.size) != 0)
2100 0 break;
2101 }
2102  
2103 0 iter->skip_tree = false;
2104 0 return 0;
2105 }
2106  
2107 760 static int index_iterator_advance(
2108 const git_index_entry **out, git_iterator *i)
2109 {
2110 760 index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base);
2111 760 const git_index_entry *entry = NULL;
2112 bool is_submodule;
2113 760 int error = 0;
2114  
2115 760 iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
2116  
2117 while (true) {
2118 847 ▶ if (iter->next_idx >= iter->entries.length) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->next_idx >= iter->entries.length) { 260 587 847
2119 260 error = GIT_ITEROVER;
2120 260 break;
2121 }
2122  
2123 /* we were not asked to expand this pseudotree. advance over it. */
2124 587 ▶ if (iter->skip_tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->skip_tree) { 0 587 587
2125 0 index_iterator_skip_pseudotree(iter);
2126 0 continue;
2127 }
2128  
2129 587 entry = iter->entries.contents[iter->next_idx];
2130 587 is_submodule = S_ISGITLINK(entry->mode);
2131  
2132 587 ▶ if (!iterator_has_started(&iter->base, entry->path, is_submodule)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!iterator_has_started(&iter->base, entry->path, is_submodule)) { 0 587 587
2133 0 iter->next_idx++;
2134 0 continue;
2135 }
2136  
2137 587 ▶ if (iterator_has_ended(&iter->base, entry->path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (iterator_has_ended(&iter->base, entry->path)) { 0 587 587
2138 0 error = GIT_ITEROVER;
2139 0 break;
2140 }
2141  
2142 /* if we have a list of paths we're interested in, examine it */
2143 587 ▶ if (!iterator_pathlist_next_is(&iter->base, entry->path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!iterator_pathlist_next_is(&iter->base, entry->path)) { 65 522 587
2144 65 iter->next_idx++;
2145 65 continue;
2146 }
2147  
2148 /* if this is a conflict, skip it unless we're including conflicts */
2149 522 ▶ if (git_index_entry_is_conflict(entry) &&
Branch: 1 branch
BranchTrueFalseTotal
if (git_index_entry_is_conflict(entry) && 22 500 522
2150 22 ▶ !iterator__include_conflicts(&iter->base)) {
Branch: 1 branch
BranchTrueFalseTotal
!iterator__include_conflicts(&iter->base)) { 22 0 22
2151 22 iter->next_idx++;
2152 22 continue;
2153 }
2154  
2155 /* we've found what will be our next _file_ entry. but if we are
2156 * returning trees entries, we may need to return a pseudotree
2157 * entry that will contain this. don't advance over this entry,
2158 * though, we still need to return it on the next `advance`.
2159 */
2160 500 if (iterator__include_trees(&iter->base) &&
2161 0 index_iterator_create_pseudotree(&entry, iter, entry->path)) {
2162  
2163 /* Note whether this pseudo tree should be expanded or not */
2164 0 iter->skip_tree = iterator__dont_autoexpand(&iter->base);
2165 0 break;
2166 }
2167  
2168 500 iter->next_idx++;
2169 500 break;
2170 }
2171  
2172 760 ▶ iter->entry = (error == 0) ? entry : NULL;
Branch: 1 branch
BranchTrueFalseTotal
iter->entry = (error == 0) ? entry : NULL; 500 260 760
2173  
2174 760 ▶ if (out)
Branch: 1 branch
BranchTrueFalseTotal
if (out) 760 0 760
2175 760 *out = iter->entry;
2176  
2177 760 return error;
2178 }
2179  
2180 0 static int index_iterator_advance_into(
2181 const git_index_entry **out, git_iterator *i)
2182 {
2183 0 index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base);
2184  
2185 0 ▶ if (! S_ISDIR(iter->tree_entry.mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (! S_ISDIR(iter->tree_entry.mode)) { 0 0 0
2186 0 ▶ if (out)
Branch: 1 branch
BranchTrueFalseTotal
if (out) 0 0 0
2187 0 *out = NULL;
2188  
2189 0 return 0;
2190 }
2191  
2192 0 iter->skip_tree = false;
2193 0 return index_iterator_advance(out, i);
2194 }
2195  
2196 0 static int index_iterator_advance_over(
2197 const git_index_entry **out,
2198 git_iterator_status_t *status,
2199 git_iterator *i)
2200 {
2201 0 index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base);
2202 const git_index_entry *entry;
2203 int error;
2204  
2205 0 ▶ if ((error = index_iterator_current(&entry, i)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = index_iterator_current(&entry, i)) < 0) 0 0 0
2206 0 return error;
2207  
2208 0 ▶ if (S_ISDIR(entry->mode))
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR(entry->mode)) 0 0 0
2209 0 index_iterator_skip_pseudotree(iter);
2210  
2211 0 *status = GIT_ITERATOR_STATUS_NORMAL;
2212 0 return index_iterator_advance(out, i);
2213 }
2214  
2215 18 static void index_iterator_clear(index_iterator *iter)
2216 {
2217 18 iterator_clear(&iter->base);
2218 18 }
2219  
2220 278 static int index_iterator_init(index_iterator *iter)
2221 {
2222 278 iter->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
2223 278 iter->next_idx = 0;
2224 278 iter->skip_tree = false;
2225 278 return 0;
2226 }
2227  
2228 18 static int index_iterator_reset(git_iterator *i)
2229 {
2230 18 index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base);
2231  
2232 18 index_iterator_clear(iter);
2233 18 return index_iterator_init(iter);
2234 }
2235  
2236 260 static void index_iterator_free(git_iterator *i)
2237 {
2238 260 index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base);
2239  
2240 260 git_index_snapshot_release(&iter->entries, iter->base.index);
2241 260 git_str_dispose(&iter->tree_buf);
2242 260 }
2243  
2244 260 int git_iterator_for_index(
2245 git_iterator **out,
2246 git_repository *repo,
2247 git_index *index,
2248 git_iterator_options *options)
2249 {
2250 index_iterator *iter;
2251 int error;
2252  
2253 static git_iterator_callbacks callbacks = {
2254 index_iterator_current,
2255 index_iterator_advance,
2256 index_iterator_advance_into,
2257 index_iterator_advance_over,
2258 index_iterator_reset,
2259 index_iterator_free
2260 };
2261  
2262 260 *out = NULL;
2263  
2264 260 ▶ if (index == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (index == NULL) 0 260 260
2265 0 return git_iterator_for_nothing(out, options);
2266  
2267 260 iter = git__calloc(1, sizeof(index_iterator));
2268 260 ▶ GIT_ERROR_CHECK_ALLOC(iter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iter); 0 260 260
2269  
2270 260 iter->base.type = GIT_ITERATOR_INDEX;
2271 260 iter->base.cb = &callbacks;
2272  
2273 520 if ((error = iterator_init_common(&iter->base, repo, index, options)) < 0 ||
2274 520 ▶ (error = git_index_snapshot_new(&iter->entries, index)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_index_snapshot_new(&iter->entries, index)) < 0 || 0 260 260
2275 260 (error = index_iterator_init(iter)) < 0)
2276 0 goto on_error;
2277  
2278 260 ▶ git_vector_set_cmp(&iter->entries, iterator__ignore_case(&iter->base) ?
Branch: 1 branch
BranchTrueFalseTotal
git_vector_set_cmp(&iter->entries, iterator__ignore_case(&iter->base) ? 0 260 260
2279 git_index_entry_icmp : git_index_entry_cmp);
2280 260 git_vector_sort(&iter->entries);
2281  
2282 260 *out = &iter->base;
2283 260 return 0;
2284  
2285 0 on_error:
2286 0 git_iterator_free(&iter->base);
2287 0 return error;
2288 }
2289  
2290  
2291 /* Iterator API */
2292  
2293 64 int git_iterator_reset_range(
2294 git_iterator *i, const char *start, const char *end)
2295 {
2296 64 ▶ if (iterator_reset_range(i, start, end) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (iterator_reset_range(i, start, end) < 0) 0 64 64
2297 0 return -1;
2298  
2299 64 return i->cb->reset(i);
2300 }
2301  
2302 0 int git_iterator_set_ignore_case(git_iterator *i, bool ignore_case)
2303 {
2304 0 ▶ GIT_ASSERT(!iterator__has_been_accessed(i));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(!iterator__has_been_accessed(i)); 0 0 0
2305 0 iterator_set_ignore_case(i, ignore_case);
2306 0 return 0;
2307 }
2308  
2309 863 void git_iterator_free(git_iterator *iter)
2310 {
2311 863 ▶ if (iter == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (iter == NULL) 94 769 863
2312 94 return;
2313  
2314 769 iter->cb->free(iter);
2315  
2316 769 git_vector_free(&iter->pathlist);
2317 769 git__free(iter->start);
2318 769 git__free(iter->end);
2319  
2320 769 memset(iter, 0, sizeof(*iter));
2321  
2322 769 git__free(iter);
2323 }
2324  
2325 0 int git_iterator_foreach(
2326 git_iterator *iterator,
2327 git_iterator_foreach_cb cb,
2328 void *data)
2329 {
2330 const git_index_entry *iterator_item;
2331 0 int error = 0;
2332  
2333 0 ▶ if ((error = git_iterator_current(&iterator_item, iterator)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_iterator_current(&iterator_item, iterator)) < 0) 0 0 0
2334 0 goto done;
2335  
2336 0 ▶ if ((error = cb(iterator_item, data)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = cb(iterator_item, data)) != 0) 0 0 0
2337 0 goto done;
2338  
2339 while (true) {
2340 0 ▶ if ((error = git_iterator_advance(&iterator_item, iterator)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_iterator_advance(&iterator_item, iterator)) < 0) 0 0 0
2341 0 goto done;
2342  
2343 0 ▶ if ((error = cb(iterator_item, data)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = cb(iterator_item, data)) != 0) 0 0 0
2344 0 goto done;
2345 }
2346  
2347 0 done:
2348 0 ▶ if (error == GIT_ITEROVER)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ITEROVER) 0 0 0
2349 0 error = 0;
2350  
2351 0 return error;
2352 }
2353  
2354 22 int git_iterator_walk(
2355 git_iterator **iterators,
2356 size_t cnt,
2357 git_iterator_walk_cb cb,
2358 void *data)
2359 {
2360 const git_index_entry **iterator_item; /* next in each iterator */
2361 const git_index_entry **cur_items; /* current path in each iter */
2362 const git_index_entry *first_match;
2363 size_t i, j;
2364 22 int error = 0;
2365  
2366 22 iterator_item = git__calloc(cnt, sizeof(git_index_entry *));
2367 22 cur_items = git__calloc(cnt, sizeof(git_index_entry *));
2368  
2369 22 ▶ GIT_ERROR_CHECK_ALLOC(iterator_item);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iterator_item); 0 22 22
2370 22 ▶ GIT_ERROR_CHECK_ALLOC(cur_items);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(cur_items); 0 22 22
2371  
2372 /* Set up the iterators */
2373 88 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 66 22 88
2374 66 error = git_iterator_current(&iterator_item[i], iterators[i]);
2375  
2376 66 ▶ if (error < 0 && error != GIT_ITEROVER)
Branch: 2 branches
BranchTrueFalseTotal
if (error < 0 && error != GIT_ITEROVER) 0 66 66
if (error < 0 && error != GIT_ITEROVER) 0 0 0
2377 0 goto done;
2378 }
2379  
2380 while (true) {
2381 280 ▶ for (i = 0; i < cnt; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) 210 70 280
2382 210 cur_items[i] = NULL;
2383  
2384 70 first_match = NULL;
2385  
2386 /* Find the next path(s) to consume from each iterator */
2387 280 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 210 70 280
2388 210 ▶ if (iterator_item[i] == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (iterator_item[i] == NULL) 70 140 210
2389 70 continue;
2390  
2391 140 ▶ if (first_match == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (first_match == NULL) { 48 92 140
2392 48 first_match = iterator_item[i];
2393 48 cur_items[i] = iterator_item[i];
2394 } else {
2395 92 int path_diff = git_index_entry_cmp(iterator_item[i], first_match);
2396  
2397 92 ▶ if (path_diff < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (path_diff < 0) { 1 91 92
2398 /* Found an index entry that sorts before the one we're
2399 * looking at. Forget that we've seen the other and
2400 * look at the other iterators for this path.
2401 */
2402 3 ▶ for (j = 0; j < i; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < i; j++) 2 1 3
2403 2 cur_items[j] = NULL;
2404  
2405 1 first_match = iterator_item[i];
2406 1 cur_items[i] = iterator_item[i];
2407 91 ▶ } else if (path_diff == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (path_diff == 0) { 90 1 91
2408 90 cur_items[i] = iterator_item[i];
2409 }
2410 }
2411 }
2412  
2413 70 ▶ if (first_match == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (first_match == NULL) 22 48 70
2414 22 break;
2415  
2416 48 ▶ if ((error = cb(cur_items, data)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = cb(cur_items, data)) != 0) 0 48 48
2417 0 goto done;
2418  
2419 /* Advance each iterator that participated */
2420 192 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 144 48 192
2421 144 ▶ if (cur_items[i] == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (cur_items[i] == NULL) 7 137 144
2422 7 continue;
2423  
2424 137 error = git_iterator_advance(&iterator_item[i], iterators[i]);
2425  
2426 137 ▶ if (error < 0 && error != GIT_ITEROVER)
Branch: 2 branches
BranchTrueFalseTotal
if (error < 0 && error != GIT_ITEROVER) 66 71 137
if (error < 0 && error != GIT_ITEROVER) 0 66 66
2427 0 goto done;
2428 }
2429 }
2430  
2431 22 done:
2432 22 git__free((git_index_entry **)iterator_item);
2433 22 git__free((git_index_entry **)cur_items);
2434  
2435 22 ▶ if (error == GIT_ITEROVER)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ITEROVER) 22 0 22
2436 22 error = 0;
2437  
2438 22 return error;
2439 }
« deps/libgit2/src/libgit2/reflog.c ↑ Summary const-c-error.inc »