From 655f7064b7dec960fc4619cf326911017a483114 Mon Sep 17 00:00:00 2001 From: Florian Beisel Date: Tue, 4 Jun 2024 16:55:43 +0200 Subject: [PATCH] Update .chezmoidata/packages.yaml Add .git-hooks/pre-commit --- .chezmoidata/packages.yaml | 7 ++++ dot_git-hooks/executable_pre-commit | 54 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 dot_git-hooks/executable_pre-commit diff --git a/.chezmoidata/packages.yaml b/.chezmoidata/packages.yaml index 7102123..e051609 100644 --- a/.chezmoidata/packages.yaml +++ b/.chezmoidata/packages.yaml @@ -13,11 +13,14 @@ packages: - ca-certificates - chezmoi - coreutils + - eza - gettext + - git - gmp - icu4c - jq - jsoncpp + - libgit2 - liblinear - libnghttp2 - libssh2 @@ -31,6 +34,7 @@ packages: - luv - m4 - mactop + - make - mosh - mpdecimal - msgpack @@ -57,13 +61,16 @@ packages: - unibilium - unixodbc - xz + - z - zsh-autocomplete casks: - alacritty + - batteryboi - bitwarden - crystalfetch - divvy - docker - keepingyouawake - loop + - signal - utm diff --git a/dot_git-hooks/executable_pre-commit b/dot_git-hooks/executable_pre-commit new file mode 100644 index 0000000..4c71abb --- /dev/null +++ b/dot_git-hooks/executable_pre-commit @@ -0,0 +1,54 @@ +#!/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); + }); +}