From 209f33e7a0ced2c2868d11ec9bc3ffb4f658b5ba Mon Sep 17 00:00:00 2001 From: James Brown Date: Thu, 18 Jan 2024 09:06:01 -0800 Subject: [PATCH] implementation of pre-commit hook to format justfiles (#1) --- .github/workflows/pre-commit.yml | 14 ++++++++++++++ .pre-commit-config.yaml | 25 +++++++++++++++++++++++++ .pre-commit-hooks.yaml | 5 +++++ LICENSE.txt | 7 +++++++ README.md | 20 ++++++++++++++++++++ pre-commit-just.sh | 20 ++++++++++++++++++++ 6 files changed, 91 insertions(+) create mode 100644 .github/workflows/pre-commit.yml create mode 100644 .pre-commit-config.yaml create mode 100644 .pre-commit-hooks.yaml create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100755 pre-commit-just.sh diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000..c2f7e71 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,14 @@ +name: pre-commit + +on: + pull_request: + push: + branches: [main] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + - uses: pre-commit/action@v3.0.0 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..caed8ff --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,25 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - id: fix-byte-order-marker + - id: check-json + - id: check-merge-conflict +- repo: https://github.com/Mateusz-Grzelinski/actionlint-py + rev: v1.6.26.11 + hooks: + - id: actionlint + additional_dependencies: [ pyflakes>=3.0.1, shellcheck-py>=0.9.0.5 ] +- repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.9.0.6 + hooks: + - id: shellcheck +- repo: https://github.com/scop/pre-commit-shfmt + rev: v3.7.0-4 + hooks: + - id: shfmt + args: [-w, -s, -i, '4', -ci] diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..1fc2149 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,5 @@ +- id: format-justfile + name: Format Justfiles + language: script + entry: pre-commit-just.sh + files: '\.?[jJ]ustfile' diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..022659b --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,7 @@ +ISC License + +Copyright 2024 Instrumentl, Inc. + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3a3c8fd --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +This repository contains a pre-commit config for running `just --fmt` on any +discovered [just](https://github.com/casey/just) files. It will auto-fix the +files. + +## Usage + +You must have `just` installed on your system for this hook to work. + +```yaml +- repo: https://github.com/instrument/pre-commit-justfile + rev: 'main' + hooks: + - id: format-justfile +``` + +## License + +This work is licensed under the ISC license, a copy of which can be found at [LICENSE.txt](LICENSE.txt). + +`just` itself is licensed under the CC0 license. diff --git a/pre-commit-just.sh b/pre-commit-just.sh new file mode 100755 index 0000000..fb1a8e2 --- /dev/null +++ b/pre-commit-just.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -euo pipefail + +if ! command -v just /dev/null 2>&1; then + echo >&2 "no just binary found; not running" + exit 0 +fi + +status=0 + +for file in "$@"; do + if ! just --fmt --unstable --check -f "$file" >/dev/null 2>&1; then + echo >&2 "fixing ${file}" + just --fmt --unstable -f "$file" >/dev/null 2>&1 + status=1 + fi +done + +exit $status