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