236
return old;
237
}
238
239
-typedef int (*match_fn)(const char *unmatched, void *data, void *baton);
239
+typedef int (*match_fn)(const char *unmatched, void *value, void *baton);
240
241
/*
242
* 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.
252
*
253
* The key is partially normalized: consecutive slashes are skipped.
254
*
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.
257
*
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
+ * |--------------------|----------------|------------------|--------------|
274
*/
275
static int trie_find(struct trie *root, const char *key, match_fn fn,
276
void *baton)