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

${staged} variable contains some temporary files #39

Closed ultrr closed 2 years ago

ultrr commented 2 years ago

Version

0.4.0

Details

I was running a git hook with several steps configured. I was expecting only REAL staged files to be committed but unfortunately there were some additional staged files which shouldn't be added to ${staged} variable.

Steps to reproduce

  1. dotnet new tool-manifest
  2. dotnet tool install JetBrains.ReSharper.GlobalTools
  3. dotnet tool install ReGitLint
  4. dotnet tool install Husky
  5. configure Husky, using following task-runner.json
    {
    "tasks": [
    {
      "name": "before - echo staged from husky",
      "command": "echo",
      "args": ["${staged}"]
    },
    {
      "name": "reglint",
      "command": "dotnet",
      "args": ["regitlint", "-f", "staged", "--jb", "--verbosity=WARN"]
    },
    {
      "name": "after - echo staged from husky",
      "command": "echo",
      "args": ["${staged}"]
    },
    {
      "name": "after - echo staged from git",
      "command": "git",
      "args": ["diff", "--name-only", "--cached"]
    },
    {
      "name": "after - echo staged from ???",
      "command": "git",
      "args": ["diff-index", "--cached", "--diff-filter=AM", "--no-renames", "--name-only", "HEAD"]
    }
    ]
    }
  6. as you can see in git log output, echo of ${staged} variable includes other (temporary?) files, while those files aren't included while running git diff/diff-index commands manually. In our use case, we want to run git add ${staged} after regitlint but it results in committing those temporary files as well.
alirezanet commented 2 years ago

I don't totally understand what is the problem!? Since husky doesn't change the file states.
how do you add your files to git in the first place (staging step)? Your reproducible example doesn't do anything because nothing is staged yet. image

also, I've tried git add test.txt to stage a file: 02

everything was normal except weird behavior from reglint tool (I have no idea why it is scanning my entire system and found a different project !)

If you think this is a husky-related bug, please remove other third-party tools and give me a reproducible example.

thanks

ultrr commented 2 years ago

My bad! Forgot to include the output. Managed to recreate the issue on a fresh solution, with usage of Husky.Net only.

Link to the repo with said solution: https://github.com/ultrr/husky.net-staged-bug

Steps to reproduce:

  1. Clone the repo
  2. Restore the solution: dotnet restore
  3. Change the content of file1.txt: echo someContent123 >> file1.txt
  4. Change the content of file2.txt. Has to be different from file1.txt: echo someOtherContent123 >> file2.txt
  5. Stage both files: git add file1.txt file2.txt
  6. Commit the changes: git commit -m "test"

Output:

[Husky] 🚀 Loading tasks ...
--------------------------------------------------
[Husky] ⚡ Preparing task 'before-echo-staged'
[Husky] ⌛ Executing task 'before-echo-staged' ...
file1.txt file2.txt
[Husky]  ✔ Successfully executed in 2ms
--------------------------------------------------
[Husky] ⚡ Preparing task 'cp'
[Husky] ⌛ Executing task 'cp' ...
[Husky]  ✔ Successfully executed in 9ms
--------------------------------------------------
[Husky] ⚡ Preparing task 'after-echo-staged'
[Husky] ⌛ Executing task 'after-echo-staged' ...
file1.txt 26c27_file2.txt file2.txt
[Husky]  ✔ Successfully executed in 1ms
--------------------------------------------------
[Husky] ⚡ Preparing task 'git'
[Husky] ⌛ Executing task 'git' ...
[Husky]  ✔ Successfully executed in 8ms
--------------------------------------------------
[master 059d397] test
 3 files changed, 3 insertions(+), 2 deletions(-)
 create mode 100644 63c0e_file2.txt

git status output:

On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    63c0e_file2.txt

no changes added to commit (use "git add" and/or "git commit -a")
alirezanet commented 2 years ago

Hi, Thank you for the repo because in this case, you want to control git indexing situation to prevent husky interference I've added a new option -p to the run command that you can use in v0.4.2:

image

So you just need to update your run command to:

dotnet husky run -p

image