From e7a9262f5008a922b5f5ddd5dca02b3f91514ec0 Mon Sep 17 00:00:00 2001 From: nmlgc Date: Mon, 20 Mar 2023 01:29:50 +0100 Subject: [PATCH] [Contributing] Remove trailing commas from `public` in a pre-commit hook Starting more ore less simple with a shell script calling `sed`, which should work anywhere Git is used. --- CONTRIBUTING.md | 9 +++++++++ hooks/pre-commit | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 hooks/pre-commit diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d846a068..cdcb1cae 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -17,6 +17,15 @@ For starters, simply naming functions or global variables to reflect their actual intent will already be helpful. *Any* name is better than `sub_`, and can always be fixed or improved later. +## Setup + +Before committing anything, enable the Git hooks from the `hooks/` subdirectory +to automatically catch common mistakes: + +```shell +git config --local core.hooksPath hooks/ +``` + # Contribution guidelines ## Rule #1 diff --git a/hooks/pre-commit b/hooks/pre-commit new file mode 100644 index 00000000..d39327ba --- /dev/null +++ b/hooks/pre-commit @@ -0,0 +1,18 @@ +#!/bin/sh + +if git rev-parse --verify HEAD >/dev/null 2>&1 +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 +fi + +# Remove trailing commas from `public` directives. These crash on TASM32 +# version 5.0, but work on later versions. +for f in `git diff-index --name-only --cached --diff-filter=ACMRX ${against} | grep \.asm$ -i`; do + if [ -n "$(sed -i -E 's/(public\s+[^;]+?),\s*(;.*)?$/\1\2/w /dev/stdout' "$f")" ]; then + echo "$f"': removed a trailing comma from a `public` line, please re-stage.' + exit 1 + fi +done