From ce7bb9be0e244fcf0f03aef8d322a9cb31b1f8d4 Mon Sep 17 00:00:00 2001 From: Ryan Horiguchi Date: Thu, 9 Nov 2023 17:57:42 +0100 Subject: [PATCH] Add commit-msg hook to git templates --- .../home-manager/{git.nix => git/default.nix} | 2 ++ modules/home-manager/git/template.nix | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+) rename modules/home-manager/{git.nix => git/default.nix} (98%) create mode 100644 modules/home-manager/git/template.nix diff --git a/modules/home-manager/git.nix b/modules/home-manager/git/default.nix similarity index 98% rename from modules/home-manager/git.nix rename to modules/home-manager/git/default.nix index 88981c0cc..6fedcdfd4 100755 --- a/modules/home-manager/git.nix +++ b/modules/home-manager/git/default.nix @@ -1,4 +1,6 @@ { pkgs, config, lib, colors, ... }: { + imports = [ ./template.nix ]; + home = { packages = [ pkgs.nano ]; diff --git a/modules/home-manager/git/template.nix b/modules/home-manager/git/template.nix new file mode 100644 index 000000000..bb79f4003 --- /dev/null +++ b/modules/home-manager/git/template.nix @@ -0,0 +1,23 @@ +{ pkgs, lib, config, ... }: { + programs.git.extraConfig.init.templatedir = "${config.home.homeDirectory}/.git_template"; + + home.file.".git_template/hooks/commit-msg".source = pkgs.writeShellScript "commit-msg" '' + PATH=${lib.makeBinPath [ pkgs.coreutils ]} + + readonly COMMIT_MSG_FILE="$1" + + function check_commit_msg_length { + readonly MAX_MSG_LENGTH=72 + + local title=$(head -n 1 "$COMMIT_MSG_FILE") + + if [ ''${#title} -gt $MAX_MSG_LENGTH ]; then + # TODO figure out how to use hex colors variable + echo -e "\x1b[1;38;5;203mCommit title is ''${#title} characters long, must be equal or shorter than $MAX_MSG_LENGTH characters!\e[0m"; + exit 1 + fi + } + + check_commit_msg_length + ''; +}