Skip to content

Instantly share code, notes, and snippets.

@mfontani
Created February 26, 2013 14:57
Show Gist options
  • Save mfontani/5039004 to your computer and use it in GitHub Desktop.
Save mfontani/5039004 to your computer and use it in GitHub Desktop.
#!/bin/sh
# TO BE PLACED IN .git/hooks/prepare-commit-msg
# The first comments out the "Conflicts:" part of a merge commit.
# The second creates a template for a commit message
# The third example adds a Signed-off-by line to the message, that can
# still be edited. This is rarely a good idea.
# This hook is invoked by git commit right after preparing the default log
# message, and before the editor is started.
# It takes one to three parameters. The first is the name of the file that
# contains the commit log message. The second is the source of the commit
# message, and can be: message (if a -m or -F option was given); template (if a
# -t option was given or the configuration option commit.template is set);
# merge (if the commit is a merge or a .git/MERGE_MSG file exists); squash (if
# a .git/SQUASH_MSG file exists); or commit, followed by a commit SHA1 (if a
# -c, -C or --amend option was given).
# If the exit status is non-zero, git commit will abort.
# The purpose of the hook is to edit the message file in place, and it is not
# suppressed by the --no-verify option. A non-zero exit means a failure of the
# hook and aborts the commit. It should not be used as replacement for
# pre-commit hook.
# The sample prepare-commit-msg hook that comes with git comments out the
# Conflicts: part of a merge's commit message.
case "$2,$3" in
#merge,)
# /usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;
,|template,)
/usr/bin/perl -i.bak -nE '
BEGIN { $source = $ENV{XSOURCE}; $sha = $ENV{XSHA} };
if ( /^# On branch/ ) {
print "#$_";
say "## [MODULE] Declarative purpose of change";
say "#2345678901234567890123456789012345678901234567890123456789|";
say "";
say "## Nicer longer description of the context of the change, if";
say "## needed/warranted by the change; or no_diff";
say "";
say "######################## COMMITTING ########################";
say "# $_" for split("\n", `git diff --cached --stat`);
say "######################### THE DIFF #########################";
say "# $_" for split("\n", `git diff --cached`);
}
' "$1" ;;
*) ;;
esac
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment