Repository navigation
v1.32.1 — Git discard protection in the safety hooks
Safety hooks: Git discard protection
This release closes a gap in the Bash safety hooks: commands that silently
throw away uncommitted work were being approved. The hook now blocks them,
while still allowing the ordinary recovery case it used to refuse.
Blocked
git reset --hardandgit reset --merge. The latter also overwrites the
working tree, including files that are staged but otherwise unmodified.- Any
git cleanthat is not a dry run. Checking for-fis not enough,
becauseclean.requireForce=falselets Git delete files without it. git restoreandgit checkout -- <file>when they would overwrite an
edit, or when they take content from somewhere else (--source,
--staged,--patch).
Allowed again
Recovering a tracked file that is missing from the working tree, or already
byte-identical to the index, is a recovery rather than a discard. A new
git_recovery helper permits it, and refuses anything where an edit could be
lost: intent-to-add drafts, --assume-unchanged, --skip-worktree, exec-bit
changes, symlinks, unmerged entries and directory pathspecs.
Parser hardening
The discard checks previously read command text that Git itself never
receives. They now resolve the effective argv first:
- option values are consumed before
--ends the options, so
git clean -n -e -- -f --no-dry-runno longer reads as a dry run - shell comments and redirections are dropped, so
git restore f # -Sand
git restore f > -Sare no longer mistaken for index-only restores - a leading redirection descriptor is consumed, so
2>/tmp/err git reset --hardis recognised - ANSI-C quoting is decoded, so
git $'restore' fis recognised
Recovery deliberately fails closed: it applies only to a lone git command
with no preceding cd, nothing dropped by the shell, and no pathspec magic.
Known limits are recorded in #268 — unmodelled command prefixes
(command git ..., GIT_INDEX_FILE=...), --icase-pathspecs on the
checkout spelling, and sibling verbs such as git switch -f.
918 safety-hook tests pass on this commit.
Thanks
- @fizz reported the gap and wrote the original fix in #265. All four of those
commits were carried over into #268 with authorship intact, and the work
here is built on top of them.
Full changelog: voxtype-v0.3.22...v1.32.1