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

bash exit 1 doesn't work #71

Open aliasadidev opened 1 year ago

aliasadidev commented 1 year ago

Version

0.5.4

Details

The tool doesn't care about the errors, the information attached into Steps to reproduce section

Steps to reproduce

setup and config

dotnet new console -n App1
git init

dotnet new tool-manifest
dotnet tool install Husky
dotnet husky install

dotnet husky add pre-commit -c "dotnet husky run --group pre-commit'"

add .editorconfig file

https://github.com/RehanSaeed/EditorConfig/blob/main/.editorconfig

pre-commit file content

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

dotnet husky run --group pre-commit

# I want to run a condition here, e.g if dotnet format updated some files then the git commit would be rejected 
exit 1 # <============ didn't work for c# files

Task-runner.json

{
  "tasks": [
    {
      "name": "Dotnet Format",
      "command": "cmd",
      "pathMode": "relative",
      "args": [
        "/c",
        "dotnet",
        "format",
        "--include",
        "${staged}"
      ],
      "group": "pre-commit"
    }
  ]
}

Update program.cs (with bad format)

using App1;

internal class Program
{
    private static void Main(string[] args)
    {
   int x=100;
int xx=x;
    }
}

Add all files

git add .
git commit -m "init"

The problem is when I send the git commit, it will be stored successfully, but it shouldn't be.

image

As you can see the file should be placed in the change section.

image

Dotnet format worked correctly (added the file as a new file changed)

image

aliasadidev commented 1 year ago

72 fixed on this pr

aliasadidev commented 1 year ago

@alirezanet Hi, I'm back again :) the problem solved when i added that flag

alirezanet commented 1 year ago

Hi @aliasadidev, thank you for the contribution but your pr is still open, this issue will automatically close after we merge it. also, I want to check what is going on later

alirezanet commented 1 year ago

Hi @aliasadidev, Checking your issue and PR I still don't get why this is needed, if you want to write a condition in the hook itself why do you need husky? the main goal of this library is to avoid editing hooks manually, and since dotnet format doesn't exit with a non-zero exit code husky doesn't interfere with the commit.

I think the better approach to solving similar problems is to use a csx script or some other script file and call it from the husky(instead of calling dotnet-format directly), this way your logic is separated and maintainable and your hook file is not complicated. unless I'm missing something.