From 78b3796e681032a65f7fcf1aa3975632d70cd3f8 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 16 Mar 2024 14:59:19 -0500 Subject: [PATCH] feat: add check-fmt-pre-commit.bash to git hooks --- config/default.nix | 3 ++- flake.nix | 3 ++- scripts/check-fmt-git-files.bash | 2 +- scripts/check-fmt-git-pre-commit.bash | 22 ++++++++++++++++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 scripts/check-fmt-git-pre-commit.bash diff --git a/config/default.nix b/config/default.nix index e1da9e64..a067c9d1 100644 --- a/config/default.nix +++ b/config/default.nix @@ -28,6 +28,7 @@ in { home = { packages = with pkgs; [ + Fmt nodePackages.prettier shfmt bob-nvim @@ -236,7 +237,7 @@ in { }; git = { enable = true; - hooks = { pre-commit = ../scripts/check-fmt-git-files.bash; }; + hooks = { pre-commit = ../scripts/check-fmt-git-pre-commit.bash; }; userName = "Price Hiller"; userEmail = "price@orion-technologies.io"; aliases = { unstage = "reset HEAD --"; }; diff --git a/flake.nix b/flake.nix index 3b40ba0e..b8db1827 100644 --- a/flake.nix +++ b/flake.nix @@ -41,7 +41,7 @@ defaultPackage.x86_64-linux = home-manager.defaultPackage.x86_64-linux; targets.genericLinux = { enable = true; }; homeConfigurations.${username} = - home-manager.lib.homeManagerConfiguration { + home-manager.lib.homeManagerConfiguration rec { pkgs = nixpkgs.legacyPackages.${system}; extraSpecialArgs = { inherit inputs; @@ -57,6 +57,7 @@ inputs.kanagawa-gtk.overlays.default inputs.nixgl.overlay (final: prev: { + Fmt = pkgs.writeScriptBin "Fmt" (builtins.readFile ./scripts/fmt.bash); waybar = inputs.waybar.packages.${system}.default; lxappearance = prev.lxappearance.overrideAttrs (oldAttrs: { postInstall = '' diff --git a/scripts/check-fmt-git-files.bash b/scripts/check-fmt-git-files.bash index 72a8f9d1..2895f4c8 100755 --- a/scripts/check-fmt-git-files.bash +++ b/scripts/check-fmt-git-files.bash @@ -16,7 +16,7 @@ main() { fi done < <(git ls-tree --full-name --full-tree --name-only -r HEAD) - "${root}/scripts/fmt.bash" -- "${files[@]}" + Fmt -- "${files[@]}" } main "${@}" diff --git a/scripts/check-fmt-git-pre-commit.bash b/scripts/check-fmt-git-pre-commit.bash new file mode 100644 index 00000000..01fe49bf --- /dev/null +++ b/scripts/check-fmt-git-pre-commit.bash @@ -0,0 +1,22 @@ +#!/usr/bin/env -S nix --extra-experimental-features "flakes nix-command" shell nixpkgs#bash nixpkgs#git --command bash +# vim: ft=sh + +set -euo pipefail + +main() { + local root + root="$(git rev-parse --show-toplevel)" + + local files=() + + while IFS= read -r file; do + local fpath="${root}/${file}" + if [[ -r "${fpath}" ]]; then + files+=("${fpath}") + fi + done < <(git diff --name-only --staged --diff-filter=d) + + Fmt -- "${files[@]}" +} + +main "${@}"