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 / agents-md / hooks / files / chain-root-of.ts
28 lines · 761 BRaw
1import type { InstructionFile } from 'claude-code'
2
3/**
4 * The path of the file at the head of an instruction file's `@`-import
5 * chain; the file's own path when nothing imported it.
6 *
7 * Follows `parent` among the given files until a file nothing imported.
8 *
9 * @param file the instruction file
10 * @param byPath the files it may have come through, by path
11 * @returns the chain head's path
12 */
13export function chainRootOf(
14 file: InstructionFile,
15 byPath: ReadonlyMap<string, InstructionFile>,
16): string {
17 const seen = new Set<string>([file.path])
18 let path = file.path
19 let parent = file.parent
20
21 while (parent !== undefined && !seen.has(parent)) {
22 seen.add(parent)
23 path = parent
24 parent = byPath.get(parent)?.parent
25 }
26
27 return path
28}