Files
scm_breeze/lib/scm_breeze.sh
2011-10-21 08:41:55 +08:00

53 lines
2.2 KiB
Bash

# Detect shell
if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; else shell="bash"; fi
# Detect whether zsh 'shwordsplit' option is on by default.
if [ $shell = "zsh" ]; then zsh_shwordsplit=$( (setopt | grep -q shwordsplit) && echo "true" ); fi
# Switch on/off shwordsplit for functions that require it.
zsh_compat(){ if [ $shell = "zsh" ] && [ -z $zsh_shwordsplit ]; then setopt shwordsplit; fi; }
zsh_reset(){ if [ $shell = "zsh" ] && [ -z $zsh_shwordsplit ]; then unsetopt shwordsplit; fi; }
# Alias wrapper that ignores errors if alias is not defined.
_alias(){ alias "$@" 2> /dev/null; }
# Updates SCM Breeze from GitHub.
update_scm_breeze() {
currDir=$PWD
cd "$scmbDir"
oldHEAD=$(git rev-parse HEAD 2> /dev/null)
git pull origin master
# Reload latest version of '_create_or_patch_scmbrc' function
source "$scmbDir/lib/scm_breeze.sh"
_create_or_patch_scmbrc $oldHEAD
# Reload SCM Breeze
source "$scmbDir/scm_breeze.sh"
cd "$currDir"
}
# Create '~/.*.scmbrc' files, or attempt to patch them if passed a previous revision
_create_or_patch_scmbrc() {
patchfile=$(mktemp -t tmp.XXXXXXXXXX)
for scm in git; do
# Create file from example if it doesn't already exist
if ! [ -e "$HOME/.$scm.scmbrc" ]; then
cp "$scmbDir/$scm.scmbrc.example" "$HOME/.$scm.scmbrc"
printf "== '~/.$scm.scmbrc' has been created. Please edit this file to change SCM Breeze settings for '$scm'.\n"
# If file exists, attempt to update it with any new settings
elif [ -n "$1" ]; then
# Create diff of example file, substituting example file for user's config.
git diff $1 "$scm.scmbrc.example" | sed "s/$scm.scmbrc.example/.$scm.scmbrc/g" > $patchfile
if [ -s $patchfile ]; then # If patchfile is not empty
cd $HOME
# If the patch cannot be applied cleanly, show the updates and tell user to update file manually.
if ! patch -f "$HOME/.$scm.scmbrc" $patchfile; then
printf "== \e[0;31mUpdates could not be applied to '\e[1m~/.$scm.scmbrc\e[0;31m'.\e[0m\n"
printf "== Please look at the following changes and manually update '~/.$scm.scmbrc', if necessary.\n\n"
cat "$HOME/.$scm.scmbrc.rej"
fi
cd "$scmbDir"
fi
fi
done
}