Skip to content

Commit 8e36b25

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

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
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/", "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: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo
6262
)
6363

6464
private val additionalGitIgnoreBinaryFilesRules = setOf(
65-
"*.zip",
66-
"*.bin",
67-
"*.png",
68-
"*.jpg",
69-
"*.svg",
70-
"*.pyc",
65+
"**/*.zip",
66+
"**/*.bin",
67+
"**/*.png",
68+
"**/*.jpg",
69+
"**/*.svg",
70+
"**/*.pyc",
7171
"license.txt",
7272
"License.txt",
7373
"LICENSE.txt",
@@ -279,10 +279,25 @@ 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.equals(".*")) {
285+
return "^\\.[^/]+/?$"
286+
}
287+
288+
return pattern
289+
.replace(".", "\\.")
290+
.replace("**/", "(.*/)?")
291+
.replace("**", ".*")
292+
.replace("*", "[^/]*")
293+
.let {
294+
if (it.endsWith("/")) {
295+
"^$it.*$"
296+
} else {
297+
"^$it/.*$"
298+
}
299+
}
300+
}
286301
var selectedSourceFolder: VirtualFile
287302
set(newRoot) {
288303
_selectedSourceFolder = newRoot

0 commit comments

Comments
 (0)