pre-commit.hook (1659B)
1 #!/bin/sh 2 # Source: https://gitlab.gnome.org/GNOME/fractal/blob/master/hooks/pre-commit.hook 3 4 install_rustfmt() { 5 if ! which rustup >/dev/null 2>&1; then 6 curl https://sh.rustup.rs -sSf | sh -s -- -y 7 export PATH=$PATH:$HOME/.cargo/bin 8 if ! which rustup >/dev/null 2>&1; then 9 echo "Failed to install rustup. Performing the commit without style checking." 10 exit 0 11 fi 12 fi 13 14 if ! rustup component list|grep rustfmt >/dev/null 2>&1; then 15 echo "Installing rustfmt…" 16 rustup component add rustfmt 17 fi 18 } 19 20 if ! which cargo >/dev/null 2>&1 || ! cargo fmt --help >/dev/null 2>&1; then 21 echo "Unable to check the project’s code style, because rustfmt could not be run." 22 23 if [ ! -t 1 ]; then 24 # No input is possible 25 echo "Performing commit." 26 exit 0 27 fi 28 29 echo "" 30 echo "y: Install rustfmt via rustup" 31 echo "n: Don't install rustfmt and perform the commit" 32 echo "Q: Don't install rustfmt and abort the commit" 33 34 echo "" 35 while true 36 do 37 printf "%s" "Install rustfmt via rustup? [y/n/Q]: "; read yn < /dev/tty 38 case $yn in 39 [Yy]* ) install_rustfmt; break;; 40 [Nn]* ) echo "Performing commit."; exit 0;; 41 [Qq]* | "" ) echo "Aborting commit."; exit 1 >/dev/null 2>&1;; 42 * ) echo "Invalid input";; 43 esac 44 done 45 46 fi 47 48 echo "--Checking style--" 49 cargo fmt --all -- --check 50 if test $? != 0; then 51 echo "--Checking style fail--" 52 echo "Please fix the above issues, either manually or by running: cargo fmt --all" 53 54 exit 1 55 else 56 echo "--Checking style pass--" 57 fi