Skip to content

fix(amazonq): update logic for converting gitignore pattern to regex #5360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,49 @@ class FeatureDevSessionContextTest : FeatureDevTestBase(HeavyJavaCodeInsightTest

assertEquals(zippedFiles, expectedFiles)
}

@Test
fun testConvertGitIgnorePatternToRegex() {
val sampleGitIgnorePatterns = listOf(".*", "build/", "*.txt", "*.png")
val sampleFileNames = listOf(
".gitignore/",
".env/",
"file.txt/",
".git/config/",
"src/file.txt/",
"build/",
"build/output.jar/",
"builds/",
"mybuild/",
"build.json/",
"log.txt/",
"file.txt.json/",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why each file name ends with '/' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"file.png/",
"src/file.png/"
)

val patterns = sampleGitIgnorePatterns.map { pattern -> featureDevSessionContext.convertGitIgnorePatternToRegex(pattern).toRegex() }

val matchedFiles = sampleFileNames.filter { fileName ->
patterns.any { pattern ->
pattern.matches(fileName)
}
}

val expectedFilesToMatch =
listOf(
".gitignore/",
".env/",
"file.txt/",
".git/config/",
"src/file.txt/",
"build/",
"build/output.jar/",
"log.txt/",
"file.png/",
"src/file.png/"
)

assertEquals(expectedFilesToMatch, matchedFiles)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,17 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo
}

// gitignore patterns are not regex, method update needed.
private fun convertGitIgnorePatternToRegex(pattern: String): String = pattern
.replace(".", "\\.")
.replace("*", ".*")
.let { if (it.endsWith("/")) "$it.*" else "$it/.*" } // Add a trailing /* to all patterns. (we add a trailing / to all files when matching)
fun convertGitIgnorePatternToRegex(pattern: String): String {
// Special case for ".*" to match only dotfiles
if (pattern == ".*") {
return "^\\..*/.*"
}

return pattern
.replace(".", "\\.")
.replace("*", ".*")
.let { if (it.endsWith("/")) "$it.*" else "$it/.*" } // Add a trailing /* to all patterns. (we add a trailing / to all files when matching)
}
var selectedSourceFolder: VirtualFile
set(newRoot) {
_selectedSourceFolder = newRoot
Expand Down
Loading