#!/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"]="" mapArr["my-personal-private-server.myowndomain.org"]="" 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