mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-19 23:05:21 +00:00
Match worktree includes without writing scratch files and search only the folders patterns name
Match ignored entries with check-ignore --no-index instead of recreating them on disk, search inside an ignored folder only as deep as the pattern names it, copy each file once, and note in the docs that **/ patterns search every ignored folder.
This commit is contained in:
@@ -61,6 +61,28 @@ describe("copyWorktreeIncludes", () => {
|
||||
expect(await copied()).toEqual([".env", "node_modules/pkg/.env"]);
|
||||
});
|
||||
|
||||
it("copies a file in an untracked directory once when a **/ pattern also searches it", async () => {
|
||||
await writeSource({
|
||||
".gitignore": ".env\n",
|
||||
".worktreeinclude": "**/.env\nnew-package/.env\n",
|
||||
"new-package/.env": "SECRET=new",
|
||||
});
|
||||
|
||||
const result = await copyWorktreeIncludes(worktree, source);
|
||||
expect(result).toEqual({ kind: "copied", copied: ["new-package/.env"], skipped: [] });
|
||||
});
|
||||
|
||||
it("copies a path a pattern names inside an ignored directory", async () => {
|
||||
await writeSource({
|
||||
".gitignore": "node_modules/\n",
|
||||
".worktreeinclude": "node_modules/pkg/.env\n",
|
||||
"node_modules/pkg/.env": "SECRET=package",
|
||||
"node_modules/other/.env": "SECRET=other",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual(["node_modules/pkg/.env"]);
|
||||
});
|
||||
|
||||
it("copies an ignored directory that a pattern names", async () => {
|
||||
await writeSource({
|
||||
".gitignore": ".secrets/\nbuild/\n",
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Copyright (c) 2026 Carlos Precioso.
|
||||
import { execFile } from "node:child_process";
|
||||
import { constants, existsSync } from "node:fs";
|
||||
import { copyFile, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { copyFile, mkdir, mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
@@ -65,8 +65,9 @@ export async function copyWorktreeIncludes(worktree: string, source: string): Pr
|
||||
}
|
||||
|
||||
// Git lists a directory it ignores as a whole, such as node_modules/, as one entry.
|
||||
// Only directories that a pattern matches or names a path inside are searched, so
|
||||
// creating a worktree does not walk every file in large ignored trees.
|
||||
// Only directories that a pattern matches or names a path inside are searched, and
|
||||
// only as deep as the pattern names, so creating a worktree does not walk every file
|
||||
// in large ignored trees.
|
||||
async function findIncludedFiles(source: string): Promise<string[]> {
|
||||
const ignored = splitNul(
|
||||
await git(source, [
|
||||
@@ -81,13 +82,20 @@ async function findIncludedFiles(source: string): Promise<string[]> {
|
||||
if (ignored.length === 0) return [];
|
||||
|
||||
const patterns = parsePatterns(await readFile(join(source, INCLUDE_FILE), "utf8"));
|
||||
const matched = await matchIncludePatterns(source, ignored);
|
||||
const files = ignored.filter((entry) => !isDirectory(entry) && matched(entry));
|
||||
const searched = ignored.filter(
|
||||
(entry) =>
|
||||
isDirectory(entry) && (matched(entry) || patterns.some((p) => reachesInside(p, entry))),
|
||||
);
|
||||
if (searched.length === 0) return files;
|
||||
const matched = new Set(await matchIncludePatterns(source, ignored));
|
||||
const files = ignored.filter((entry) => !isDirectory(entry) && matched.has(entry));
|
||||
const searched = new Set<string>();
|
||||
for (const directory of ignored.filter(isDirectory)) {
|
||||
if (matched.has(directory)) {
|
||||
searched.add(directory);
|
||||
continue;
|
||||
}
|
||||
for (const pattern of patterns) {
|
||||
const inside = searchedPathInside(pattern, directory);
|
||||
if (inside !== null) searched.add(inside);
|
||||
}
|
||||
}
|
||||
if (searched.size === 0) return files;
|
||||
|
||||
const inside = splitNul(
|
||||
await git(source, [
|
||||
@@ -97,41 +105,33 @@ async function findIncludedFiles(source: string): Promise<string[]> {
|
||||
`--exclude-from=${INCLUDE_FILE}`,
|
||||
"-z",
|
||||
"--",
|
||||
...searched.map((directory) => `:(literal)${directory}`),
|
||||
...[...searched].map((path) => `:(literal)${path}`),
|
||||
]),
|
||||
);
|
||||
return [...files, ...inside];
|
||||
return [...new Set([...files, ...inside])];
|
||||
}
|
||||
|
||||
// Match entries against .worktreeinclude alone by recreating them, empty, in a scratch
|
||||
// repository where no other ignore rules apply.
|
||||
async function matchIncludePatterns(
|
||||
source: string,
|
||||
entries: string[],
|
||||
): Promise<(entry: string) => boolean> {
|
||||
// Match entries against .worktreeinclude alone, in a scratch repository where no other
|
||||
// ignore rules apply. Directory entries keep their trailing slash, so directory
|
||||
// patterns match them without anything on disk.
|
||||
async function matchIncludePatterns(source: string, entries: string[]): Promise<string[]> {
|
||||
const scratch = await mkdtemp(join(tmpdir(), "paseo-worktree-include-"));
|
||||
try {
|
||||
await git(scratch, ["init", "-q"]);
|
||||
for (const entry of entries) {
|
||||
if (isDirectory(entry)) {
|
||||
await mkdir(join(scratch, entry), { recursive: true });
|
||||
} else {
|
||||
await mkdir(dirname(join(scratch, entry)), { recursive: true });
|
||||
await writeFile(join(scratch, entry), "");
|
||||
}
|
||||
}
|
||||
const matched = splitNul(
|
||||
await git(scratch, [
|
||||
"ls-files",
|
||||
"--others",
|
||||
"--ignored",
|
||||
`--exclude-from=${join(source, INCLUDE_FILE)}`,
|
||||
"--directory",
|
||||
"-z",
|
||||
]),
|
||||
return splitNul(
|
||||
await gitAllowExit1(
|
||||
scratch,
|
||||
[
|
||||
"-c",
|
||||
`core.excludesFile=${join(source, INCLUDE_FILE)}`,
|
||||
"check-ignore",
|
||||
"--no-index",
|
||||
"--stdin",
|
||||
"-z",
|
||||
],
|
||||
entries.join("\0") + "\0",
|
||||
),
|
||||
);
|
||||
return (entry) =>
|
||||
matched.some((match) => match === entry || (isDirectory(match) && entry.startsWith(match)));
|
||||
} finally {
|
||||
await rm(scratch, { recursive: true, force: true });
|
||||
}
|
||||
@@ -146,9 +146,13 @@ function parsePatterns(contents: string): string[] {
|
||||
|
||||
// A pattern with a slash before its end is anchored to the checkout root, so it can
|
||||
// name a path inside `directory` when its literal start and the directory overlap.
|
||||
function reachesInside(pattern: string, directory: string): boolean {
|
||||
// Returns the narrowest path to search: the folder the pattern names inside
|
||||
// `directory`, or `directory` itself.
|
||||
function searchedPathInside(pattern: string, directory: string): string | null {
|
||||
const path = pattern.replace(/^\//, "");
|
||||
if (!path.replace(/\/$/, "").includes("/")) return false;
|
||||
if (!path.replace(/\/$/, "").includes("/")) return null;
|
||||
const literal = path.slice(0, path.search(/[*?[\\]|$/));
|
||||
return literal.startsWith(directory) || directory.startsWith(literal);
|
||||
if (!literal.startsWith(directory) && !directory.startsWith(literal)) return null;
|
||||
const literalFolder = literal.slice(0, literal.lastIndexOf("/") + 1);
|
||||
return literalFolder.length > directory.length ? literalFolder : directory;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ config/*.local.json
|
||||
|
||||
A pattern only selects files that Git already ignores. Adding a path here does not ignore it, so also configure `.gitignore` when needed. Tracked files and untracked files that Git does not ignore are excluded.
|
||||
|
||||
Paseo does not search inside a directory that Git ignores as a whole, such as `node_modules/`, unless a pattern names that directory or a path inside it. A bare name like `.env` matches `.env` and `apps/web/.env`, but not `node_modules/pkg/.env`. To copy from inside an ignored directory, name it in the pattern, as in `node_modules/pkg/.env`, `.secrets/`, or `**/.env`.
|
||||
Paseo does not search inside a directory that Git ignores as a whole, such as `node_modules/`, unless a pattern names that directory or a path inside it. A bare name like `.env` matches `.env` and `apps/web/.env`, but not `node_modules/pkg/.env`. To copy from inside an ignored directory, name it in the pattern, as in `node_modules/pkg/.env` or `.secrets/`. A pattern that starts with `**/`, such as `**/.env`, searches every ignored directory, including `node_modules/`, so name the directory instead when it is large.
|
||||
|
||||
## When files are copied
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ config/*.local.json
|
||||
|
||||
模式只会选择 Git 已忽略的文件。在此添加路径不会使 Git 忽略该文件,需要时还应配置 `.gitignore`。已跟踪的文件和未被 Git 忽略的未跟踪文件都不会被复制。
|
||||
|
||||
对于被 Git 整体忽略的目录(例如 `node_modules/`),除非某个模式指明了该目录或其中的路径,否则 Paseo 不会在其中查找。像 `.env` 这样的纯文件名会匹配 `.env` 和 `apps/web/.env`,但不会匹配 `node_modules/pkg/.env`。要从被忽略的目录中复制文件,请在模式中写出该目录,例如 `node_modules/pkg/.env`、`.secrets/` 或 `**/.env`。
|
||||
对于被 Git 整体忽略的目录(例如 `node_modules/`),除非某个模式指明了该目录或其中的路径,否则 Paseo 不会在其中查找。像 `.env` 这样的纯文件名会匹配 `.env` 和 `apps/web/.env`,但不会匹配 `node_modules/pkg/.env`。要从被忽略的目录中复制文件,请在模式中写出该目录,例如 `node_modules/pkg/.env` 或 `.secrets/`。以 `**/` 开头的模式(例如 `**/.env`)会在所有被忽略的目录中查找,包括 `node_modules/`,因此目录较大时请直接写出目录。
|
||||
|
||||
## 文件复制时机 {#when-files-are-copied}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user