Skip to content

git-hooks-csharp is a library enabling developers to execute C# code within Git hooks, offering automated checks to maintain code quality and consistency in Git workflows.

License

Notifications You must be signed in to change notification settings

PabloStarOk/git-hooks-csharp

Repository files navigation

git-hooks-csharp

git-hooks-csharp is a library enabling developers to execute C# code within Git hooks, offering automated checks to maintain code quality and consistency in Git workflows. Currently, it provides three hooks: pre-commit, prepare-commit-msg, and commit-msg.

Table of Contents

  1. Hook Overview
  2. Key Features
  3. Usage
  4. License
  5. Acknowledgments

Hook Overview

Note

Part of this library is based on the Medium article Using C# code in your git hooks.

Key Features

pre-commit Hook

  • Format code using dotnet format
  • Clean code using dotnet clean
  • Build projects using dotnet build
  • Run tests using dotnet test

prepare-commit-msg Hook

  • Prepares a basic commit message template when using git commit without the -m flag.

commit-msg Hook

  • Commit message format validation.
  • Configuration in a JSON format file to modify the constraints of the commit message format.

Usage

  1. You must install the dotnet-script tool:

    dotnet tool install -g dotnet-script
  2. Compile this project and add the compiled DLL to the git hooks folder or wherever you need it.

  3. Create a git hook to execute the DLL. The git hook file must be a dotnet script file, for example:

Pre Commit

#!/usr/bin/env dotnet-script
#r "./GitHooksCSharp.dll"

using GitHooksCsharp.Hooks.PreCommit;

PreCommitHook.ProjectPaths.Add("./Sample/Sample.csproj");
PreCommitHook.ProjectPaths.Add("./Sample2/Sample2.csproj");
PreCommitHook.TestProjectPaths.Add("./Sample.Test/Sample.Test.csproj");
PreCommitHook.TestProjectPaths.Add("./Sample2.Test/Sample2.Test.csproj");

PreCommitHook.Execute();
Prepare Commit Msg
#!/usr/bin/env dotnet-script
#r "./GitHooksCSharp.dll"

using GitHooksCsharp.Hooks.PrepareCommitMsg;

PrepareCommitMsgHook.Execute();
Commit Msg
#!/usr/bin/env dotnet-script
#r "./GitHooksCSharp.dll"

using GitHooksCsharp.Hooks.CommitMsg;

CommitMsgHook.Execute();
Commit Message Constraints

cshooks-config.json file to configure the validation used in commit-msg:

{
  
  "subject": {
    "allowedTypes": [
      "feat",
      "fix",
      "chore",
      "test",
      "docs",
      "build",
      "ci",
      "style",
      "refactor",
      "perf",
      "revert"
    ],
    "allowedScopes": [
      "[\\w\\s!@\\#\\$%\\^\u0026\\*\\(\\)_=\\\u002B~\u0060\u0027\\[\\{}\\*\u00BF\\?\\|:;,\\.\u003E\u003C\\\\\\-\\]\\/]{2,20}"
    ],
    "isTypeRequired": true,
    "isScopeRequired": false,
    "descriptionRules": {
      "isRequired": true,
      "letterCase": "sentence",
      "minLength": 10,
      "maxLength": 70,
      "allowedSpecialChars": [],
      "mayEndWithPeriod": false,
      "mustEndWithPeriod": false
    }
  },
  
  "body": {
    "isRequired": false,
    "letterCase": "any",
    "minLength": 0,
    "maxLength": 0,
    "allowedSpecialChars": [],
    "mayEndWithPeriod": true,
    "mustEndWithPeriod": false
  },
  
  "footer": {
    "isRequired": false,
    "allowedKeywords": [
      "BREAKING CHANGE",
      "BREAKING-CHANGE",
      "[a-z]\u002B(?:-[a-z]\u002B)*"
    ],
    "descriptionRules": {
      "isRequired": true,
      "letterCase": "sentence",
      "minLength": 10,
      "maxLength": 0,
      "allowedSpecialChars": [],
      "mayEndWithPeriod": false,
      "mustEndWithPeriod": false
    }
  }
  
}

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Acknowledgments

About

git-hooks-csharp is a library enabling developers to execute C# code within Git hooks, offering automated checks to maintain code quality and consistency in Git workflows.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages