Skip to content

Instantly share code, notes, and snippets.

@matthiasbeyer
Last active May 1, 2022 08:07
Show Gist options
  • Save matthiasbeyer/44350d5d9e30c0c5e8649fd9a55d19f6 to your computer and use it in GitHub Desktop.
Save matthiasbeyer/44350d5d9e30c0c5e8649fd9a55d19f6 to your computer and use it in GitHub Desktop.
Non-failing cargo fmt git hook for files patched in current staging
#!/usr/bin/env bash
Green='\e[0;32m' # Green
Red='\e[0;31m' # Red
Color_Off='\e[0m' # Text Reset
files=$(cargo fmt -- --check --files-with-diff)
staged=$(git diff --name-only --staged)
list_files=""
for file in $staged; do
f=$(echo "$files" | xargs -n 1 echo | grep "$file")
list_files="$list_files $f"
done
if [ -z "$list_files" ]; then
echo -e "${Green}No format fails${Color_Off}"
else
echo -e "${Red}These files in HEAD have formatting issues:${Color_Off}"
echo -e ""
echo -e "$list_files" | sed "s,$(realpath "$PWD")/,,g" | xargs -n 1 echo | sed 's,^,\t,'
echo -e ""
echo -e "${Red}Your code has formatting issues in files listed above.${Color_Off}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment