@@ -142,12 +142,13 @@ class FileValidationUtils {
142142 val gitignore = it.readText()
143143 if (gitignore.split(" \n " ).any { line ->
144144 try {
145- if (line.trim().isEmpty()) return @any false
146- if (line.startsWith(" #" )) return @any false
147- val pattern =
148- line.trim().trimStart(' /' ).trimEnd(' /' )
149- .replace(" ." , " \\ ." ).replace(" *" , " .*" )
150- return @any path.fileName.toString().trimEnd(' /' ).matches(Regex (pattern))
145+ val trimmedLine = line.trim()
146+ if (trimmedLine.isEmpty() || trimmedLine.startsWith(" #" )) return @any false
147+ // Convert gitignore pattern to regex
148+ val regexPattern = " ^" + Regex .escape(trimmedLine)
149+ .replace(" \\ *" , " .*" )
150+ .replace(" \\ ?" , " ." ) + " $"
151+ return @any path.fileName.toString().matches(Regex (regexPattern))
151152 } catch (e: Throwable ) {
152153 return @any false
153154 }
@@ -156,14 +157,17 @@ class FileValidationUtils {
156157 }
157158 currentDir = currentDir.parentFile ? : return false
158159 }
160+ // After .git is found, check the final directory's .gitignore
159161 currentDir.resolve(" .gitignore" ).let {
160162 if (it.exists()) {
161163 val gitignore = it.readText()
162164 if (gitignore.split(" \n " ).any { line ->
163- val pattern = line.trim().trimEnd(' /' ).replace(" ." , " \\ ." ).replace(" *" , " .*" )
164- line.trim().isNotEmpty()
165- && ! line.startsWith(" #" )
166- && path.fileName.toString().trimEnd(' /' ).matches(Regex (pattern))
165+ val trimmedLine = line.trim()
166+ if (trimmedLine.isEmpty() || trimmedLine.startsWith(" #" )) return @any false
167+ val regexPattern = " ^" + Regex .escape(trimmedLine)
168+ .replace(" \\ *" , " .*" )
169+ .replace(" \\ ?" , " ." ) + " $"
170+ path.fileName.toString().matches(Regex (regexPattern))
167171 }) {
168172 return true
169173 }
0 commit comments