Backed out changeset e7acb4e12051 (bug 1893666) for causing xpcshell failures on...
[gecko.git] / security / nss / coreconf / precommit.clang-format.sh
blobb638b298e5e1456c6b03807f72e69b522da53653
1 #!/usr/bin/env bash
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:
8 # [hook]
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"
25 exit
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"
33 exit
36 echo "Hook already installed, or not in NSS repo"
37 exit 2
40 err=0
41 files=()
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'))
48 tmp=$(mktemp)
49 trap 'rm -f "$tmp"' ERR EXIT
50 for f in "${files[@]}"; do
51 ext="${f##*.}"
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
58 echo " $f" 1>&2
59 err=1
62 done
63 exit "$err"