Skip to content

Instantly share code, notes, and snippets.

@simonauner
Forked from EtherZa/pre-commit
Last active September 9, 2023 16:42
Show Gist options
  • Save simonauner/09d4e3241f26d2bd139261816e6c18f6 to your computer and use it in GitHub Desktop.
Save simonauner/09d4e3241f26d2bd139261816e6c18f6 to your computer and use it in GitHub Desktop.
Format c# with dotnet-format with pre-commit hook
#!/bin/sh
# Modified from https://gist.github.com/EtherZa/581d9276336353838b2c939f9554d479
#
# This script finds the files that are about to be committed,
# and runs dotnet format on them before adding them back to staging
#
# install dotnet-format: dotnet tool install -g dotnet-format
# make sure installed dotnet tools are on your path:
# export PATH="$PATH:$HOME/.dotnet/tools/"
# copy to .git/hooks/pre-commit and make executable
#
FILES=$(git diff --cached --name-only --diff-filter=ACM "*.cs")
[ -z "$FILES" ] && exit 0
echo "Running dotnet format for staged files"
dotnet format --include $FILES
# Add back the modified files to staging
echo "$FILES" | xargs git add
echo "dotnet format done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment