Skip to content

v1.32.1 — Git discard protection in the safety hooks

Choose a tag to compare

@pchalasani pchalasani released this 24 Nov 20:22
· 10 commits to main since this release

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 --hard and git reset --merge. The latter also overwrites the
    working tree, including files that are staged but otherwise unmodified.
  • Any git clean that is not a dry run. Checking for -f is not enough,
    because clean.requireForce=false lets Git delete files without it.
  • git restore and git 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-run no longer reads as a dry run
  • shell comments and redirections are dropped, so git restore f # -S and
    git restore f > -S are no longer mistaken for index-only restores
  • a leading redirection descriptor is consumed, so 2>/tmp/err git reset --hard is recognised
  • ANSI-C quoting is decoded, so git $'restore' f is 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