Skip to content

Commit

Permalink
devshell: add (tiny) but useful pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
David Arnold committed Apr 23, 2021
1 parent 5068abf commit b6ddcef
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions devshell.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
imports = [ "git.hooks" ]

[devshell]
name = "nixpkgs"
packages = [
"fd",
"nixpkgs-fmt",
"editorconfig-checker",
]

[[commands]]
package = "editorconfig-checker"
category = "linters"

[[commands]]
name = "fmt"
help = "Check Nix formatting"
Expand All @@ -17,3 +24,34 @@ help = "Check Nix parsing"
category = "folder tree checks"
command = "fd --extension nix --exec nix-instantiate --parse --quiet {} >/dev/null"

[git.hooks]
enable = true
pre-commit.text = """
#!/usr/bin/env bash
set -eou pipefail
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(${git}/bin/git hash-object -t tree /dev/null)
fi
diff="git diff-index --name-only --cached $against --diff-filter d"
nix_files=($($diff -- '*.nix'))
all_files=($($diff))
# Format staged nix files.
if [[ -n "${nix_files[@]}" ]]; then
nixpkgs-fmt "${nix_files[@]}"
git add "${nix_files[@]}"
fi
# check editorconfig
if ! editorconfig-checker -- "${all_files[@]}"; then
echo -e "\nCode is not aligned with .editorconfig
Review the output and commit your fixes" >&2
exit 1
fi
"""

0 comments on commit b6ddcef

Please sign in to comment.