coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] maint: add git hook scripts


From: Jim Meyering
Subject: [PATCH] maint: add git hook scripts
Date: Sun, 23 Oct 2011 11:53:00 +0200

FYI, I've just pushed this.
If you find yourself making commits for coreutils or running
"git am FILE", you can try these hooks by running e.g.,

  cd .git/hooks && cp -a ../../scripts/git-hooks/* .

You could also make symlinks, but if ever you switched to a branch
without these new files, your symlinks would dangle.

Before too long, I expect to make ./bootstrap perform that copy
operation, assuming that the destination files don't already exist.
Even if they do exist, I may have bootstrap use cp --backup=numbered.
Then, any previous script (if you had one) would not be lost.



>From 2ade806cf5740fa0ef0f7211699e5b4e7ce90464 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Thu, 20 Oct 2011 17:26:01 +0200
Subject: [PATCH] maint: add git hook scripts
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

We find it worthwhile to use consistent commit summary prefixes.
To that end, the commit-msg script requires that all commits I make
start with "$P: " (where $P is one of ~100 programs in coreutils)
or one of a few other words, like gnulib tests maint doc build.
It allows more than one word, so e.g., "cat tail head: " would also
be accepted.  Pádraig Brady wrote the initial version, with its
72-column and blank-if-present second line checks.
The pre-commit script is the same as the git-supplied sample script,
modulo a bug fix and the "exec 1>&2" redirection.
* scripts/git-hooks/commit-msg: New file.
* scripts/git-hooks/pre-commit: New file.
* scripts/git-hooks/applypatch-msg: New file.  Verbatim from .sample.
* cfg.mk: Exempt two of the new scripts from the no-leading-TABs check,
since they're nearly verbatim from git, and we want to stay in sync.
Exempt the commit-msg script from the no-"fail=0" check.
---
 cfg.mk                           |   10 +++--
 scripts/git-hooks/applypatch-msg |   15 ++++++++
 scripts/git-hooks/commit-msg     |   74 ++++++++++++++++++++++++++++++++++++++
 scripts/git-hooks/pre-commit     |   50 +++++++++++++++++++++++++
 4 files changed, 145 insertions(+), 4 deletions(-)
 create mode 100755 scripts/git-hooks/applypatch-msg
 create mode 100755 scripts/git-hooks/commit-msg
 create mode 100755 scripts/git-hooks/pre-commit

diff --git a/cfg.mk b/cfg.mk
index c3102be..fa7f844 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -384,13 +384,15 @@ exclude_file_name_regexp--sc_file_system = \
 exclude_file_name_regexp--sc_prohibit_always_true_header_tests = \
   ^m4/stat-prog\.m4$$
 exclude_file_name_regexp--sc_prohibit_fail_0 = \
-  (^tests/init\.sh|Makefile\.am|\.mk)$$
+  (^scripts/git-hooks/commit-msg|^tests/init\.sh|Makefile\.am|\.mk)$$
 exclude_file_name_regexp--sc_prohibit_atoi_atof = ^lib/euidaccess-stat\.c$$
+
+tbi_1 = 
^tests/pr/|(^gl/lib/reg.*\.c\.diff|Makefile(\.am)?|\.mk|^man/help2man)$$
+tbi_2 = ^scripts/git-hooks/(pre-commit|applypatch-msg)$$
 exclude_file_name_regexp--sc_prohibit_tab_based_indentation = \
-  ^tests/pr/|(^gl/lib/reg.*\.c\.diff|Makefile(\.am)?|\.mk|^man/help2man)$$
+  $(tbi_1)|$(tbi_2)
+
 exclude_file_name_regexp--sc_preprocessor_indentation = \
   ^(gl/lib/rand-isaac\.[ch]|gl/tests/test-rand-isaac\.c)$$
-
-
 exclude_file_name_regexp--sc_prohibit_stat_st_blocks = \
   ^(src/system\.h|tests/du/2g)$$
diff --git a/scripts/git-hooks/applypatch-msg b/scripts/git-hooks/applypatch-msg
new file mode 100755
index 0000000..8b2a2fe
--- /dev/null
+++ b/scripts/git-hooks/applypatch-msg
@@ -0,0 +1,15 @@
+#!/bin/sh
+#
+# An example hook script to check the commit log message taken by
+# applypatch from an e-mail 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.
+#
+# To enable this hook, rename this file to "applypatch-msg".
+
+. git-sh-setup
+test -x "$GIT_DIR/hooks/commit-msg" &&
+       exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"}
+:
diff --git a/scripts/git-hooks/commit-msg b/scripts/git-hooks/commit-msg
new file mode 100755
index 0000000..77703fa
--- /dev/null
+++ b/scripts/git-hooks/commit-msg
@@ -0,0 +1,74 @@
+#!/bin/sh
+# A hook script to check the commit log message.
+
+log_file=$1
+export log_file
+
+re_edit()
+{
+  read -p "Hit return to edit. Ctrl-C to abort..." v 1>&2
+  ${EDITOR:-vi} "$log_file"
+}
+
+get_msg()
+{
+  sed '/^#/d' "$log_file" #filter comments
+}
+
+check_msg()
+{
+  # First line must contain a colon, e.g., "keyword: ...".
+  line_1=$(get_msg | sed 1q)
+  case $line_1 in
+    *:*) ;;
+    [Vv]ersion' '[0-9]*) return 0;;
+    *) echo "missing colon on first line of log message"; return 1;;
+  esac
+
+  # The token(s) before the colon on the first line must be one of
+  # the following.  Tokens may be space- or comma-separated.
+  fail=0
+  for w in $(echo "$line_1"|sed 's/:.*//'|tr -s ' ,' '  '); do
+    case $w in
+      # program names
+      \[|arch|base64|basename|cat|chcon|chgrp|chmod|chown|chroot) ;;
+      cksum|comm|cp|csplit|cut|date|dd|df|dir|dircolors|dirname|du) ;;
+      echo|env|expand|expr|factor|false|fmt|fold|groups|head|hostid) ;;
+      hostname|id|install|join|kill|link|ln|logname|ls|md5sum|mkdir) ;;
+      mkfifo|mknod|mktemp|mv|nice|nl|nohup|nproc|od|paste|pathchk) ;;
+      pinky|pr|printenv|printf|ptx|pwd|readlink|rm|rmdir|runcon) ;;
+      seq|sha1sum|sha224sum|sha256sum|sha384sum|sha512sum|shred|shuf) ;;
+      sleep|sort|split|stat|stdbuf|stty|su|sum|sync|tac|tail|tee) ;;
+      test|timeout|touch|tr|true|truncate|tsort|tty|uname|unexpand) ;;
+      uniq|unlink|uptime|users|vdir|wc|who|whoami|yes) ;;
+      # other tags
+      gnulib|tests|maint|doc|build|scripts) ;;
+      *) echo "invalid first word of summary line: $w"; fail=1;;
+    esac
+  done
+  test $fail = 1 && return 1
+
+  # Limit line length to allow for tab in changelog
+  test $(get_msg | wc -L | cut -f1 -d' ') -gt 72 \
+    && { echo "line > 72 chars"; return 1; }
+
+  # Second line should be blank or not present
+  test "$(get_msg | sed -n 2p)" \
+    && { echo "second line should be blank"; return 1; }
+
+  # Flag redundant use of "issue"
+  get_msg | grep -Fi "issue reported by" >&2 \
+    && { echo "just say: Reported by ..."; return 1; }
+
+  return 0;
+}
+
+while :; do
+  err=$(check_msg) && break
+  ME=${0##*/}
+  err="$ME: $err"
+  # Insert the diagnostic as a comment on the first line of $log_file.
+  perl -ni -e '$. == 1 and print "# '"$err"'\n"; print' $log_file
+  printf '%s\n' "$err" 1>&2
+  re_edit
+done
diff --git a/scripts/git-hooks/pre-commit b/scripts/git-hooks/pre-commit
new file mode 100755
index 0000000..1addec5
--- /dev/null
+++ b/scripts/git-hooks/pre-commit
@@ -0,0 +1,50 @@
+#!/bin/sh
+#
+# An example hook script to verify what is about to be committed.
+# Called by "git commit" with no arguments.  The hook should
+# exit with non-zero status after issuing an appropriate message if
+# it wants to stop the commit.
+#
+# To enable this hook, rename this file to "pre-commit".
+
+if git rev-parse --verify HEAD >/dev/null 2>&1
+then
+       against=HEAD
+else
+       # Initial commit: diff against an empty tree object
+       against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
+fi
+
+# If you want to allow non-ascii filenames set this variable to true.
+allownonascii=$(git config hooks.allownonascii)
+
+# Redirect output to stderr.
+exec 1>&2
+
+# Cross platform projects tend to avoid non-ascii filenames; prevent
+# them from being added to the repository. We exploit the fact that the
+# printable range starts at the space character and ends with tilde.
+if [ "$allownonascii" != "true" ] &&
+       # Note that the use of brackets around a tr range is ok here, (it's
+       # even required, for portability to Solaris 10's /usr/bin/tr), since
+       # the square bracket bytes happen to fall in the designated range.
+       test "$(git diff --cached --name-only --diff-filter=A -z $against |
+         LC_ALL=C tr -d '[ -~]\0' | wc -c)" != 0
+then
+       echo "Error: Attempt to add a non-ascii file name."
+       echo
+       echo "This can cause problems if you want to work"
+       echo "with people on other platforms."
+       echo
+       echo "To be portable it is advisable to rename the file ..."
+       echo
+       echo "If you know what you are doing you can disable this"
+       echo "check using:"
+       echo
+       echo "  git config hooks.allownonascii true"
+       echo
+       exit 1
+fi
+
+# If there are whitespace errors, print the offending file names and fail.
+exec git diff-index --check --cached $against --
--
1.7.7.419.g87009



reply via email to

[Prev in Thread] Current Thread [Next in Thread]