Using C# code in your git hooks

You can use task runner exec command to execute a C# script.

e.g

dotnet husky exec <csx-file-path>
# e.g
# dotnet husky exec .husky/csx/hello.csx
1
2
3

Also, you can use your csx scripts in your tasks.

{
   "command": "dotnet",
   "args": ["husky", "exec", ".husky/csx/hello.csx"]
}
1
2
3
4

Examples

Simple commit message linter

This repo is using a csharp script to lint the commit messages, you can check it here:

commit-lint.csxopen in new window

using System.Text.RegularExpressions;

private var pattern = @"^(?=.{1,90}$)(?:build|feat|ci|chore|docs|fix|perf|refactor|revert|style|test)(?:\(.+\))*(?::).{4,}(?:#\d+)*(?<![\.\s])quot;;
private var msg = File.ReadAllLines(Args[0])[0];

if (Regex.IsMatch(msg, pattern))
   return 0;

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Invalid commit message");
Console.ResetColor();
Console.WriteLine("e.g: 'feat(scope): subject' or 'fix: subject'");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("more info: https://www.conventionalcommits.org/en/v1.0.0/");

return 1;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

commit-msg hookopen in new window

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

dotnet husky run --name "commit-message-linter" --args "$1"
echo
echo Great work! 🥂
echo
1
2
3
4
5
6
7

task-runner.jsonopen in new window

   "tasks": [
      {
         "name": "commit-message-linter",
         "command": "dotnet",
         "args": ["husky", "exec", ".husky/csx/commit-lint.csx", "--args", "${args}"]
      },
1
2
3
4
5
6