8 #define MAX_TRAVERSE_TREES 8
11 * The tree walking API is used to traverse and inspect trees.
15 * An entry in a tree. Each entry has a sha1 identifier, pathname, and mode.
25 * A semi-opaque data structure used to maintain the current state of the walk.
29 * pointer into the memory representation of the tree. It always
30 * points at the current entry being visited.
34 /* points to the current entry being visited. */
35 struct name_entry entry
;
37 /* counts the number of bytes left in the `buffer`. */
40 /* option flags passed via init_tree_desc_gently() */
41 enum tree_desc_flags
{
42 TREE_DESC_RAW_MODES
= (1 << 0),
47 * Decode the entry currently being visited (the one pointed to by
48 * `tree_desc's` `entry` member) and return the sha1 of the entry. The
49 * `pathp` and `modep` arguments are set to the entry's pathname and mode
52 static inline const struct object_id
*tree_entry_extract(struct tree_desc
*desc
, const char **pathp
, unsigned short *modep
)
54 *pathp
= desc
->entry
.path
;
55 *modep
= desc
->entry
.mode
;
56 return &desc
->entry
.oid
;
60 * Calculate the length of a tree entry's pathname. This utilizes the
61 * memory structure of a tree entry to avoid the overhead of using a
64 static inline int tree_entry_len(const struct name_entry
*ne
)
70 * The _gently versions of these functions warn and return false on a
71 * corrupt tree entry rather than dying,
75 * Walk to the next entry in a tree. This is commonly used in conjunction
76 * with `tree_entry_extract` to inspect the current entry.
78 void update_tree_entry(struct tree_desc
*);
80 int update_tree_entry_gently(struct tree_desc
*);
83 * Initialize a `tree_desc` and decode its first entry. The buffer and
84 * size parameters are assumed to be the same as the buffer and size
85 * members of `struct tree`.
87 void init_tree_desc(struct tree_desc
*desc
, const void *buf
, unsigned long size
);
89 int init_tree_desc_gently(struct tree_desc
*desc
, const void *buf
, unsigned long size
,
90 enum tree_desc_flags flags
);
93 * Visit the next entry in a tree. Returns 1 when there are more entries
94 * left to visit and 0 when all entries have been visited. This is
95 * commonly used in the test of a while loop.
97 int tree_entry(struct tree_desc
*, struct name_entry
*);
99 int tree_entry_gently(struct tree_desc
*, struct name_entry
*);
102 * Initialize a `tree_desc` and decode its first entry given the
103 * object ID of a tree. Returns the `buffer` member if the latter
104 * is a valid tree identifier and NULL otherwise.
106 void *fill_tree_descriptor(struct repository
*r
,
107 struct tree_desc
*desc
,
108 const struct object_id
*oid
);
110 struct traverse_info
;
111 typedef int (*traverse_callback_t
)(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*entry
, struct traverse_info
*);
114 * Traverse `n` number of trees in parallel. The `fn` callback member of
115 * `traverse_info` is called once for each tree entry.
117 int traverse_trees(struct index_state
*istate
, int n
, struct tree_desc
*t
, struct traverse_info
*info
);
119 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
);
122 * A structure used to maintain the state of a traversal.
124 struct traverse_info
{
125 const char *traverse_path
;
128 * points to the traverse_info which was used to descend into the
129 * current tree. If this is the top-level tree `prev` will point to
130 * a dummy traverse_info.
132 struct traverse_info
*prev
;
134 /* is the entry for the current tree (if the tree is a subtree). */
140 /* is the length of the full path for the current tree. */
143 struct pathspec
*pathspec
;
145 /* can be used by callbacks to maintain directory-file conflicts. */
146 unsigned long df_conflicts
;
148 /* a callback called for each entry in the tree.
150 * The arguments passed to the traverse callback are as follows:
152 * - `n` counts the number of trees being traversed.
154 * - `mask` has its nth bit set if something exists in the nth entry.
156 * - `dirmask` has its nth bit set if the nth tree's entry is a directory.
158 * - `entry` is an array of size `n` where the nth entry is from the nth tree.
160 * - `info` maintains the state of the traversal.
162 * Returning a negative value will terminate the traversal. Otherwise the
163 * return value is treated as an update mask. If the nth bit is set the nth tree
164 * will be updated and if the bit is not set the nth tree entry will be the
165 * same in the next callback invocation.
167 traverse_callback_t fn
;
169 /* can be anything the `fn` callback would want to use. */
172 /* tells whether to stop at the first error or not. */
177 * Find an entry in a tree given a pathname and the sha1 of a tree to
178 * search. Returns 0 if the entry is found and -1 otherwise. The third
179 * and fourth parameters are set to the entry's sha1 and mode respectively.
181 int get_tree_entry(struct repository
*, const struct object_id
*, const char *, struct object_id
*, unsigned short *);
184 * Generate the full pathname of a tree entry based from the root of the
185 * traversal. For example, if the traversal has recursed into another
186 * tree named "bar" the pathname of an entry "baz" in the "bar"
187 * tree would be "bar/baz".
189 char *make_traverse_path(char *path
, size_t pathlen
, const struct traverse_info
*info
,
190 const char *name
, size_t namelen
);
193 * Convenience wrapper to `make_traverse_path` into a strbuf.
195 void strbuf_make_traverse_path(struct strbuf
*out
,
196 const struct traverse_info
*info
,
197 const char *name
, size_t namelen
);
200 * Initialize a `traverse_info` given the pathname of the tree to start
203 void setup_traverse_info(struct traverse_info
*info
, const char *base
);
206 * Calculate the length of a pathname returned by `make_traverse_path`.
207 * This utilizes the memory structure of a tree entry to avoid the
208 * overhead of using a generic strlen().
210 static inline size_t traverse_path_len(const struct traverse_info
*info
,
213 return st_add(info
->pathlen
, namelen
);
216 /* in general, positive means "kind of interesting" */
218 all_entries_not_interesting
= -1, /* no, and no subsequent entries will be either */
219 entry_not_interesting
= 0,
220 entry_interesting
= 1,
221 all_entries_interesting
= 2 /* yes, and all subsequent entries will be */
224 enum interesting
tree_entry_interesting(struct index_state
*istate
,
225 const struct name_entry
*,
226 struct strbuf
*, int,
227 const struct pathspec
*ps
);