Skip to content

Instantly share code, notes, and snippets.

@bessgeor
Last active May 17, 2018 18:16
Show Gist options
  • Save bessgeor/2bdc15949401aa6c0de0cf8df970a97d to your computer and use it in GitHub Desktop.
Save bessgeor/2bdc15949401aa6c0de0cf8df970a97d to your computer and use it in GitHub Desktop.
#!/bin/sh
#inspired by HellBrick
commit_msg_file=$1
detached_head='unnamed branch'
current_branch="$(git symbolic-ref HEAD 2>/dev/null)" ||
current_branch="$detached_head"
pipe_separated_system_branches="smartgit-temp|bitbucket-temp"
ticket=`echo $current_branch | awk -F '__' '{ print $NF }' | grep -P '^[0-9]+\$'`
if [ ! -z "$ticket" ]
then
to_insert_prefix="issue::"
old_ticket=`cat $commit_msg_file | awk -F "$to_insert_prefix" '{ print $NF }' | grep -Po '^[0-9]+'`
echo "old ticket : $old_ticket" >&2
to_replace=" ( $to_insert_prefix$old_ticket )"
to_insert=" ( $to_insert_prefix$ticket )"
# hook should replace ticket id if it is already inserted
if [ -n "$old_ticket" ]
then
echo `sed "s/$to_replace/$to_insert/" $commit_msg_file` > $commit_msg_file
else
first_line=`head $commit_msg_file -n1`
text="${first_line}${to_insert}"
lines_count=$( cat $commit_msg_file | wc -l)
if [ $lines_count -gt 1 ]
then
lines_after_first=`tail $commit_msg_file -n +2`
text="${text}
${lines_after_first}"
fi
echo "$text" > $commit_msg_file
fi
else
should_fail=`echo $current_branch | grep -oP "(?:$pipe_separated_system_branches|$detached_head)"`
if [ -z "$should_fail" ]
then
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