Skip to content

Instantly share code, notes, and snippets.

@entrity
Last active January 2, 2016 21:09
Show Gist options
  • Save entrity/8361013 to your computer and use it in GitHub Desktop.
Save entrity/8361013 to your computer and use it in GitHub Desktop.
#!/bin/bash
# check for -f flag as first argument; if present, bypass all restrictions
if [[ "$1" == "-f" ]]; then
shift
git-commit "$@"
exit 0
fi
fail()
{
echo ==========================
echo -e $1
echo ==========================
exit 1
}
# copy args so that $@ can be shifted but we can pass all args to git-merge
args=$@
# Determine whether this is the Dataraptor proj.
# If so, impose constraints. If not, skip to execution.
if [[ -r "config/application.rb" && ! -z $(grep Clu2 "config/application.rb") ]]; then
# determine current branch
dst=$(git status | grep "On branch" | cut -d " " -f 4)
if [[ "$dst" =~ ^(develop|master)$ ]]; then
# Allow commit on merge (i.e. revision has more than one parent)
if [[ -f ".git/MERGE_MSG" ]]; then
shift
args=$@
else
fail "You cannot commit to develop or master.\nCommit to a fix or feature branch instead."
fi
fi
# Forbid commit if diff contains a pry
git diff --cached | egrep -q "\+.*binding\.pry" && {
fail "You cannot commit 'binding.pry' to source control.\nRun \`git diff --cached | egrep -l \"\+.*binding\.pry\"\`"
}
# Forbid commit if diff contains debugger
git diff --cached | egrep -q "\+.*debugger" && {
fail "You cannot commit 'debugger' to source control.\nRun \`git diff --cached | egrep -l \"\+.*debugger\"\`"
}
fi
# Execute merge
git-commit "$args"
#!/bin/bash
# Echo usage if there are no args
if [[ $# == 0 ]]; then
echo =============================
echo 'You are using "git safetymerge"'
echo 'Please supply args just as you would for "git merge"'
echo =============================
exit 1
fi
# subroutine for handling failures
fail ()
{
echo "== ERROR: $1"
exit 1
}
# copy args so that $@ can be shifted but we can pass all args to git-merge
args=$@
# Determine whether this is the Dataraptor proj.
# If so, impose constraints. If not, skip to execution.
if [[ -r "config/application.rb" && ! -z $(grep Clu2 "config/application.rb") ]]; then
# determine current branch
dst=$(git status | grep "On branch" | cut -d " " -f 4)
# determine source of merge
while (( $# )); do
[[ "$1" =~ ^- ]] || break
shift
done
src=$1
# Echo debug message
echo Merge requested from $src to $dst
# determine whether current branch be behind its remote tracking branch
branch_description=$( git branch --list -vv | grep '^\*' )
is_behind=$( echo "$branch_description" | grep -e '\[.*behind.*\]' )
# Fail if current branch be behind its remote tracking branch
[[ ! -z "$is_behind" ]] && fail "Your branch is behind the remote branch. Pull before merging."
# Fail if master|develop merges to master|develop
[[ "$src" =~ ^(master|develop)$ && "$dst" =~ ^(master|develop)$ ]] && fail "You may not merge between master and develop"
# Fail if source of merge is master
[[ "$src" == "master" ]] && fail "You may not merge from master to anything"
fi
# Execute merge
git-merge $args
# Echo ending message
echo end without error
#!/bin/bash
# Usage:
#
# $ sudo curl https://gist.github.com/Vaselinessa/8361013/raw/install-gitsafety.sh | bash
#
# Destination for downloaded git extensions
dst=
# If user is ROOT (sudo), install at usr/local/bin
if [[ "$(whoami)" == "root" ]]; then
dst="/usr/local/bin"
else
# If user is not ROOT (sudo), find earliest PATH directory which is writable for current user
while IFS=':' read -ra DIRS; do
for d in "${DIRS[@]}"; do
if [[ -w $d ]]; then
dst=$d
break
fi
done
done <<< $PATH
fi
if [[ -z "$dst" ]]; then
echo "== ERROR: you have no write permissions to anything on your \$PATH. Please update your \$PATH variable OR use sudo and try again"
exit 1
fi
echo Downloading git extensions
# Download extension files
wget https://gist.github.com/Vaselinessa/8361013/raw/git-safetymerge -O $dst/git-safetymerge
wget https://gist.github.com/Vaselinessa/8361013/raw/git-safetycommit -O $dst/git-safetycommit
# Make safety extensions executable
chmod +x $dst/git-safetymerge
chmod +x $dst/git-safetycommit
# Alias git in .bash_profile
cat << 'HERED' >> $HOME/.bash_profile
function git_safety {
cmd=$1
shift
extra=""
if [ "$cmd" == "commit" ]; then
cmd=safetycommit
elif [ "$cmd" == "merge" ]; then
cmd=safetymerge
fi
"`which git`" "$cmd" "$@"
}
alias git=git_safety
HERED
# Echo instructions
echo !!! IN ALL OF YOUR SHELL WINDOWS/TABS, SOURCE ~/.bash_profile \
OR CLOSE AND RE-OPEN ALL OF YOUR SHELL WINDOWS/TABS !!!
#!/bin/bash
# Usage:
#
# $ sudo update-gitsafety.sh
#
# Destination for downloaded git extensions
dst=
newInstall=false
# find PATH directory where git-safeties are installed curently
while IFS=':' read -ra DIRS; do
for d in "${DIRS[@]}"; do
if [[ -r "$d/git-safetymerge" ]]; then
dst=$d
break
fi
done
done <<< $PATH
if [[ -z "$dst" ]]; then dst=/usr/local/bin; newInstall=true; fi
if [[ ! -w $dst ]]; then
echo "=============== ERROR: you have no write permissions to $dst. Please use sudo and try again"
exit 1
fi
echo == Downloading git extensions to $dst ==
# Download extension files
wget https://gist.github.com/Vaselinessa/8361013/raw/git-safetymerge -O $dst/git-safetymerge
wget https://gist.github.com/Vaselinessa/8361013/raw/git-safetycommit -O $dst/git-safetycommit
wget https://gist.github.com/Vaselinessa/8361013/raw/update-gitsafety.sh -O $dst/update-gitsafety.sh
# Make safety extensions executable
chmod +x $dst/git-safetymerge
chmod +x $dst/git-safetycommit
chmod +x $dst/update-gitsafety.sh
if $newInstall; then
# Alias git in .bash_profile
cat <<-HERED >> $HOME/.bash_profile
function git_safety {
cmd=$1
shift
extra=""
if [ "$cmd" == "commit" ]; then
cmd=safetycommit
elif [ "$cmd" == "merge" ]; then
cmd=safetymerge
fi
"`which git`" "$cmd" "$@"
}
alias git=git_safety
HERED
# Echo instructions
echo !!! IN ALL OF YOUR SHELL WINDOWS/TABS, SOURCE ~/.bash_profile \
OR CLOSE AND RE-OPEN ALL OF YOUR SHELL WINDOWS/TABS !!!
else
# Echo instructions
echo ...success
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment