Skip to content

Commit

Permalink
Merge pull request #596 from dbast/pre-commit
Browse files Browse the repository at this point in the history
Add pre-commit hook definition with test
  • Loading branch information
dbale-altoros authored Dec 19, 2024
2 parents 371840a + 10d8e58 commit dbb75f6
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .github/workflows/PRE-COMMIT-HOOK-TEST.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- id: solhint
name: solhint
description: Lint Solidity files with Solhint
entry: solhint
types: ["solidity"]
language: node
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
Expand Down
3 changes: 3 additions & 0 deletions e2e/pre-commit-hook/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "solhint:recommended"
}
14 changes: 14 additions & 0 deletions e2e/pre-commit-hook/Counter.sol
Original file line number Diff line number Diff line change
@@ -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++;
}
}
31 changes: 31 additions & 0 deletions e2e/pre-commit-hook/test.sh
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit dbb75f6

Please sign in to comment.