🌐 AI搜索 & 代理 主页
blob: a5058469e9b3a83fdd58a04f2af72690742d08bd [file] [log] [blame]
Junio C Hamano1b0c7172006-03-30 06:55:431#ifndef TREE_WALK_H
2#define TREE_WALK_H
3
brian m. carlsonea82b2a2019-01-15 00:39:444#include "cache.h"
Elijah Newrenef3ca952018-08-15 17:54:055
Jeff King5290d452020-02-01 11:39:226#define MAX_TRAVERSE_TREES 8
7
Heba Walybbcfa302019-11-17 21:04:578/**
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 */
Junio C Hamano1b0c7172006-03-30 06:55:4315struct name_entry {
brian m. carlsonea82b2a2019-01-15 00:39:4416 struct object_id oid;
Junio C Hamano1b0c7172006-03-30 06:55:4317 const char *path;
brian m. carlsonea82b2a2019-01-15 00:39:4418 int pathlen;
Junio C Hamano1b0c7172006-03-30 06:55:4319 unsigned int mode;
Junio C Hamano1b0c7172006-03-30 06:55:4320};
21
Heba Walybbcfa302019-11-17 21:04:5722/**
23 * A semi-opaque data structure used to maintain the current state of the walk.
24 */
Linus Torvalds4651ece2007-03-21 17:09:5625struct tree_desc {
Heba Walybbcfa302019-11-17 21:04:5726 /*
27 * pointer into the memory representation of the tree. It always
28 * points at the current entry being visited.
29 */
Linus Torvalds4651ece2007-03-21 17:09:5630 const void *buffer;
Heba Walybbcfa302019-11-17 21:04:5731
32 /* points to the current entry being visited. */
Linus Torvalds4651ece2007-03-21 17:09:5633 struct name_entry entry;
Heba Walybbcfa302019-11-17 21:04:5734
35 /* counts the number of bytes left in the `buffer`. */
Linus Torvalds4651ece2007-03-21 17:09:5636 unsigned int size;
37};
38
Heba Walybbcfa302019-11-17 21:04:5739/**
40 * Decode the entry currently being visited (the one pointed to by
41 * `tree_desc's` `entry` member) and return the sha1 of the entry. The
42 * `pathp` and `modep` arguments are set to the entry's pathname and mode
43 * respectively.
44 */
Elijah Newren5ec1e722019-04-05 15:00:1245static inline const struct object_id *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned short *modep)
Linus Torvalds4651ece2007-03-21 17:09:5646{
47 *pathp = desc->entry.path;
Kirill Smelkov7146e662014-02-06 11:36:3148 *modep = desc->entry.mode;
brian m. carlsonea82b2a2019-01-15 00:39:4449 return &desc->entry.oid;
Linus Torvalds4651ece2007-03-21 17:09:5650}
51
Heba Walybbcfa302019-11-17 21:04:5752/**
53 * Calculate the length of a tree entry's pathname. This utilizes the
54 * memory structure of a tree entry to avoid the overhead of using a
55 * generic strlen().
56 */
Nguyễn Thái Ngọc Duy0de16332011-10-24 06:36:0957static inline int tree_entry_len(const struct name_entry *ne)
Linus Torvalds304de2d2007-03-18 03:06:2458{
brian m. carlsonea82b2a2019-01-15 00:39:4459 return ne->pathlen;
Linus Torvalds304de2d2007-03-18 03:06:2460}
61
David Turner8354fa32016-09-27 20:59:5162/*
63 * The _gently versions of these functions warn and return false on a
64 * corrupt tree entry rather than dying,
65 */
66
Heba Walybbcfa302019-11-17 21:04:5767/**
68 * Walk to the next entry in a tree. This is commonly used in conjunction
69 * with `tree_entry_extract` to inspect the current entry.
70 */
Junio C Hamano1b0c7172006-03-30 06:55:4371void update_tree_entry(struct tree_desc *);
Heba Walybbcfa302019-11-17 21:04:5772
David Turner8354fa32016-09-27 20:59:5173int update_tree_entry_gently(struct tree_desc *);
Heba Walybbcfa302019-11-17 21:04:5774
75/**
76 * Initialize a `tree_desc` and decode its first entry. The buffer and
77 * size parameters are assumed to be the same as the buffer and size
78 * members of `struct tree`.
79 */
Linus Torvalds6fda5e52007-03-21 17:08:2580void init_tree_desc(struct tree_desc *desc, const void *buf, unsigned long size);
Heba Walybbcfa302019-11-17 21:04:5781
David Turner8354fa32016-09-27 20:59:5182int init_tree_desc_gently(struct tree_desc *desc, const void *buf, unsigned long size);
Junio C Hamano1b0c7172006-03-30 06:55:4383
Elijah Newren2244eab2010-08-25 02:53:1184/*
Heba Walybbcfa302019-11-17 21:04:5785 * Visit the next entry in a tree. Returns 1 when there are more entries
86 * left to visit and 0 when all entries have been visited. This is
87 * commonly used in the test of a while loop.
Elijah Newren2244eab2010-08-25 02:53:1188 */
Linus Torvalds4c068a92006-05-30 16:45:4589int tree_entry(struct tree_desc *, struct name_entry *);
Heba Walybbcfa302019-11-17 21:04:5790
David Turner8354fa32016-09-27 20:59:5191int tree_entry_gently(struct tree_desc *, struct name_entry *);
Linus Torvalds4c068a92006-05-30 16:45:4592
Heba Walybbcfa302019-11-17 21:04:5793/**
94 * Initialize a `tree_desc` and decode its first entry given the
95 * object ID of a tree. Returns the `buffer` member if the latter
96 * is a valid tree identifier and NULL otherwise.
97 */
Nguyễn Thái Ngọc Duy5e575802019-06-27 09:28:4898void *fill_tree_descriptor(struct repository *r,
99 struct tree_desc *desc,
100 const struct object_id *oid);
Junio C Hamano1b0c7172006-03-30 06:55:43101
Linus Torvalds40d934d2008-03-06 02:59:29102struct traverse_info;
Linus Torvalds91e4f032008-03-06 04:06:18103typedef int (*traverse_callback_t)(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *);
Heba Walybbcfa302019-11-17 21:04:57104
105/**
106 * Traverse `n` number of trees in parallel. The `fn` callback member of
107 * `traverse_info` is called once for each tree entry.
108 */
Nguyễn Thái Ngọc Duy67022e02018-11-18 16:47:57109int traverse_trees(struct index_state *istate, int n, struct tree_desc *t, struct traverse_info *info);
Junio C Hamano1b0c7172006-03-30 06:55:43110
Nguyễn Thái Ngọc Duy0dd1f0c2019-06-27 09:28:50111enum 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);
David Turner275721c2015-05-20 17:03:38112
Heba Walybbcfa302019-11-17 21:04:57113/**
114 * A structure used to maintain the state of a traversal.
115 */
Linus Torvalds40d934d2008-03-06 02:59:29116struct traverse_info {
David Turnerd9c2bd52015-12-21 22:34:20117 const char *traverse_path;
Heba Walybbcfa302019-11-17 21:04:57118
119 /*
120 * points to the traverse_info which was used to descend into the
121 * current tree. If this is the top-level tree `prev` will point to
122 * a dummy traverse_info.
123 */
Linus Torvalds40d934d2008-03-06 02:59:29124 struct traverse_info *prev;
Heba Walybbcfa302019-11-17 21:04:57125
126 /* is the entry for the current tree (if the tree is a subtree). */
Jeff King90553842019-07-31 04:38:15127 const char *name;
Heba Walybbcfa302019-11-17 21:04:57128
Jeff King90553842019-07-31 04:38:15129 size_t namelen;
130 unsigned mode;
131
Heba Walybbcfa302019-11-17 21:04:57132 /* is the length of the full path for the current tree. */
Jeff King37806082019-07-31 04:38:18133 size_t pathlen;
Heba Walybbcfa302019-11-17 21:04:57134
Junio C Hamano2842c0f2011-08-29 19:26:05135 struct pathspec *pathspec;
Linus Torvalds40d934d2008-03-06 02:59:29136
Heba Walybbcfa302019-11-17 21:04:57137 /* can be used by callbacks to maintain directory-file conflicts. */
René Scharfe603d2492013-06-15 23:44:43138 unsigned long df_conflicts;
Heba Walybbcfa302019-11-17 21:04:57139
140 /* a callback called for each entry in the tree.
141 *
142 * The arguments passed to the traverse callback are as follows:
143 *
144 * - `n` counts the number of trees being traversed.
145 *
146 * - `mask` has its nth bit set if something exists in the nth entry.
147 *
148 * - `dirmask` has its nth bit set if the nth tree's entry is a directory.
149 *
150 * - `entry` is an array of size `n` where the nth entry is from the nth tree.
151 *
152 * - `info` maintains the state of the traversal.
153 *
154 * Returning a negative value will terminate the traversal. Otherwise the
155 * return value is treated as an update mask. If the nth bit is set the nth tree
156 * will be updated and if the bit is not set the nth tree entry will be the
157 * same in the next callback invocation.
158 */
Linus Torvalds40d934d2008-03-06 02:59:29159 traverse_callback_t fn;
Heba Walybbcfa302019-11-17 21:04:57160
161 /* can be anything the `fn` callback would want to use. */
Linus Torvalds40d934d2008-03-06 02:59:29162 void *data;
Heba Walybbcfa302019-11-17 21:04:57163
164 /* tells whether to stop at the first error or not. */
Matthieu Moye6c111b2010-08-11 08:38:07165 int show_all_errors;
Linus Torvalds40d934d2008-03-06 02:59:29166};
Junio C Hamano1b0c7172006-03-30 06:55:43167
Heba Walybbcfa302019-11-17 21:04:57168/**
169 * Find an entry in a tree given a pathname and the sha1 of a tree to
170 * search. Returns 0 if the entry is found and -1 otherwise. The third
171 * and fourth parameters are set to the entry's sha1 and mode respectively.
172 */
Nguyễn Thái Ngọc Duy50ddb082019-06-27 09:28:49173int get_tree_entry(struct repository *, const struct object_id *, const char *, struct object_id *, unsigned short *);
Heba Walybbcfa302019-11-17 21:04:57174
175/**
176 * Generate the full pathname of a tree entry based from the root of the
177 * traversal. For example, if the traversal has recursed into another
178 * tree named "bar" the pathname of an entry "baz" in the "bar"
179 * tree would be "bar/baz".
180 */
Jeff King5aa02f92019-07-31 04:38:25181char *make_traverse_path(char *path, size_t pathlen, const struct traverse_info *info,
Jeff King90553842019-07-31 04:38:15182 const char *name, size_t namelen);
Heba Walybbcfa302019-11-17 21:04:57183
184/**
185 * Convenience wrapper to `make_traverse_path` into a strbuf.
186 */
Jeff Kingc43ab062019-07-31 04:38:23187void strbuf_make_traverse_path(struct strbuf *out,
188 const struct traverse_info *info,
189 const char *name, size_t namelen);
Heba Walybbcfa302019-11-17 21:04:57190
191/**
192 * Initialize a `traverse_info` given the pathname of the tree to start
193 * traversing from.
194 */
Denton Liu55454422019-04-29 08:28:14195void setup_traverse_info(struct traverse_info *info, const char *base);
Linus Torvalds40d934d2008-03-06 02:59:29196
Heba Walybbcfa302019-11-17 21:04:57197/**
198 * Calculate the length of a pathname returned by `make_traverse_path`.
199 * This utilizes the memory structure of a tree entry to avoid the
200 * overhead of using a generic strlen().
201 */
Jeff Kingb3b3cbc2019-07-31 04:38:20202static inline size_t traverse_path_len(const struct traverse_info *info,
203 size_t namelen)
Linus Torvalds40d934d2008-03-06 02:59:29204{
Jeff Kingb3b3cbc2019-07-31 04:38:20205 return st_add(info->pathlen, namelen);
Linus Torvalds40d934d2008-03-06 02:59:29206}
Junio C Hamano4dcff632006-04-19 21:05:47207
Nguyễn Thái Ngọc Duyd688cf02011-10-24 06:36:10208/* in general, positive means "kind of interesting" */
209enum interesting {
210 all_entries_not_interesting = -1, /* no, and no subsequent entries will be either */
211 entry_not_interesting = 0,
212 entry_interesting = 1,
213 all_entries_interesting = 2 /* yes, and all subsequent entries will be */
214};
215
Nguyễn Thái Ngọc Duy67022e02018-11-18 16:47:57216enum interesting tree_entry_interesting(struct index_state *istate,
217 const struct name_entry *,
218 struct strbuf *, int,
219 const struct pathspec *ps);
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 15:02:40220
Junio C Hamano1b0c7172006-03-30 06:55:43221#endif