]>
Commit | Line | Data |
---|---|---|
1b0c7174 JH |
1 | #ifndef TREE_WALK_H |
2 | #define TREE_WALK_H | |
3 | ||
ea82b2a0 | 4 | #include "cache.h" |
ef3ca954 | 5 | |
5290d451 JK |
6 | #define MAX_TRAVERSE_TREES 8 |
7 | ||
bbcfa300 HW |
8 | /** |
9 | * The tree walking API is used to traverse and inspect trees. | |
10 | */ | |
11 | ||
12 | /** | |
13 | * An entry in a tree. Each entry has a sha1 identifier, pathname, and mode. | |
14 | */ | |
1b0c7174 | 15 | struct name_entry { |
ea82b2a0 | 16 | struct object_id oid; |
1b0c7174 | 17 | const char *path; |
ea82b2a0 | 18 | int pathlen; |
1b0c7174 | 19 | unsigned int mode; |
1b0c7174 JH |
20 | }; |
21 | ||
bbcfa300 HW |
22 | /** |
23 | * A semi-opaque data structure used to maintain the current state of the walk. | |
24 | */ | |
4651ece8 | 25 | struct tree_desc { |
bbcfa300 HW |
26 | /* |
27 | * pointer into the memory representation of the tree. It always | |
28 | * points at the current entry being visited. | |
29 | */ | |
4651ece8 | 30 | const void *buffer; |
bbcfa300 HW |
31 | |
32 | /* points to the current entry being visited. */ | |
4651ece8 | 33 | struct name_entry entry; |
bbcfa300 HW |
34 | |
35 | /* counts the number of bytes left in the `buffer`. */ | |
4651ece8 | 36 | unsigned int size; |
ec18b10b JK |
37 | |
38 | /* option flags passed via init_tree_desc_gently() */ | |
39 | enum tree_desc_flags { | |
40 | TREE_DESC_RAW_MODES = (1 << 0), | |
41 | } flags; | |
4651ece8 LT |
42 | }; |
43 | ||
bbcfa300 HW |
44 | /** |
45 | * Decode the entry currently being visited (the one pointed to by | |
46 | * `tree_desc's` `entry` member) and return the sha1 of the entry. The | |
47 | * `pathp` and `modep` arguments are set to the entry's pathname and mode | |
48 | * respectively. | |
49 | */ | |
5ec1e728 | 50 | static inline const struct object_id *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned short *modep) |
4651ece8 LT |
51 | { |
52 | *pathp = desc->entry.path; | |
7146e66f | 53 | *modep = desc->entry.mode; |
ea82b2a0 | 54 | return &desc->entry.oid; |
4651ece8 LT |
55 | } |
56 | ||
bbcfa300 HW |
57 | /** |
58 | * Calculate the length of a tree entry's pathname. This utilizes the | |
59 | * memory structure of a tree entry to avoid the overhead of using a | |
60 | * generic strlen(). | |
61 | */ | |
0de16337 | 62 | static inline int tree_entry_len(const struct name_entry *ne) |
304de2d2 | 63 | { |
ea82b2a0 | 64 | return ne->pathlen; |
304de2d2 LT |
65 | } |
66 | ||
8354fa3d DT |
67 | /* |
68 | * The _gently versions of these functions warn and return false on a | |
69 | * corrupt tree entry rather than dying, | |
70 | */ | |
71 | ||
bbcfa300 HW |
72 | /** |
73 | * Walk to the next entry in a tree. This is commonly used in conjunction | |
74 | * with `tree_entry_extract` to inspect the current entry. | |
75 | */ | |
1b0c7174 | 76 | void update_tree_entry(struct tree_desc *); |
bbcfa300 | 77 | |
8354fa3d | 78 | int update_tree_entry_gently(struct tree_desc *); |
bbcfa300 HW |
79 | |
80 | /** | |
81 | * Initialize a `tree_desc` and decode its first entry. The buffer and | |
82 | * size parameters are assumed to be the same as the buffer and size | |
83 | * members of `struct tree`. | |
84 | */ | |
6fda5e51 | 85 | void init_tree_desc(struct tree_desc *desc, const void *buf, unsigned long size); |
bbcfa300 | 86 | |
ec18b10b JK |
87 | int init_tree_desc_gently(struct tree_desc *desc, const void *buf, unsigned long size, |
88 | enum tree_desc_flags flags); | |
1b0c7174 | 89 | |
2244eab0 | 90 | /* |
bbcfa300 HW |
91 | * Visit the next entry in a tree. Returns 1 when there are more entries |
92 | * left to visit and 0 when all entries have been visited. This is | |
93 | * commonly used in the test of a while loop. | |
2244eab0 | 94 | */ |
4c068a98 | 95 | int tree_entry(struct tree_desc *, struct name_entry *); |
bbcfa300 | 96 | |
8354fa3d | 97 | int tree_entry_gently(struct tree_desc *, struct name_entry *); |
4c068a98 | 98 | |
bbcfa300 HW |
99 | /** |
100 | * Initialize a `tree_desc` and decode its first entry given the | |
101 | * object ID of a tree. Returns the `buffer` member if the latter | |
102 | * is a valid tree identifier and NULL otherwise. | |
103 | */ | |
5e575807 NTND |
104 | void *fill_tree_descriptor(struct repository *r, |
105 | struct tree_desc *desc, | |
106 | const struct object_id *oid); | |
1b0c7174 | 107 | |
40d934df | 108 | struct traverse_info; |
91e4f036 | 109 | typedef int (*traverse_callback_t)(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *); |
bbcfa300 HW |
110 | |
111 | /** | |
112 | * Traverse `n` number of trees in parallel. The `fn` callback member of | |
113 | * `traverse_info` is called once for each tree entry. | |
114 | */ | |
67022e02 | 115 | int traverse_trees(struct index_state *istate, int n, struct tree_desc *t, struct traverse_info *info); |
1b0c7174 | 116 | |
0dd1f0c3 | 117 | 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); |
275721c2 | 118 | |
bbcfa300 HW |
119 | /** |
120 | * A structure used to maintain the state of a traversal. | |
121 | */ | |
40d934df | 122 | struct traverse_info { |
d9c2bd56 | 123 | const char *traverse_path; |
bbcfa300 HW |
124 | |
125 | /* | |
126 | * points to the traverse_info which was used to descend into the | |
127 | * current tree. If this is the top-level tree `prev` will point to | |
128 | * a dummy traverse_info. | |
129 | */ | |
40d934df | 130 | struct traverse_info *prev; |
bbcfa300 HW |
131 | |
132 | /* is the entry for the current tree (if the tree is a subtree). */ | |
90553847 | 133 | const char *name; |
bbcfa300 | 134 | |
90553847 JK |
135 | size_t namelen; |
136 | unsigned mode; | |
137 | ||
bbcfa300 | 138 | /* is the length of the full path for the current tree. */ |
37806080 | 139 | size_t pathlen; |
bbcfa300 | 140 | |
2842c0f9 | 141 | struct pathspec *pathspec; |
40d934df | 142 | |
bbcfa300 | 143 | /* can be used by callbacks to maintain directory-file conflicts. */ |
603d2498 | 144 | unsigned long df_conflicts; |
bbcfa300 HW |
145 | |
146 | /* a callback called for each entry in the tree. | |
147 | * | |
148 | * The arguments passed to the traverse callback are as follows: | |
149 | * | |
150 | * - `n` counts the number of trees being traversed. | |
151 | * | |
152 | * - `mask` has its nth bit set if something exists in the nth entry. | |
153 | * | |
154 | * - `dirmask` has its nth bit set if the nth tree's entry is a directory. | |
155 | * | |
156 | * - `entry` is an array of size `n` where the nth entry is from the nth tree. | |
157 | * | |
158 | * - `info` maintains the state of the traversal. | |
159 | * | |
160 | * Returning a negative value will terminate the traversal. Otherwise the | |
161 | * return value is treated as an update mask. If the nth bit is set the nth tree | |
162 | * will be updated and if the bit is not set the nth tree entry will be the | |
163 | * same in the next callback invocation. | |
164 | */ | |
40d934df | 165 | traverse_callback_t fn; |
bbcfa300 HW |
166 | |
167 | /* can be anything the `fn` callback would want to use. */ | |
40d934df | 168 | void *data; |
bbcfa300 HW |
169 | |
170 | /* tells whether to stop at the first error or not. */ | |
e6c111b4 | 171 | int show_all_errors; |
40d934df | 172 | }; |
1b0c7174 | 173 | |
bbcfa300 HW |
174 | /** |
175 | * Find an entry in a tree given a pathname and the sha1 of a tree to | |
176 | * search. Returns 0 if the entry is found and -1 otherwise. The third | |
177 | * and fourth parameters are set to the entry's sha1 and mode respectively. | |
178 | */ | |
50ddb089 | 179 | int get_tree_entry(struct repository *, const struct object_id *, const char *, struct object_id *, unsigned short *); |
bbcfa300 HW |
180 | |
181 | /** | |
182 | * Generate the full pathname of a tree entry based from the root of the | |
183 | * traversal. For example, if the traversal has recursed into another | |
184 | * tree named "bar" the pathname of an entry "baz" in the "bar" | |
185 | * tree would be "bar/baz". | |
186 | */ | |
5aa02f98 | 187 | char *make_traverse_path(char *path, size_t pathlen, const struct traverse_info *info, |
90553847 | 188 | const char *name, size_t namelen); |
bbcfa300 HW |
189 | |
190 | /** | |
191 | * Convenience wrapper to `make_traverse_path` into a strbuf. | |
192 | */ | |
c43ab062 JK |
193 | void strbuf_make_traverse_path(struct strbuf *out, |
194 | const struct traverse_info *info, | |
195 | const char *name, size_t namelen); | |
bbcfa300 HW |
196 | |
197 | /** | |
198 | * Initialize a `traverse_info` given the pathname of the tree to start | |
199 | * traversing from. | |
200 | */ | |
55454427 | 201 | void setup_traverse_info(struct traverse_info *info, const char *base); |
40d934df | 202 | |
bbcfa300 HW |
203 | /** |
204 | * Calculate the length of a pathname returned by `make_traverse_path`. | |
205 | * This utilizes the memory structure of a tree entry to avoid the | |
206 | * overhead of using a generic strlen(). | |
207 | */ | |
b3b3cbcb JK |
208 | static inline size_t traverse_path_len(const struct traverse_info *info, |
209 | size_t namelen) | |
40d934df | 210 | { |
b3b3cbcb | 211 | return st_add(info->pathlen, namelen); |
40d934df | 212 | } |
4dcff634 | 213 | |
d688cf07 NTND |
214 | /* in general, positive means "kind of interesting" */ |
215 | enum interesting { | |
216 | all_entries_not_interesting = -1, /* no, and no subsequent entries will be either */ | |
217 | entry_not_interesting = 0, | |
218 | entry_interesting = 1, | |
219 | all_entries_interesting = 2 /* yes, and all subsequent entries will be */ | |
220 | }; | |
221 | ||
67022e02 NTND |
222 | enum interesting tree_entry_interesting(struct index_state *istate, |
223 | const struct name_entry *, | |
224 | struct strbuf *, int, | |
225 | const struct pathspec *ps); | |
2c389fc8 | 226 | |
1b0c7174 | 227 | #endif |