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
672 stars 31 forks source link

Csharpier pre commit hook with custom variable not included in commit #120

Open VinceBroAmilia opened 3 days ago

VinceBroAmilia commented 3 days ago

Version

Seen on v0.6.4 to v0.7.1

Details

So I am trying to run csharpier as a pre commit hook and support formatting files which have been renamed in git, I opted for using a custom variable that was inspired by the ${staged} variable in the husky code.

{
  "variables": [
    {
      "name": "staged-diff-files",
      "command": "git",
      "args": ["diff", "--cached", "--name-only", "--no-ext-diff", "--diff-filter=ACMRTUXB"]
    }
  ],
  "tasks": [
    {
      "name": "Run csharpier",
      "group": "pre-commit",
      "command": "dotnet",
      "args": [ "csharpier", "${staged-diff-files}" ],
      "include": [ "**/*.cs" ]
    },
  ]
}

I noticed that the formatting changes only appear post commit, unstaged.

When I use this instead :

{
  "tasks": [
    {
      "name": "Run csharpier",
      "group": "pre-commit",
      "command": "dotnet",
      "args": [ "csharpier", "${staged}" ],
      "include": [ "**/*.cs" ]
    },
  ]
}

It adds the formatting to the commit as is expected with a pre commit hook.

Even when I set the variable to the exact same as in the code for the ${staged} variable using --diff-filter=AM it does not have the same behavior, it does not add the formatting changes to the commit.

Wondering if this is expected behavior with custom variables or if this is a bug.

Steps to reproduce

Set task runner json with a variable in the csharpier task, with husky running it a pre-commit hook :

{
  "variables": [
    {
      "name": "staged-diff-files",
      "command": "git",
      "args": ["diff", "--cached", "--name-only", "--no-ext-diff", "--diff-filter=AM"]
    }
  ],
  "tasks": [
    {
      "name": "Run csharpier",
      "group": "pre-commit",
      "command": "dotnet",
      "args": [ "csharpier", "${staged-diff-files}" ],
      "include": [ "**/*.cs" ]
    },
  ]
}

Take a .cs file, change the formatting so it triggers csharpier, add it, commit it. The formatting will not be included in the commit, but will be an unstaged change after the commit, not the case when using ${staged}

alirezanet commented 2 days ago

Hello @VinceBroAmilia, I will investigate this as soon as I have some free time. Currently, my schedule is quite full. I believe this issue might be addressed by Husky, so I am treating it as a bug for the time being. Thank you for your feedback.

VinceBroAmilia commented 2 days ago

Thank you @alirezanet