2 # This is a pre-commit hook for use with either mercurial or git.
4 # Install this by running the script with an argument of "install".
6 # All that does is add the following lines to .hg/hgrc:
9 # pretxncommit.clang-format = [ ! -x ./coreconf/precommit.clang-format.sh ] || ./coreconf/precommit.clang-format.sh
11 # Or installs a symlink to .git/hooks/precommit:
12 # $ ln -s ../../coreconf/precommit.clang-format.sh .git/hooks/pre-commit
14 hash clang-format ||
exit 1
15 [ "$(hg root 2>/dev/null)" = "$PWD" ] && hg
=1 || hg
=0
16 [ "$(git rev-parse --show-toplevel 2>/dev/null)" = "$PWD" ] && git
=1 || git
=0
18 if [ "$1" = "install" ]; then
19 if [ "$hg" -eq 1 ]; then
20 hgrc
="$(hg root)"/.hg
/hgrc
21 if ! grep -q '^pretxncommit.clang-format' "$hgrc"; then
22 echo '[hooks]' >> "$hgrc"
23 echo 'pretxncommit.clang-format = [ ! -x ./coreconf/precommit.clang-format.sh ] || ./coreconf/precommit.clang-format.sh' >> "$hgrc"
24 echo "Installed mercurial pretxncommit hook"
28 if [ "$git" -eq 1 ]; then
29 hook
="$(git rev-parse --show-toplevel)"/.git
/hooks
/pre-commit
30 if [ ! -e "$hook" ]; then
31 ln -s ..
/..
/coreconf
/precommit.clang-format.sh
"$hook"
32 echo "Installed git pre-commit hook"
36 echo "Hook already installed, or not in NSS repo"
42 if [ "$hg" -eq 1 ]; then
43 files
=($
(hg status
-m -a --rev tip^
:tip | cut
-f 2 -d ' ' -))
45 if [ "$git" -eq 1 ]; then
46 files
=($
(git status
--porcelain |
sed '/^[MACU]/{s/..//;p;};/^R/{s/^.* -> //;p;};d'))
49 trap 'rm -f "$tmp"' ERR EXIT
50 for f
in "${files[@]}"; do
52 if [ "$ext" = "c" -o "$ext" = "h" -o "$ext" = "cc" ]; then
53 [ "$hg" -eq 1 ] && hg
cat -r tip
"$f" > "$tmp"
54 [ "$git" -eq 1 ] && git show
:"$f" > "$tmp"
55 if ! cat "$tmp" | clang-format
-assume-filename="$f" | \
56 diff -q "$tmp" - >/dev
/null
; then
57 [ "$err" -eq 0 ] && echo "Formatting errors found in:" 1>&2