Skip to content

Commit db14fc8

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

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,37 @@ 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+
185+
val patterns = sampleGitIgnorePatterns.map { pattern -> featureDevSessionContext.convertGitIgnorePatternToRegex(pattern).toRegex() }
186+
187+
val matchedFiles = sampleFileNames.filter { fileName ->
188+
patterns.any { pattern ->
189+
pattern.matches(fileName)
190+
}
191+
}
192+
193+
val expectedFilesToMatch = listOf(".gitignore/", ".env/", "file.txt/", ".git/config/", "src/file.txt/", "build/", "build/output.jar/", "log.txt/", "file.png/", "src/file.png/")
194+
195+
assertEquals(expectedFilesToMatch, matchedFiles)
196+
}
164197
}

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)