Skip to content

Commit 89545c2

Browse files
committed
Reverting the FeatureDevSessionContext pattern matching chnages.
1 parent 62f896d commit 89545c2

File tree

2 files changed

+8
-122
lines changed

2 files changed

+8
-122
lines changed

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

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import com.intellij.openapi.vfs.VirtualFile
55
import com.intellij.testFramework.RuleChain
6-
import org.junit.Assert.assertEquals
76
import org.junit.Assert.assertFalse
87
import org.junit.Assert.assertTrue
98
import org.junit.Before
@@ -118,72 +117,11 @@ class FeatureDevSessionContextTest : FeatureDevTestBase(HeavyJavaCodeInsightTest
118117
"gradlew.bat",
119118
"README.md",
120119
"gradle/wrapper/gradle-wrapper.properties",
121-
"builder/GetTestBuilder.java"
120+
"builder/GetTestBuilder.java",
121+
"settings.gradle",
122+
"build.gradle",
122123
)
123124

124125
assertTrue(zippedFiles == expectedFiles)
125126
}
126-
127-
@Test
128-
fun `test basic pattern conversion`() {
129-
val input = "*.txt"
130-
val expected = "(?:^|.*/?)[^/]*[^/]\\.txt(?:/.*)?\$"
131-
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
132-
}
133-
134-
@Test
135-
fun `test pattern with special characters`() {
136-
val input = "test[abc].txt"
137-
val expected = "(?:^|.*/?)test\\[abc\\]\\.txt(?:/.*)?$"
138-
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
139-
}
140-
141-
@Test
142-
fun `test pattern with double asterisk`() {
143-
val input = "**/build"
144-
val expected = "(?:^|.*/?).[^/]*[^/][^/]/build(?:/.*)?\$"
145-
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
146-
}
147-
148-
@Test
149-
fun `test pattern starting with slash`() {
150-
val input = "/root/file.txt"
151-
val expected = "^root/file\\.txt(?:/.*)?$"
152-
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
153-
}
154-
155-
@Test
156-
fun `test pattern ending with slash`() {
157-
val input = "build/"
158-
val expected = "(?:^|.*/?)build/.*"
159-
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
160-
}
161-
162-
@Test
163-
fun `test pattern with question mark`() {
164-
val input = "file?.txt"
165-
val expected = "(?:^|.*/?)file[^/]\\.txt(?:/.*)?$"
166-
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
167-
}
168-
169-
@Test
170-
fun `test complex pattern with multiple special characters`() {
171-
val input = "**/test-[0-9]*.{java,kt}"
172-
val expected = "(?:^|.*/?).[^/]*[^/][^/]/test-\\[0-9\\][^/]*[^/]\\.\\{java\\,kt\\}(?:/.*)?\$"
173-
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
174-
}
175-
176-
@Test
177-
fun `test empty pattern`() {
178-
val input = ""
179-
val expected = "(?:^|.*/?)(?:/.*)?$"
180-
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
181-
}
182-
183-
@Test
184-
fun `test pattern with all special regex characters`() {
185-
val input = ".$+()[]{}^|"
186-
val expected = "(?:^|.*/?)\\.\\\$\\+\\(\\)\\[\\]\\{\\}\\^\\|(?:/.*)?$"
187-
assertEquals(expected, featureDevSessionContext.convertGitIgnorePatternToRegex(input))
188-
}
189127
}

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

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import kotlinx.coroutines.flow.channelFlow
1515
import kotlinx.coroutines.launch
1616
import kotlinx.coroutines.runBlocking
1717
import kotlinx.coroutines.withContext
18-
import kotlinx.coroutines.withTimeout
1918
import org.apache.commons.codec.digest.DigestUtils
2019
import org.apache.commons.io.FileUtils
2120
import software.aws.toolkits.jetbrains.core.coroutines.getCoroutineBgContext
@@ -137,13 +136,7 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo
137136
// entries against them by adding a trailing /.
138137
// TODO: Add unit tests for gitignore matching
139138
val relative = if (path.startsWith(projectRootPath.toString())) Paths.get(path).relativeTo(projectRootPath) else path
140-
async {
141-
try {
142-
withTimeout(REGEX_TIMEOUT_MS) { pattern.matches("$relative/") }
143-
} catch (e: Exception) {
144-
false
145-
}
146-
}
139+
async { pattern.matches("$relative/") }
147140
}
148141
}
149142

@@ -247,7 +240,7 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo
247240
tempFilePath
248241
}
249242

250-
private fun parseGitIgnore(): Set<String?> {
243+
private fun parseGitIgnore(): Set<String> {
251244
if (!gitIgnoreFile.exists()) {
252245
return emptySet()
253246
}
@@ -259,59 +252,14 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo
259252
}
260253

261254
// gitignore patterns are not regex, method update needed.
262-
fun convertGitIgnorePatternToRegex(pattern: String): String? {
263-
// Skip invalid patterns for length check
264-
if (pattern.length > MAX_PATTERN_LENGTH) {
265-
return null
266-
}
267-
268-
// Escape special regex characters except * and ?
269-
var result = pattern
270-
.replace(".", "\\.")
271-
.replace("+", "\\+")
272-
.replace("(", "\\(")
273-
.replace(")", "\\)")
274-
.replace("[", "\\[")
275-
.replace("]", "\\]")
276-
.replace("{", "\\{")
277-
.replace("}", "\\}")
278-
.replace(",", "\\,")
279-
.replace("^", "\\^")
280-
.replace("$", "\\$")
281-
.replace("|", "\\|")
282-
283-
// Convert gitignore glob patterns to regex
284-
result = result
285-
.replace("**", ".*?") // Match any directory depth
286-
.replace("*", "[^/]*?") // Match any character except path separator
287-
.replace("?", "[^/]") // Match single character except path separator
288-
289-
// Handle start of pattern
290-
result = if (result.startsWith("/")) {
291-
"^${result.substring(1)}"
292-
} else {
293-
"(?:^|.*/?)$result"
294-
}
295-
296-
// Handle end of pattern
297-
result = if (result.endsWith("/")) {
298-
"$result.*"
299-
} else {
300-
"$result(?:/.*)?$"
301-
}
302-
303-
return result
304-
}
255+
fun convertGitIgnorePatternToRegex(pattern: String): String = pattern.replace(".", "\\.")
256+
.replace("*", ".*")
257+
.let { if (it.endsWith("/")) "$it.*" else "$it/.*" } // Add a trailing /* to all patterns. (we add a trailing / to all files when matching)
305258
var selectedSourceFolder: VirtualFile
306259
set(newRoot) {
307260
_selectedSourceFolder = newRoot
308261
}
309262
get() = _selectedSourceFolder
310-
311-
companion object {
312-
private const val MAX_PATTERN_LENGTH = 256 // Maximum allowed pattern length
313-
private const val REGEX_TIMEOUT_MS = 100L // Timeout for regex operations in milliseconds
314-
}
315263
}
316264

317265
data class ZipCreationResult(val payload: File, val checksum: String, val contentLength: Long)

0 commit comments

Comments
 (0)