Move setup_diff_pager to libgit.a
[git/jnareb-git.git] / Documentation / technical / api-tree-walking.txt
blob14af37c3f14854e87d9d28f60b5ff72eb189c37f
1 tree walking API
2 ================
4 The tree walking API is used to traverse and inspect trees.
6 Data Structures
7 ---------------
9 `struct name_entry`::
11         An entry in a tree. Each entry has a sha1 identifier, pathname, and
12         mode.
14 `struct tree_desc`::
16         A semi-opaque data structure used to maintain the current state of the
17         walk.
19 * `buffer` is a pointer into the memory representation of the tree. It always
20 points at the current entry being visited.
22 * `size` counts the number of bytes left in the `buffer`.
24 * `entry` points to the current entry being visited.
26 `struct traverse_info`::
28         A structure used to maintain the state of a traversal.
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.
34 * `name` is the entry for the current tree (if the tree is a subtree).
36 * `pathlen` is the length of the full path for the current tree.
38 * `conflicts` can be used by callbacks to maintain directory-file conflicts.
40 * `fn` is a callback called for each entry in the tree. See Traversing for more
41 information.
43 * `data` can be anything the `fn` callback would want to use.
45 * `show_all_errors` tells whether to stop at the first error or not.
47 Initializing
48 ------------
50 `init_tree_desc`::
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`.
56 `fill_tree_descriptor`::
58         Initialize a `tree_desc` and decode its first entry given the sha1 of
59         a tree. Returns the `buffer` member if the sha1 is a valid tree
60         identifier and NULL otherwise.
62 `setup_traverse_info`::
64         Initialize a `traverse_info` given the pathname of the tree to start
65         traversing from. The `base` argument is assumed to be the `path`
66         member of the `name_entry` being recursed into unless the tree is a
67         top-level tree in which case the empty string ("") is used.
69 Walking
70 -------
72 `tree_entry`::
74         Visit the next entry in a tree. Returns 1 when there are more entries
75         left to visit and 0 when all entries have been visited. This is
76         commonly used in the test of a while loop.
78 `tree_entry_len`::
80         Calculate the length of a tree entry's pathname. This utilizes the
81         memory structure of a tree entry to avoid the overhead of using a
82         generic strlen().
84 `update_tree_entry`::
86         Walk to the next entry in a tree. This is commonly used in conjunction
87         with `tree_entry_extract` to inspect the current entry.
89 `tree_entry_extract`::
91         Decode the entry currently being visited (the one pointed to by
92         `tree_desc's` `entry` member) and return the sha1 of the entry. The
93         `pathp` and `modep` arguments are set to the entry's pathname and mode
94         respectively.
96 `get_tree_entry`::
98         Find an entry in a tree given a pathname and the sha1 of a tree to
99         search. Returns 0 if the entry is found and -1 otherwise. The third
100         and fourth parameters are set to the entry's sha1 and mode
101         respectively.
103 Traversing
104 ----------
106 `traverse_trees`::
108         Traverse `n` number of trees in parallel. The `fn` callback member of
109         `traverse_info` is called once for each tree entry.
111 `traverse_callback_t`::
112         The arguments passed to the traverse callback are as follows:
114 * `n` counts the number of trees being traversed.
116 * `mask` has its nth bit set if something exists in the nth entry.
118 * `dirmask` has its nth bit set if the nth tree's entry is a directory.
120 * `entry` is an array of size `n` where the nth entry is from the nth tree.
122 * `info` maintains the state of the traversal.
125 Returning a negative value will terminate the traversal. Otherwise the
126 return value is treated as an update mask. If the nth bit is set the nth tree
127 will be updated and if the bit is not set the nth tree entry will be the
128 same in the next callback invocation.
130 `make_traverse_path`::
132         Generate the full pathname of a tree entry based from the root of the
133         traversal. For example, if the traversal has recursed into another
134         tree named "bar" the pathname of an entry "baz" in the "bar"
135         tree would be "bar/baz".
137 `traverse_path_len`::
139         Calculate the length of a pathname returned by `make_traverse_path`.
140         This utilizes the memory structure of a tree entry to avoid the
141         overhead of using a generic strlen().
143 Authors
144 -------
146 Written by Junio C Hamano <gitster@pobox.com> and Linus Torvalds
147 <torvalds@linux-foundation.org>