* List files using git ls-files
({
dir,
matchDirs,
}: {
dir: string
matchDirs: boolean
})
| 167 | * List files using git ls-files |
| 168 | */ |
| 169 | async function listFilesWithGit({ |
| 170 | dir, |
| 171 | matchDirs, |
| 172 | }: { |
| 173 | dir: string |
| 174 | matchDirs: boolean |
| 175 | }): Promise<string[]> { |
| 176 | // Get relative path from git root |
| 177 | const relativePath = await getGitRelativePath(dir) |
| 178 | const gitRoot = await getGitRoot(dir) |
| 179 | if (!gitRoot) return [] |
| 180 | |
| 181 | // List tracked files |
| 182 | const result = await execCmd("git", ["ls-files"], gitRoot) |
| 183 | if (!result.success) return [] |
| 184 | |
| 185 | let files = result.stdout |
| 186 | .split("\n") |
| 187 | .map((f) => f.trim()) |
| 188 | .filter((f) => f.length > 0) |
| 189 | |
| 190 | // Filter to only files in the current subdirectory |
| 191 | if (relativePath) { |
| 192 | const prefix = relativePath + "/" |
| 193 | files = files.filter((f) => f.startsWith(prefix)).map((f) => f.slice(prefix.length)) |
| 194 | } |
| 195 | |
| 196 | if (matchDirs) { |
| 197 | // Extract unique directory paths |
| 198 | const dirs = new Set<string>() |
| 199 | for (const file of files) { |
| 200 | let dir = path.dirname(file) |
| 201 | while (dir && dir !== ".") { |
| 202 | dirs.add(dir) |
| 203 | dir = path.dirname(dir) |
| 204 | } |
| 205 | } |
| 206 | return Array.from(dirs).sort() |
| 207 | } |
| 208 | |
| 209 | return files |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Check if ripgrep is available in PATH |
no test coverage detected