They fill: - the default commit message with the modified files list - the ChangeLog with the commit message
* trunk/git-hooks/README: New. * trunk/git-hooks/commit-msg: New. * trunk/git-hooks/post-commit: New. * trunk/git-hooks/post-receive: New. * trunk/git-hooks/prepare-commit-msg: New.
--- trunk/ChangeLog | 14 ++++++++++ trunk/git-hooks/README | 18 +++++++++++++ trunk/git-hooks/commit-msg | 50 ++++++++++++++++++++++++++++++++++++ trunk/git-hooks/post-commit | 44 +++++++++++++++++++++++++++++++ trunk/git-hooks/post-receive | 11 ++++++++ trunk/git-hooks/prepare-commit-msg | 46 +++++++++++++++++++++++++++++++++ 6 files changed, 183 insertions(+), 0 deletions(-) create mode 100644 trunk/git-hooks/README create mode 100755 trunk/git-hooks/commit-msg create mode 100755 trunk/git-hooks/post-commit create mode 100755 trunk/git-hooks/post-receive create mode 100755 trunk/git-hooks/prepare-commit-msg
diff --git a/trunk/ChangeLog b/trunk/ChangeLog index 5ffe38e..d8f8f7b 100644 --- a/trunk/ChangeLog +++ b/trunk/ChangeLog @@ -1,3 +1,17 @@ +2009-02-17 Vincent Ordy vincent.ordy@gmail.com + + Add Git hooks. + +They fill: +- the default commit message with the modified files list +- the ChangeLog with the commit message + + * trunk/git-hooks/README: New. + * trunk/git-hooks/commit-msg: New. + * trunk/git-hooks/post-commit: New. + * trunk/git-hooks/post-receive: New. + * trunk/git-hooks/prepare-commit-msg: New. + 2008-09-15 Roland Levillain roland@lrde.epita.fr
* src/git-lrde (lrde_dcommit): Be more verbose. diff --git a/trunk/git-hooks/README b/trunk/git-hooks/README new file mode 100644 index 0000000..319b1d5 --- /dev/null +++ b/trunk/git-hooks/README @@ -0,0 +1,18 @@ + README for LRDE git-hooks +--------------------------- + +* How to use: + +- cd to the root of the git repository of your project +- replace the .git/hooks directory with this directory (symbolic link) +- set Git options user.name and user.email with your full name and email + address + + +* What does it do? + +It fills: +- the commit message with the list of new/deleted/modified files +- the ChangeLog with the commit message + +If using Git to track a SVN repository, use git-lrde to commit your patches. diff --git a/trunk/git-hooks/commit-msg b/trunk/git-hooks/commit-msg new file mode 100755 index 0000000..38c4dd7 --- /dev/null +++ b/trunk/git-hooks/commit-msg @@ -0,0 +1,50 @@ +#!/bin/sh + +## Init +set -e +file="$1" +error=false + + +## Functions +stderr() +{ + echo >&2 "$@" +} + +warning() +{ + stderr "warning: $@" +} + + +## Main +# Check commit title. +if head -1 "$file"|grep -q '^.$'; then + warning 'It looks like you did not fill your commit title.' + error=true +elif head -1 "$file"|grep -q -v '.'; then + warning 'It looks like your commit title is invalid.' + error=true +fi + +# Check entries. +if grep -q ': .$' "$file"; then + warning 'It looks like you did not fill all entries in the ChangeLog:' + grep >&2 ': .$' "$file" + error=true +fi + +# Check 80 columns. +if grep -q '^.{80,}' "$file"; then + warning 'Please avoid long lines in your ChangeLog entry (80 columns max):' + grep >&2 '^.{80,}' "$file" + error=true +fi + +# Restart editor on error +if $error; then + sleep 2 + "$GIT_EDITOR" "$file" # FIXME: ViM mess the terminal up... + exec "$0" "$@" +fi diff --git a/trunk/git-hooks/post-commit b/trunk/git-hooks/post-commit new file mode 100755 index 0000000..f6b6509 --- /dev/null +++ b/trunk/git-hooks/post-commit @@ -0,0 +1,44 @@ +#!/bin/sh + +## Init +set -e + +## Functions +stderr() +{ + echo >&2 "$@" +} + +warning() +{ + stderr "warning: $@" +} + +## Find the ChangeLog +#FIXME: Try to find the deeper ChangeLog in hierarchy that match all the files +# instead of taking the more generic one. +changelog=$(find -name ChangeLog | \ + awk 'BEGIN { FS="/"; } { print NF-1, $0 }' | \ + sort -k1 | \ + head -1 | \ + sed 's/^[0-9]* (.*)$/\1/') +if test x"$changelog" = x -o ! -f "$changelog"; then + warning 'Your commit will NOT be added to a ChangeLog' + exit 0 +fi + +## Add the log to the ChangeLog +mv "$changelog" "$changelog".old +git > "$changelog" \ + log -n1 --date=short \ + --pretty="format:%ad %an <%ae>%n%n %s%n%n%b%n" +cat >> "$changelog" "$changelog".old +rm "$changelog".old + +## Amend the last commit to add the ChangeLog +git add "$changelog" +tree_ref=$(git write-tree) +commit_ref=$(git log -n1 --pretty="format:%s%n%n%b%n" | \ + git commit-tree $tree_ref -p 'HEAD^') +git update-ref HEAD $commit_ref +git gc --auto diff --git a/trunk/git-hooks/post-receive b/trunk/git-hooks/post-receive new file mode 100755 index 0000000..7ab4c3d --- /dev/null +++ b/trunk/git-hooks/post-receive @@ -0,0 +1,11 @@ +#!/bin/sh + +# Please set: +# * hooks.mailinglist (patches) +# * hooks.announcelist (tags) +# * hooks.envelopesender (EMAIL) +# * hooks.emailprefix (project name) + +# /!\ Does not work with git-svn. + +. /usr/share/doc/git-core/contrib/hooks/post-receive-email diff --git a/trunk/git-hooks/prepare-commit-msg b/trunk/git-hooks/prepare-commit-msg new file mode 100755 index 0000000..c26884e --- /dev/null +++ b/trunk/git-hooks/prepare-commit-msg @@ -0,0 +1,46 @@ +#!/bin/sh + +## Init +set -e +file="$1" +type="$2" + +test x"$type" = x || exit 0 ## not a normal commit message + +## Functions + +# Format the "git st" output +format_filelist() +{ + sed -n -e ' +s/deleted/Remove/g +s/modified//g +s/new file/New/g +s/^# renamed: *(.*) -> (.*)/ * \1: Rename as...\n * \2: ...this./pg +s/^# ([^:]*): *(.*)/ * \2: \1./pg +' +} + +# Check whether $1 is set in Git config. +check_var() +{ + if test x"$(git config --get $1 || :)" = x; then + echo >&2 "Please set variable $1 (git config --add $1 value)" + exit 1 + fi +} + +## Main +# Check whether FULLNAME and EMAIL are set and not empty. +check_var user.name +check_var user.email + +# Insert filelist at beginning of commit message +mv "$file" "$file.bak" +cat > "$file" <<EOF +. + +EOF +git-status|format_filelist >> "$file" +cat >> "$file" "$file.bak" +rm "$file.bak"