-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lefthook does not stash untracked files #833
Comments
Hey @j-chmielewski! Thank you for creating this issue. I can describe you the difficulties with hiding the untracked files. Lefthook uses This changes the whole implementation of hiding the files. Currently I don't have enough resources to develop this approach. But I'd be glad to support an effort from the community enthusiasts! |
Hi @mrexox, thanks for answering my questions. I'm considering implementing this but fist I'd like to make sure that I won't realize halfway through that there are significant roadblocks to this approach. My concerns:
I see two ways to do this:
The second approach seems more doable for me since it uses current code and essentially boils down to appending the two commands I mentioned to current stashing functions. What are your thoughts on that? Do you see any other issues I didn't mention? Cheers 🖖 |
I don't think this will be an issue. By hiding all unstaged and untracked changes before executing the
I hope only the part in
My inspiration for this feature came from the lint-staged utility. They also face a similar issue (see issue #1473). It's hard to predict user expectations, but if untracked files remain available and untracked after the pre-commit hook, it shouldn't significantly affect users. I believe hiding untracked files is a more intuitive behavior. Here’s the PR that introduced hiding unstaged changes, for reference.
My main concern is this issue with stashing partially staged files: ❯ git init
❯ echo A > A
❯ echo B > B
❯ echo C > C
❯ git status --short
?? A
?? B
?? C
❯ git add A
❯ git add B
❯ git status --short
A A
A B
?? C
❯ echo BB >> B
❯ git status --short
A A
AM B
?? C
❯ git stash push --keep-index --include-untracked
Saved working directory and index state WIP on main: eb74add test
❯ git status --short
A A
A B
❯ cat B
B
❯ git stash apply
Auto-merging B
CONFLICT (add/add): Merge conflict in B
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: A
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both added: B
Untracked files:
(use "git add <file>..." to include in what will be committed)
C
❯ echo $?
1
❯ git status --short
A A
AA B
?? C This shows that applying a stash with a partially staged file can lead to merge conflicts. I’m unsure if there’s a way to resolve this.
This sounds promising! I didn’t find a way to append untracked changes to a diff, but it might be possible with a special stash. The flow could look like this: # git diff ... – existing flow
# special flow for untracked files
❯ files=$(git ls-files --others --exclude-standard)
❯ git stash push --include-untracked -- $files # must be saved in a stash with a specific message
# run pre-commit hook
❯ git stash apply
# apply the diff to revert unstaged chages ... |
@j-chmielewski I don't want Lefthook to stash all files. I have my own Git-related commands that detect some state, which would be influenced by this. Furthermore, this would cause Lefthook to mutate the Git working copy with all the risks for destruction. Moreover, it could be a costly operation if a lot of untracked files are generated in some process, like CI. |
I keep running into |
🔧 Summary
This may be me misunderstanding lefthook's philosophy, but after reading through those issues and PRs:
git stash
onpre-commit
andpre-push
#131I was convinced that lefthook stashes the changes before running pre-commit hooks. It seems to me however that this does not include untracked files. This means that if I add new files to the project without staging them, then they will be a part of hook's evaluation unless I explicitly specify the files for the linting / formatting tool with {staged_files} placeholder.
This is an issue for me since in our projects we often prepare npm scripts, like:
Those are pretty elaborate and I'd like to avoid rewriting and duplicating them in lefthook config file. Ideally lefthook would just run the scripts and the scripts would only see staged files. This is achievable by simply stashing all the changes - including new, untracked files, like
git stash -a
does.I suspect that this may be in conflict with the {all_files} placeholder. If that's the case then is there an elegant way to achieve what I'm trying to do?
Lefthook version
1.7.18
Steps to reproduce
https://github.com/j-chmielewski/lefthook-unstaged-file
The text was updated successfully, but these errors were encountered: