Skip to content

Conversation

@zouguangxian
Copy link
Collaborator

@zouguangxian zouguangxian commented Dec 31, 2025

This PR updates .ghk/pre-commit to correctly handle partially staged files (files that are staged but also have additional unstaged edits).

What changed

  • Replace the previous targeted git stash push --keep-index -- <paths> + git stash pop flow with a patch-based flow.
  • When running in --staged mode, for any file that is both staged and has unstaged edits:
    • Save the unstaged portion to a patch stored inside .git/.
    • git checkout --force -- <files> to temporarily hide unstaged edits while tasks run.
    • Re-apply the patch after tasks complete, retrying with git apply --3way on conflict.
    • If restoration still fails, leave the patch on disk and print its path for manual recovery.

Why

Stash-pop based restoration often conflicts when formatters rewrite the same lines as unstaged hunks. The patch + checkout + apply approach is more deterministic and mirrors the strategy used by lint-staged.

Behavior notes

  • Only affects --staged mode; --dirty mode intentionally does not modify working tree state.
  • Unrelated unstaged changes and untracked files are not touched in --staged mode.

… files

- Refactor logic to hide and restore unstaged changes for staged files using patch files.
- Introduce new functions for managing git directory and applying patches.
- Enhance error handling during restoration of unstaged changes.
@zouguangxian zouguangxian changed the title This PR improves pre-commit handling of partially-staged files chore(pre-commit): improves pre-commit handling of partially-staged files Dec 31, 2025
Copy link

@absolute0kelvin absolute0kelvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zouguangxian zouguangxian merged commit a1afe7e into main Dec 31, 2025
15 checks passed
@zouguangxian zouguangxian deleted the gx/lint-staged branch December 31, 2025 06:49
@zouguangxian zouguangxian restored the gx/lint-staged branch December 31, 2025 07:58
@zouguangxian zouguangxian deleted the gx/lint-staged branch December 31, 2025 08:00
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.

2 participants