MCPcopy Create free account
hub / github.com/Noumena-Network/code / patternWithRoot

Function patternWithRoot

src/utils/permissions/filesystem.ts:863–927  ·  view source on GitHub ↗
(
  pattern: string,
  source: PermissionRuleSource,
)

Source from the content-addressed store, hash-verified

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

Callers 1

getPatternsByRootFunction · 0.85

Calls 2

getPlatformFunction · 0.85
rootPathForSourceFunction · 0.85

Tested by

no test coverage detected