-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |