Skip to content

Instantly share code, notes, and snippets.

@deniskyashif
Last active September 30, 2021 08:01
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 deniskyashif/6d4ee707efa94b11bc3950856eff5688 to your computer and use it in GitHub Desktop.
Save deniskyashif/6d4ee707efa94b11bc3950856eff5688 to your computer and use it in GitHub Desktop.
Git pre-commit hook to run dotnet format on the staged files.
#!/bin/sh
cs_files="$(git diff --name-only --cached --diff-filter=d | grep -E '\.cs$')"
if [ -n "$cs_files" ]; then
cs_files_seperated_by_comma="$(echo $cs_files | tr ' ' ,)"
dotnet format --workspace <Project-Root> --files $cs_files_seperated_by_comma
git add $cs_files
fi
@kjeske
Copy link

kjeske commented Sep 29, 2021

I suggest using: git diff --name-only --cached --diff-filter=d | grep -E '\.cs$' to omit deleted files, so dotnet format doesn't crash.

@deniskyashif
Copy link
Author

I suggest using: git diff --name-only --cached --diff-filter=d | grep -E '\.cs$' to omit deleted files, so dotnet format doesn't crash.

Good catch, noted, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment