iterator.h 9.22 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8 9 10 11
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
#ifndef INCLUDE_iterator_h__
#define INCLUDE_iterator_h__

#include "common.h"
#include "git2/index.h"
12
#include "vector.h"
13
#include "buffer.h"
14
#include "ignore.h"
15

16 17 18
typedef struct git_iterator git_iterator;

typedef enum {
19 20 21 22
	GIT_ITERATOR_TYPE_EMPTY = 0,
	GIT_ITERATOR_TYPE_TREE = 1,
	GIT_ITERATOR_TYPE_INDEX = 2,
	GIT_ITERATOR_TYPE_WORKDIR = 3,
23
	GIT_ITERATOR_TYPE_FS = 4,
24 25
} git_iterator_type_t;

26
typedef enum {
27
	/** ignore case for entry sort order */
28
	GIT_ITERATOR_IGNORE_CASE = (1u << 0),
29
	/** force case sensitivity for entry sort order */
30
	GIT_ITERATOR_DONT_IGNORE_CASE = (1u << 1),
31
	/** return tree items in addition to blob items */
32
	GIT_ITERATOR_INCLUDE_TREES    = (1u << 2),
33
	/** don't flatten trees, requiring advance_into (implies INCLUDE_TREES) */
34 35 36
	GIT_ITERATOR_DONT_AUTOEXPAND  = (1u << 3),
	/** convert precomposed unicode to decomposed unicode */
	GIT_ITERATOR_PRECOMPOSE_UNICODE = (1u << 4),
37 38
	/** include conflicts */
	GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 5),
39 40
} git_iterator_flag_t;

41
typedef struct {
42 43
	int (*current)(const git_index_entry **, git_iterator *);
	int (*advance)(const git_index_entry **, git_iterator *);
44
	int (*advance_into)(const git_index_entry **, git_iterator *);
45
	int (*seek)(git_iterator *, const char *prefix);
46
	int (*reset)(git_iterator *, const char *start, const char *end);
47
	int (*at_end)(git_iterator *);
48
	void (*free)(git_iterator *);
49 50 51 52 53 54 55 56
} git_iterator_callbacks;

struct git_iterator {
	git_iterator_type_t type;
	git_iterator_callbacks *cb;
	git_repository *repo;
	char *start;
	char *end;
57
	int (*prefixcomp)(const char *str, const char *prefix);
58
	size_t stat_calls;
59
	unsigned int flags;
60 61
};

62
extern int git_iterator_for_nothing(
63 64 65 66
	git_iterator **out,
	git_iterator_flag_t flags,
	const char *start,
	const char *end);
67

68 69 70
/* tree iterators will match the ignore_case value from the index of the
 * repository, unless you override with a non-zero flag value
 */
71
extern int git_iterator_for_tree(
72 73 74 75 76
	git_iterator **out,
	git_tree *tree,
	git_iterator_flag_t flags,
	const char *start,
	const char *end);
77

78 79 80
/* index iterators will take the ignore_case value from the index; the
 * ignore_case flags are not used
 */
81
extern int git_iterator_for_index(
82 83 84 85 86
	git_iterator **out,
	git_index *index,
	git_iterator_flag_t flags,
	const char *start,
	const char *end);
87

88 89 90 91
extern int git_iterator_for_workdir_ext(
	git_iterator **out,
	git_repository *repo,
	const char *repo_workdir,
92 93
	git_index *index,
	git_tree *tree,
94 95 96 97
	git_iterator_flag_t flags,
	const char *start,
	const char *end);

98 99 100
/* workdir iterators will match the ignore_case value from the index of the
 * repository, unless you override with a non-zero flag value
 */
101
GIT_INLINE(int) git_iterator_for_workdir(
102 103
	git_iterator **out,
	git_repository *repo,
104 105
	git_index *index,
	git_tree *tree,
106 107
	git_iterator_flag_t flags,
	const char *start,
108 109
	const char *end)
{
110
	return git_iterator_for_workdir_ext(out, repo, NULL, index, tree, flags, start, end);
111
}
112

113 114 115 116 117 118 119 120 121 122
/* for filesystem iterators, you have to explicitly pass in the ignore_case
 * behavior that you desire
 */
extern int git_iterator_for_filesystem(
	git_iterator **out,
	const char *root,
	git_iterator_flag_t flags,
	const char *start,
	const char *end);

123 124
extern void git_iterator_free(git_iterator *iter);

125 126 127 128 129 130
/* Return a git_index_entry structure for the current value the iterator
 * is looking at or NULL if the iterator is at the end.
 *
 * The entry may noy be fully populated.  Tree iterators will only have a
 * value mode, OID, and path.  Workdir iterators will not have an OID (but
 * you can use `git_iterator_current_oid()` to calculate it on demand).
131 132
 *
 * You do not need to free the entry.  It is still "owned" by the iterator.
133 134
 * Once you call `git_iterator_advance()` then the old entry is no longer
 * guaranteed to be valid - it may be freed or just overwritten in place.
135 136
 */
GIT_INLINE(int) git_iterator_current(
137
	const git_index_entry **entry, git_iterator *iter)
138
{
139
	return iter->cb->current(entry, iter);
140 141
}

142 143 144 145 146 147 148
/**
 * Advance to the next item for the iterator.
 *
 * If GIT_ITERATOR_INCLUDE_TREES is set, this may be a tree item.  If
 * GIT_ITERATOR_DONT_AUTOEXPAND is set, calling this again when on a tree
 * item will skip over all the items under that tree.
 */
149
GIT_INLINE(int) git_iterator_advance(
150
	const git_index_entry **entry, git_iterator *iter)
151
{
152
	return iter->cb->advance(entry, iter);
153 154
}

155 156 157 158 159 160 161 162 163 164 165
/**
 * Iterate into a tree item (when GIT_ITERATOR_DONT_AUTOEXPAND is set).
 *
 * git_iterator_advance() steps through all items being iterated over
 * (either with or without trees, depending on GIT_ITERATOR_INCLUDE_TREES),
 * but if GIT_ITERATOR_DONT_AUTOEXPAND is set, it will skip to the next
 * sibling of a tree instead of going to the first child of the tree.  In
 * that case, use this function to advance to the first child of the tree.
 *
 * If the current item is not a tree, this is a no-op.
 *
166 167 168
 * For filesystem and working directory iterators, a tree (i.e. directory)
 * can be empty.  In that case, this function returns GIT_ENOTFOUND and
 * does not advance.  That can't happen for tree and index iterators.
169 170 171 172 173 174 175
 */
GIT_INLINE(int) git_iterator_advance_into(
	const git_index_entry **entry, git_iterator *iter)
{
	return iter->cb->advance_into(entry, iter);
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
/**
 * Advance into a tree or skip over it if it is empty.
 *
 * Because `git_iterator_advance_into` may return GIT_ENOTFOUND if the
 * directory is empty (only with filesystem and working directory
 * iterators) and a common response is to just call `git_iterator_advance`
 * when that happens, this bundles the two into a single simple call.
 */
GIT_INLINE(int) git_iterator_advance_into_or_over(
	const git_index_entry **entry, git_iterator *iter)
{
	int error = iter->cb->advance_into(entry, iter);
	if (error == GIT_ENOTFOUND) {
		giterr_clear();
		error = iter->cb->advance(entry, iter);
	}
	return error;
}

/* Seek is currently unimplemented */
196 197 198
GIT_INLINE(int) git_iterator_seek(
	git_iterator *iter, const char *prefix)
{
199
	return iter->cb->seek(iter, prefix);
200 201
}

202 203 204 205 206 207 208
/**
 * Go back to the start of the iteration.
 *
 * This resets the iterator to the start of the iteration.  It also allows
 * you to reset the `start` and `end` pathname boundaries of the iteration
 * when doing so.
 */
209 210
GIT_INLINE(int) git_iterator_reset(
	git_iterator *iter, const char *start, const char *end)
211
{
212
	return iter->cb->reset(iter, start, end);
213 214
}

215 216 217 218 219
/**
 * Check if the iterator is at the end
 *
 * @return 0 if not at end, >0 if at end
 */
220 221 222 223 224
GIT_INLINE(int) git_iterator_at_end(git_iterator *iter)
{
	return iter->cb->at_end(iter);
}

225 226 227 228 229
GIT_INLINE(git_iterator_type_t) git_iterator_type(git_iterator *iter)
{
	return iter->type;
}

Russell Belfer committed
230 231 232 233 234
GIT_INLINE(git_repository *) git_iterator_owner(git_iterator *iter)
{
	return iter->repo;
}

235 236 237 238 239 240 241 242 243 244
GIT_INLINE(git_iterator_flag_t) git_iterator_flags(git_iterator *iter)
{
	return iter->flags;
}

GIT_INLINE(bool) git_iterator_ignore_case(git_iterator *iter)
{
	return ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0);
}

245 246
extern int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case);

247
extern int git_iterator_current_tree_entry(
248
	const git_tree_entry **entry_out, git_iterator *iter);
249

250
extern int git_iterator_current_parent_tree(
251
	const git_tree **tree_out, git_iterator *iter, const char *parent_path);
252

253
extern bool git_iterator_current_is_ignored(git_iterator *iter);
254

255 256
extern bool git_iterator_current_tree_is_ignored(git_iterator *iter);

257 258 259
extern int git_iterator_cmp(
	git_iterator *iter, const char *path_prefix);

260
/**
261 262 263
 * Get full path of the current item from a workdir iterator.  This will
 * return NULL for a non-workdir iterator.  The git_buf is still owned by
 * the iterator; this is exposed just for efficiency.
264 265
 */
extern int git_iterator_current_workdir_path(
266
	git_buf **path, git_iterator *iter);
267

268 269
/* Return index pointer if index iterator, else NULL */
extern git_index *git_iterator_get_index(git_iterator *iter);
270

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
typedef enum {
	GIT_ITERATOR_STATUS_NORMAL = 0,
	GIT_ITERATOR_STATUS_IGNORED = 1,
	GIT_ITERATOR_STATUS_EMPTY = 2
} git_iterator_status_t;

/* Advance over a directory and check if it contains no files or just
 * ignored files.
 *
 * In a tree or the index, all directories will contain files, but in the
 * working directory it is possible to have an empty directory tree or a
 * tree that only contains ignored files.  Many Git operations treat these
 * cases specially.  This advances over a directory (presumably an
 * untracked directory) but checks during the scan if there are any files
 * and any non-ignored files.
286
 */
287 288
extern int git_iterator_advance_over_with_status(
	const git_index_entry **entry, git_iterator_status_t *status, git_iterator *iter);
289

290 291 292 293 294 295 296
/**
 * Retrieve the index stored in the iterator.
 *
 * Only implemented for the workdir iterator
 */
extern int git_iterator_index(git_index **out, git_iterator *iter);

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
typedef int (*git_iterator_walk_cb)(
	const git_index_entry **entries,
	void *data);

/**
 * Walk the given iterators in lock-step.  The given callback will be
 * called for each unique path, with the index entry in each iterator
 * (or NULL if the given iterator does not contain that path).
 */
extern int git_iterator_walk(
	git_iterator **iterators,
	size_t cnt,
	git_iterator_walk_cb cb,
	void *data);

312
#endif