#ifndef TREE_WALK_H #define TREE_WALK_H #include "hash.h" struct index_state; struct repository; /** * The tree walking API is used to traverse and inspect trees. */ /** * An entry in a tree. Each entry has a sha1 identifier, pathname, and mode. */ struct name_entry { … }; /** * A semi-opaque data structure used to maintain the current state of the walk. */ struct tree_desc { … }; /** * Decode the entry currently being visited (the one pointed to by * `tree_desc's` `entry` member) and return the sha1 of the entry. The * `pathp` and `modep` arguments are set to the entry's pathname and mode * respectively. */ static inline const struct object_id *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned short *modep) { … } /** * Calculate the length of a tree entry's pathname. This utilizes the * memory structure of a tree entry to avoid the overhead of using a * generic strlen(). */ static inline int tree_entry_len(const struct name_entry *ne) { … } /* * The _gently versions of these functions warn and return false on a * corrupt tree entry rather than dying, */ /** * Walk to the next entry in a tree. This is commonly used in conjunction * with `tree_entry_extract` to inspect the current entry. */ void update_tree_entry(struct tree_desc *); int update_tree_entry_gently(struct tree_desc *); /** * Initialize a `tree_desc` and decode its first entry. The buffer and * size parameters are assumed to be the same as the buffer and size * members of `struct tree`. */ void init_tree_desc(struct tree_desc *desc, const struct object_id *tree_oid, const void *buf, unsigned long size); int init_tree_desc_gently(struct tree_desc *desc, const struct object_id *oid, const void *buf, unsigned long size, enum tree_desc_flags flags); /* * Visit the next entry in a tree. Returns 1 when there are more entries * left to visit and 0 when all entries have been visited. This is * commonly used in the test of a while loop. */ int tree_entry(struct tree_desc *, struct name_entry *); int tree_entry_gently(struct tree_desc *, struct name_entry *); /** * Initialize a `tree_desc` and decode its first entry given the * object ID of a tree. Returns the `buffer` member if the latter * is a valid tree identifier and NULL otherwise. */ void *fill_tree_descriptor(struct repository *r, struct tree_desc *desc, const struct object_id *oid); struct traverse_info; traverse_callback_t; /** * Traverse `n` number of trees in parallel. The `fn` callback member of * `traverse_info` is called once for each tree entry. */ int traverse_trees(struct index_state *istate, int n, struct tree_desc *t, struct traverse_info *info); enum get_oid_result get_tree_entry_follow_symlinks(struct repository *r, struct object_id *tree_oid, const char *name, struct object_id *result, struct strbuf *result_path, unsigned short *mode); /** * A structure used to maintain the state of a traversal. */ struct traverse_info { … }; /** * Find an entry in a tree given a pathname and the sha1 of a tree to * search. Returns 0 if the entry is found and -1 otherwise. The third * and fourth parameters are set to the entry's sha1 and mode respectively. */ int get_tree_entry(struct repository *, const struct object_id *, const char *, struct object_id *, unsigned short *); /** * Generate the full pathname of a tree entry based from the root of the * traversal. For example, if the traversal has recursed into another * tree named "bar" the pathname of an entry "baz" in the "bar" * tree would be "bar/baz". */ char *make_traverse_path(char *path, size_t pathlen, const struct traverse_info *info, const char *name, size_t namelen); /** * Convenience wrapper to `make_traverse_path` into a strbuf. */ void strbuf_make_traverse_path(struct strbuf *out, const struct traverse_info *info, const char *name, size_t namelen); /** * Initialize a `traverse_info` given the pathname of the tree to start * traversing from. */ void setup_traverse_info(struct traverse_info *info, const char *base); /** * Calculate the length of a pathname returned by `make_traverse_path`. * This utilizes the memory structure of a tree entry to avoid the * overhead of using a generic strlen(). */ static inline size_t traverse_path_len(const struct traverse_info *info, size_t namelen) { … } /* in general, positive means "kind of interesting" */ enum interesting { … }; enum interesting tree_entry_interesting(struct index_state *istate, const struct name_entry *, struct strbuf *, const struct pathspec *ps); #endif