Created
May 5, 2016 14:39
-
-
Save mfontani/5ed8904652115a6bc9983fb6dfa0b51d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# 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. | |
>&2 echo "File: '$1' Source: '$2' Commit: '$3'" | |
>&2 echo "Environment:" | |
_git_vars=$( env | grep GIT ) | |
>&2 echo "$_git_vars" | |
>&2 echo "#######################" | |
>&2 cat "$1" | |
>&2 echo "#######################" | |
case "$2,$3" in | |
#merge,) | |
# /usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;; | |
# During a git commit --amend: | |
commit,HEAD) | |
/usr/bin/perl -i.bak -nE ' | |
BEGIN { $source = $ENV{XSOURCE}; $sha = $ENV{XSHA} }; | |
if (!$first_line++) { | |
say "## [MODULE] Declarative purpose of change"; | |
say "#2345678901234567890123456789012345678901234567890123456789|"; | |
print; | |
} | |
elsif (/^# Please enter the commit message for your changes/) { | |
say "######################### AMENDING #########################"; | |
say "# $_" for split("\n", `git diff HEAD^ --stat`); | |
say "######################### THE DIFF #########################"; | |
say "# $_" for split("\n", `git diff HEAD^`); | |
say "############################################################"; | |
print; | |
} | |
else { | |
print; | |
} | |
' "$1" ;; | |
# During a squash | |
message,) | |
/usr/bin/perl -i.bak -nE ' | |
BEGIN { $source = $ENV{XSOURCE}; $sha = $ENV{XSHA} }; | |
if (/^# The first commit/) { | |
print; | |
say "## [MODULE] Declarative purpose of change"; | |
say "#2345678901234567890123456789012345678901234567890123456789|"; | |
} | |
elsif (/^# Please enter the commit message for your changes/) { | |
say "######################## SQUASHING #########################"; | |
say "# $_" for split("\n", `git diff HEAD^ --stat`); | |
say "######################### THE DIFF #########################"; | |
say "# $_" for split("\n", `git diff HEAD^`); | |
say "############################################################"; | |
print; | |
} | |
else { | |
print; | |
} | |
' "$1" ;; | |
# During a standard git commit, or during a commit "edit" | |
,|template,) | |
splitting_commit=$( grep "You are currently splitting a commit while rebasing" "$1" ) | |
SPLITTING="$splitting_commit" /usr/bin/perl -i.bak -nE ' | |
BEGIN { $source = $ENV{XSOURCE}; $sha = $ENV{XSHA}; $printed = 0 } | |
if (!$first_line++) { | |
say "## [MODULE] Declarative purpose of change"; | |
say "#2345678901234567890123456789012345678901234567890123456789|"; | |
if ($ENV{SPLITTING}) { | |
say "# Original message:"; | |
say for split("\n", `git show -s --format="%B" ORIG_HEAD`); | |
} else { | |
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`); | |
print "# $_"; | |
} else { print } | |
' "$1" ;; | |
*) | |
>&2 echo "prepare-commit-message had '$2,$3'" | |
;; | |
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