参考:C# 项目添加 husky - jesn - 博客园 (cnblogs.com)
官方文档:Getting Started | Husky.Net (alirezanet.github.io)
什么是Husky.net?
Husky是一款git hook(钩子)工具,让我们在git commit之前可以做一些操作,例如,代码格式化,重生生成,提交规范检查等,而Husky.net便是适用于.net平台的Husky,同时可以通过C#编写检查脚本
1.安装Husky.net
1.1当前项目安装 (推荐使用这种方式)
cd <Your project root directory>
dotnet new tool-manifest
dotnet tool install Husky
1.2全局安装
Dotnet tool install –global Husky
2.在项目中添加Husky.net
2.1进入项目根目录
2.2打开cmd
2.3cmd依次输入
dotnet new tool-manifest
dotnet tool install Husky
2.4然后输入
dotnet husky install
项目中即会添加husky文件
3添加一个hook
3.1依次输入
dotnet husky add pre-commit -c “echo ‘Husky.Net is awesome!’“
git add .husky/pre-commit”
3.2测试hook
Git提交一次
输出Husky.Net is awesome!表示刚才的hook添加成功
4添加commit-lint git提交检查
4.1添加commit-lint.csx检查脚本
内容如下
using System.Text.RegularExpressions; private var pattern = @"^(?=.{1,90}$)(?:build|feat|ci|chore|docs|fix|perf|refactor|revert|style|test)(?:\(.+\))*(?::).{4,}(?:#\d+)*(?<![\.\s])$"; 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;
4.2添加commit-msg文件,目录为.husky/commit-msg
将其中的内容修改为
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
dotnet husky run --name "commit-message-linter" --args "$1"
echo
echo Great work!
标签:commit,添加,husky,dotnet,Husky,net,Net From: https://www.cnblogs.com/SaoJian/p/17301106.html