Skip to main content

Pre-commit Hook

CSharpier can be used with a pre-commit hook to ensure that all staged files are formatted before being committed.

pre-commit

Install pre-commit via your preferred Python package manager. Run pre-commit install to install the Git hook scripts.

MegaLinter

CSharpier runs as part of MegaLinter's pre-commit hooks.

Standalone

If you prefer not to run the other linters included in MegaLinter, you can alternatively run CSharpier as a local pre-commit hook.

Add the following to your .pre-commit-config.yaml:

repos:
- repo: local
hooks:
- id: dotnet-tool-restore
name: Install .NET tools
entry: dotnet tool restore
language: system
always_run: true
pass_filenames: false
stages:
- commit
- push
- post-checkout
- post-rewrite
description: Install the .NET tools listed at .config/dotnet-tools.json.
- id: csharpier
name: Run CSharpier on C# files
entry: dotnet tool run dotnet-csharpier
language: system
types:
- c#
description: CSharpier is an opinionated C# formatter inspired by Prettier.

Husky.Net

From the root of your repository

cd <Your project root directory>
dotnet new tool-manifest
dotnet tool install husky
dotnet husky install

Modify the file at .husky/task-runner.json

{
"tasks": [{
"name": "Run csharpier",
"command": "dotnet",
"args": [ "csharpier", "${staged}" ],
"include": [ "**/*.cs" ]
}]
}

You can run and test your task with the following command.

dotnet husky run

Optionally - add this to one of your projects to automate the installation for future developers You can set the HUSKY environment variable to 0 to disable Husky in CI/CD pipelines.

dotnet husky attach <path-to-project-file>

Once you are sure the task is working properly, you can add it as a pre-commit hook.

dotnet husky add pre-commit -c "dotnet husky run"

If you want the pre-commit hook to be opt in, ignore the .husky/pre-commit file. It can be enabled by individual developers if the run the command above.