Blame src/iterator.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_iterator_h__
Packit Service 20376f
#define INCLUDE_iterator_h__
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "git2/index.h"
Packit Service 20376f
#include "vector.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
#include "ignore.h"
Packit Service 20376f
Packit Service 20376f
typedef struct git_iterator git_iterator;
Packit Service 20376f
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_ITERATOR_TYPE_EMPTY = 0,
Packit Service 20376f
	GIT_ITERATOR_TYPE_TREE = 1,
Packit Service 20376f
	GIT_ITERATOR_TYPE_INDEX = 2,
Packit Service 20376f
	GIT_ITERATOR_TYPE_WORKDIR = 3,
Packit Service 20376f
	GIT_ITERATOR_TYPE_FS = 4,
Packit Service 20376f
} git_iterator_type_t;
Packit Service 20376f
Packit Service 20376f
typedef enum {
Packit Service 20376f
	/** ignore case for entry sort order */
Packit Service 20376f
	GIT_ITERATOR_IGNORE_CASE = (1u << 0),
Packit Service 20376f
	/** force case sensitivity for entry sort order */
Packit Service 20376f
	GIT_ITERATOR_DONT_IGNORE_CASE = (1u << 1),
Packit Service 20376f
	/** return tree items in addition to blob items */
Packit Service 20376f
	GIT_ITERATOR_INCLUDE_TREES    = (1u << 2),
Packit Service 20376f
	/** don't flatten trees, requiring advance_into (implies INCLUDE_TREES) */
Packit Service 20376f
	GIT_ITERATOR_DONT_AUTOEXPAND  = (1u << 3),
Packit Service 20376f
	/** convert precomposed unicode to decomposed unicode */
Packit Service 20376f
	GIT_ITERATOR_PRECOMPOSE_UNICODE = (1u << 4),
Packit Service 20376f
	/** never convert precomposed unicode to decomposed unicode */
Packit Service 20376f
	GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE = (1u << 5),
Packit Service 20376f
	/** include conflicts */
Packit Service 20376f
	GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 6),
Packit Service 20376f
} git_iterator_flag_t;
Packit Service 20376f
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_ITERATOR_STATUS_NORMAL = 0,
Packit Service 20376f
	GIT_ITERATOR_STATUS_IGNORED = 1,
Packit Service 20376f
	GIT_ITERATOR_STATUS_EMPTY = 2,
Packit Service 20376f
	GIT_ITERATOR_STATUS_FILTERED = 3
Packit Service 20376f
} git_iterator_status_t;
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	const char *start;
Packit Service 20376f
	const char *end;
Packit Service 20376f
Packit Service 20376f
	/* paths to include in the iterator (literal).  if set, any paths not
Packit Service 20376f
	 * listed here will be excluded from iteration.
Packit Service 20376f
	 */
Packit Service 20376f
	git_strarray pathlist;
Packit Service 20376f
Packit Service 20376f
	/* flags, from above */
Packit Service 20376f
	unsigned int flags;
Packit Service 20376f
} git_iterator_options;
Packit Service 20376f
Packit Service 20376f
#define GIT_ITERATOR_OPTIONS_INIT {0}
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	int (*current)(const git_index_entry **, git_iterator *);
Packit Service 20376f
	int (*advance)(const git_index_entry **, git_iterator *);
Packit Service 20376f
	int (*advance_into)(const git_index_entry **, git_iterator *);
Packit Service 20376f
	int (*advance_over)(
Packit Service 20376f
		const git_index_entry **, git_iterator_status_t *, git_iterator *);
Packit Service 20376f
	int (*reset)(git_iterator *);
Packit Service 20376f
	void (*free)(git_iterator *);
Packit Service 20376f
} git_iterator_callbacks;
Packit Service 20376f
Packit Service 20376f
struct git_iterator {
Packit Service 20376f
	git_iterator_type_t type;
Packit Service 20376f
	git_iterator_callbacks *cb;
Packit Service 20376f
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	git_index *index;
Packit Service 20376f
Packit Service 20376f
	char *start;
Packit Service 20376f
	size_t start_len;
Packit Service 20376f
Packit Service 20376f
	char *end;
Packit Service 20376f
	size_t end_len;
Packit Service 20376f
Packit Service 20376f
	bool started;
Packit Service 20376f
	bool ended;
Packit Service 20376f
	git_vector pathlist;
Packit Service 20376f
	size_t pathlist_walk_idx;
Packit Service 20376f
	int (*strcomp)(const char *a, const char *b);
Packit Service 20376f
	int (*strncomp)(const char *a, const char *b, size_t n);
Packit Service 20376f
	int (*prefixcomp)(const char *str, const char *prefix);
Packit Service 20376f
	int (*entry_srch)(const void *key, const void *array_member);
Packit Service 20376f
	size_t stat_calls;
Packit Service 20376f
	unsigned int flags;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
extern int git_iterator_for_nothing(
Packit Service 20376f
	git_iterator **out,
Packit Service 20376f
	git_iterator_options *options);
Packit Service 20376f
Packit Service 20376f
/* tree iterators will match the ignore_case value from the index of the
Packit Service 20376f
 * repository, unless you override with a non-zero flag value
Packit Service 20376f
 */
Packit Service 20376f
extern int git_iterator_for_tree(
Packit Service 20376f
	git_iterator **out,
Packit Service 20376f
	git_tree *tree,
Packit Service 20376f
	git_iterator_options *options);
Packit Service 20376f
Packit Service 20376f
/* index iterators will take the ignore_case value from the index; the
Packit Service 20376f
 * ignore_case flags are not used
Packit Service 20376f
 */
Packit Service 20376f
extern int git_iterator_for_index(
Packit Service 20376f
	git_iterator **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	git_iterator_options *options);
Packit Service 20376f
Packit Service 20376f
extern int git_iterator_for_workdir_ext(
Packit Service 20376f
	git_iterator **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *repo_workdir,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	git_tree *tree,
Packit Service 20376f
	git_iterator_options *options);
Packit Service 20376f
Packit Service 20376f
/* workdir iterators will match the ignore_case value from the index of the
Packit Service 20376f
 * repository, unless you override with a non-zero flag value
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(int) git_iterator_for_workdir(
Packit Service 20376f
	git_iterator **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	git_tree *tree,
Packit Service 20376f
	git_iterator_options *options)
Packit Service 20376f
{
Packit Service 20376f
	return git_iterator_for_workdir_ext(out, repo, NULL, index, tree, options);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* for filesystem iterators, you have to explicitly pass in the ignore_case
Packit Service 20376f
 * behavior that you desire
Packit Service 20376f
 */
Packit Service 20376f
extern int git_iterator_for_filesystem(
Packit Service 20376f
	git_iterator **out,
Packit Service 20376f
	const char *root,
Packit Service 20376f
	git_iterator_options *options);
Packit Service 20376f
Packit Service 20376f
extern void git_iterator_free(git_iterator *iter);
Packit Service 20376f
Packit Service 20376f
/* Return a git_index_entry structure for the current value the iterator
Packit Service 20376f
 * is looking at or NULL if the iterator is at the end.
Packit Service 20376f
 *
Packit Service 20376f
 * The entry may noy be fully populated.  Tree iterators will only have a
Packit Service 20376f
 * value mode, OID, and path.  Workdir iterators will not have an OID (but
Packit Service 20376f
 * you can use `git_iterator_current_oid()` to calculate it on demand).
Packit Service 20376f
 *
Packit Service 20376f
 * You do not need to free the entry.  It is still "owned" by the iterator.
Packit Service 20376f
 * Once you call `git_iterator_advance()` then the old entry is no longer
Packit Service 20376f
 * guaranteed to be valid - it may be freed or just overwritten in place.
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(int) git_iterator_current(
Packit Service 20376f
	const git_index_entry **entry, git_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	return iter->cb->current(entry, iter);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Advance to the next item for the iterator.
Packit Service 20376f
 *
Packit Service 20376f
 * If GIT_ITERATOR_INCLUDE_TREES is set, this may be a tree item.  If
Packit Service 20376f
 * GIT_ITERATOR_DONT_AUTOEXPAND is set, calling this again when on a tree
Packit Service 20376f
 * item will skip over all the items under that tree.
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(int) git_iterator_advance(
Packit Service 20376f
	const git_index_entry **entry, git_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	return iter->cb->advance(entry, iter);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Iterate into a tree item (when GIT_ITERATOR_DONT_AUTOEXPAND is set).
Packit Service 20376f
 *
Packit Service 20376f
 * git_iterator_advance() steps through all items being iterated over
Packit Service 20376f
 * (either with or without trees, depending on GIT_ITERATOR_INCLUDE_TREES),
Packit Service 20376f
 * but if GIT_ITERATOR_DONT_AUTOEXPAND is set, it will skip to the next
Packit Service 20376f
 * sibling of a tree instead of going to the first child of the tree.  In
Packit Service 20376f
 * that case, use this function to advance to the first child of the tree.
Packit Service 20376f
 *
Packit Service 20376f
 * If the current item is not a tree, this is a no-op.
Packit Service 20376f
 *
Packit Service 20376f
 * For filesystem and working directory iterators, a tree (i.e. directory)
Packit Service 20376f
 * can be empty.  In that case, this function returns GIT_ENOTFOUND and
Packit Service 20376f
 * does not advance.  That can't happen for tree and index iterators.
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(int) git_iterator_advance_into(
Packit Service 20376f
	const git_index_entry **entry, git_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	return iter->cb->advance_into(entry, iter);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Advance over a directory and check if it contains no files or just
Packit Service 20376f
 * ignored files.
Packit Service 20376f
 *
Packit Service 20376f
 * In a tree or the index, all directories will contain files, but in the
Packit Service 20376f
 * working directory it is possible to have an empty directory tree or a
Packit Service 20376f
 * tree that only contains ignored files.  Many Git operations treat these
Packit Service 20376f
 * cases specially.  This advances over a directory (presumably an
Packit Service 20376f
 * untracked directory) but checks during the scan if there are any files
Packit Service 20376f
 * and any non-ignored files.
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(int) git_iterator_advance_over(
Packit Service 20376f
	const git_index_entry **entry,
Packit Service 20376f
	git_iterator_status_t *status,
Packit Service 20376f
	git_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	return iter->cb->advance_over(entry, status, iter);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Go back to the start of the iteration.
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(int) git_iterator_reset(git_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	return iter->cb->reset(iter);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Go back to the start of the iteration after updating the `start` and
Packit Service 20376f
 * `end` pathname boundaries of the iteration.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_iterator_reset_range(
Packit Service 20376f
	git_iterator *iter, const char *start, const char *end);
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(git_iterator_type_t) git_iterator_type(git_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	return iter->type;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(git_repository *) git_iterator_owner(git_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	return iter->repo;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(git_index *) git_iterator_index(git_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	return iter->index;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(git_iterator_flag_t) git_iterator_flags(git_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	return iter->flags;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) git_iterator_ignore_case(git_iterator *iter)
Packit Service 20376f
{
Packit Service 20376f
	return ((iter->flags & GIT_ITERATOR_IGNORE_CASE) != 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
extern void git_iterator_set_ignore_case(
Packit Service 20376f
	git_iterator *iter, bool ignore_case);
Packit Service 20376f
Packit Service 20376f
extern int git_iterator_current_tree_entry(
Packit Service 20376f
	const git_tree_entry **entry_out, git_iterator *iter);
Packit Service 20376f
Packit Service 20376f
extern int git_iterator_current_parent_tree(
Packit Service 20376f
	const git_tree **tree_out, git_iterator *iter, size_t depth);
Packit Service 20376f
Packit Service 20376f
extern bool git_iterator_current_is_ignored(git_iterator *iter);
Packit Service 20376f
Packit Service 20376f
extern bool git_iterator_current_tree_is_ignored(git_iterator *iter);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Get full path of the current item from a workdir iterator.  This will
Packit Service 20376f
 * return NULL for a non-workdir iterator.  The git_buf is still owned by
Packit Service 20376f
 * the iterator; this is exposed just for efficiency.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_iterator_current_workdir_path(
Packit Service 20376f
	git_buf **path, git_iterator *iter);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Retrieve the index stored in the iterator.
Packit Service 20376f
 *
Packit Service 20376f
 * Only implemented for the workdir and index iterators.
Packit Service 20376f
 */
Packit Service 20376f
extern git_index *git_iterator_index(git_iterator *iter);
Packit Service 20376f
Packit Service 20376f
typedef int (*git_iterator_walk_cb)(
Packit Service 20376f
	const git_index_entry **entries,
Packit Service 20376f
	void *data);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Walk the given iterators in lock-step.  The given callback will be
Packit Service 20376f
 * called for each unique path, with the index entry in each iterator
Packit Service 20376f
 * (or NULL if the given iterator does not contain that path).
Packit Service 20376f
 */
Packit Service 20376f
extern int git_iterator_walk(
Packit Service 20376f
	git_iterator **iterators,
Packit Service 20376f
	size_t cnt,
Packit Service 20376f
	git_iterator_walk_cb cb,
Packit Service 20376f
	void *data);
Packit Service 20376f
Packit Service 20376f
#endif