Skip to content

Commit

Permalink
add pre push hook generator script
Browse files Browse the repository at this point in the history
  • Loading branch information
gianfra-t committed Feb 16, 2024
1 parent e2958d8 commit 9a4dce9
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .maintain/add-prepush-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh

# check that rustfmt is installed, or else this hook doesn't make much sense
command -v rustfmt >/dev/null 2>&1 || { echo >&2 "Rustfmt is required but it's not installed. Aborting."; exit 1; }

# write a whole script to pre-push hook
# NOTE: it will overwrite pre-push file!
cat > .git/hooks/pre-push <<'EOF'
#!/bin/bash -e
echo "Running cargo clippy --fix on project"
before_clippy=$(git diff --name-only)
$(command -v cargo) clippy --fix --allow-dirty --allow-staged --all-features
after_clippy=$(git diff --name-only)
wait
before_files=()
while IFS= read -r line; do
before_files+=("$line")
done <<< "$before_clippy"
after_files=()
while IFS= read -r line; do
after_files+=("$line")
done <<< "$after_clippy"
clippy_modified_files=()
for i in "${after_files[@]}"; do
skip=
for j in "${before_files[@]}"; do
[[ $i == $j ]] && { skip=1; break; }
done
[[ -n $skip ]] || clippy_modified_files+=("$i")
done
# Adds only the files modified by clippy
if [ ${#clippy_modified_files[@]} -ne 0 ]; then
git add "${clippy_modified_files[@]}"
git commit -m "Apply automatic cargo clippy fixes"
echo "Clippy modifications added for: ${clippy_modified_files[*]}"
else
echo "No modifications by clippy"
fi
wait
echo "Pre-push hook execution completed."
EOF

chmod +x .git/hooks/pre-push

echo "Hooks updated"

0 comments on commit 9a4dce9

Please sign in to comment.