Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format in pre-commit hook #1207

Closed
nojaf opened this issue Oct 28, 2020 · 3 comments · Fixed by #1392
Closed

Format in pre-commit hook #1207

nojaf opened this issue Oct 28, 2020 · 3 comments · Fixed by #1392

Comments

@nojaf
Copy link
Contributor

nojaf commented Oct 28, 2020

As mentioned in https://twitter.com/rastreus/status/1321296796124143617?s=20, how can Fantomas be used as a precommit hook?

On top of my head I think the local dotnet tool can be used for this. However, as I find this a good question we should write something out in the documentation to explain this.

Also mention in the docs that .fantomasignore file exist in case Fantomas is not able to format the file for some reason.

@nojaf
Copy link
Contributor Author

nojaf commented Nov 2, 2020

@rastreus this seems to work for me (on Windows):

#!/bin/sh
FILES=$(git diff --cached --name-only --diff-filter=ACMR "*.fs" "*.fsx" | sed 's| |\\ |g')
[ -z "$FILES" ] && exit 0

# Format all selected files
echo "$FILES" | xargs dotnet fantomas

# Add back the modified/prettified files to staging
echo "$FILES" | xargs git add

exit 0

I stole some code from other places so I can't really explain each line 😅, but the dotnet fantomas part is doing the formatting.

@danalexilewis
Copy link

danalexilewis commented Nov 4, 2020

@nojaf i wrote something very similar to this once to take the diff and pass only those files with changes to the shopify cli for deployment.

Basically:

Line 1 gets the changed files filtering the git diff to what your after
Line 2 passes the files as args to Fantomas executed with dotnet
Line 3 stage them with git

I thinK the way to to this in a more automated way will be to create a git hook that runs the lines 1 and 2 of your script as a ‘pre-commit hook’.

That should do it.

@nojaf
Copy link
Contributor Author

nojaf commented Nov 4, 2020

Hey @agentlewis, this is the content of the pre-commit file, so it is already a hook ;)

nojaf added a commit that referenced this issue Jan 27, 2021
* Documentation.md: add pre-commit hook sample

Following https://stackoverflow.com/a/42345379/1623521

Fixes #1207

* Add fair warning

Co-authored-by: nojaf <florian.verdonck@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants