-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve git template and add post-chehckout hook to alias master to main
- Loading branch information
1 parent
56c9fb7
commit 9aefef7
Showing
1 changed file
with
48 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,57 @@ | ||
{ 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 = let | ||
# Copy of https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/trivial-builders/default.nix but uses `env bash` | ||
writeShellScript = name: text: | ||
pkgs.writeTextFile { | ||
inherit name; | ||
executable = true; | ||
text = '' | ||
#!/usr/bin/env bash | ||
function check_commit_msg_length { | ||
readonly MAX_MSG_LENGTH=72 | ||
${text} | ||
''; | ||
checkPhase = '' | ||
${pkgs.stdenv.shellDryRun} "$target" | ||
''; | ||
}; | ||
in { | ||
".git_template/hooks/commit-msg".source = writeShellScript "commit-msg" '' | ||
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 = writeShellScript "post-checkout" '' | ||
readonly PREV_HEAD="$1" | ||
readonly NEW_HEAD="$2" | ||
# Retrieving a file from the index, flag=0 / changing branches, flag=1 | ||
readonly CHECKOUT_TYPE="$3" | ||
function rename_main { | ||
local current_branch="$(git branch --show-current)" | ||
if [ "$current_branch" = "main" ]; then | ||
git branch -m master | ||
fi | ||
} | ||
} | ||
check_commit_msg_length | ||
''; | ||
if [ "$CHECKOUT_TYPE" = "1" ]; then | ||
rename_main | ||
fi | ||
''; | ||
}; | ||
} |