Skip to content

Instantly share code, notes, and snippets.

@zgohr
Created January 17, 2013 17:45
Show Gist options
  • Select an option

  • Save zgohr/4557894 to your computer and use it in GitHub Desktop.

Select an option

Save zgohr/4557894 to your computer and use it in GitHub Desktop.
Post-checkout git hook to auto-add a commit when new branch is created from ```develop```. Useful because ```reflog``` doesn't stay around forever, and we don't want to hold onto defunct feature branches until we are able to run reports. There is an existing flaw in this code, can you spot it?
#! /bin/sh
from_hash=$1
to_hash=$2
checkout_type=$3
ignored=("develop" "staging" "production")
develop_hash=$(git rev-parse develop)
branch_name=$(git rev-parse --abbrev-ref HEAD)
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
if [ $checkout_type -ne 1 ]
then
exit 0 ; # Not a branch checkout
fi
if containsElement $branch_name "${ignored[@]}"
then
exit 0 ; # Checked out an ignored branch
fi
if [ $to_hash != $develop_hash ]
then
exit 0 ; # Must checkout branch at same commit as develop
fi
if [ $from_hash != $to_hash ]
then
exit 0 ; # Not checking out a new branch
fi
# Create a commit
git commit --allow-empty -m "Created branch $branch_name"
@ashutoshm2006
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment