File size: 6,137 Bytes
d739b1b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | /**
* policy-guard-cli.js โ BADA Terminal ์๊ฐ ์น์ Pre-Commit & ์ ์ฑ
๊ฐ๋ CLI
*
* ์ค๋ช
๋ฌธ์: /docs/specs/bada_trio_actionable_spec.md (SPEC-ACT-001 v1.0.0)
* ๊ด๋ จ ๊ท์ฝ: AGENTS.md, Zero-Trust DLP, Pre-Commit Hook Security
*
* ์ฃผ์ ๊ธฐ๋ฅ:
* 1. Git Staged Diff ๊ฒ์ฌ: ์ปค๋ฐ ์ staged ์ํ์ ์ฝ๋ diff๋ฅผ ValueAnchorEvaluator๋ก ์ ๋ฐ ๋ถ์
* 2. ์๊ฐ ์น์ ํผ๋๋ฐฑ ์ ๊ณต: ์๋ฐ ๋ฐ์ ์ ๋จ์ ์๋ฌ ์ข
๋ฃ๊ฐ ์๋ ๊ตฌ์ฒด์ ์ธ ๊ต์ ๊ฐ์ด๋ ์ถ๋ ฅ
* 3. ์ํด๋ฆญ Git Hook ์ค์น/์ ๊ฑฐ: cross-platform ํธํ sh ๊ธฐ๋ฐ pre-commit ํ
์๋ ์์ฑ
* 4. ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ CLI ์์ฉ ์ง์
*/
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const valueAnchorEvaluator = require('../orchestration/value-anchor-evaluator');
/**
* Git staged ์ํ์ ๋ณ๊ฒฝ์ฌํญ์ ์์งํ์ฌ ๊ฐ์น ์ต์ปค ์ ์ฑ
์ ํ๊ฐํฉ๋๋ค.
*
* @param {Object} [options]
* @param {string} [options.cwd=process.cwd()] - ์์
๋๋ ํ ๋ฆฌ
* @returns {Object} ํ๊ฐ ๊ฒฐ๊ณผ ๊ฐ์ฒด { passed, hardBlocked, score, violations, feedbackPrompt }
*/
function evaluateStaged(options = {}) {
const cwd = options.cwd || process.cwd();
// 1. Staged๋ ํ์ผ ๋ชฉ๋ก ์ถ์ถ (git diff --cached --name-only)
let modifiedFiles = [];
try {
const filesOutput = execSync('git diff --cached --name-only', {
cwd,
encoding: 'utf8',
timeout: 10000,
maxBuffer: 10 * 1024 * 1024
});
modifiedFiles = filesOutput.split('\n').map(f => f.trim()).filter(Boolean);
} catch (err) {
// git ์ ์ฅ์๊ฐ ์๋๊ฑฐ๋ staged๋ ๋ด์ฉ์ด ์๋ ๊ฒฝ์ฐ
return {
passed: true,
hardBlocked: false,
score: 100,
hardViolations: [],
softViolations: [],
feedbackPrompt: null,
message: 'Git ์ ์ฅ์๊ฐ ์๋๊ฑฐ๋ staged ๋ณ๊ฒฝ์ฌํญ์ด ์์ต๋๋ค.'
};
}
if (modifiedFiles.length === 0) {
return {
passed: true,
hardBlocked: false,
score: 100,
hardViolations: [],
softViolations: [],
feedbackPrompt: null,
message: 'Staged๋ ํ์ผ์ด ์์ต๋๋ค.'
};
}
// 2. Staged๋ ์ฝ๋ ์ ์ฒด diff ์์ง
let diff = '';
let diffWarning = null;
try {
diff = execSync('git diff --cached', {
cwd,
encoding: 'utf8',
timeout: 15000,
maxBuffer: 15 * 1024 * 1024 // 15MB ๋ฒํผ (๋ํ ๋ณ๊ฒฝ ๋๋น)
});
} catch (err) {
// diff ์์ง ์คํจ ์ ํ์ผ ๊ฒฝ๋ก ๊ธฐ๋ฐ ๊ฒ์ฌ๋ง ์ํ๋จ์ ๊ฒฝ๊ณ
diffWarning = `[๊ฒฝ๊ณ ] git diff --cached ์์ง ์คํจ (${err.message?.slice(0, 80)}). ์ฝ๋ ๋ด์ฉ(์ํฌ๋ฆฟ ๋ฑ) ๊ฒ์ฌ๊ฐ ์๋ต๋ ์ ์์ต๋๋ค.`;
diff = '';
}
// 3. ValueAnchorEvaluator ์คํ
const result = valueAnchorEvaluator.evaluate({
diff,
modifiedFiles,
commands: []
});
return {
...result,
modifiedFiles,
diffWarning
};
}
/**
* .git/hooks/pre-commit ํ
์ ์ํด๋ฆญ์ผ๋ก ์ค์นํฉ๋๋ค.
* ํฌ๋ก์ค ํ๋ซํผ(macOS, Linux, Windows Git Bash) ํธํ sh ์คํฌ๋ฆฝํธ ์์ฑ.
*
* @param {string} [targetGitDir] - .git ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก (์๋ต ์ ์๋ ๊ฐ์ง)
* @returns {boolean} ์ฑ๊ณต ์ฌ๋ถ
*/
function installHook(targetGitDir = null) {
const gitDir = targetGitDir || path.resolve(process.cwd(), '.git');
const hooksDir = path.join(gitDir, 'hooks');
if (!fs.existsSync(hooksDir)) {
fs.mkdirSync(hooksDir, { recursive: true });
}
const hookPath = path.join(hooksDir, 'pre-commit');
const scriptContent = `#!/bin/sh
# BADA Terminal Value Anchor & Policy Guard Pre-Commit Hook
# Auto-generated by BADA policy-guard-cli
# ํ๋ก์ ํธ ๋ฃจํธ๋ก ์ด๋ (git hook์ GIT_DIR ๊ธฐ์ค์ผ๋ก ์คํ๋ ์ ์์)
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)"
if [ -n "$REPO_ROOT" ]; then
cd "$REPO_ROOT" || exit 1
fi
node src/cli/policy-guard-cli.js --staged
STATUS=$?
if [ $STATUS -ne 0 ]; then
echo ""
echo "โ [BADA-GUARD] ์ ์ฑ
์๋ฐ์ผ๋ก ์ธํด ์ปค๋ฐ์ด ์ฐจ๋จ๋์์ต๋๋ค."
echo "์ ๊ฐ์ด๋๋ฅผ ์ฐธ์กฐํ์ฌ ์ฝ๋๋ฅผ ์์ ํ ๋ค ๋ค์ ์ปค๋ฐํ์ญ์์ค."
exit 1
fi
exit 0
`;
fs.writeFileSync(hookPath, scriptContent, { encoding: 'utf8', mode: 0o755 });
// POSIX ํ๊ฒฝ์์ ์คํ ๊ถํ ๋ถ์ฌ
try {
fs.chmodSync(hookPath, 0o755);
} catch (e) {
// Windows์์๋ ๋ฌด์
}
return true;
}
/**
* .git/hooks/pre-commit ํ
์ ์์ ํ๊ฒ ์ ๊ฑฐํฉ๋๋ค.
*
* @param {string} [targetGitDir]
* @returns {boolean}
*/
function uninstallHook(targetGitDir = null) {
const gitDir = targetGitDir || path.resolve(process.cwd(), '.git');
const hookPath = path.join(gitDir, 'hooks', 'pre-commit');
if (fs.existsSync(hookPath)) {
fs.unlinkSync(hookPath);
return true;
}
return false;
}
/**
* CLI ๋ฉ์ธ ์ง์
์
*/
function cliMain(args = process.argv.slice(2)) {
if (args.includes('--install-hook')) {
const ok = installHook();
console.log(ok ? 'โ
BADA Policy Guard Git Pre-Commit Hook ์ค์น ์๋ฃ!' : 'โ ์ค์น ์คํจ');
process.exit(ok ? 0 : 1);
}
if (args.includes('--uninstall-hook')) {
const ok = uninstallHook();
console.log(ok ? 'โ
BADA Policy Guard Git Pre-Commit Hook ์ ๊ฑฐ ์๋ฃ!' : 'โน๏ธ ์ค์น๋ ํ
์ด ์์ต๋๋ค.');
process.exit(0);
}
if (args.includes('--staged') || args.length === 0) {
console.log('๐ก๏ธ [BADA Policy Guard] Git Staged ์ฝ๋ ์ ์ฑ
๊ฒ์ฌ ์ค...');
const result = evaluateStaged();
if (result.passed) {
console.log(`โ
[BADA-GUARD PASS] ๋ชจ๋ ๋ณด์ ๋ฐ ๊ฐ๋ฐ ์ ์ฑ
ํต๊ณผ! (์ ์: ${result.score}/100)`);
process.exit(0);
} else {
console.error(`\n๐จ [BADA-GUARD BLOCKED] ์น๋ช
์ ์ ์ฑ
์๋ฐ์ด ๊ฐ์ง๋์์ต๋๋ค! (์ ์: ${result.score}/100)`);
if (result.feedbackPrompt) {
console.error('\n' + result.feedbackPrompt + '\n');
}
process.exit(1);
}
}
}
// CLI ์ง์ ์คํ ์ ์คํ
if (require.main === module) {
cliMain();
}
module.exports = {
evaluateStaged,
installHook,
uninstallHook,
cliMain
};
|