Skip to content

Commit

Permalink
Scriptify patching local gitconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
vrillusions committed Feb 21, 2016
1 parent 3506cec commit ade318f
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions script/update_gitconfig.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash
#
# Quick script for patching the local gitconfig with the changes from git.
#
# Environment Variables:
# FORCE_PATCH: Set to 'true' to not prompt before changing. Default is false
#
# Exit codes:
# 0 No error
# 1 When exiting from help
#

set -e
set -u


if [[ "${1:-}" == '-h' ]]; then
echo "Patches the local gitconfig with the changes made to" >&2
echo "gitconfig in version control. Will prompt first unless" >&2
echo "the environment variable FORCE_PATCH is 'true'" >&2
exit 1
fi


# -- script constants --
# set script_dir to location this script is running in
readonly script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"


# -- logging functions --
# Usage: log "What to log"
log () {
# logger will output to syslog, useful for background tasks
#logger -s -t "${script_name}" -- "$*"
# printf is good for scripts run manually when needed
printf "%b\n" "$(date +"%Y-%m-%dT%H:%M:%S%z") $*"
}


# This assumes this script is located in script subfolder of repo
cd "${script_dir}/.."

# Turning off exit on error as diff exits non zero if changes are made
set +e
diff -u -B <(grep -vE '^\s*(signingkey|email)' gitconfig) \
<(grep -vE '^\s*(signingkey|email)' gitconfig.versioned)
declare -i retcode=$?
set -e

if [[ $retcode -eq 0 ]]; then
log "no changes are needed"
exit 0
fi

echo
log "The above changes will be made to your local gitconfig."
if [[ "${FORCE_PATCH:-false}" != 'false' ]]; then
log "FORCE_PATCH is set, continue"
else
log "Press ctrl-c if you do not wish to make these changes."
log "Otherwise press enter to continue"
echo
read -n 1 -p "Continue?" __unused
fi

diff -u -B <(grep -vE '^\s*(signingkey|email)' gitconfig) \
<(grep -vE '^\s*(signingkey|email)' gitconfig.versioned) \
| patch --no-backup-if-mismatch -p2 gitconfig

log "Change finished successfully"

exit 0

0 comments on commit ade318f

Please sign in to comment.