Skip to content

Instantly share code, notes, and snippets.

@tarjei
Created April 20, 2012 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarjei/2427245 to your computer and use it in GitHub Desktop.
Save tarjei/2427245 to your computer and use it in GitHub Desktop.
Git callaback to add issuenumber to commit message
#!/bin/bash
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
# Based on another example commit-msg hook I found here:
# https://review.typo3.org/Documentation/user-changeid.html
CHANGE_ID_AFTER="Bug|Issue"
MSG="$1"
add_ChangeId() {
clean_message=`sed -e '
/^diff --git a\/.*/{
s///
q
}
/^Re #/d
/^#/d
' "$MSG" | git stripspace`
if test -z "$clean_message"
then
echo "Clean message ok:"
echo $clean_message
return
fi
echo "Cleaned message:"
echo $clean_message
if grep -i '^Re: #' "$MSG" >/dev/null
then
return
fi
echo "Please enter relevant issuenr:"
exec < /dev/tty
while true; do
read -t 100 issueNr
break
done
if [ "$issueNr" = "" ]; then
echo "No issuenumber entered. Not changing message"
exit 0
fi
perl -e '
$MSG = shift;
$id = shift;
$CHANGE_ID_AFTER = shift;
undef $/;
open(I, $MSG); $_ = <I>; close I;
s|^diff --git a/.*||ms;
s|^#.*$||mg;
exit unless $_;
@message = split /\n/;
$haveFooter = 0;
$startFooter = @message;
for($line = @message - 1; $line >= 0; $line--) {
$_ = $message[$line];
if (/^[a-zA-Z0-9-]+:/ && !m,^[a-z0-9-]+://,) {
$haveFooter++;
next;
}
next if /^[ []/;
$startFooter = $line if ($haveFooter && /^\r?$/);
last;
}
@footer = @message[$startFooter+1..@message];
@message = @message[0..$startFooter];
push(@footer, "") unless @footer;
for ($line = 0; $line < @footer; $line++) {
$_ = $footer[$line];
next if /^($CHANGE_ID_AFTER):/i;
last;
}
splice(@footer, $line, 0, "Re: #$id");
$_ = join("\n", @message, @footer);
open(O, ">$MSG"); print O; close O;
' "$MSG" "$issueNr" "$CHANGE_ID_AFTER"
}
add_ChangeId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment