C# 项目添加 husky

husky是什么?#

    husky是一个 git hook工具,可以帮助我们触发git提交的各个阶段:pre- commit.、 commitmsg、 pre-pushu。在做前端开发的时候 husky现在差不多是必备的,他可以规范开发,规范git提交等一系操作,在.net等后端开发我们也可以使用husky,不过现在GitHub上有一款专门针对.NET的husky, Husky.Net https://github.com/alirezanet/Husky.Net

Husky.Net 配置#

1、安装 Husky#

    有两种安装方式,一种是:当前项目安装,一种是:全局安装

  • 当前项目安装
cd <Your project root directory>
dotnet new tool-manifest
dotnet tool install Husky
  • 全局安装
dotnet tool install --global Husky

2、为项目添加 Husky#

cd <Your project root directory>
dotnet husky install

3、添加一个Hook测试下#

# 1、添加 pre-commit
dotnet husky add pre-commit -c "echo 'Husky.Net is awesome!'"
git add .husky/pre-commit

# 2、git commit 提交的时候,在控制台会显示对应的提示 Husky.Net is awesome!
git commit -m "Keep calm and commit"
# `echo 'Husky.Net is awesome!'` will run every time you commit

添加commit-lint.csx#

  • 1、添加 commit-lint.csx文件,目录为 .husky/csx/commit-lint.csx
/// <summary>
/// a simple regex commit linter example
/// https://www.conventionalcommits.org/en/v1.0.0/
/// https://github.com/angular/angular/blob/22b96b9/CONTRIBUTING.md#type
/// </summary>

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;
  • 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! 🥂
echo
  • 3、在task-runner.json中添加对应的task
 {
      "name": "commit-message-linter",
      "command": "dotnet",
      "args": ["husky", "exec", ".husky/csx/commit-lint.csx", "--args", "${args}"]
 },
  • 4、git commit提交测试

1、我们提交格式如下: git commit -m "新增一个功能",这个时候在控制台页面会提示我们Error信息

[Husky] ? Executing task 'commit-message-linter' ...
Invalid commit message
e.g: 'feat(scope): subject' or 'fix: subject'
more info: https://www.conventionalcommits.org/en/v1.0.0/
script execution failed

  ? Task 'commit-message-linter' failed in 2,838ms

这里的提示就是说我们的commit提交的subject内容格式不对,应该是: 前缀:空格 内容

2、我们修改下commit内容,git commit -m "feat: 新增一个功能",控制台显示信息如下:

[Husky] ? Preparing task 'commit-message-linter'
[Husky] ? Executing task 'commit-message-linter' ...
[Husky]  ? Successfully executed in 2,829ms

至此,我们的提交格式就正常了

躺坑总结#

  • 1、commit-message-linter 这个在官方的github中是存在的,但是在安装的时候是没有的,必须自己手动把文件复制过来
  • 2、运行速度还有待提升
  • 3、dotnet format格式验证好像目前没什么效果
  • 4、commit-lint.csx正则里面缺少init

参考文档#

作者:Darren

出处:https://www.cnblogs.com/jesn/p/16336768.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   jesn  阅读(168)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
more_horiz
keyboard_arrow_up light_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示