mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-29 02:39:13 +00:00
Merge pull request #1285 from mathieustan/feat/cli-project-filter
Some checks are pending
CI / semgrep (push) Waiting to run
Tests / test (22) (push) Waiting to run
Tests / test (22.13.0) (push) Waiting to run
Tests / test-platforms (macos-latest) (push) Waiting to run
Tests / test-platforms (windows-latest) (push) Waiting to run
Tests / desktop (push) Waiting to run
Some checks are pending
CI / semgrep (push) Waiting to run
Tests / test (22) (push) Waiting to run
Tests / test (22.13.0) (push) Waiting to run
Tests / test-platforms (macos-latest) (push) Waiting to run
Tests / test-platforms (windows-latest) (push) Waiting to run
Tests / desktop (push) Waiting to run
fix(cli): honor --project/--exclude on every reporting command, and anchor absolute-path patterns
This commit is contained in:
commit
effa656ef2
16 changed files with 610 additions and 74 deletions
101
src/parser.ts
101
src/parser.ts
|
|
@ -1,3 +1,4 @@
|
|||
import { homedir } from 'node:os'
|
||||
import { existsSync } from 'fs'
|
||||
import { lstat, readFile, readdir, stat } from 'fs/promises'
|
||||
import { createHash } from 'crypto'
|
||||
|
|
@ -3170,6 +3171,10 @@ export function setInteractiveScanUI(active = true): void {
|
|||
interactiveScanUI = active
|
||||
}
|
||||
|
||||
export function isInteractiveScanUI(): boolean {
|
||||
return interactiveScanUI
|
||||
}
|
||||
|
||||
// Machine-readable scan progress for the desktop app's first-run splash. Plain
|
||||
// CLI/terminal usage is untouched: emission is gated on CODEBURN_PROGRESS=1,
|
||||
// which only the app's cold-start warmup spawn sets. Each event is one
|
||||
|
|
@ -4413,29 +4418,89 @@ export function setCachePutMeta(meta: { startMs: number; endMs: number; sig: str
|
|||
putMeta = meta
|
||||
}
|
||||
|
||||
export function isRootedProjectPattern(pattern: string): boolean {
|
||||
const raw = pattern.trim().replace(/\\/g, '/')
|
||||
return raw.startsWith('/') || raw === '~' || raw.startsWith('~/') || /^[a-zA-Z]:\//.test(raw)
|
||||
}
|
||||
|
||||
/// A quoted "~/proj" reaches us unexpanded, and so does one typed into a field
|
||||
/// with no shell behind it. Left as a loose word it would match nothing and say
|
||||
/// nothing, since no stored path contains a tilde.
|
||||
function expandTilde(pattern: string): string {
|
||||
const raw = pattern.trim().replace(/\\/g, '/')
|
||||
if (raw !== '~' && !raw.startsWith('~/')) return raw
|
||||
return homedir().replace(/\\/g, '/') + raw.slice(1)
|
||||
}
|
||||
|
||||
/// A pattern is normalized once and matched many times: the day cache runs the
|
||||
/// filter for every project of every day, and again per provider slice.
|
||||
type CompiledPattern = { rooted: true; anchor: string | null } | { rooted: false; needle: string }
|
||||
|
||||
export type ProjectFilterTarget = { project: string; projectPath?: string }
|
||||
|
||||
function compile(patterns: readonly string[]): CompiledPattern[] {
|
||||
return patterns.map(pattern => isRootedProjectPattern(pattern)
|
||||
? { rooted: true as const, anchor: normalizeAbsProjectPathKey(expandTilde(pattern)) }
|
||||
: { rooted: false as const, needle: pattern.toLowerCase() })
|
||||
}
|
||||
|
||||
/// An absolute path names ONE project, so it anchors on a segment boundary (the
|
||||
/// isProxiedPath rule): "/a/proj" takes "/a/proj/sub" but not "/a/proj-ui-kit".
|
||||
/// Both sides key through normalizeAbsProjectPathKey (Windows casefolds, POSIX
|
||||
/// does not, #1260), and rootedness alone picks the branch, so "/" names none.
|
||||
function hit(entry: ProjectFilterTarget, pattern: CompiledPattern, key: string | null): boolean {
|
||||
if (pattern.rooted) {
|
||||
const anchor = pattern.anchor
|
||||
return anchor !== null && key !== null && (key === anchor || key.startsWith(anchor + '/'))
|
||||
}
|
||||
return entry.project.toLowerCase().includes(pattern.needle)
|
||||
|| (entry.projectPath ?? '').toLowerCase().includes(pattern.needle)
|
||||
}
|
||||
|
||||
/// The filter as one closure: patterns are compiled here, so the caller can
|
||||
/// hold it across a loop, and include-then-exclude is composed in one place for
|
||||
/// the live parse and the day cache alike.
|
||||
export function makeProjectFilter(
|
||||
include?: readonly string[],
|
||||
exclude?: readonly string[],
|
||||
): (entry: ProjectFilterTarget) => boolean {
|
||||
const inc = compile(include ?? [])
|
||||
const exc = compile(exclude ?? [])
|
||||
// The key costs a trim, a global replace and three regex passes. The day cache
|
||||
// runs this per project, per day, per provider slice, so it is only paid when
|
||||
// some pattern is rooted and can actually read it.
|
||||
const needsKey = inc.some(p => p.rooted) || exc.some(p => p.rooted)
|
||||
return entry => {
|
||||
const key = needsKey ? normalizeAbsProjectPathKey(entry.projectPath ?? '') : null
|
||||
if (inc.length > 0 && !inc.some(pattern => hit(entry, pattern, key))) return false
|
||||
if (exc.length > 0 && exc.some(pattern => hit(entry, pattern, key))) return false
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
export function matchesProjectPattern(project: ProjectFilterTarget, pattern: string): boolean {
|
||||
return makeProjectFilter([pattern])(project)
|
||||
}
|
||||
|
||||
/// Which rooted patterns name no project here? Each is tried on its own against
|
||||
/// the UNFILTERED list: judged inside filterProjectsByName instead, an earlier
|
||||
/// --project would mask a later one that selects the same project, and an
|
||||
/// --exclude would be tested against what --project had already removed.
|
||||
export function unmatchedRootedPatterns(projects: readonly ProjectFilterTarget[], patterns: readonly string[]): string[] {
|
||||
return patterns.filter(pattern => {
|
||||
if (!isRootedProjectPattern(pattern)) return false
|
||||
return !projects.some(entry => matchesProjectPattern(entry, pattern))
|
||||
})
|
||||
}
|
||||
|
||||
export function filterProjectsByName(
|
||||
projects: ProjectSummary[],
|
||||
include?: string[],
|
||||
exclude?: string[],
|
||||
): ProjectSummary[] {
|
||||
let result = projects
|
||||
if (include && include.length > 0) {
|
||||
const patterns = include.map(s => s.toLowerCase())
|
||||
result = result.filter(p => {
|
||||
const name = p.project.toLowerCase()
|
||||
const path = p.projectPath.toLowerCase()
|
||||
return patterns.some(pat => name.includes(pat) || path.includes(pat))
|
||||
})
|
||||
}
|
||||
if (exclude && exclude.length > 0) {
|
||||
const patterns = exclude.map(s => s.toLowerCase())
|
||||
result = result.filter(p => {
|
||||
const name = p.project.toLowerCase()
|
||||
const path = p.projectPath.toLowerCase()
|
||||
return !patterns.some(pat => name.includes(pat) || path.includes(pat))
|
||||
})
|
||||
}
|
||||
return result
|
||||
if ((include?.length ?? 0) === 0 && (exclude?.length ?? 0) === 0) return projects
|
||||
const matches = makeProjectFilter(include, exclude)
|
||||
return projects.filter(matches)
|
||||
}
|
||||
|
||||
function turnDayString(turn: ClassifiedTurn): string | null {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue