tree-walk: move doc to tree-walk.h
Move the documentation from Documentation/technical/api-tree-walking.txt to tree-walk.h as it's easier for the developers to find the usage information beside the code instead of looking for it in another doc file. Documentation/technical/api-tree-walking.txt is removed because the information it has is now redundant and it'll be hard to keep it up to date and synchronized with the documentation in the header file. Signed-off-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Heba Waly committed
Nov 17, 2019 at 21:04 UTC
bbcfa3002a6534613d8b74ecac0e19876dbfc2d3
2 files changed
+120
-151
Documentation/technical/api-tree-walking.txt
deleted
-149
@@ -1,149 +0,0 @@
1
-tree walking API
2
-================
3
-
4
-The tree walking API is used to traverse and inspect trees.
5
-
6
-Data Structures
7
----------------
8
-
9
-`struct name_entry`::
10
-
11
- An entry in a tree. Each entry has a sha1 identifier, pathname, and
12
- mode.
13
-
14
-`struct tree_desc`::
15
-
16
- A semi-opaque data structure used to maintain the current state of the
17
- walk.
18
-+
19
-* `buffer` is a pointer into the memory representation of the tree. It always
20
-points at the current entry being visited.
21
-
22
-* `size` counts the number of bytes left in the `buffer`.
23
-
24
-* `entry` points to the current entry being visited.
25
-
26
-`struct traverse_info`::
27
-
28
- A structure used to maintain the state of a traversal.
29
-+
30
-* `prev` points to the traverse_info which was used to descend into the
31
-current tree. If this is the top-level tree `prev` will point to
32
-a dummy traverse_info.
33
-
34
-* `name` is the entry for the current tree (if the tree is a subtree).
35
-
36
-* `pathlen` is the length of the full path for the current tree.
37
-
38
-* `conflicts` can be used by callbacks to maintain directory-file conflicts.
39
-
40
-* `fn` is a callback called for each entry in the tree. See Traversing for more
41
-information.
42
-
43
-* `data` can be anything the `fn` callback would want to use.
44
-
45
-* `show_all_errors` tells whether to stop at the first error or not.
46
-
47
-Initializing
48
-------------
49
-
50
-`init_tree_desc`::
51
-
52
- Initialize a `tree_desc` and decode its first entry. The buffer and
53
- size parameters are assumed to be the same as the buffer and size
54
- members of `struct tree`.
55
-
56
-`fill_tree_descriptor`::
57
-
58
- Initialize a `tree_desc` and decode its first entry given the
59
- object ID of a tree. Returns the `buffer` member if the latter
60
- is a valid tree identifier and NULL otherwise.
61
-
62
-`setup_traverse_info`::
63
-
64
- Initialize a `traverse_info` given the pathname of the tree to start
65
- traversing from.
66
-
67
-Walking
68
--------
69
-
70
-`tree_entry`::
71
-
72
- Visit the next entry in a tree. Returns 1 when there are more entries
73
- left to visit and 0 when all entries have been visited. This is
74
- commonly used in the test of a while loop.
75
-
76
-`tree_entry_len`::
77
-
78
- Calculate the length of a tree entry's pathname. This utilizes the
79
- memory structure of a tree entry to avoid the overhead of using a
80
- generic strlen().
81
-
82
-`update_tree_entry`::
83
-
84
- Walk to the next entry in a tree. This is commonly used in conjunction
85
- with `tree_entry_extract` to inspect the current entry.
86
-
87
-`tree_entry_extract`::
88
-
89
- Decode the entry currently being visited (the one pointed to by
90
- `tree_desc's` `entry` member) and return the sha1 of the entry. The
91
- `pathp` and `modep` arguments are set to the entry's pathname and mode
92
- respectively.
93
-
94
-`get_tree_entry`::
95
-
96
- Find an entry in a tree given a pathname and the sha1 of a tree to
97
- search. Returns 0 if the entry is found and -1 otherwise. The third
98
- and fourth parameters are set to the entry's sha1 and mode
99
- respectively.
100
-
101
-Traversing
102
-----------
103
-
104
-`traverse_trees`::
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
-
109
-`traverse_callback_t`::
110
- The arguments passed to the traverse callback are as follows:
111
-+
112
-* `n` counts the number of trees being traversed.
113
-
114
-* `mask` has its nth bit set if something exists in the nth entry.
115
-
116
-* `dirmask` has its nth bit set if the nth tree's entry is a directory.
117
-
118
-* `entry` is an array of size `n` where the nth entry is from the nth tree.
119
-
120
-* `info` maintains the state of the traversal.
121
-
122
-+
123
-Returning a negative value will terminate the traversal. Otherwise the
124
-return value is treated as an update mask. If the nth bit is set the nth tree
125
-will be updated and if the bit is not set the nth tree entry will be the
126
-same in the next callback invocation.
127
-
128
-`make_traverse_path`::
129
-
130
- Generate the full pathname of a tree entry based from the root of the
131
- traversal. For example, if the traversal has recursed into another
132
- tree named "bar" the pathname of an entry "baz" in the "bar"
133
- tree would be "bar/baz".
134
-
135
-`traverse_path_len`::
136
-
137
- Calculate the length of a pathname returned by `make_traverse_path`.
138
- This utilizes the memory structure of a tree entry to avoid the
139
- overhead of using a generic strlen().
140
-
141
-`strbuf_make_traverse_path`::
142
-
143
- Convenience wrapper to `make_traverse_path` into a strbuf.
144
-
145
-Authors
146
--------
147
-
148
-Written by Junio C Hamano <gitster@pobox.com> and Linus Torvalds
149
-<torvalds@linux-foundation.org>
tree-walk.h
+120
-2
@@ -3,6 +3,13 @@
3
4
#include "cache.h"
5
6
+/**
7
+ * The tree walking API is used to traverse and inspect trees.
8
+ */
9
+
10
+/**
11
+ * An entry in a tree. Each entry has a sha1 identifier, pathname, and mode.
12
+ */
13
struct name_entry {
14
struct object_id oid;
15
const char *path;
@@ -10,12 +17,29 @@ struct name_entry {
17
unsigned int mode;
18
};
19
20
+/**
21
+ * A semi-opaque data structure used to maintain the current state of the walk.
22
+ */
23
struct tree_desc {
24
+ /*
25
+ * pointer into the memory representation of the tree. It always
26
+ * points at the current entry being visited.
27
+ */
28
const void *buffer;
29
+
30
+ /* points to the current entry being visited. */
31
struct name_entry entry;
32
+
33
+ /* counts the number of bytes left in the `buffer`. */
34
unsigned int size;
35
};
36
37
+/**
38
+ * Decode the entry currently being visited (the one pointed to by
39
+ * `tree_desc's` `entry` member) and return the sha1 of the entry. The
40
+ * `pathp` and `modep` arguments are set to the entry's pathname and mode
41
+ * respectively.
42
+ */
43
static inline const struct object_id *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned short *modep)
44
{
45
*pathp = desc->entry.path;
@@ -23,6 +47,11 @@ static inline const struct object_id *tree_entry_extract(struct tree_desc *desc,
47
return &desc->entry.oid;
48
}
49
50
+/**
51
+ * Calculate the length of a tree entry's pathname. This utilizes the
52
+ * memory structure of a tree entry to avoid the overhead of using a
53
+ * generic strlen().
54
+ */
55
static inline int tree_entry_len(const struct name_entry *ne)
56
{
57
return ne->pathlen;
@@ -33,52 +62,141 @@ static inline int tree_entry_len(const struct name_entry *ne)
62
* corrupt tree entry rather than dying,
63
*/
64
65
+/**
66
+ * Walk to the next entry in a tree. This is commonly used in conjunction
67
+ * with `tree_entry_extract` to inspect the current entry.
68
+ */
69
void update_tree_entry(struct tree_desc *);
70
+
71
int update_tree_entry_gently(struct tree_desc *);
72
+
73
+/**
74
+ * Initialize a `tree_desc` and decode its first entry. The buffer and
75
+ * size parameters are assumed to be the same as the buffer and size
76
+ * members of `struct tree`.
77
+ */
78
void init_tree_desc(struct tree_desc *desc, const void *buf, unsigned long size);
79
+
80
int init_tree_desc_gently(struct tree_desc *desc, const void *buf, unsigned long size);
81
82
/*
42
- * Helper function that does both tree_entry_extract() and update_tree_entry()
43
- * and returns true for success
83
+ * Visit the next entry in a tree. Returns 1 when there are more entries
84
+ * left to visit and 0 when all entries have been visited. This is
85
+ * commonly used in the test of a while loop.
86
*/
87
int tree_entry(struct tree_desc *, struct name_entry *);
88
+
89
int tree_entry_gently(struct tree_desc *, struct name_entry *);
90
91
+/**
92
+ * Initialize a `tree_desc` and decode its first entry given the
93
+ * object ID of a tree. Returns the `buffer` member if the latter
94
+ * is a valid tree identifier and NULL otherwise.
95
+ */
96
void *fill_tree_descriptor(struct repository *r,
97
struct tree_desc *desc,
98
const struct object_id *oid);
99
100
struct traverse_info;
101
typedef int (*traverse_callback_t)(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *);
102
+
103
+/**
104
+ * Traverse `n` number of trees in parallel. The `fn` callback member of
105
+ * `traverse_info` is called once for each tree entry.
106
+ */
107
int traverse_trees(struct index_state *istate, int n, struct tree_desc *t, struct traverse_info *info);
108
109
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);
110
111
+/**
112
+ * A structure used to maintain the state of a traversal.
113
+ */
114
struct traverse_info {
115
const char *traverse_path;
116
+
117
+ /*
118
+ * points to the traverse_info which was used to descend into the
119
+ * current tree. If this is the top-level tree `prev` will point to
120
+ * a dummy traverse_info.
121
+ */
122
struct traverse_info *prev;
123
+
124
+ /* is the entry for the current tree (if the tree is a subtree). */
125
const char *name;
126
+
127
size_t namelen;
128
unsigned mode;
129
130
+ /* is the length of the full path for the current tree. */
131
size_t pathlen;
132
+
133
struct pathspec *pathspec;
134
135
+ /* can be used by callbacks to maintain directory-file conflicts. */
136
unsigned long df_conflicts;
137
+
138
+ /* a callback called for each entry in the tree.
139
+ *
140
+ * The arguments passed to the traverse callback are as follows:
141
+ *
142
+ * - `n` counts the number of trees being traversed.
143
+ *
144
+ * - `mask` has its nth bit set if something exists in the nth entry.
145
+ *
146
+ * - `dirmask` has its nth bit set if the nth tree's entry is a directory.
147
+ *
148
+ * - `entry` is an array of size `n` where the nth entry is from the nth tree.
149
+ *
150
+ * - `info` maintains the state of the traversal.
151
+ *
152
+ * Returning a negative value will terminate the traversal. Otherwise the
153
+ * return value is treated as an update mask. If the nth bit is set the nth tree
154
+ * will be updated and if the bit is not set the nth tree entry will be the
155
+ * same in the next callback invocation.
156
+ */
157
traverse_callback_t fn;
158
+
159
+ /* can be anything the `fn` callback would want to use. */
160
void *data;
161
+
162
+ /* tells whether to stop at the first error or not. */
163
int show_all_errors;
164
};
165
166
+/**
167
+ * Find an entry in a tree given a pathname and the sha1 of a tree to
168
+ * search. Returns 0 if the entry is found and -1 otherwise. The third
169
+ * and fourth parameters are set to the entry's sha1 and mode respectively.
170
+ */
171
int get_tree_entry(struct repository *, const struct object_id *, const char *, struct object_id *, unsigned short *);
172
+
173
+/**
174
+ * Generate the full pathname of a tree entry based from the root of the
175
+ * traversal. For example, if the traversal has recursed into another
176
+ * tree named "bar" the pathname of an entry "baz" in the "bar"
177
+ * tree would be "bar/baz".
178
+ */
179
char *make_traverse_path(char *path, size_t pathlen, const struct traverse_info *info,
180
const char *name, size_t namelen);
181
+
182
+/**
183
+ * Convenience wrapper to `make_traverse_path` into a strbuf.
184
+ */
185
void strbuf_make_traverse_path(struct strbuf *out,
186
const struct traverse_info *info,
187
const char *name, size_t namelen);
188
+
189
+/**
190
+ * Initialize a `traverse_info` given the pathname of the tree to start
191
+ * traversing from.
192
+ */
193
void setup_traverse_info(struct traverse_info *info, const char *base);
194
195
+/**
196
+ * Calculate the length of a pathname returned by `make_traverse_path`.
197
+ * This utilizes the memory structure of a tree entry to avoid the
198
+ * overhead of using a generic strlen().
199
+ */
200
static inline size_t traverse_path_len(const struct traverse_info *info,
201
size_t namelen)
202
{