Skip to content

Commit 666798a

Browse files
gandhi-21Gaurav Gandhirli
authored
fix(amazonq): update logic for converting gitignore pattern to regex (aws#5360)
* fix(amazonq): update logic for converting gitignore pattern to regex * fix(amazonq): update logic for converting gitignore pattern to regex --------- Co-authored-by: Gaurav Gandhi <gangaur@amazon.com> Co-authored-by: Richard Li <742829+rli@users.noreply.github.com>
1 parent 2eca5a7 commit 666798a

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

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

+45
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,49 @@ 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 =
194+
listOf(
195+
".gitignore/",
196+
".env/",
197+
"file.txt/",
198+
".git/config/",
199+
"src/file.txt/",
200+
"build/",
201+
"build/output.jar/",
202+
"log.txt/",
203+
"file.png/",
204+
"src/file.png/"
205+
)
206+
207+
assertEquals(expectedFilesToMatch, matchedFiles)
208+
}
164209
}

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

+11-4
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)