Skip to content

Commit 7cb8c92

Browse files
szedergitster
authored andcommitted
path.c: clarify trie_find()'s in-code comment
A fairly long comment describes trie_find()'s behavior and shows examples, but it's slightly incomplete/inaccurate. Update this comment to specify how trie_find() handles a negative return value from the given match function. Furthermore, update the list of examples to include not only two but three levels of path components. This makes the examples slightly more complicated, but it can illustrate the behavior in more corner cases. Finally, basically everything refers to the data stored for a key as "value", with two confusing exceptions: - The type definition of the match function calls its corresponding parameter 'data'. Rename that parameter to 'value'. (check_common(), the only function of this type already calls it 'value'). - The table of examples above trie_find() has a "val from node" column, which has nothing to do with the value stored in the trie: it's a "prefix of the key for which the trie contains a value" that led to that node. Rename that column header to "prefix to node". Note that neither the original nor the updated description and examples correspond 100% to the current implementation, because the implementation is a bit buggy, but the comment describes the desired behavior. The bug will be fixed in the last patch of this series. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e536b1f commit 7cb8c92

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

path.c

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -236,30 +236,41 @@ static void *add_to_trie(struct trie *root, const char *key, void *value)
236236
return old;
237237
}
238238

239-
typedef int (*match_fn)(const char *unmatched, void *data, void *baton);
239+
typedef int (*match_fn)(const char *unmatched, void *value, void *baton);
240240

241241
/*
242242
* Search a trie for some key. Find the longest /-or-\0-terminated
243-
* prefix of the key for which the trie contains a value. Call fn
244-
* with the unmatched portion of the key and the found value, and
245-
* return its return value. If there is no such prefix, return -1.
243+
* prefix of the key for which the trie contains a value. If there is
244+
* no such prefix, return -1. Otherwise call fn with the unmatched
245+
* portion of the key and the found value. If fn returns 0 or
246+
* positive, then return its return value. If fn returns negative,
247+
* then call fn with the next-longest /-terminated prefix of the key
248+
* (i.e. a parent directory) for which the trie contains a value, and
249+
* handle its return value the same way. If there is no shorter
250+
* /-terminated prefix with a value left, then return the negative
251+
* return value of the most recent fn invocation.
246252
*
247253
* The key is partially normalized: consecutive slashes are skipped.
248254
*
249-
* For example, consider the trie containing only [refs,
250-
* refs/worktree] (both with values).
251-
*
252-
* | key | unmatched | val from node | return value |
253-
* |-----------------|------------|---------------|--------------|
254-
* | a | not called | n/a | -1 |
255-
* | refs | \0 | refs | as per fn |
256-
* | refs/ | / | refs | as per fn |
257-
* | refs/w | /w | refs | as per fn |
258-
* | refs/worktree | \0 | refs/worktree | as per fn |
259-
* | refs/worktree/ | / | refs/worktree | as per fn |
260-
* | refs/worktree/a | /a | refs/worktree | as per fn |
261-
* |-----------------|------------|---------------|--------------|
255+
* For example, consider the trie containing only [logs,
256+
* logs/refs/bisect], both with values, but not logs/refs.
262257
*
258+
* | key | unmatched | prefix to node | return value |
259+
* |--------------------|----------------|------------------|--------------|
260+
* | a | not called | n/a | -1 |
261+
* | logstore | not called | n/a | -1 |
262+
* | logs | \0 | logs | as per fn |
263+
* | logs/ | / | logs | as per fn |
264+
* | logs/refs | /refs | logs | as per fn |
265+
* | logs/refs/ | /refs/ | logs | as per fn |
266+
* | logs/refs/b | /refs/b | logs | as per fn |
267+
* | logs/refs/bisected | /refs/bisected | logs | as per fn |
268+
* | logs/refs/bisect | \0 | logs/refs/bisect | as per fn |
269+
* | logs/refs/bisect/ | / | logs/refs/bisect | as per fn |
270+
* | logs/refs/bisect/a | /a | logs/refs/bisect | as per fn |
271+
* | (If fn in the previous line returns -1, then fn is called once more:) |
272+
* | logs/refs/bisect/a | /refs/bisect/a | logs | as per fn |
273+
* |--------------------|----------------|------------------|--------------|
263274
*/
264275
static int trie_find(struct trie *root, const char *key, match_fn fn,
265276
void *baton)

0 commit comments

Comments
 (0)