Browse Source

build: add gitlint commit-msg hook

gitlint provides an excellent standard ruleset for conventional
commits

the commit-msg shell script is default produced by gitlint

Consideration was given to using a tool such as pre-commit-hooks to
produce git hooks but ultimately using shell scripts reduces
dependancies and simplifies producing a determinaistic development
enviornment with nix.
master
DanConwayDev 3 years ago
parent
commit
6d87b3aec6
No known key found for this signature in database
GPG Key ID: 68E15486D73F75E1
  1. 34
      flake.nix
  2. 35
      git_hooks/commit-msg

34
flake.nix

@ -8,19 +8,21 @@ @@ -8,19 +8,21 @@
};
};
outputs = {
self,
nixpkgs,
gitignore,
flake-utils,
...
}:
outputs =
{ self
, nixpkgs
, gitignore
, flake-utils
, ...
}:
flake-utils.lib.eachDefaultSystem (
system: let
system:
let
pkgs = nixpkgs.legacyPackages.${system};
packageJSON = pkgs.lib.importJSON ./package.json;
gitignoreSource = gitignore.lib.gitignoreSource;
in {
in
{
packages = rec {
site-src = pkgs.mkYarnPackage rec {
name = "${packageJSON.name}-site-${version}";
@ -36,7 +38,7 @@ @@ -36,7 +38,7 @@
default = pkgs.writeShellApplication {
name = packageJSON.name;
runtimeInputs = [site-src pkgs.nodejs];
runtimeInputs = [ site-src pkgs.nodejs ];
text = ''
node ${site-src}/libexec/${packageJSON.name}/deps/${packageJSON.name}/build
'';
@ -44,7 +46,17 @@ @@ -44,7 +46,17 @@
};
devShell = pkgs.mkShell {
buildInputs = [pkgs.yarn pkgs.nodejs];
buildInputs = [
pkgs.gitlint
pkgs.nodejs
pkgs.yarn
];
shellHook = ''
# auto-install git hooks
dot_git="$(git rev-parse --git-common-dir)"
if [[ ! -d "$dot_git/hooks" ]]; then mkdir "$dot_git/hooks"; fi
for hook in git_hooks/* ; do ln -sf "$(pwd)/$hook" "$dot_git/hooks/" ; done
'';
};
}
);

35
git_hooks/commit-msg

@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
#!/bin/sh
### gitlint commit-msg hook start ###
# Determine whether we have a tty available by trying to access it.
# This allows us to deal with UI based gitclient's like Atlassian SourceTree.
# NOTE: "exec < /dev/tty" sets stdin to the keyboard
stdin_available=1
(exec < /dev/tty) 2> /dev/null || stdin_available=0
if [ $stdin_available -eq 1 ]; then
# Now that we know we have a functional tty, set stdin to it so we can ask the user questions :-)
exec < /dev/tty
# On Windows, we need to explicitly set our stdout to the tty to make terminal editing work (e.g. vim)
# See SO for windows detection in bash (slight modified to work on plain shell (not bash)):
# https://stackoverflow.com/questions/394230/how-to-detect-the-os-from-a-bash-script
if [ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ] || [ "$OSTYPE" = "win32" ]; then
exec > /dev/tty
fi
fi
gitlint --staged --msg-filename "$1" run-hook
exit_code=$?
# If we fail to find the gitlint binary (command not found), let's retry by executing as a python module.
# This is the case for Atlassian SourceTree, where $PATH deviates from the user's shell $PATH.
if [ $exit_code -eq 127 ]; then
echo "Fallback to python module execution"
python -m gitlint.cli --staged --msg-filename "$1" run-hook
exit_code=$?
fi
exit $exit_code
### gitlint commit-msg hook end ###
Loading…
Cancel
Save