alirezanet / Husky.Net

Git hooks made easy with Husky.Net internal task runner! 🐢 It brings the dev-dependency concept to the .NET world!
https://alirezanet.github.io/Husky.Net/
MIT License
632 stars 29 forks source link

Commit is not present on precommit arguments ($1) #108

Closed DiegoEstrada closed 2 months ago

DiegoEstrada commented 2 months ago

Version

0.6.4

Details

I was setting up the husky in my project to verify that the commit follows the conventional commits. I created a new tool-manifest then installed Husky and added some dotnet commands (build and test). Finally I used the script on your doc to match the regular expression against the current commit. I faced with issues, no mater if the commit followed or not the pattern.

After debugging I noticed that the argument passed to the csx file is always empty,

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

## pass hook arguments to task
#husky run --args "$1" "$2"

echo 'Husky is awesome! 😎'
echo $0

echo "$(cat $1)"

dotnet husky run --name "commit-message-linter" --args "$1"
echo
echo Great work! πŸ₯‚

I am printing the value of $1 where is supposed that the commit message would be assigned, but if we look at the console output, it is empty.

git commit -m "feat: adding commit validation" Husky is awesome! 😎 .husky/pre-commit

[Husky] πŸš€ Loading tasks ...

[Husky] ⚑ Preparing task 'commit-message-linter' [Husky] βŒ› Executing task 'commit-message-linter' ... Hi

script execution failed

❌ Task 'commit-message-linter' failed in 722ms

husky - pre-commit hook exited with code 1 (error)

OS: Windows 10. Shell: PowerShell: 5.1

Steps to reproduce

  1. Create a new tool-manifest dotnet new tool-manifest
  2. Install Husky dotnet tool install Husky dotnet husky install
  3. Add the pre-commit tool dotnet husky add pre-commit -c "echo 'Welcome to pre-commit git hook with Husky.Net'" git add .husky/pre-commit
  4. Edit the pre-commit file generated inside .husky folder with
    
    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"

pass hook arguments to task

husky run --args "$1" "$2"

echo 'Husky is awesome! 😎' echo $0

echo "$(cat $1)"

dotnet husky run --name "commit-message-linter" --args "$1" echo echo Great work! πŸ₯‚

echo 'Building code πŸ”¨' dotnet build

echo 'Running tests πŸ§ͺ' dotnet test

5. Edit the `task-runner.json ` file to define the script to be used to match the commit against regex.
```JSON
{
  "tasks": [
    {
      "name": "commit-message-linter",
      "command": "dotnet",
      "args": [ "husky", "exec", ".husky/csx/commit-lint.csx", "--args", "${args}" ]
    }
  ]
  }
  1. Create the commit-lint.csx file on the csx folder
using System;
using System.Text.RegularExpressions;

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

foreach (var a in Args){
   Console.WriteLine("", a);
}

//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. Test the tool by running.

git commit -m "feat: adding commit validation"

DiegoEstrada commented 2 months ago

Closing this issue as found the reason of why commit is not being validate. Reviewing code I fount an example to lint commits and you need to use a different hook commit-message instead of pre-commit You can do it by runningdotnet husky add commit-message Then you can refer to the script defined before and it should work