mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-09 05:19:32 +08:00
chore: clean up git hooks and actually install them again.
This commit is contained in:
@@ -9,7 +9,6 @@ RUN apt-get update \
|
||||
|
||||
WORKDIR /repo
|
||||
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
|
||||
COPY scripts/setup-git-hooks.js ./scripts/setup-git-hooks.js
|
||||
RUN corepack enable \
|
||||
&& pnpm install --frozen-lockfile
|
||||
|
||||
|
||||
@@ -1,149 +0,0 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const OXFMT_EXTENSIONS = new Set([".cjs", ".js", ".json", ".jsonc", ".jsx", ".mjs", ".ts", ".tsx"]);
|
||||
|
||||
function getRepoRoot() {
|
||||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||||
return path.resolve(here, "..");
|
||||
}
|
||||
|
||||
function runGitCommand(args, options = {}) {
|
||||
return spawnSync("git", args, {
|
||||
cwd: options.cwd,
|
||||
encoding: "utf-8",
|
||||
stdio: options.stdio ?? "pipe",
|
||||
});
|
||||
}
|
||||
|
||||
function splitNullDelimited(value) {
|
||||
if (!value) {
|
||||
return [];
|
||||
}
|
||||
const text = String(value);
|
||||
return text.split("\0").filter(Boolean);
|
||||
}
|
||||
|
||||
function normalizeGitPath(filePath) {
|
||||
return filePath.replace(/\\/g, "/");
|
||||
}
|
||||
|
||||
function filterOxfmtTargets(paths) {
|
||||
return paths
|
||||
.map(normalizeGitPath)
|
||||
.filter(
|
||||
(filePath) =>
|
||||
(filePath.startsWith("src/") || filePath.startsWith("test/")) &&
|
||||
OXFMT_EXTENSIONS.has(path.posix.extname(filePath)),
|
||||
);
|
||||
}
|
||||
|
||||
function findPartiallyStagedFiles(stagedFiles, unstagedFiles) {
|
||||
const unstaged = new Set(unstagedFiles.map(normalizeGitPath));
|
||||
return stagedFiles.filter((filePath) => unstaged.has(normalizeGitPath(filePath)));
|
||||
}
|
||||
|
||||
function filterOutPartialTargets(targets, partialTargets) {
|
||||
if (partialTargets.length === 0) {
|
||||
return targets;
|
||||
}
|
||||
const partial = new Set(partialTargets.map(normalizeGitPath));
|
||||
return targets.filter((filePath) => !partial.has(normalizeGitPath(filePath)));
|
||||
}
|
||||
|
||||
function resolveOxfmtCommand(repoRoot) {
|
||||
const binName = process.platform === "win32" ? "oxfmt.cmd" : "oxfmt";
|
||||
const local = path.join(repoRoot, "node_modules", ".bin", binName);
|
||||
if (fs.existsSync(local)) {
|
||||
return { command: local, args: [] };
|
||||
}
|
||||
|
||||
const result = spawnSync("oxfmt", ["--version"], { stdio: "ignore" });
|
||||
if (result.status === 0) {
|
||||
return { command: "oxfmt", args: [] };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getGitPaths(args, repoRoot) {
|
||||
const result = runGitCommand(args, { cwd: repoRoot });
|
||||
if (result.status !== 0) {
|
||||
return [];
|
||||
}
|
||||
return splitNullDelimited(result.stdout ?? "");
|
||||
}
|
||||
|
||||
function formatFiles(repoRoot, oxfmt, files) {
|
||||
const result = spawnSync(oxfmt.command, ["--write", ...oxfmt.args, ...files], {
|
||||
cwd: repoRoot,
|
||||
stdio: "inherit",
|
||||
});
|
||||
return result.status === 0;
|
||||
}
|
||||
|
||||
function stageFiles(repoRoot, files) {
|
||||
if (files.length === 0) {
|
||||
return true;
|
||||
}
|
||||
const result = runGitCommand(["add", "--", ...files], { cwd: repoRoot, stdio: "inherit" });
|
||||
return result.status === 0;
|
||||
}
|
||||
|
||||
function main() {
|
||||
const repoRoot = getRepoRoot();
|
||||
const staged = getGitPaths(
|
||||
["diff", "--cached", "--name-only", "-z", "--diff-filter=ACMR"],
|
||||
repoRoot,
|
||||
);
|
||||
const targets = filterOxfmtTargets(staged);
|
||||
if (targets.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const unstaged = getGitPaths(["diff", "--name-only", "-z"], repoRoot);
|
||||
const partial = findPartiallyStagedFiles(targets, unstaged);
|
||||
if (partial.length > 0) {
|
||||
process.stderr.write("[pre-commit] Skipping partially staged files:\n");
|
||||
for (const filePath of partial) {
|
||||
process.stderr.write(`- ${filePath}\n`);
|
||||
}
|
||||
process.stderr.write("Stage full files to format them automatically.\n");
|
||||
}
|
||||
|
||||
const filteredTargets = filterOutPartialTargets(targets, partial);
|
||||
if (filteredTargets.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const oxfmt = resolveOxfmtCommand(repoRoot);
|
||||
if (!oxfmt) {
|
||||
process.stderr.write("[pre-commit] oxfmt not found; skipping format.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!formatFiles(repoRoot, oxfmt, filteredTargets)) {
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!stageFiles(repoRoot, filteredTargets)) {
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
filterOxfmtTargets,
|
||||
filterOutPartialTargets,
|
||||
findPartiallyStagedFiles,
|
||||
getRepoRoot,
|
||||
normalizeGitPath,
|
||||
resolveOxfmtCommand,
|
||||
splitNullDelimited,
|
||||
};
|
||||
|
||||
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
||||
main();
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const DEFAULT_HOOKS_PATH = "git-hooks";
|
||||
const PRE_COMMIT_HOOK = "pre-commit";
|
||||
|
||||
function getRepoRoot() {
|
||||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||||
return path.resolve(here, "..");
|
||||
}
|
||||
|
||||
function runGitCommand(args, options = {}) {
|
||||
return spawnSync("git", args, {
|
||||
cwd: options.cwd,
|
||||
encoding: "utf-8",
|
||||
stdio: options.stdio ?? "pipe",
|
||||
});
|
||||
}
|
||||
|
||||
function ensureExecutable(targetPath) {
|
||||
if (process.platform === "win32") {
|
||||
return;
|
||||
}
|
||||
if (!fs.existsSync(targetPath)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const mode = fs.statSync(targetPath).mode & 0o777;
|
||||
if (mode & 0o100) {
|
||||
return;
|
||||
}
|
||||
fs.chmodSync(targetPath, 0o755);
|
||||
} catch (err) {
|
||||
console.warn(`[setup-git-hooks] chmod failed: ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
function isGitAvailable({ repoRoot = getRepoRoot(), runGit = runGitCommand } = {}) {
|
||||
const result = runGit(["--version"], { cwd: repoRoot, stdio: "ignore" });
|
||||
return result.status === 0;
|
||||
}
|
||||
|
||||
function isGitRepo({ repoRoot = getRepoRoot(), runGit = runGitCommand } = {}) {
|
||||
const result = runGit(["rev-parse", "--is-inside-work-tree"], {
|
||||
cwd: repoRoot,
|
||||
stdio: "pipe",
|
||||
});
|
||||
if (result.status !== 0) {
|
||||
return false;
|
||||
}
|
||||
return String(result.stdout ?? "").trim() === "true";
|
||||
}
|
||||
|
||||
function setHooksPath({
|
||||
repoRoot = getRepoRoot(),
|
||||
hooksPath = DEFAULT_HOOKS_PATH,
|
||||
runGit = runGitCommand,
|
||||
} = {}) {
|
||||
const result = runGit(["config", "core.hooksPath", hooksPath], {
|
||||
cwd: repoRoot,
|
||||
stdio: "ignore",
|
||||
});
|
||||
return result.status === 0;
|
||||
}
|
||||
|
||||
function setupGitHooks({
|
||||
repoRoot = getRepoRoot(),
|
||||
hooksPath = DEFAULT_HOOKS_PATH,
|
||||
runGit = runGitCommand,
|
||||
} = {}) {
|
||||
if (!isGitAvailable({ repoRoot, runGit })) {
|
||||
return { ok: false, reason: "git-missing" };
|
||||
}
|
||||
|
||||
if (!isGitRepo({ repoRoot, runGit })) {
|
||||
return { ok: false, reason: "not-repo" };
|
||||
}
|
||||
|
||||
if (!setHooksPath({ repoRoot, hooksPath, runGit })) {
|
||||
return { ok: false, reason: "config-failed" };
|
||||
}
|
||||
|
||||
ensureExecutable(path.join(repoRoot, hooksPath, PRE_COMMIT_HOOK));
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
export {
|
||||
DEFAULT_HOOKS_PATH,
|
||||
PRE_COMMIT_HOOK,
|
||||
ensureExecutable,
|
||||
getRepoRoot,
|
||||
isGitAvailable,
|
||||
isGitRepo,
|
||||
runGitCommand,
|
||||
setHooksPath,
|
||||
setupGitHooks,
|
||||
};
|
||||
|
||||
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
||||
setupGitHooks();
|
||||
}
|
||||
Reference in New Issue
Block a user