Skip to content

Comments

fix: stash untracked files during partial stashes when HK_STASH_UNTRACKED=true#357

Merged
jdx merged 3 commits intomainfrom
jdx/fix-stash-untracked
Oct 7, 2025
Merged

fix: stash untracked files during partial stashes when HK_STASH_UNTRACKED=true#357
jdx merged 3 commits intomainfrom
jdx/fix-stash-untracked

Conversation

@jdx
Copy link
Owner

@jdx jdx commented Oct 7, 2025

Summary

Fixes a bug where untracked files were not being stashed during partial stashes when HK_STASH_UNTRACKED=true (the default). This caused untracked files to remain in the working directory during hook execution, which could lead to them being incorrectly staged by steps with broad stage globs like stage = "**/*".

Changes

  1. In stash_unstaged(): Now includes untracked files in files_to_stash when HK_STASH_UNTRACKED=true, so the stash operation is triggered even when there are only untracked files.

  2. In push_stash(): When HK_STASH_UNTRACKED=true, avoid passing pathspecs to git stash push --include-untracked. This allows git to stash all untracked files automatically, rather than trying to specify them as pathspecs (which would fail since untracked files aren't known to git).

Test

Added test/stash_untracked_with_partial_stash.bats to verify the fix.

Example Issue

Before this fix:

# Create a staged file
echo 'staged' > tracked.txt
git add tracked.txt

# Create an untracked file
echo 'untracked' > foo

# Commit with a step that has stage = "**/*"
git commit -m 'test'

# Result: foo was incorrectly added to the commit

After this fix, foo is properly stashed before the hook runs and restored afterward, preventing it from being staged.


Note

Ensures partial stashes include and correctly restore untracked files when HK_STASH_UNTRACKED=true, with tests validating the behavior.

  • Git stash logic:
    • stash_unstaged(): Includes status.untracked_files in files_to_stash when HK_STASH_UNTRACKED=true.
    • push_stash(): Filters untracked from pathspecs; if no tracked paths and HK_STASH_UNTRACKED=true, performs full stash (no pathspecs). Sets --include-untracked/INCLUDE_UNTRACKED and keeps index.
    • pop_stash(): Uses git stash show --include-untracked and restores untracked files directly from stash^3, skipping normal merge; retains existing large-file and fixer merge logic.
  • Tests:
    • Adds test/stash_untracked_with_partial_stash.bats with two cases ensuring untracked files are stashed, not committed, and restored after partial stashes.

Written by Cursor Bugbot for commit f5e05b3. This will update automatically on new commits. Configure here.

jdx added 2 commits October 7, 2025 16:13
…CKED=true

When HK_STASH_UNTRACKED=true (the default), untracked files were not being
stashed during partial stashes. This caused untracked files to remain in the
working directory during hook execution, which could lead to them being
incorrectly staged by steps with broad stage globs like `stage = "**/*"`.

This commit fixes two issues:

1. In `stash_unstaged()`: Now includes untracked files in `files_to_stash`
   when HK_STASH_UNTRACKED=true, so the stash operation is triggered even
   when there are only untracked files.

2. In `push_stash()`: When HK_STASH_UNTRACKED=true, avoid passing pathspecs
   to `git stash push --include-untracked`. This allows git to stash all
   untracked files automatically, rather than trying to specify them as
   pathspecs (which would fail since untracked files aren't known to git).

Added test `test/stash_untracked_with_partial_stash.bats` to verify the fix.

Fixes the issue where running `git commit` with staged changes and an
untracked file would incorrectly add the untracked file to the commit
when a step had a broad stage glob.
When HK_STASH_UNTRACKED=true, we should still pass pathspecs to git stash,
but include both tracked and untracked files in those pathspecs. The previous
logic was setting pathspecs to None, which caused git stash to stash ALL
files in the repo rather than just the specific files we wanted to stash.

This fixes the test failures where untracked files were being stashed
incorrectly.
cursor[bot]

This comment was marked as outdated.

The previous fix was incomplete. When HK_STASH_UNTRACKED=true, we need to:

1. Filter out untracked files from pathspecs when calling git stash push,
   since git doesn't accept untracked files as pathspecs
2. Use --include-untracked flag to automatically stash all untracked files
3. When restoring the stash, use --include-untracked with git stash show
   to list untracked files
4. Read untracked files from stash^3 (the untracked files parent commit)
   instead of from the main stash commits

This fixes test failures where untracked files were not being properly
stashed and restored.
@jdx jdx merged commit e15d5cd into main Oct 7, 2025
12 checks passed
@jdx jdx deleted the jdx/fix-stash-untracked branch October 7, 2025 23:45
@jdx jdx mentioned this pull request Oct 7, 2025
jdx added a commit that referenced this pull request Oct 7, 2025
## [1.18.3](https://github.com/jdx/hk/compare/v1.18.2..v1.18.3) -
2025-10-07

### 🐛 Bug Fixes

- stash untracked files during partial stashes when
HK_STASH_UNTRACKED=true by [@jdx](https://github.com/jdx) in
[#357](#357)

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Release 1.18.3 with bug fix for stashing untracked files during
partial stashes and synchronized version/docs/templates plus minor
dependency updates.
> 
> - **Release/version bump**
> - Set `hk` to `1.18.3` in `Cargo.toml`, `Cargo.lock`,
`docs/cli/commands.json`, `docs/cli/index.md`, `hk.usage.kdl`.
> - Update docs/examples to reference `v1.18.3` package URLs in
`docs/**/*.md`, `docs/public/*.pkl`, `hk-example.pkl`, `hk.pkl`
(comment), and `src/cli/init.rs` template.
> - **Bug fix (changelog)**
> - Document fix: stash untracked files during partial stashes when
`HK_STASH_UNTRACKED=true` in `CHANGELOG.md`.
> - **Code/messages**
> - Update invalid module URI hint in `src/config.rs` to `v1.18.3` URL.
> - **Dependencies/tooling**
>   - Bump `webpki-roots` to `1.0.3` in `Cargo.lock`.
> - Add/update platform assets in `mise.lock` for `ruff`, `shellcheck`,
`taplo`.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
30d0bb6. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

Co-authored-by: mise-en-dev <[email protected]>
@jdx jdx mentioned this pull request Oct 7, 2025
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Oct 10, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [hk](https://github.com/jdx/hk) | minor | `1.15.7` -> `1.18.3` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>jdx/hk (hk)</summary>

### [`v1.18.3`](https://github.com/jdx/hk/blob/HEAD/CHANGELOG.md#1183---2025-10-07)

[Compare Source](jdx/hk@v1.18.2...v1.18.3)

##### 🐛 Bug Fixes

- stash untracked files during partial stashes when HK\_STASH\_UNTRACKED=true by [@&#8203;jdx](https://github.com/jdx) in [#&#8203;357](jdx/hk#357)

### [`v1.18.2`](https://github.com/jdx/hk/blob/HEAD/CHANGELOG.md#1182---2025-10-06)

[Compare Source](jdx/hk@v1.18.1...v1.18.2)

##### 🐛 Bug Fixes

- stage directive to include untracked files matching globs by [@&#8203;jdx](https://github.com/jdx) in [#&#8203;355](jdx/hk#355)

### [`v1.18.1`](https://github.com/jdx/hk/blob/HEAD/CHANGELOG.md#1181---2025-10-05)

[Compare Source](jdx/hk@v1.18.0...v1.18.1)

##### 🐛 Bug Fixes

- prevent race condition when files are deleted between collection and execution by [@&#8203;jdx](https://github.com/jdx) in [#&#8203;353](jdx/hk#353)

### [`v1.18.0`](https://github.com/jdx/hk/blob/HEAD/CHANGELOG.md#1180---2025-10-05)

[Compare Source](jdx/hk@v1.16.0...v1.18.0)

##### 🚀 Features

- add fix-smart-quotes util by [@&#8203;joonas](https://github.com/joonas) in [#&#8203;348](jdx/hk#348)

##### 🐛 Bug Fixes

- add Windows support by guarding Unix-specific file permission APIs by [@&#8203;jdx](https://github.com/jdx) in [#&#8203;349](jdx/hk#349)
- handle missing files in update-version script by [@&#8203;jdx](https://github.com/jdx) in [#&#8203;350](jdx/hk#350)
- rewrite update-version script to avoid pipefail issues by [@&#8203;jdx](https://github.com/jdx) in [211b1ac](jdx/hk@211b1ac)
- run render before update-version in release script by [@&#8203;jdx](https://github.com/jdx) in [35d2df3](jdx/hk@35d2df3)
- use \[0-9] instead of \d in ripgrep pattern for better compatibility by [@&#8203;jdx](https://github.com/jdx) in [cf8ebb0](jdx/hk@cf8ebb0)
- explicitly specify search path for ripgrep in update-version script by [@&#8203;jdx](https://github.com/jdx) in [5666f96](jdx/hk@5666f96)

##### 🔍 Other Changes

- add diagnostic output to update-version script by [@&#8203;jdx](https://github.com/jdx) in [aaeea63](jdx/hk@aaeea63)
- add more file existence checks by [@&#8203;jdx](https://github.com/jdx) in [cbace40](jdx/hk@cbace40)
- test rg pattern matching in CI environment by [@&#8203;jdx](https://github.com/jdx) in [a52ea46](jdx/hk@a52ea46)

##### New Contributors

- [@&#8203;joonas](https://github.com/joonas) made their first contribution in [#&#8203;348](jdx/hk#348)

### [`v1.16.0`](https://github.com/jdx/hk/blob/HEAD/CHANGELOG.md#1160---2025-10-02)

[Compare Source](jdx/hk@v1.15.7...v1.16.0)

##### 🚀 Features

- add HK\_STAGE setting to control automatic staging of fixed files by [@&#8203;jdx](https://github.com/jdx) in [#&#8203;313](jdx/hk#313)
- suppress check output\_summary when fixer runs with check\_first by [@&#8203;jdx](https://github.com/jdx) in [#&#8203;315](jdx/hk#315)

##### 🐛 Bug Fixes

- \--slow flag now properly enables slow profile by [@&#8203;jdx](https://github.com/jdx) in [#&#8203;317](jdx/hk#317)

##### 📚 Documentation

- Update getting\_started.md by [@&#8203;jdx](https://github.com/jdx) in [a8c1a35](jdx/hk@a8c1a35)

##### 🔍 Other Changes

- Update getting\_started.md by [@&#8203;jdx](https://github.com/jdx) in [58c0564](jdx/hk@58c0564)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xMzIuNSIsInVwZGF0ZWRJblZlciI6IjQxLjEzOC4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant