|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Count changes in Solidity files between two branches, categorizing by code, comments, and blank lines |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +# Check if cloc is installed |
| 7 | +if ! command -v cloc &> /dev/null; then |
| 8 | + echo "Error: cloc is not installed. Install with: sudo npm install -g cloc" |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +# Define the branches to compare |
| 13 | +BASE_BRANCH="${1:-main}" # Default to 'main' if no argument provided |
| 14 | +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 15 | + |
| 16 | +echo "Comparing Solidity changes between $BASE_BRANCH and $CURRENT_BRANCH..." |
| 17 | +echo |
| 18 | + |
| 19 | +# Get all modified Solidity contract files |
| 20 | +ALL_FILES=$(git diff --name-only "$BASE_BRANCH" -- 'packages/*/contracts/**/*.sol') |
| 21 | + |
| 22 | +# Get excluded files (mock and test files) |
| 23 | +EXCLUDED_FILES=$(echo "$ALL_FILES" | grep -E '(/mocks?/|/tests?/|Mock[A-Z].*\.sol$|.*Test\.sol$|Test[A-Z].*\.sol$)' || true) |
| 24 | + |
| 25 | +# Get included files (non-mock, non-test files) |
| 26 | +FILES=$(echo "$ALL_FILES" | grep -v -E '(/mocks?/|/tests?/|Mock[A-Z].*\.sol$|.*Test\.sol$|Test[A-Z].*\.sol$)' || true) |
| 27 | + |
| 28 | +# Check if there are any files to process |
| 29 | +if [ -z "$FILES" ]; then |
| 30 | + echo "No Solidity files changed between $BASE_BRANCH and $CURRENT_BRANCH." |
| 31 | + exit 0 |
| 32 | +fi |
| 33 | + |
| 34 | +echo "Found changed Solidity files (excluding mocks and tests):" |
| 35 | +echo "$FILES" | sed 's/^/- /' |
| 36 | +echo |
| 37 | + |
| 38 | +# Display excluded files if any |
| 39 | +if [ -n "$EXCLUDED_FILES" ]; then |
| 40 | + echo "Excluded mock and test files:" |
| 41 | + echo "$EXCLUDED_FILES" | sed 's/^/- /' |
| 42 | + echo |
| 43 | +fi |
| 44 | + |
| 45 | +# Create directories for diff analysis |
| 46 | +TEMP_DIR=$(mktemp -d) |
| 47 | +trap 'rm -rf "$TEMP_DIR"' EXIT |
| 48 | +mkdir -p "$TEMP_DIR/old" |
| 49 | +mkdir -p "$TEMP_DIR/new" |
| 50 | + |
| 51 | +# Extract all changed files |
| 52 | +for file in $FILES; do |
| 53 | + if [ -f "$file" ]; then |
| 54 | + # Create directory structure |
| 55 | + dir=$(dirname "$file") |
| 56 | + mkdir -p "$TEMP_DIR/new/$dir" |
| 57 | + mkdir -p "$TEMP_DIR/old/$dir" |
| 58 | + |
| 59 | + # Copy current version |
| 60 | + cp "$file" "$TEMP_DIR/new/$file" |
| 61 | + |
| 62 | + # Get old version if it exists |
| 63 | + if git show "$BASE_BRANCH:$file" &>/dev/null; then |
| 64 | + git show "$BASE_BRANCH:$file" > "$TEMP_DIR/old/$file" |
| 65 | + fi |
| 66 | + fi |
| 67 | +done |
| 68 | + |
| 69 | +# Run cloc diff on all files |
| 70 | +echo "Summary of changes (excluding mock and test files):" |
| 71 | +echo "================================================" |
| 72 | +cloc --diff "$TEMP_DIR/old" "$TEMP_DIR/new" --include-lang=Solidity --quiet |
| 73 | + |
| 74 | +echo |
| 75 | +echo "Note: This analysis only counts changes in Solidity files, excluding mock and test contracts." |
| 76 | +echo "The 'same' category shows lines that were unchanged in modified files." |
0 commit comments