Skip to content

Instantly share code, notes, and snippets.

@CalvinAllen
Forked from simonauner/pre-commit
Created March 12, 2022 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CalvinAllen/b5c7fe325db4c61703fe36f0fd3afd4e to your computer and use it in GitHub Desktop.
Save CalvinAllen/b5c7fe325db4c61703fe36f0fd3afd4e 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