mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 20:50:50 +00:00
fix(tooling): harden changed checks for sparse worktrees
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import {
|
||||
acquireLocalHeavyCheckLockSync,
|
||||
@@ -20,10 +22,105 @@ const OXLINT_PREPARE_SKIP_FLAGS = new Set([
|
||||
"--init",
|
||||
"--lsp",
|
||||
]);
|
||||
const OXLINT_VALUE_FLAGS = new Set([
|
||||
"--config",
|
||||
"--deny",
|
||||
"--env",
|
||||
"--format",
|
||||
"--globals",
|
||||
"--ignore-path",
|
||||
"--max-warnings",
|
||||
"--output-file",
|
||||
"--plugin",
|
||||
"--rules",
|
||||
"--tsconfig",
|
||||
"--warn",
|
||||
]);
|
||||
|
||||
export function shouldPrepareExtensionPackageBoundaryArtifacts(args) {
|
||||
return !args.some((arg) => OXLINT_PREPARE_SKIP_FLAGS.has(arg));
|
||||
}
|
||||
|
||||
export function filterSparseMissingOxlintTargets(
|
||||
args,
|
||||
{
|
||||
cwd = process.cwd(),
|
||||
fileExists = fs.existsSync,
|
||||
isSparseCheckoutEnabled = getSparseCheckoutEnabled,
|
||||
isTrackedPath = hasTrackedPath,
|
||||
} = {},
|
||||
) {
|
||||
if (!isSparseCheckoutEnabled({ cwd })) {
|
||||
return { args, hadExplicitTargets: false, remainingExplicitTargets: 0, skippedTargets: [] };
|
||||
}
|
||||
|
||||
const filteredArgs = [];
|
||||
const skippedTargets = [];
|
||||
let hadExplicitTargets = false;
|
||||
let remainingExplicitTargets = 0;
|
||||
let consumeNextValue = false;
|
||||
|
||||
for (const arg of args) {
|
||||
if (consumeNextValue) {
|
||||
filteredArgs.push(arg);
|
||||
consumeNextValue = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg === "--") {
|
||||
filteredArgs.push(arg);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg.startsWith("--")) {
|
||||
filteredArgs.push(arg);
|
||||
if (!arg.includes("=") && OXLINT_VALUE_FLAGS.has(arg)) {
|
||||
consumeNextValue = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg.startsWith("-")) {
|
||||
filteredArgs.push(arg);
|
||||
continue;
|
||||
}
|
||||
|
||||
hadExplicitTargets = true;
|
||||
const absoluteTarget = path.resolve(cwd, arg);
|
||||
if (!fileExists(absoluteTarget) && isTrackedPath({ cwd, target: arg })) {
|
||||
skippedTargets.push(arg);
|
||||
continue;
|
||||
}
|
||||
|
||||
remainingExplicitTargets += 1;
|
||||
filteredArgs.push(arg);
|
||||
}
|
||||
|
||||
return { args: filteredArgs, hadExplicitTargets, remainingExplicitTargets, skippedTargets };
|
||||
}
|
||||
|
||||
function getSparseCheckoutEnabled({ cwd }) {
|
||||
const result = spawnSync("git", ["config", "--get", "--bool", "core.sparseCheckout"], {
|
||||
cwd,
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
shell: process.platform === "win32",
|
||||
});
|
||||
|
||||
return result.status === 0 && result.stdout.trim() === "true";
|
||||
}
|
||||
|
||||
function hasTrackedPath({ cwd, target }) {
|
||||
const result = spawnSync("git", ["ls-files", "--", target], {
|
||||
cwd,
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
shell: process.platform === "win32",
|
||||
});
|
||||
|
||||
return result.status === 0 && result.stdout.trim().length > 0;
|
||||
}
|
||||
|
||||
async function prepareExtensionPackageBoundaryArtifacts(env) {
|
||||
const releaseArtifactsLock = acquireLocalHeavyCheckLockSync({
|
||||
cwd: process.cwd(),
|
||||
@@ -50,7 +147,19 @@ async function prepareExtensionPackageBoundaryArtifacts(env) {
|
||||
}
|
||||
|
||||
export async function main(argv = process.argv.slice(2), runtimeEnv = process.env) {
|
||||
const { args: finalArgs, env } = applyLocalOxlintPolicy(argv, runtimeEnv);
|
||||
const { args: policyArgs, env } = applyLocalOxlintPolicy(argv, runtimeEnv);
|
||||
const sparseTargets = filterSparseMissingOxlintTargets(policyArgs);
|
||||
const finalArgs = sparseTargets.args;
|
||||
if (sparseTargets.skippedTargets.length > 0) {
|
||||
console.error(
|
||||
`[oxlint] sparse checkout is missing tracked target(s); skipping ${sparseTargets.skippedTargets.join(", ")}`,
|
||||
);
|
||||
}
|
||||
if (sparseTargets.hadExplicitTargets && sparseTargets.remainingExplicitTargets === 0) {
|
||||
console.error("[oxlint] no present sparse-checkout targets remain; skipping oxlint.");
|
||||
return;
|
||||
}
|
||||
|
||||
const releaseLock =
|
||||
env.OPENCLAW_OXLINT_SKIP_LOCK === "1"
|
||||
? () => {}
|
||||
|
||||
Reference in New Issue
Block a user