shell: add editorconfig hook

This commit is contained in:
Timothy DeHerrera 2021-02-01 23:16:26 -07:00
parent 46a7e7d4ce
commit c4699ed682
No known key found for this signature in database
GPG Key ID: 8985725DB5B0C122

View File

@ -59,6 +59,8 @@ pkgs.devshell.mkShell {
git.hooks = with pkgs; { git.hooks = with pkgs; {
enable = true; enable = true;
pre-commit.text = '' pre-commit.text = ''
#!/usr/bin/env bash
if ${git}/bin/git rev-parse --verify HEAD >/dev/null 2>&1 if ${git}/bin/git rev-parse --verify HEAD >/dev/null 2>&1
then then
against=HEAD against=HEAD
@ -66,15 +68,25 @@ pkgs.devshell.mkShell {
# Initial commit: diff against an empty tree object # Initial commit: diff against an empty tree object
against=$(${git}/bin/git hash-object -t tree /dev/null) against=$(${git}/bin/git hash-object -t tree /dev/null)
fi fi
# Redirect output to stderr.
exec 1>&2 diff="${git}/bin/git diff-index --name-only --cached $against --diff-filter d"
nix_files=($($diff -- '*.nix'))
all_files=($($diff))
# Format staged nix files. # Format staged nix files.
exec ${nixpkgs-fmt}/bin/nixpkgs-fmt \ ${nixpkgs-fmt}/bin/nixpkgs-fmt "${"\${nix_files[@]}"}"
$(
${git}/bin/git diff-index --name-only --cached $against --diff-filter d -- \ # check editorconfig
| ${ripgrep}/bin/rg '\.nix$' ${editorconfig-checker}/bin/editorconfig-checker -- "${"\${all_files[@]}"}"
) if [[ $? != '0' ]]; then
{
echo -e "\nCode is not aligned with .editorconfig"
echo "Review the output and commit your fixes"
} >&2
exit 1
fi
''; '';
}; };