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 / is-outside-working-tree / is-outside-working-tree.ts
29 lines · 1.0 KBRaw
1import Git from '../git'
2import { normalPathOf } from './normal-path-of.js'
3
4/**
5 * Whether a file a tool edited lies outside the pinned working tree, by
6 * how the paths are spelled alone (normalPathOf): no row could show it.
7 *
8 * A relative path is read from the session's directory. Where that
9 * directory is itself not spelled under the top (a symbolic link on the
10 * way to it), spelling proves nothing and nothing is outside.
11 *
12 * @param path the path the tool was called with
13 * @param tree the session's directory and the working tree's top
14 * @returns true only for a path surely outside the tree
15 */
16export function isOutsideWorkingTree(
17 path: string,
18 tree: { cwd: string; toplevel: string },
19): boolean {
20 const top = normalPathOf(tree.toplevel)
21 const absolute = Git.isAbsolutePath(path) ? path : `${tree.cwd}/${path}`
22
23 const isUnderTop = (spelled: string) =>
24 top === '' || spelled === top || spelled.startsWith(`${top}/`)
25
26 return (
27 isUnderTop(normalPathOf(tree.cwd)) && !isUnderTop(normalPathOf(absolute))
28 )
29}