From 9e7909c2276ffb4903b675c9afd234f4bd9c724f Mon Sep 17 00:00:00 2001 From: Mattis Bratland Date: Tue, 21 Jul 2026 15:54:32 +0200 Subject: [PATCH] chore: add ktlint pre-commit hook --- get_started.sh | 6 ++++++ scripts/hooks/pre-commit | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100755 get_started.sh create mode 100755 scripts/hooks/pre-commit diff --git a/get_started.sh b/get_started.sh new file mode 100755 index 0000000..a13b26c --- /dev/null +++ b/get_started.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +set -eu + +git config --local core.hooksPath scripts/hooks/ +chmod +x scripts/hooks/* diff --git a/scripts/hooks/pre-commit b/scripts/hooks/pre-commit new file mode 100755 index 0000000..913387b --- /dev/null +++ b/scripts/hooks/pre-commit @@ -0,0 +1,26 @@ +#!/bin/sh + +repository_root=$(git rev-parse --show-toplevel) +cd "$repository_root" || exit 1 + +echo "[git pre-commit hook] Running ktlintFormat before commit." + +staged_files=$(git diff --name-only --cached --diff-filter=d) +if [ -z "$staged_files" ]; then + echo "No staged files, skipping formatting." + exit 0 +fi + +./gradlew :ktlintFormat --configuration-cache +status=$? + +echo "Finished ktlintFormat with status: $status" + +if [ "$status" -ne 0 ]; then + echo "Error running ktlintFormat, aborting commit." + exit "$status" +fi + +printf '%s\n' "$staged_files" | while IFS= read -r file; do + git add -- "$file" +done