Skip to content

Commit d860beb

Browse files
Gaurav Gandhigandhi-21
authored andcommitted
fix(amazonq): update logic for converting gitignore pattern to regex
1 parent b440147 commit d860beb

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

plugins/amazonq/chat/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonqFeatureDev/FeatureDevSessionContextTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,40 @@ class FeatureDevSessionContextTest : FeatureDevTestBase(HeavyJavaCodeInsightTest
161161

162162
assertEquals(zippedFiles, expectedFiles)
163163
}
164+
165+
@Test
166+
fun testConvertGitIgnorePatternToRegex() {
167+
val sampleGitIgnorePatterns = listOf(".*", "build/", "*.txt", "*.png")
168+
val sampleFileNames = listOf(
169+
".gitignore/",
170+
".env/",
171+
"file.txt/",
172+
".git/config/",
173+
"src/file.txt/",
174+
"build/",
175+
"build/output.jar/",
176+
"builds/",
177+
"mybuild/",
178+
"build.json/",
179+
"log.txt/",
180+
"file.txt.json/",
181+
"file.png/",
182+
"src/file.png/"
183+
)
184+
val expectedFilesToMatch = listOf(".gitignore/", ".env/", "file.txt/", ".git/config/", "build/", "build/output.jar/", "log.txt/", "file.png/", "src/file.png/")
185+
val matchedFiles = mutableListOf<String>()
186+
187+
val patterns = sampleGitIgnorePatterns.map { pattern -> featureDevSessionContext.convertGitIgnorePatternToRegex(pattern).toRegex() }
188+
189+
for (sampleFileName in sampleFileNames) {
190+
for (pattern in patterns) {
191+
if (pattern.matches(sampleFileName)) {
192+
matchedFiles.add(sampleFileName)
193+
break
194+
}
195+
}
196+
}
197+
198+
assertEquals(expectedFilesToMatch, matchedFiles)
199+
}
164200
}

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/FeatureDevSessionContext.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,17 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo
279279
}
280280

281281
// gitignore patterns are not regex, method update needed.
282-
private fun convertGitIgnorePatternToRegex(pattern: String): String = pattern
283-
.replace(".", "\\.")
284-
.replace("*", ".*")
285-
.let { if (it.endsWith("/")) "$it.*" else "$it/.*" } // Add a trailing /* to all patterns. (we add a trailing / to all files when matching)
282+
fun convertGitIgnorePatternToRegex(pattern: String): String {
283+
// Special case for ".*" to match only dotfiles
284+
if (pattern == ".*") {
285+
return "^\\..*/.*"
286+
}
287+
288+
return pattern
289+
.replace(".", "\\.")
290+
.replace("*", ".*")
291+
.let { if (it.endsWith("/")) "$it.*" else "$it/.*" } // Add a trailing /* to all patterns. (we add a trailing / to all files when matching)
292+
}
286293
var selectedSourceFolder: VirtualFile
287294
set(newRoot) {
288295
_selectedSourceFolder = newRoot

0 commit comments

Comments
 (0)