Created
August 12, 2019 02:53
-
-
Save EtherZa/581d9276336353838b2c939f9554d479 to your computer and use it in GitHub Desktop.
Format c# with dotnet-format with pre-commit hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# modified from sample: https://prettier.io/docs/en/precommit.html | |
# | |
# install dotnet-format: dotnet tool install -g dotnet-format | |
# copy to .git/hooks/pre-commit and make executable | |
# | |
FILES=$(git diff --cached --name-only --diff-filter=ACM "*.cs" | sed 's| |\\ |g') | |
[ -z "$FILES" ] && exit 0 | |
# Format all selected files | |
echo "$FILES" | cat | xargs | sed -e 's/ /,/g' | xargs dotnet-format --files | |
# Add back the modified files to staging | |
echo "$FILES" | xargs git add | |
exit 0 |
Thanks @EtherZa!
Also, you could use the --staged
tag to select staged files only (but of course, if one always stages all the edited files, then there's no difference).
git diff --staged --name-only --diff-filter=ACM "*.cs"
I have abandoned this implementation as it is prone to errors. It assumes that a staged file does not have an unstaged change. Formatting is completed in the unstaged file which is then staged and committed.
Please refer to https://www.olioapps.com/blog/automatic-code-formatting/ for a better implementation.
@EtherZa are there instructions or a sample project on how to use that with dotnet format
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I didn't get this script working but I used it as a base and modified it to my needs.
dotnet format
has replaced--files
with--include
.https://gist.github.com/simonauner/09d4e3241f26d2bd139261816e6c18f6