#!/bin/sh
# setup-pre-commit.sh
# Path to the pre-commit hook
HOOK_PATH=".git/hooks/pre-commit"
# Pre-commit hook script
HOOK_SCRIPT="#!/bin/sh\n\n# Increment version in package.json using jq\nVERSION=\$(jq -r '.version' package.json)\nNEW_VERSION=\$(echo \$VERSION | awk -F. -v OFS=. '{\$NF = \$NF + 1 ; print}')\n\n# Update the version in package.json\njq \".version = \\\"\$NEW_VERSION\\\"\" package.json > temp.json && mv temp.json package.json\n\n# Add the package.json to the commit\ngit add package.json\n\n# Exit successfully\nexit 0"
# Create the pre-commit hook
echo "$HOOK_SCRIPT" > "$HOOK_PATH"
# Make the pre-commit hook executable
chmod +x "$HOOK_PATH"
echo "Pre-commit hook set up successfully."
會自動把以上 HOOK_SCRIPT 腳本 加入到 ./git/hooks/pre-commit 檔案,每次 commit 前就會執行 pre-commit 檔案內容。
使用第一行的#!決定要用哪一種方式解譯,如果更改檔案副檔名則無效
#!/usr/bin/env node
console.log(6678);
// process.exit(1); 加上後會取消commit
#!/bin/sh
echo 123;
# exit 1; 加上後會取消commit
但之後改完因為.git無法加入commit,我們必須使用link file的方式讓別人執行,然後加入到git hook
var fs = require("fs");
var path = require("path");
["applypatch-msg", "commit-msg", "post-commit", "post-receive", "post-update", "pre-applypatch", "pre-commit",
"prepare-commit-msg", "pre-rebase", "update"].forEach(function (hook) {
var hookInSourceControl = path.resolve(__dirname, hook);
if (fs.existsSync(hookInSourceControl)) {
var hookInHiddenDirectory = path.resolve(__dirname, "../..", ".git", "hooks", hook);
if (fs.existsSync(hookInHiddenDirectory)) {
fs.unlinkSync(hookInHiddenDirectory);
}
fs.linkSync(hookInSourceControl, hookInHiddenDirectory);
}
});
#!/usr/bin/env node
const { exec } = require('child_process');
// 如果修改的檔案為/dist 會要求修改/js 並使用babel compile
exec('git diff --cached --name-only', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
if(stdout.indexOf('/dist') !== -1) { // 修改了dist資料夾內的檔案
console.log(`您修改了包含/dist檔案內的資料,所以被禁止commit!!`)
console.log(stdout);
process.exit(1);
}
});