@proagentstore/cli 0.4.144 → 0.4.145
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { globMatches, isGlob } from "./path-glob.js";
|
|
1
2
|
import { execFileSync } from "node:child_process";
|
|
2
3
|
import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
3
4
|
import { relative, resolve, sep } from "node:path";
|
|
@@ -331,9 +332,12 @@ export function repoSearch(workDir, opts) {
|
|
|
331
332
|
const lines = out.split("\n").filter((l) => l !== "");
|
|
332
333
|
if (mode === "path") {
|
|
333
334
|
// The pattern never reaches git here — the filter is a plain case-insensitive substring
|
|
334
|
-
// over the path, which is what "find the file called X" actually means.
|
|
335
|
-
|
|
336
|
-
|
|
335
|
+
// over the path, which is what "find the file called X" actually means. A `glob` (#1070) is
|
|
336
|
+
// matched instead, over the WHOLE list, so `total` counts real matches, not the literal's.
|
|
337
|
+
const glob = (opts.glob ?? "").trim();
|
|
338
|
+
const useGlob = glob !== "" && isGlob(glob);
|
|
339
|
+
const all = lines.filter((p) => (useGlob ? globMatches(glob, p) : p.toLowerCase().includes(pattern.toLowerCase()))).map((p) => ({ path: p }));
|
|
340
|
+
return { mode, pattern, matches: all.slice(0, limit), shown: Math.min(all.length, limit), total: all.length, truncated: all.length > limit, ...(useGlob ? { glob: true } : {}) };
|
|
337
341
|
}
|
|
338
342
|
const all = lines.map(parseGrepLine).filter((m) => m !== null);
|
|
339
343
|
const perFile = new Map();
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Glob patterns over repo-relative paths, for `repo_find` (#1070).
|
|
3
|
+
*
|
|
4
|
+
* `repo_find` matched its pattern as a plain substring, so `*.test.js` returned NOTHING — no path
|
|
5
|
+
* contains an asterisk — and an agent concluded a repository with ~90 test files had none. Silent false
|
|
6
|
+
* negatives are the failure; this is the matcher that ends them.
|
|
7
|
+
*
|
|
8
|
+
* Semantics (gitignore/ripgrep-like, case-insensitive like the substring search it extends):
|
|
9
|
+
* `*` any run of characters within ONE path segment (never a `/`)
|
|
10
|
+
* `**` any run across segments; `**` followed by `/` also matches zero folders
|
|
11
|
+
* `?` one character, not `/`
|
|
12
|
+
* `[ab]` `[a-z]` `[!ab]` a character class (`!` negates), never `/`
|
|
13
|
+
* A pattern WITHOUT a `/` matches any single segment — a file name or a folder name at any depth, a
|
|
14
|
+
* folder matching taking everything beneath it — so `*.test.js` and `*e2e*` mean what they mean in a
|
|
15
|
+
* shell or `rg -g`. A pattern WITH a `/` is anchored at the repository root (`src/*.ts`,
|
|
16
|
+
* `**\/handlers/*.js`); a leading `/` or `./` is accepted and means the same.
|
|
17
|
+
*
|
|
18
|
+
* DUPLICATED in `workers/api/src/lib/path-glob.ts`: the cloud filters with it when the runner is too old
|
|
19
|
+
* to (an old runner ignores `glob`). `workers/api/src/lib/path-glob.parity.test.ts` holds the two equal.
|
|
20
|
+
*/
|
|
21
|
+
/** Does this pattern use glob syntax? A pattern without any is the plain substring search, unchanged. */
|
|
22
|
+
export function isGlob(pattern) {
|
|
23
|
+
return /[*?[]/.test(pattern);
|
|
24
|
+
}
|
|
25
|
+
function toRegExp(glob) {
|
|
26
|
+
let re = "";
|
|
27
|
+
for (let i = 0; i < glob.length; i++) {
|
|
28
|
+
const c = glob[i];
|
|
29
|
+
if (c === "*") {
|
|
30
|
+
if (glob[i + 1] === "*") {
|
|
31
|
+
i++;
|
|
32
|
+
if (glob[i + 1] === "/") {
|
|
33
|
+
i++;
|
|
34
|
+
re += "(?:.*/)?";
|
|
35
|
+
}
|
|
36
|
+
else
|
|
37
|
+
re += ".*";
|
|
38
|
+
}
|
|
39
|
+
else
|
|
40
|
+
re += "[^/]*";
|
|
41
|
+
}
|
|
42
|
+
else if (c === "?")
|
|
43
|
+
re += "[^/]";
|
|
44
|
+
else if (c === "[") {
|
|
45
|
+
const end = glob.indexOf("]", i + 2);
|
|
46
|
+
if (end === -1) {
|
|
47
|
+
re += "\\[";
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
let body = glob.slice(i + 1, end).replace(/[\\^\]]/g, "\\$&");
|
|
51
|
+
const negate = body.startsWith("!");
|
|
52
|
+
if (negate)
|
|
53
|
+
body = body.slice(1);
|
|
54
|
+
re += `(?!/)[${negate ? "^" : ""}${body}]`;
|
|
55
|
+
i = end;
|
|
56
|
+
}
|
|
57
|
+
else
|
|
58
|
+
re += c.replace(/[.+^${}()|\\\]/]/g, "\\$&");
|
|
59
|
+
}
|
|
60
|
+
return new RegExp(`^${re}$`, "i");
|
|
61
|
+
}
|
|
62
|
+
/** Does `path` (repo-relative, `/`-separated) match `glob`? */
|
|
63
|
+
export function globMatches(glob, path) {
|
|
64
|
+
const g = glob.trim().replace(/^\.?\/+/, "").replace(/\/+$/, "");
|
|
65
|
+
if (!g)
|
|
66
|
+
return false;
|
|
67
|
+
const re = toRegExp(g);
|
|
68
|
+
if (g.includes("/"))
|
|
69
|
+
return re.test(path);
|
|
70
|
+
return path.split("/").some((segment) => re.test(segment));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The longest run of literal text a match MUST contain — what an old runner's substring search is given,
|
|
74
|
+
* so the glob filter then runs over a superset that cannot have dropped a match. `""` when there is none
|
|
75
|
+
* (`*`, `**`, `???`): nothing literal to search by.
|
|
76
|
+
*/
|
|
77
|
+
export function globLiteral(glob) {
|
|
78
|
+
// Normalised exactly as `globMatches` normalises, or `./src/*.ts` would ask for "./src", which no path contains.
|
|
79
|
+
const pieces = glob.trim().replace(/^\.?\/+/, "").split(/\[[^\]]*\]|[*?]+/);
|
|
80
|
+
const longest = pieces.reduce((a, b) => (b.length > a.length ? b : a), "");
|
|
81
|
+
return longest.replace(/^\/+|\/+$/g, "");
|
|
82
|
+
}
|