Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .claude/hooks/protect-operational-data.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
// Saída 2 bloqueia a ferramenta e devolve a mensagem de stderr ao agente.
"use strict";

const path = require("node:path");
const nodePath = require("node:path");

// Caminho com letra de unidade (C:) segue as regras do Windows (separadores e maiúsculas) em qualquer SO.
function pathFor(value) {
return /^[a-zA-Z]:/.test(String(value)) ? nodePath.win32 : nodePath;
}

const SPREADSHEET_EXTENSIONS = new Set([".xlsx", ".xls", ".xlsm", ".csv", ".tsv", ".zip"]);
const ALLOWED_STATE_FILES = new Set([".gitkeep", "readme.md"]);
Expand All @@ -16,29 +21,32 @@ function fromPosixDrive(value) {
}

function relativeToProject(filePath, projectDir) {
const path = pathFor(fromPosixDrive(projectDir));
const root = path.resolve(fromPosixDrive(projectDir));
// Unidades do Windows não diferenciam maiúsculas; path.win32.relative já compara assim em qualquer SO.
const relative = path.relative(root, path.resolve(root, fromPosixDrive(filePath)));
if (!relative || relative.startsWith("..") || path.isAbsolute(relative)) return null;
return relative.split(path.sep).join("/").toLowerCase();
}

// Em worktree (<principal>/.claude/worktrees/<nome>), devolve a raiz do checkout principal.
function mainCheckoutRoot(projectDir) {
const path = pathFor(fromPosixDrive(projectDir));
const root = path.resolve(fromPosixDrive(projectDir));
const match = root.split(path.sep).join("/").match(/^(.*?)\/\.claude\/worktrees\/[^/]+(?:\/|$)/i);
return match ? path.resolve(match[1]) : null;
}

function reasonForRelative(relative) {
const name = path.posix.basename(relative);
const name = nodePath.posix.basename(relative);
if (relative.startsWith("data/state/")) {
return ALLOWED_STATE_FILES.has(name) ? null : "históricos, anotações e filas em data/state";
}
if (relative.startsWith(".transactions/")) return "transações pendentes em .transactions";
for (const root of ["data/planilhas_entrada/", "planilhas_entrada/"]) {
if (!relative.startsWith(root)) continue;
if (relative.startsWith(`${root}_backups/`)) return "backups de planilhas";
if (SPREADSHEET_EXTENSIONS.has(path.posix.extname(relative))) return "planilhas operacionais";
if (SPREADSHEET_EXTENSIONS.has(nodePath.posix.extname(relative))) return "planilhas operacionais";
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/frontend/test_protect_operational_data_hook.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const path = require("node:path");
const path = require("node:path").win32; // fixtures do Windows em qualquer SO

const { blockReason } = require("../../.claude/hooks/protect-operational-data.cjs");

Expand Down