spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / hooks / git / probes / kind-walk-of / kind-walk-of.ts
70 lines · 1.8 KBRaw
1import type Types from '../../types'
2
3/**
4 * A KindWalk: each directory is listed once, only after its own entry
5 * read as `dir` in its parent, and only while listings remain.
6 *
7 * Read from the root down, one listing a component: the depth is the
8 * budget's, never the stack's. A symbolic link is never listed or descended
9 * and what lies under it reads as null; the unlisted reads `over-budget`.
10 *
11 * @param entryKindsOf the host's directory listing
12 * @param root the absolute directory the walk's paths are relative to
13 * @param budget how many directories this walk may list
14 * @returns the walk
15 */
16export function kindWalkOf(
17 entryKindsOf: Types.GitDeps['entryKindsOf'],
18 root: string,
19 budget: number,
20): Types.KindWalk {
21 const listings = new Map<
22 string,
23 Promise<ReadonlyMap<string, Types.EntryKind> | null>
24 >()
25
26 function listingOf(directory: string) {
27 const held = listings.get(directory)
28 const isOverBudget = held === undefined && listings.size >= budget
29
30 if (isOverBudget) {
31 return 'over-budget'
32 }
33
34 const listed =
35 held ?? entryKindsOf(directory === '' ? root : `${root}/${directory}`)
36 listings.set(directory, listed)
37
38 return listed
39 }
40
41 async function kindOf(path: string): Promise<Types.WalkKind> {
42 const names = path === '' ? [] : path.split('/')
43 let directory = ''
44
45 for (const [depth, name] of names.entries()) {
46 const listing = listingOf(directory)
47
48 if (listing === 'over-budget') {
49 return 'over-budget'
50 }
51
52 const kind = (await listing)?.get(name) ?? null
53 const isLeaf = depth === names.length - 1
54
55 if (isLeaf) {
56 return kind
57 }
58
59 if (kind !== 'dir') {
60 return null
61 }
62
63 directory = directory === '' ? name : `${directory}/${name}`
64 }
65
66 return 'dir'
67 }
68
69 return { kindOf }
70}