From 930923512c03179fe75e4209c27eb3da368e7766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 3 Aug 2023 14:25:26 +0200 Subject: [PATCH] replace pre-commit with shell script This saves one flake input and some python dependencies --- flake-parts/devShells/default.nix | 4 +- flake-parts/formatting.nix | 16 +--- flake.lock | 125 ------------------------------ flake.nix | 1 - scripts/pre-commit | 52 +++++++++++++ 5 files changed, 56 insertions(+), 142 deletions(-) create mode 100755 scripts/pre-commit diff --git a/flake-parts/devShells/default.nix b/flake-parts/devShells/default.nix index 35261723..4a71b7af 100644 --- a/flake-parts/devShells/default.nix +++ b/flake-parts/devShells/default.nix @@ -7,7 +7,6 @@ }: { devShells.default = pkgs.mkShell { inputsFrom = [ - config.pre-commit.devShell config.treefmt.build.devShell ]; packages = [ @@ -15,6 +14,9 @@ self'.packages.tea-create-pr self'.packages.merge-after-ci ]; + shellHook = '' + ln -sf ../../scripts/pre-commit .git/hooks/pre-commit + ''; }; }; } diff --git a/flake-parts/formatting.nix b/flake-parts/formatting.nix index 99b30085..15cbff2c 100644 --- a/flake-parts/formatting.nix +++ b/flake-parts/formatting.nix @@ -3,10 +3,9 @@ , ... }: { imports = [ - inputs.pre-commit-hooks-nix.flakeModule inputs.treefmt-nix.flakeModule ]; - perSystem = { pkgs, config, ... }: { + perSystem = { pkgs, ... }: { treefmt.projectRootFile = "flake.nix"; treefmt.flakeCheck = true; treefmt.flakeFormatter = true; @@ -37,18 +36,5 @@ ]; includes = [ "*.py" ]; }; - - # we already run treefmt in ci - pre-commit.check.enable = false; - # activated in devShells via inputsFrom = [config.pre-commit.devShell]; - pre-commit.settings.hooks.format-all = { - name = "format-all"; - enable = true; - pass_filenames = true; - entry = toString (pkgs.writeScript "treefmt" '' - #!${pkgs.bash}/bin/bash - ${config.treefmt.build.wrapper}/bin/treefmt --clear-cache --fail-on-change "$@" - ''); - }; }; } diff --git a/flake.lock b/flake.lock index 62baf34a..06a2de05 100644 --- a/flake.lock +++ b/flake.lock @@ -20,22 +20,6 @@ "type": "github" } }, - "flake-compat": { - "flake": false, - "locked": { - "lastModified": 1673956053, - "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", - "owner": "edolstra", - "repo": "flake-compat", - "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", - "type": "github" - }, - "original": { - "owner": "edolstra", - "repo": "flake-compat", - "type": "github" - } - }, "flake-parts": { "inputs": { "nixpkgs-lib": [ @@ -56,45 +40,6 @@ "type": "github" } }, - "flake-utils": { - "inputs": { - "systems": "systems" - }, - "locked": { - "lastModified": 1685518550, - "narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "gitignore": { - "inputs": { - "nixpkgs": [ - "pre-commit-hooks-nix", - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1660459072, - "narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=", - "owner": "hercules-ci", - "repo": "gitignore.nix", - "rev": "a20de23b925fd8264fd7fad6454652e142fd7f73", - "type": "github" - }, - "original": { - "owner": "hercules-ci", - "repo": "gitignore.nix", - "type": "github" - } - }, "nixlib": { "locked": { "lastModified": 1689469483, @@ -147,85 +92,15 @@ "type": "github" } }, - "nixpkgs-stable": { - "locked": { - "lastModified": 1685801374, - "narHash": "sha256-otaSUoFEMM+LjBI1XL/xGB5ao6IwnZOXc47qhIgJe8U=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "c37ca420157f4abc31e26f436c1145f8951ff373", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixos-23.05", - "repo": "nixpkgs", - "type": "github" - } - }, - "nixpkgs_2": { - "locked": { - "lastModified": 1689261696, - "narHash": "sha256-LzfUtFs9MQRvIoQ3MfgSuipBVMXslMPH/vZ+nM40LkA=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "df1eee2aa65052a18121ed4971081576b25d6b5c", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "pre-commit-hooks-nix": { - "inputs": { - "flake-compat": "flake-compat", - "flake-utils": "flake-utils", - "gitignore": "gitignore", - "nixpkgs": "nixpkgs_2", - "nixpkgs-stable": "nixpkgs-stable" - }, - "locked": { - "lastModified": 1690743255, - "narHash": "sha256-dsJzQsyJGWCym1+LMyj2rbYmvjYmzeOrk7ypPrSFOPo=", - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "rev": "fcbf4705d98398d084e6cb1c826a0b90a91d22d7", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "type": "github" - } - }, "root": { "inputs": { "disko": "disko", "flake-parts": "flake-parts", "nixos-generators": "nixos-generators", "nixpkgs": "nixpkgs", - "pre-commit-hooks-nix": "pre-commit-hooks-nix", "treefmt-nix": "treefmt-nix" } }, - "systems": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } - }, "treefmt-nix": { "inputs": { "nixpkgs": [ diff --git a/flake.nix b/flake.nix index b64fefe3..7e7553b7 100644 --- a/flake.nix +++ b/flake.nix @@ -11,7 +11,6 @@ flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; treefmt-nix.url = "github:numtide/treefmt-nix"; treefmt-nix.inputs.nixpkgs.follows = "nixpkgs"; - pre-commit-hooks-nix.url = "github:cachix/pre-commit-hooks.nix"; }; outputs = inputs @ { flake-parts, ... }: diff --git a/scripts/pre-commit b/scripts/pre-commit new file mode 100755 index 00000000..e2410448 --- /dev/null +++ b/scripts/pre-commit @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# +# Run treefmt on every commit and abort if some files have changed. +# +# To install, copy this file to .git/hooks/pre-commit and make sure it's +# executable. +# +set -euo pipefail + +# Redirect stdout to stderr +exec 1>&2 + +# Get list of files that will be committed +mapfile -t commit_files < <(git diff --name-only --cached) + +log() { + echo "treefmt pre-commit: $*" +} + +# If the commit has no files, skip everything as there is nothing to format +if [[ ${#commit_files} = 0 ]]; then + log "no files to format" + exit 0 +fi + +# Will be called at the end +restore_stash() { + # Store exit status + local ret=$? + # Don't fail on error from now on + set +e + # Put bash the staged files + git stash pop -q + + if [[ $ret -gt 0 ]]; then + log "aborting commit, detected unformatted files" + fi + exit "$ret" +} + +# Stash index and work dir, keeping only the to-be-committed changes in +# the working directory. +git stash push --quiet --keep-index --message "treefmt pre-commit" + +# Install the callback to restore the stash on script exit +trap restore_stash EXIT + +# Run treefmt on the files in the index and record the result. +nix fmt -- --no-cache --quiet "${commit_files[@]}" + +# Check if there is a diff +git diff --name-only --exit-code