-
-
Save EtherZa/581d9276336353838b2c939f9554d479 to your computer and use it in GitHub Desktop.
#!/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 |
@dazinator you could try removing the --cached
option.
In other words change this...
FILES=$(git diff --cached --name-only --diff-filter=ACM "*.cs" | sed 's| |\\ |g')
...to this.
FILES=$(git diff --name-only --diff-filter=ACM "*.cs" | sed 's| |\\ |g')
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
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
?
Thanks for this @EtherZa. I have tried this, but it doesn't seem to be working.
If I add some weird formatting to "Program.cs" - 2 extra spaces before "public class Program":
Now, if I run dotnet-format in the project directory, it corrects this.
However if I don't run dotnet-format, and instead commit this file and rely on the pre-commit hook, it doesn't get corrected.
Any ideas why this might be the case?