Skip to content

Commit

Permalink
tools: Add pre-hook script
Browse files Browse the repository at this point in the history
  • Loading branch information
gnuton committed Jan 8, 2025
1 parent 2a1834a commit b22d855
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
55 changes: 55 additions & 0 deletions scripts/hooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

# pre-push hook script
# This script checks the files in commits being pushed for prohibited patterns
# and exits with status 2 if such patterns are found.

REMOTE="$1"
URL="$2"

# Find the range of commits to be pushed
read LOCAL_REF LOCAL_SHA REMOTE_REF REMOTE_SHA

# Exit if the range can't be determined
if [ "$LOCAL_SHA" = "0000000000000000000000000000000000000000" ]; then
echo "Nothing to push. Exiting."
exit 0
fi

if [ "$REMOTE_SHA" = "0000000000000000000000000000000000000000" ]; then
# Push to new branch or repo, analyze all commits
RANGE="$LOCAL_SHA"
else
# Standard push, analyze commits in the range
RANGE="$REMOTE_SHA..$LOCAL_SHA"
fi

# Get the list of files changed in the range
FILES=$(git diff --name-only "$RANGE")

# Patterns to search for
PATTERNS=(
"BRCM:[0-9]+:proprietary:"
"BRCM:[0-9]+:NONE:RED"
"Broadcom Proprietary and Confidential"
)

# Check each file
for FILE in $FILES; do
if [ -f "$FILE" ]; then
FILE_CONTENT=$(git show "$LOCAL_REF:$FILE" 2>/dev/null || echo "")
for PATTERN in "${PATTERNS[@]}"; do
if echo "$FILE_CONTENT" | grep -E -q "$PATTERN"; then
echo "Prohibited pattern found in file: $FILE"
echo "Pattern: $PATTERN"
echo "Push blocked due to prohibited content."
exit 2
fi
done
fi
done

# Allow the push to proceed
echo "No prohibited content found. Push allowed."
exit 0

10 changes: 10 additions & 0 deletions scripts/install-hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
REPO_BASE=$(git rev-parse --show-toplevel)
SCRIPT_DIR="$REPO_BASE/scripts/hooks"
INSTALL_DIR="$REPO_BASE/.git/hooks"

echo "Repository base directory:$REPO_BASE"

mkdir -p "$INSTALL_DIR"
cp -a "${SCRIPT_DIR}"/* "${INSTALL_DIR}"
echo "All webhooks have been copied"

0 comments on commit b22d855

Please sign in to comment.