Last active
September 26, 2022 11:17
-
-
Save mg98/7ca420b680d51b5e19baf28224166269 to your computer and use it in GitHub Desktop.
Detect newly added vars in .env.example and automatically merge them into .env
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
addedVars=$(git diff ORIG_HEAD HEAD --exit-code -- .env.example | tail -n +6 | grep '^\+' | sed '/^\+/ s/^\+//' | sed '/^[[:space:]]*$/d') | |
new=$(echo "$addedVars" | while read v; do | |
if [[ !$(cat .env | grep ^$v=) ]]; then | |
echo $v | |
fi | |
done) | |
if [[ "$new" ]]; then | |
printf "New environment variables detected:\n" | |
printf "$new" | |
printf "\n" | |
exec < /dev/tty | |
read -p "Merge to .env? (y/n) " -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
echo "$new" >> .env | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is meant to be added as
post-merge
hook under.git/hooks/post-merge
in repositories. It will then be executed after every merge, including agit pull
.If the merge includes additions to
.env.example
and those additions represent variables that also do not exist in your local (gitignored).env
, it will offer to merge (add) those for you into this file.It will NOT change variable values in your
.env
, nor will it ever delete variables.