MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / patternWithRoot

Function patternWithRoot

source code/utils/permissions/filesystem.ts:855–919  ·  view source on GitHub ↗
(
  pattern: string,
  source: PermissionRuleSource,
)

Source from the content-addressed store, hash-verified

853}
854
855function patternWithRoot(
856 pattern: string,
857 source: PermissionRuleSource,
858): {
859 relativePattern: string
860 root: string | null
861} {
862 if (pattern.startsWith(`${DIR_SEP}${DIR_SEP}`)) {
863 // Patterns starting with // resolve relative to /
864 const patternWithoutDoubleSlash = pattern.slice(1)
865
866 // On Windows, check if this is a POSIX-style drive path like //c/Users/...
867 // Note: UNC paths (//server/share) will not match this regex and will be treated
868 // as root-relative patterns, which may need separate handling in the future
869 if (
870 getPlatform() === 'windows' &&
871 patternWithoutDoubleSlash.match(/^\/[a-z]\//i)
872 ) {
873 // Convert POSIX path to Windows format
874 // The pattern is like /c/Users/... so we convert it to C:\Users\...
875 const driveLetter = patternWithoutDoubleSlash[1]?.toUpperCase() ?? 'C'
876 // Keep the pattern in POSIX format since relativePath returns POSIX paths
877 const pathAfterDrive = patternWithoutDoubleSlash.slice(2)
878
879 // Extract the drive root (C:\) and the rest of the pattern
880 const driveRoot = `${driveLetter}:\\`
881 const relativeFromDrive = pathAfterDrive.startsWith('/')
882 ? pathAfterDrive.slice(1)
883 : pathAfterDrive
884
885 return {
886 relativePattern: relativeFromDrive,
887 root: driveRoot,
888 }
889 }
890
891 return {
892 relativePattern: patternWithoutDoubleSlash,
893 root: DIR_SEP,
894 }
895 } else if (pattern.startsWith(`~${DIR_SEP}`)) {
896 // Patterns starting with ~/ resolve relative to homedir
897 return {
898 relativePattern: pattern.slice(1),
899 root: homedir().normalize('NFC'),
900 }
901 } else if (pattern.startsWith(DIR_SEP)) {
902 // Patterns starting with / resolve relative to the directory where settings are stored (without .claude/)
903 return {
904 relativePattern: pattern,
905 root: rootPathForSource(source),
906 }
907 }
908 // No root specified, put it with all the other patterns
909 // Normalize patterns that start with "./" to remove the prefix
910 // This ensures that patterns like "./.env" match files like ".env"
911 let normalizedPattern = pattern
912 if (pattern.startsWith(`.${DIR_SEP}`)) {

Callers 1

getPatternsByRootFunction · 0.85

Calls 2

getPlatformFunction · 0.85
rootPathForSourceFunction · 0.85

Tested by

no test coverage detected