Description
A lot of times we have extra local files that either:
- We want to only commit/push some changes at at time to organize into multiple small commits/PRs
- We don't want to commit ever - local experiments
But then hooks (clippy/fmt) are executed on the whole code locally and but only on those "partial changes" in CI. The CI might fail because we missed pushing/commiting some other files.
Value
Shipping smaller and cleaner commit/PRs with confidence. Fewer CI failures.
Workaround:
I currently do this with bash but it would be great if rusty-hook does it internally with just a flag. Would more people discover this approach/trick:
#!/bin/bash
COMMAND="$1"
shift # Remove first argument, rest are passed to the command
# Check if there are unstaged changes
STASHED=0
if ! git diff --quiet; then
echo "🗃️ Stashing unstaged changes..."
git stash push -m "rusty-hook temporary stash" --keep-index --include-untracked
STASHED=1
fi
# Function to restore stash on exit
restore_stash() {
if [ "$STASHED" = "1" ]; then
echo "📦 Restoring stashed changes..."
if git stash list | grep -q "rusty-hook temporary stash"; then
git stash pop
else
echo "⚠️ Warning: Could not find stash to restore"
fi
fi
}
# Set trap to restore stash on exit (success or failure)
trap restore_stash EXIT
# Run the actual command and capture its exit status
set -e # Exit on error
echo "🚀 Running: $COMMAND $*"
eval "$COMMAND $*"
# Trigger directly with rusty-hook run --hook <pre-commit|pre-push>
[hooks]
pre-commit = "./tools/git-hook.sh 'cargo +nightly fmt --all -- --check'"
pre-push = "./tools/git-hook.sh 'cargo clippy --all-targets --all-features -- -D warnings'"
[logging]
verbose = true
Description
A lot of times we have extra local files that either:
But then hooks (clippy/fmt) are executed on the whole code locally and but only on those "partial changes" in CI. The CI might fail because we missed pushing/commiting some other files.
Value
Shipping smaller and cleaner commit/PRs with confidence. Fewer CI failures.
Workaround:
I currently do this with bash but it would be great if rusty-hook does it internally with just a flag. Would more people discover this approach/trick: