tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

precommit.clang-format.sh (2165B)


      1 #!/usr/bin/env bash
      2 # This is a pre-commit hook for use with either mercurial or git.
      3 #
      4 # Install this by running the script with an argument of "install".
      5 #
      6 # All that does is add the following lines to .hg/hgrc:
      7 #
      8 # [hook]
      9 # pretxncommit.clang-format = [ ! -x ./coreconf/precommit.clang-format.sh ] || ./coreconf/precommit.clang-format.sh
     10 #
     11 # Or installs a symlink to .git/hooks/precommit:
     12 # $ ln -s ../../coreconf/precommit.clang-format.sh .git/hooks/pre-commit
     13 
     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
     17 
     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
     26        fi
     27    fi
     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
     34        fi
     35    fi
     36    echo "Hook already installed, or not in NSS repo"
     37    exit 2
     38 fi
     39 
     40 err=0
     41 files=()
     42 if [ "$hg" -eq 1 ]; then
     43    files=($(hg status -m -a --rev tip^:tip | cut -f 2 -d ' ' -))
     44 fi
     45 if [ "$git" -eq 1 ]; then
     46    files=($(git status --porcelain | sed '/^[MACU]/{s/..//;p;};/^R/{s/^.* -> //;p;};d'))
     47 fi
     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
     60        fi
     61    fi
     62 done
     63 exit "$err"