Skip to content

Commit 9ef9cf7

Browse files
Sync public snapshot from freebuff-private
1 parent 7f68a98 commit 9ef9cf7

7 files changed

Lines changed: 865 additions & 34 deletions

File tree

‎common/src/project-file-tree.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
addIgnoreFileContents,
1212
isIgnoredByIgnoreChain,
1313
PROJECT_IGNORE_FILES,
14+
testChain,
1415
} from './util/project-ignore'
1516

1617
import type { CodebuffFileSystem } from './types/filesystem'
@@ -249,6 +250,71 @@ export async function parseGitignore(params: {
249250
return parseGitignoreWithMode({ ...params, throwOnReadError: false })
250251
}
251252

253+
/**
254+
* `getProjectFileTree`'s ignore decision for a flat list of files that another
255+
* lister found (the SDK's ripgrep listing), built from the same rules in the
256+
* same order: the defaults, then each directory's ignore files from the root
257+
* down. `ignoreFileDirs` names every directory (POSIX, relative, `''` for the
258+
* root) holding one of PROJECT_IGNORE_FILES; any other directory adds no rules.
259+
* Returns whether a file is ignored.
260+
*/
261+
export async function createProjectIgnoreFilter(params: {
262+
projectRoot: string
263+
fs: CodebuffFileSystem
264+
ignoreFileDirs: Iterable<string>
265+
}): Promise<(relativeFilePath: string) => boolean> {
266+
const { projectRoot, fs } = params
267+
const rulesByDir = new Map<string, ignore.Ignore>()
268+
await Promise.all(
269+
[...new Set(params.ignoreFileDirs)].map(async (dir) => {
270+
const fullDirPath = path.join(projectRoot, dir)
271+
rulesByDir.set(dir, await parseGitignore({ fullDirPath, fs }))
272+
}),
273+
)
274+
275+
const chainByDir = new Map<string, DirIgnore[]>()
276+
const chainFor = (dir: string): DirIgnore[] => {
277+
const cached = chainByDir.get(dir)
278+
if (cached) return cached
279+
const inherited =
280+
dir === ''
281+
? [{ base: '', ig: createDefaultIgnore() }]
282+
: chainFor(parentDir(dir))
283+
const own = rulesByDir.get(dir)
284+
const chain = own ? [...inherited, { base: dir, ig: own }] : inherited
285+
chainByDir.set(dir, chain)
286+
return chain
287+
}
288+
289+
// What the walk decides for a directory, decided once instead of once per
290+
// file under it. The walk prunes it when its parent's chain ignores either
291+
// its bare name, tested before it descends, or its `dir/` form, tested for
292+
// each file inside; a `!` that matches only one form does not save it.
293+
const dirIgnored = new Map<string, boolean>()
294+
const isDirIgnored = (dir: string): boolean => {
295+
if (dir === '') return false
296+
const cached = dirIgnored.get(dir)
297+
if (cached !== undefined) return cached
298+
const parent = parentDir(dir)
299+
const chain = chainFor(parent)
300+
const ignored =
301+
isDirIgnored(parent) ||
302+
testChain(chain, dir) ||
303+
testChain(chain, `${dir}/`)
304+
dirIgnored.set(dir, ignored)
305+
return ignored
306+
}
307+
308+
return (relativeFilePath) => {
309+
const dir = parentDir(relativeFilePath)
310+
return isDirIgnored(dir) || testChain(chainFor(dir), relativeFilePath)
311+
}
312+
}
313+
314+
function parentDir(posixPath: string): string {
315+
return posixPath.slice(0, Math.max(posixPath.lastIndexOf('/'), 0))
316+
}
317+
252318
export function getAllFilePaths(
253319
nodes: FileTreeNode[],
254320
basePath: string = '',

‎common/src/util/project-ignore.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ function normalizeRelativePath(filePath: string): string | null {
2222
return segments.join('/') || null
2323
}
2424

25-
function testChain(chain: DirIgnore[], candidate: string): boolean {
25+
/**
26+
* Whether the chain's last matching rule ignores exactly `candidate`, without
27+
* looking at its parent directories (`isIgnoredByIgnoreChain` does both).
28+
*/
29+
export function testChain(chain: DirIgnore[], candidate: string): boolean {
2630
let ignored = false
2731
for (const { base, ig } of chain) {
2832
if (base && !candidate.startsWith(`${base}/`)) continue

‎sdk/src/__tests__/glob.test.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import { mkdirSync, mkdtempSync, promises, rmSync, writeFileSync } from 'fs'
2+
import os from 'os'
3+
import path from 'path'
4+
15
import { DEFAULT_MAX_FILES } from '@codebuff/common/project-file-tree'
26
import { createMockFs } from '@codebuff/common/testing/mocks/filesystem'
3-
import { describe, expect, it } from 'bun:test'
7+
import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
48

59
import { glob } from '../tools/glob'
610

@@ -54,6 +58,57 @@ describe('glob', () => {
5458
expect(result.message).toContain(`${DEFAULT_MAX_FILES}-file limit`)
5559
})
5660

61+
describe('on the local disk, past that cap', () => {
62+
let root: string
63+
const kotlin = 'app/src/main/java/com/example/ui/Inventory.kt'
64+
const classFiles = DEFAULT_MAX_FILES + 50
65+
beforeAll(() => {
66+
root = mkdtempSync(path.join(os.tmpdir(), 'codebuff-glob-'))
67+
for (let i = 0; i < classFiles; i++) {
68+
const dir = path.join(root, 'app/build', `chunk${Math.floor(i / 500)}`)
69+
if (i % 500 === 0) mkdirSync(dir, { recursive: true })
70+
writeFileSync(path.join(dir, `f${i}.class`), '')
71+
}
72+
mkdirSync(path.join(root, path.dirname(kotlin)), { recursive: true })
73+
writeFileSync(path.join(root, kotlin), '')
74+
})
75+
afterAll(() => rmSync(root, { recursive: true, force: true }))
76+
77+
const globDisk = async (pattern: string, maxResults?: number) => {
78+
const [output] = await glob({
79+
pattern,
80+
projectPath: root,
81+
maxResults,
82+
fs: promises,
83+
useRipgrep: true,
84+
})
85+
return output.value as { count: number; files: string[]; message: string }
86+
}
87+
88+
it('finds the file the walk never reached', async () => {
89+
const result = await globDisk('**/*.kt')
90+
91+
expect(result).toMatchObject({ files: [kotlin], count: 1 })
92+
expect(result.message).not.toContain('limit')
93+
})
94+
95+
it('returns at most 10,000 paths when no maxResults is given', async () => {
96+
// A Desktop result over its transport limit fails the whole call.
97+
const result = await globDisk('**/*.class')
98+
99+
expect(result.count).toBe(classFiles)
100+
expect(result.files.length).toBe(10_000)
101+
expect(result.message).toContain('Showing the first 10000')
102+
})
103+
104+
it('returns past 10,000 paths when maxResults asks for them', async () => {
105+
const result = await globDisk('**/*.class', classFiles)
106+
107+
expect(result.files.length).toBe(classFiles)
108+
expect(result.message).not.toContain('Showing the first')
109+
})
110+
})
111+
57112
it('does not warn when the whole project was scanned', async () => {
58113
const fs = fsWithFiles([
59114
'app/src/main/java/com/example/ui/Inventory.kt',

0 commit comments

Comments
 (0)