From a49e17b3520916c37ea12a9796287399c192a950 Mon Sep 17 00:00:00 2001 From: Daniel Bast <2790401+dbast@users.noreply.github.com> Date: Sat, 17 Aug 2024 10:00:52 +0200 Subject: [PATCH 1/2] Add pre-commit hook with test --- .github/workflows/PRE-COMMIT-HOOK-TEST.yml | 34 ++++++++++++++++++++++ .pre-commit-hooks.yaml | 6 ++++ README.md | 12 ++++++++ e2e/pre-commit-hook/.solhint.json | 3 ++ e2e/pre-commit-hook/Counter.sol | 14 +++++++++ e2e/pre-commit-hook/test.sh | 31 ++++++++++++++++++++ package.json | 1 - 7 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/PRE-COMMIT-HOOK-TEST.yml create mode 100644 .pre-commit-hooks.yaml create mode 100644 e2e/pre-commit-hook/.solhint.json create mode 100644 e2e/pre-commit-hook/Counter.sol create mode 100755 e2e/pre-commit-hook/test.sh diff --git a/.github/workflows/PRE-COMMIT-HOOK-TEST.yml b/.github/workflows/PRE-COMMIT-HOOK-TEST.yml new file mode 100644 index 00000000..4e15bbc8 --- /dev/null +++ b/.github/workflows/PRE-COMMIT-HOOK-TEST.yml @@ -0,0 +1,34 @@ +name: PRE-COMMIT + +on: + push: + branches: + - master + - develop + tags: + - '*' + pull_request: + paths: + - .github/workflows/PRE-COMMIT-HOOK-TEST.yml + - .pre-commit-hooks.yaml + - e2e/pre-commit-hook/* + - package.json + +jobs: + hook-test: + runs-on: ubuntu-latest + name: Test Hook + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install pre-commit + run: | + python -m pip install pre-commit + - name: Test hooks + run: | + ./e2e/pre-commit-hook/test.sh diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 00000000..2ee0b022 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,6 @@ +- id: solhint + name: solhint + description: Lint Solidity files with Solhint + entry: solhint + files: \.sol$ + language: node diff --git a/README.md b/README.md index 38361573..46378fbb 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,18 @@ Or disable all validations for a group of lines: ### Solhint has an official Docker Image Go to docker folder and follow [this](docker/docker.md) instructions. +## pre-commit +### Solhint can also be used as [pre-commit](https://pre-commit.com/) hook + +Replace `$GIT_TAG` with real tag: + +```YAML +- repo: https://github.com/protofire/solhint + rev: $GIT_TAG + hooks: + - id: solhint +``` + ## Documentation Related documentation you may find [here](https://protofire.github.io/solhint/). diff --git a/e2e/pre-commit-hook/.solhint.json b/e2e/pre-commit-hook/.solhint.json new file mode 100644 index 00000000..ce2220e0 --- /dev/null +++ b/e2e/pre-commit-hook/.solhint.json @@ -0,0 +1,3 @@ +{ + "extends": "solhint:recommended" +} diff --git a/e2e/pre-commit-hook/Counter.sol b/e2e/pre-commit-hook/Counter.sol new file mode 100644 index 00000000..087b4069 --- /dev/null +++ b/e2e/pre-commit-hook/Counter.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.8.26; + +contract Counter { + uint256 public number; + + function setNumber(uint256 newNumber) public { + number = newNumber; + } + + function increment() public { + number++; + } +} diff --git a/e2e/pre-commit-hook/test.sh b/e2e/pre-commit-hook/test.sh new file mode 100755 index 00000000..e26554ba --- /dev/null +++ b/e2e/pre-commit-hook/test.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +set -o errtrace -o nounset -o pipefail -o errexit + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" + +# Create temp working directory for mock repo +MOCK_REPO=$(mktemp -d) +if [[ ! "$MOCK_REPO" || ! -d "$MOCK_REPO" ]]; then + echo "Could not create temp dir" + exit 1 +fi +function cleanup { + echo "Deleting temp working directory $MOCK_REPO" + rm -rf "$MOCK_REPO" +} + +trap cleanup EXIT + +# Filling the mock repo +pushd "$MOCK_REPO" >/dev/null || exit 1 +git init --initial-branch=master +git config user.email "test@example.com" +git config user.name "pre-commit test" +cp "$SCRIPT_DIR/.solhint.json" "$SCRIPT_DIR/Counter.sol" . +git add . +git commit -m "Initial commit" + +# Run pre-commit inside the mock repo while referencing the solhint directory, +# where the .pre-commit-hooks.yaml is located. +pre-commit try-repo "$SCRIPT_DIR/../.." solhint --verbose --color=always --all-files diff --git a/package.json b/package.json index 74324f37..82e01688 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "lint": "eslint .", "generate-rulesets": "node scripts/generate-rulesets.js && prettier --write conf/rulesets", "docs": "node scripts/generate-rule-docs.js", - "prepare": "husky install", "prepublishOnly": "npm run lint && npm run test && npm run generate-rulesets" }, "bin": { From 88c4cb740aacc470a9550df6d2d0315ba2695bd7 Mon Sep 17 00:00:00 2001 From: Daniel Bast <2790401+dbast@users.noreply.github.com> Date: Tue, 20 Aug 2024 21:40:40 +0200 Subject: [PATCH 2/2] Use types instead of files regex --- .pre-commit-hooks.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 2ee0b022..a168aea4 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -2,5 +2,5 @@ name: solhint description: Lint Solidity files with Solhint entry: solhint - files: \.sol$ + types: ["solidity"] language: node