From e9eab79ec3359af7fc6663db6bf6b6da56c89b35 Mon Sep 17 00:00:00 2001 From: Sadman Ahmed Shanto Date: Fri, 26 Jul 2024 22:06:59 -0700 Subject: [PATCH] feat: added pre-commit hooks to auto encrypt and stage lab_members.json file when it is updated --- README.md | 6 ++++++ scripts/hooks/pre-commit | 23 +++++++++++++++++++++++ scripts/setup_hooks.sh | 8 ++++++++ 3 files changed, 37 insertions(+) create mode 100644 scripts/hooks/pre-commit create mode 100644 scripts/setup_hooks.sh diff --git a/README.md b/README.md index 3c7803e..1074d1e 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,12 @@ This project serves as a lab manager for the [Levenson-Falk Lab](https://dornsif 4. **Commit Encrypted Files to Repository:** Commit and push the encrypted files (`*.enc`) to your repository. +5. Run the following command to set up Git hooks: + +```bash +./scripts/setup-hooks.sh +``` + ### GitHub Actions Setup 1. **Create GitHub Secrets:** diff --git a/scripts/hooks/pre-commit b/scripts/hooks/pre-commit new file mode 100644 index 0000000..2491779 --- /dev/null +++ b/scripts/hooks/pre-commit @@ -0,0 +1,23 @@ +#!/bin/bash + +# Check if lab_members.json is staged for commit +if git diff --cached --name-only | grep -q 'lab_members.json'; then + echo "lab_members.json has changed. Encrypting the file..." + + # Prompt for the secret key + read -sp "Enter encryption key: " SECRET_KEY + echo + + # Encrypt lab_members.json + openssl aes-256-cbc -salt -a -e -in lab_members.json -out lab_members.json.enc -pass pass:$SECRET_KEY -pbkdf2 + + # Clear the secret key variable + unset SECRET_KEY + + # Stage the encrypted file + git add lab_members.json.enc + + # Notify the user + echo "lab_members.json has been encrypted and lab_members.json.enc has been staged for commit." +fi + diff --git a/scripts/setup_hooks.sh b/scripts/setup_hooks.sh new file mode 100644 index 0000000..ef4923b --- /dev/null +++ b/scripts/setup_hooks.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Copy hooks to .git/hooks +cp scripts/hooks/pre-commit .git/hooks/pre-commit +chmod +x .git/hooks/pre-commit + +echo "Git hooks installed." +