From f46fd3ace63d02a52a71465b9da3a09a28721804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Mon, 27 May 2024 10:58:55 +0200 Subject: [PATCH] make git-hooks opt-in pre-commit hook break git commits and are disruptive. Therefore people that want to enable this feature, should enable it locally instead. I.e. treefmt will also check untracked files that are not meant for the current commit. --- devShell.nix | 10 +--------- flake.lock | 17 ----------------- flake.nix | 2 -- scripts/pre-commit | 28 ++++++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 28 deletions(-) create mode 100755 scripts/pre-commit diff --git a/devShell.nix b/devShell.nix index ad72eb21..ce682606 100644 --- a/devShell.nix +++ b/devShell.nix @@ -1,4 +1,4 @@ -{ inputs, ... }: +{ ... }: { perSystem = { @@ -9,7 +9,6 @@ }: let writers = pkgs.callPackage ./pkgs/builders/script-writers.nix { }; - inherit (pkgs.callPackage inputs.git-hooks { }) lib; ansiEscapes = { reset = ''\033[0m''; @@ -22,11 +21,6 @@ select-shell = writers.writePython3Bin "select-shell" { flakeIgnore = [ "E501" ]; } ./pkgs/scripts/select-shell.py; - - # run treefmt before each commit - install-pre-commit-hook = - with lib.git-hooks; - pre-commit (wrap.abort-on-change config.treefmt.build.wrapper); in { devShells.default = pkgs.mkShell { @@ -41,8 +35,6 @@ config.treefmt.build.wrapper ]; shellHook = '' - ${install-pre-commit-hook} - echo -e "${ansiEscapes.green}switch to another dev-shell using: select-shell${ansiEscapes.reset}" ''; }; diff --git a/flake.lock b/flake.lock index f991e74f..00ba773c 100644 --- a/flake.lock +++ b/flake.lock @@ -40,22 +40,6 @@ "type": "github" } }, - "git-hooks": { - "flake": false, - "locked": { - "lastModified": 1716413087, - "narHash": "sha256-nSTIB7JeJGBGsvtqlyfhUByh/isyK1nfOq2YMxUOFJQ=", - "owner": "fricklerhandwerk", - "repo": "git-hooks", - "rev": "99a78fcf7dc03ba7b1d5c00af109c1e28ced3490", - "type": "github" - }, - "original": { - "owner": "fricklerhandwerk", - "repo": "git-hooks", - "type": "github" - } - }, "nixlib": { "locked": { "lastModified": 1712450863, @@ -149,7 +133,6 @@ "inputs": { "disko": "disko", "flake-parts": "flake-parts", - "git-hooks": "git-hooks", "nixos-generators": "nixos-generators", "nixos-images": "nixos-images", "nixpkgs": "nixpkgs", diff --git a/flake.nix b/flake.nix index 5a9ee77c..3403db97 100644 --- a/flake.nix +++ b/flake.nix @@ -21,8 +21,6 @@ flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; treefmt-nix.url = "github:numtide/treefmt-nix"; treefmt-nix.inputs.nixpkgs.follows = "nixpkgs"; - git-hooks.url = "github:fricklerhandwerk/git-hooks"; - git-hooks.flake = false; }; outputs = diff --git a/scripts/pre-commit b/scripts/pre-commit new file mode 100755 index 00000000..bbc63a16 --- /dev/null +++ b/scripts/pre-commit @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# To install: +# ln -sf ../../scripts/pre-commit .git/hooks/pre-commit +set -o errexit +set -o nounset +set -o pipefail + +readarray staged < <(git diff --name-only --cached) +[[ ${#staged[@]} = 0 ]] && exit +unstash() { + local ret=$? + set +e + git stash pop -q + exit "$ret" +} +git stash push --quiet --keep-index --message "pre-commit" +trap unstash EXIT +nix fmt +{ + changed=$(git diff --name-only --exit-code); + status=$?; +} || true +if [[ $status -ne 0 ]]; then + exec 1>&2 + echo Files changed by pre-commit hook: + echo "$changed" + exit $status +fi