Last active
November 12, 2023 23:13
-
-
Save SuperSandro2000/77439b7eb5f7a0da6967524190a4ce8f to your computer and use it in GitHub Desktop.
prepare commit message for nixpkgs. put into .git/hooks/prepare-commit-msg
This file contains hidden or 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
#!/usr/bin/env bash | |
# shellcheck disable=SC2034 | |
COMMIT_MSG_FILE=$1 # file which contains the commmit message | |
# shellcheck disable=SC2034 | |
COMMIT_SOURCE=$2 # eg commit | |
# shellcheck disable=SC2034 | |
SHA1=$3 # eg head | |
perl -i -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE" | |
if [[ -z $SHA1 && -z $COMMIT_SOURCE ]] && type -P sponge &>/dev/null; then | |
staged_files=$(git diff --cached --name-status | awk '{print $2}') | |
staged_dirnames=() | |
for line in $staged_files; do | |
staged_dirnames+=("$(dirname "$line")") | |
done | |
if [[ $(echo "${staged_dirnames[@]}" | sed 's| |\n|g' | sort -u | wc -l) == 1 ]]; then | |
changed_path=${staged_files[0]} | |
# prefix optional, gets prefixed to package | |
# package string to inject, a colon is added | |
# message whole message, no colon or prefix is added | |
# nixpkgs | |
if [[ $changed_path == nixos/modules/* ]]; then | |
prefix="nixos/" | |
package=$(basename "$staged_files" .nix) | |
elif [[ $changed_path == pkgs/applications/kde/* ]]; then | |
prefix="plasma5Packages." | |
package=$(basename "$staged_files" .nix) | |
elif [[ $changed_path == pkgs/applications/window-managers/i3/* ]]; then | |
prefix="i3/" | |
package=$(basename "$staged_files" .nix) | |
elif [[ $changed_path == pkgs/applications/editors/vim/plugins/* ]]; then | |
prefix="vimPlugins." | |
elif [[ $changed_path == pkgs/desktops/gnome-3/* ]]; then | |
prefix="gnome3." | |
elif [[ $changed_path == pkgs/desktops/plasma-5/* ]]; then | |
prefix="plasma5Packages." | |
elif [[ $changed_path == pkgs/desktops/pantheon/* ]]; then | |
prefix="pantheon." | |
elif [[ $changed_path == pkgs/development/haskell-modules/* ]]; then | |
prefix="haskellPackages" | |
elif [[ $changed_path == pkgs/development/libraries/gstreamer/* ]]; then | |
prefix="gst_all_1.gst-plugins-" | |
elif [[ $changed_path == pkgs/development/libraries/kde-frameworks/* ]]; then | |
prefix="kde-frameworks." | |
package=$(basename "$staged_files" .nix) | |
elif [[ $changed_path == pkgs/development/python-modules/* ]]; then | |
prefix="python310Packages." | |
package="$(basename "$(dirname "$changed_path")")" | |
elif [[ $changed_path == pkgs/development/tools/ocaml/* ]]; then | |
prefix="ocamlPackages." | |
elif [[ $changed_path == ppkgs/development/tools/parsing/tree-sitter/grammars/* ]]; then | |
message="tree-sitter: update grammars" | |
elif [[ $changed_path == ppkgs/development/tools/parsing/tree-sitter/* ]]; then | |
package="tree-sitter" | |
elif [[ $changed_path == pkgs/misc/vscode-extensions/* ]]; then | |
message="vscode-extensions." | |
elif [[ $changed_path == pkgs/servers/monitoring/prometheus ]]; then | |
prefix="prometheus." | |
package=$(basename "$staged_files" .nix) | |
elif [[ $changed_path == pkgs/top-level/perl-packages.nix ]]; then | |
package="perlPackages." | |
elif [[ $changed_path == pkgs/top-level/python-packages.nix ]]; then | |
package="python310Packages" | |
elif [[ $changed_path == pkgs/misc/vim-plugins/* ]]; then | |
prefix="vimPlugins." | |
elif [[ $changed_path == pkgs/* ]]; then | |
package="$(basename "$(dirname "$changed_path")")" | |
fi | |
if [[ ! -v package ]]; then | |
package=$(basename "$changed_path") | |
fi | |
if [[ ! -v message ]]; then | |
# two spaces because core.editor vim trick puts us at the second last character for whatever reason | |
message="$prefix$package: " | |
fi | |
echo -en "$message\n" | cat - "$COMMIT_MSG_FILE" | sponge "$COMMIT_MSG_FILE" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment