Skip to content

Commit

Permalink
Merge pull request #2814 from dusk-network/add-hooks-install-script
Browse files Browse the repository at this point in the history
scripts: Add pre-push Git hook clippy run on updated workspace members
  • Loading branch information
HDauven authored Nov 4, 2024
2 parents b363a49 + 8a9113e commit d7d4536
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions scripts/install-hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
echo "Installing Git pre-push hook..."

# Check if we're in the git repository
if [ ! -d .git ]; then
echo "This script must be run from the root of the Rusk repository."
exit 1
fi

# Copy the pre-push.sh script to .git/hooks/pre-push
cp ./scripts/pre-push.sh .git/hooks/pre-push

echo "Git hook installed successfully."
47 changes: 47 additions & 0 deletions scripts/pre-push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# Check if SKIP_CLIPPY_CHECK is set
if [ "$SKIP_CLIPPY_CHECK" = "true" ]; then
echo "SKIP_CLIPPY_CHECK is set. Skipping clippy checks."
exit 0
fi

echo "Detecting changes and running clippy only on modified workspace members..."

# Extract workspace members from the Cargo.toml file and members from the `make clippy` in the Makefile
WORKSPACE_MEMBERS=$(awk '/members = \[/,/\]/' ./Cargo.toml | grep -o '"[^"]*"' | tr -d '"' | sed 's/\/.*//g' | sort -u)
MAKEFILE_CLIPPY_TARGETS=$(awk '/clippy:/,/^$/' ./Makefile | grep -o '\./[^ ]*' | sed 's/\.\///;s/\/$//' | sort -u)

# Filter workspace members to include only those part of the `make clippy` target
WORKSPACE_MEMBERS=$(echo "$WORKSPACE_MEMBERS" | grep -Fxf - <(echo "$MAKEFILE_CLIPPY_TARGETS"))

# Find the common ancestor between the feature branch and master
BASE_COMMIT=$(git merge-base HEAD master)

# Get the list of changed directories since the branch point with master
CHANGED_DIRS=$(git diff --name-only "$BASE_COMMIT"...HEAD | awk -F'/' '{print $1}' | sort -u)

# Variable to track if any `clippy` checks have failed
CLIPPY_FAILED=0

# Run clippy for each workspace member if it has changes
for MEMBER in $WORKSPACE_MEMBERS; do
if echo "$CHANGED_DIRS" | grep -q "^${MEMBER}$"; then
echo "Running clippy on $MEMBER..."
if ! make -C ./$MEMBER clippy; then
CLIPPY_FAILED=1
echo "Clippy check failed for $MEMBER."
fi
else
echo "No changes detected in $MEMBER. Skipping clippy."
fi
done

# Exit if at least 1 clippy run failed. Exit 1 aborts, exit 0 allows for pushing
if [ "$CLIPPY_FAILED" -eq 1 ]; then
echo "One or more clippy checks failed. Push aborted."
exit 1
else
echo "All clippy checks passed. Proceeding with push."
exit 0
fi

0 comments on commit d7d4536

Please sign in to comment.