Skip to content

Instantly share code, notes, and snippets.

@TomerFi
Last active February 10, 2024 16:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TomerFi/0911f573ea0474b9ab74bcfcef0f2a49 to your computer and use it in GitHub Desktop.
Save TomerFi/0911f573ea0474b9ab74bcfcef0f2a49 to your computer and use it in GitHub Desktop.
Git smudge-clean script for using as filter for mutiple reducted values for various file types in various repositories.
#!/bin/bash
#####################################################################################################
# This script is meant to be used as a global smudge-clean filter for removing sensitive data #
# from your commits. #
# #
# 1. Place this script in an acceisble path, i.e. ~/scripts/git-smudge-clean-filter.sh. #
# #
# 2. Populate the 'mapArr' using what you need hidden as the key, and the replacment as the value. #
# DO NOT use same values for multiple keys (this will work only in one direction). #
# #
# 3. Set up the filter with git (2 options): #
# 3.1. You can either add the following section in your global ~/.gitconfig file: #
# [filter "reductScript"] #
# smudge = ~/scripts/git-smudge-clean-filter.sh smudge #
# clean = ~/scripts/git-smudge-clean-filter.sh clean #
# 3.2. Or run the following command from you cli: #
# git config --global filter.reductScript.smudge "~/scripts/git-smudge-clean-filter.sh smudge" #
# git config --global filter.reductScript.clean "~/scripts/git-smudge-clean-filter.sh clean" #
# #
# 4. For every file type, in every repository you are working on, and need sensitive data removed, #
# add the 'filter=reductScript' property in the attributes file and you're good to go. #
# For example for filtering yaml files: `*.yaml text eol=lf filter=reductScript`. #
# #
# Tip: for shared repositories, you can store your attributes in in '$GIT_DIR/info/attributes' #
# instead of the standard '.gitattributes' file. #
# Follow this https://git-scm.com/docs/gitattributes for more attibtues inforamtion. #
#####################################################################################################
declare -A mapArr
mapArr["my-work-private-server.mywork.com"]="<reducted-work-server>"
mapArr["my-personal-private-server.myowndomain.org"]="<reducted-personal-server>"
mapArr["A*&#QAADDA(77##F"]="super-secret-token"
mapArr["oops@mypersonal.email"]="support@correct.email"
# mac users: use gsed instead of sed
sedcmd="sed"
if [[ "$1" == "clean" ]]; then
for key in ${!mapArr[@]}; do
sedcmd+=" -e \"s/${key}/${mapArr[${key}]}/g\""
done
elif [[ "$1" == "smudge" ]]; then
for key in ${!mapArr[@]}; do
sedcmd+=" -e \"s/${mapArr[${key}]}/${key}/g\""
done
else
echo "use smudge/clean as the first argument"
exit 1
fi
eval $sedcmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment