replace pre-commit with shell script
All checks were successful
build / test (push) Successful in 9s

This saves one flake input and some python dependencies
This commit is contained in:
Jörg Thalheim 2023-08-03 14:25:26 +02:00
parent 7fa6808478
commit 930923512c
5 changed files with 56 additions and 142 deletions

View File

@ -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
'';
};
};
}

View File

@ -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 "$@"
'');
};
};
}

View File

@ -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": [

View File

@ -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, ... }:

52
scripts/pre-commit Executable file
View File

@ -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