Husky.Net Documentation

repository·master·Indexed 21 days ago

https://github.com/alirezanet/husky.net

A Git hooks automation tool for .NET developers inspired by husky and lint-staged. Husky.Net enables automated linting, testing, and formatting during Git lifecycle events like commits and pushes. It features an internal task runner, support for all Git and git-flow hooks, C# scripting (.csx) for complex logic, and file state awareness (staged, last-commit, etc.). It is cross-platform and compatible with tools like dotnet-format, CSharpier, and ReSharper command line tools.

Tokens
8.7K
Snippets
33
Records
41
Agent score
74%

What's inside Husky.Net

  1. What is Husky.Net

    master
    Husky.Net is a tool for .NET developers that brings Git hooks (inspired by the JS husky and lint-staged tools) to the .NET ecosystem. It allows you to automate tasks such as linting commit messages, running tests, formatting code, or performing analysis automatically during Git events like commit or push. It is designed to prevent poor-quality code from entering your codebase by running checks against staged files or other Git states.
  2. Overview of Husky.Net features

    master

    Husky.Net brings the dev-dependency concept to the .NET ecosystem, allowing you to manage Git hooks and automate development workflows.

    Key features include:

    • Git hooks support: Works with all Git and Git-flow hooks using modern Git features.
    • Task Runner: A powerful engine to manage and execute linters, tests, scripts, and other development tasks.
    • Automation: Can be configured to run tasks automatically when contributors interact with the repository.
    • Multiple File States: Supports filtering tasks based on file states such as staged, last-commit, git-files, and custom user-defined states.
    • C# Scripting: Allows for complex task logic using C# scripting.
  3. Overview of Husky.Net

    master
    Husky.Net is a tool for .NET developers inspired by the JavaScript husky and lint-staged ecosystem. It allows you to automate git hooks to run linters, tests, code formatters, or any other quality checks automatically during commit or push operations. It aims to prevent broken code or poor commit messages from entering the repository by catching issues locally before they reach CI/CD pipelines.
  4. Use Glob patterns for file selection

    master

    Husky.Net uses standard .NET FileSystemGlobbing patterns for the include and exclude properties.

    Common Patterns:

    • *.txt: All files with a .txt extension.
    • *.*: All files with any extension.
    • *: All files in the top-level directory.
    • *word*: All files containing 'word' in the filename.
    • styles/*.css: All .css files in the styles/ directory.
    • **/*: All files in any subdirectory (arbitrary depth).
    • dir/**/*: All files in any subdirectory under dir/.
    • ../shared/*: Relative paths to match files in a sibling directory.
  5. How Husky handles Git submodules

    master

    Husky manages Git submodules using two distinct behaviors depending on your configuration:

    1. Default Mode: Husky installs hooks directly into the submodule's Git directory (e.g., .git/modules/mySubmodule). In this mode, hooks are only executed when you issue Git commands from inside the submodule folder.
    2. Ignored Mode: Husky completely ignores project hooks and Husky steps when the project is identified as a Git submodule. This is achieved by using the --ignore-submodule flag during installation.

    When running the default installation in a submodule, Husky will output a notification indicating where the hooks are being attached: Submodule detected, attaching .../Repository/Project/mySubmodule/.husky hooks to .../Repository/Project/.git/modules/mySubmodule

  6. Key features of Husky.Net

    master

    Husky.Net provides several advanced capabilities for managing Git workflows:

    • Task Runner: Includes an internal task runner to execute various checks.
    • Git Hook Support: Supports all Git and gitflow hooks.
    • File State Awareness: Can target specific file states such as staged, last-commit, or general git-files.
    • Scripting: Supports CSharp scripts (.csx) for complex logic.
    • Cross-Platform: Works on macOS, Linux, and Windows.
    • Modern Git Integration: Powered by the core.hooksPath Git feature.
    • Extensibility: Supports user-defined variables and custom directories.
    • Tool Compatibility: Works seamlessly with dotnet-format, CSharpier, ReSharper command line tools, and other formatting tools.
    • Advanced Workflows: Supports Monorepos and Git GUIs.
  7. Attach Husky to a project using MsBuild targets

    master

    When attaching Husky via MsBuild, you can use the --ignore-submodule option within the generated target. To skip this target, set the IgnoreSubmodule variable to 0 (similar to how the HUSKY variable is used).

    If you are attaching Husky manually, copy the following target block to your .csproj file and ensure you adjust the <HuskyRoot> value to the relative path from your project to the repository root.

    Note: If you want the MsBuild target to run but still want the actual submodule hooks to be ignored, remove the and '$(IgnoreSubmodule)' != 0 condition from the Condition attribute of the <Target>.

    <PropertyGroup>
       <!-- Update this to the relative path from your project to the repo root -->
       <HuskyRoot Condition="$(HuskyRoot)' == ''">../../</HuskyRoot>
    </PropertyGroup>
    <Target Name="Husky" AfterTargets="Restore" Condition="'$(HUSKY)' != 0  and '$(IgnoreSubmodule)' != 0"
            Inputs="$(HuskyRoot).config/dotnet-tools.json"
            Outputs="$(HuskyRoot).husky/_/install.stamp">
       <Exec Command="dotnet tool restore"  StandardOutputImportance="Low" StandardErrorImportance="High"/>
       <Exec Command="dotnet husky install --ignore-submodule" StandardOutputImportance="Low" StandardErrorImportance="High"
             WorkingDirectory="$(HuskyRoot)" />
       <Touch Files="$(HuskyRoot).husky/_/install.stamp" AlwaysCreate="true"
              Condition="Exists('$(HuskyRoot).husky/_')" />
       <ItemGroup>
          <FileWrites Include="$(HuskyRoot).husky/_/install.stamp" />
       </ItemGroup>
    </Target>
  8. Add a git hook with Husky.Net

    master

    You can add a new git hook by using the dotnet husky add command. You must specify the hook name (e.g., pre-commit) and the command to execute using the -c flag.

    After adding a hook, ensure the newly created hook file in the .husky/ directory is staged in git.

    dotnet husky add pre-commit -c "echo 'Husky.Net is awesome!'"
    git add .husky/pre-commit
  9. Run the Husky.Net documentation site locally

    master

    The Husky.Net documentation is powered by VuePress, with markdown source files located in the docs directory. You can run the documentation site locally using either yarn or npm to preview changes to the documentation.

    # Using yarn
    yarn install
    yarn dev
    
    # Using npm
    npm install
    npm run dev
  10. Attach Husky via package.json (npm alternative)

    master

    If your project uses npm, you can automate the installation of Husky by adding a prepare script to your package.json. This ensures that dotnet tool restore and dotnet husky install are executed automatically after npm install.

     "scripts": {
          "prepare": "dotnet tool restore && dotnet husky install"
     }
  11. Attach Husky to your project automatically

    master

    You can automate the attachment of Husky to your project so that other contributors use your pre-configured tasks automatically without adding extra dependencies. Use the dotnet husky attach command followed by the path to your project file.

    dotnet husky attach <path-to-project-file>
  12. Setup Husky.Net for your project

    master

    After installation, you must initialize Husky in your project directory to create the necessary git hooks configuration. Run the dotnet husky install command from your project root.

    Note: If you installed Husky globally, you can omit the dotnet prefix from commands.

    cd <Your project root directory>
    dotnet husky install