Skip to content

Instantly share code, notes, and snippets.

@bessgeor
Created January 11, 2018 06:50
Show Gist options
  • Save bessgeor/bdd80fa2b336552f6b17cae079f6965f to your computer and use it in GitHub Desktop.
Save bessgeor/bdd80fa2b336552f6b17cae079f6965f to your computer and use it in GitHub Desktop.
#!/bin/sh
#inspired by HellBrick
function replace_commit_message {
commit_msg=$1
ticket=$2
to_insert=" ( ticket #$ticket )"
# hook should not insert ticket id if it is already inserted
already_ticketed=`grep -c "$to_insert" $commit_msg_file`
if [ $already_ticketed -eq 0 ]
then
first_line=$(echo "$commit_msg" | head -n1)
text="${first_line}${to_insert}"
lines_count=$(echo "$commit_msg" | wc -l)
if [ $lines_count -gt 1 ]
then
lines_after_first=$(echo "$commit_msg" | tail -n +2)
text="${text}
${lines_after_first}"
fi
echo "$text"
fi
}
commit_msg_file=$1
commit_type=$2
if [ "$commit_type" = "merge" ]
then
githead=$(env | grep GITHEAD)
source="${githead##*=}"
target=$(git symbolic-ref HEAD)
ticket=`awk -F '/' '{ print $NF }' <<< "$source" | grep -P '^[0-9]+\$'`
if [ ! -z "$ticket" ]
then
# func call against cat & ticket should be instead of 'lel'
git filter-branch --msg-filter 'lel' $target..$source
else
echo 'no ticket id in the branch name' >&2
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment