Skip to content

Commit d9ae817

Browse files
authored
Add debug logging and extract glob pattern helper module for repo-memory validation (#8242)
1 parent 2dd6883 commit d9ae817

File tree

4 files changed

+821
-17
lines changed

4 files changed

+821
-17
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// @ts-check
2+
3+
/**
4+
* Convert a glob pattern to a RegExp
5+
* @param {string} pattern - Glob pattern (e.g., "*.json", "metrics/**", "data/**\/*.csv")
6+
* @returns {RegExp} - Regular expression that matches the pattern
7+
*
8+
* Supports:
9+
* - * matches any characters except /
10+
* - ** matches any characters including /
11+
* - . is escaped to match literal dots
12+
* - \ is escaped properly
13+
*
14+
* @example
15+
* const regex = globPatternToRegex("*.json");
16+
* regex.test("file.json"); // true
17+
* regex.test("file.txt"); // false
18+
*
19+
* @example
20+
* const regex = globPatternToRegex("metrics/**");
21+
* regex.test("metrics/data.json"); // true
22+
* regex.test("metrics/daily/data.json"); // true
23+
*/
24+
function globPatternToRegex(pattern) {
25+
// Convert glob pattern to regex that supports directory wildcards
26+
// ** matches any path segment (including /)
27+
// * matches any characters except /
28+
let regexPattern = pattern
29+
.replace(/\\/g, "\\\\") // Escape backslashes
30+
.replace(/\./g, "\\.") // Escape dots
31+
.replace(/\*\*/g, "<!DOUBLESTAR>") // Temporarily replace **
32+
.replace(/\*/g, "[^/]*") // Single * matches non-slash chars
33+
.replace(/<!DOUBLESTAR>/g, ".*"); // ** matches everything including /
34+
return new RegExp(`^${regexPattern}$`);
35+
}
36+
37+
/**
38+
* Parse a space-separated list of glob patterns into RegExp objects
39+
* @param {string} fileGlobFilter - Space-separated glob patterns (e.g., "*.json *.jsonl *.csv *.md")
40+
* @returns {RegExp[]} - Array of regular expressions
41+
*
42+
* @example
43+
* const patterns = parseGlobPatterns("*.json *.jsonl");
44+
* patterns[0].test("file.json"); // true
45+
* patterns[1].test("file.jsonl"); // true
46+
*/
47+
function parseGlobPatterns(fileGlobFilter) {
48+
return fileGlobFilter.trim().split(/\s+/).filter(Boolean).map(globPatternToRegex);
49+
}
50+
51+
/**
52+
* Check if a file path matches any of the provided glob patterns
53+
* @param {string} filePath - File path to test (e.g., "data/file.json")
54+
* @param {string} fileGlobFilter - Space-separated glob patterns
55+
* @returns {boolean} - True if the file matches at least one pattern
56+
*
57+
* @example
58+
* matchesGlobPattern("file.json", "*.json *.jsonl"); // true
59+
* matchesGlobPattern("file.txt", "*.json *.jsonl"); // false
60+
*/
61+
function matchesGlobPattern(filePath, fileGlobFilter) {
62+
const patterns = parseGlobPatterns(fileGlobFilter);
63+
return patterns.some(pattern => pattern.test(filePath));
64+
}
65+
66+
module.exports = {
67+
globPatternToRegex,
68+
parseGlobPatterns,
69+
matchesGlobPattern,
70+
};

0 commit comments

Comments
 (0)