dotfiles/dot_git-hooks/executable_pre-commit

55 lines
1.6 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env node
const { execSync } = require('child_process');
// Define the allowed email addresses for each directory pattern
const EMAILS = {
"/Users/beisel/git/privat": "florian@beisel.it",
"/Users/beisel/git/": "florian.beisel@intersport.de",
"/Users/beisel/.local/share/chezmoi": "florian@beisel.it",
};
// Get the current repository path
const REPO_PATH = execSync('git rev-parse --show-toplevel', { encoding: 'utf8' }).trim();
// Get the current user email
const CURRENT_EMAIL = execSync('git config user.email', { encoding: 'utf8' }).trim();
// Initialize allowed email as empty
let ALLOWED_EMAIL = "";
// Check each directory pattern
for (const DIR in EMAILS) {
if (REPO_PATH.startsWith(DIR)) {
ALLOWED_EMAIL = EMAILS[DIR];
break;
}
}
// If no matching directory pattern was found, exit
if (!ALLOWED_EMAIL) {
console.error("Error: No allowed email found for this repository");
process.exit(1);
}
// Check if the current email is allowed
if (CURRENT_EMAIL !== ALLOWED_EMAIL) {
console.error(`Error: You are using the wrong email for this repository
Your current email: ${CURRENT_EMAIL}
Allowed email: ${ALLOWED_EMAIL}`);
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question(`Do you want to change the email for this repository to ${ALLOWED_EMAIL}? (y/n) `, (answer) => {
if (answer.toLowerCase() === 'y') {
execSync(`git config user.email "${ALLOWED_EMAIL}"`);
console.log(`Email changed to ${ALLOWED_EMAIL}`);
}
readline.close();
process.exit(answer.toLowerCase() !== 'y' ? 1 : 0);
});
}