Skip to content

Commit

Permalink
Improve git template and add post-chehckout hook to alias master to main
Browse files Browse the repository at this point in the history
  • Loading branch information
rhoriguchi committed Dec 21, 2023
1 parent 56c9fb7 commit a5e7703
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions modules/home-manager/git/template.nix
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
{ pkgs, config, ... }: {
programs.git.extraConfig.init.templatedir = "${config.home.homeDirectory}/.git_template";

home.file.".git_template/hooks/commit-msg".source = pkgs.writeShellScript "commit-msg" ''
readonly COMMIT_MSG_FILE="$1"
home.file = {
".git_template/hooks/commit-msg".source = pkgs.writeShellScript "commit-msg" ''
#!/usr/bin/env bash
function check_commit_msg_length {
readonly MAX_MSG_LENGTH=72
readonly COMMIT_MSG_FILE="$1"
local title=$(head -n 1 "$COMMIT_MSG_FILE")
function check_commit_msg_length {
readonly MAX_MSG_LENGTH=72
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
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
'';

".git_template/hooks/post-checkout".source = pkgs.writeShellScript "post-checkout" ''
#!/usr/bin/env bash
readonly CURRENT_BRANCH="$2"
if [ "$CURRENT_BRANCH" = "main" ]; then
if [ $(git show-ref --verify --quiet refs/heads/master) ]; then
git branch -D master
fi
}
check_commit_msg_length
'';
git symbolic-ref refs/heads/master refs/heads/main
git switch master
fi
'';
};
}

0 comments on commit a5e7703

Please sign in to comment.