dotfiles/files/git/.config/git/template/hooks/prepare-commit-msg
2024-07-09 17:39:25 +02:00

40 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
# SPDX-FileCopyrightText: 2023 Benjamin Grande M. S. <ben.grande.b@gmail.com>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
## Removes the default message "Please enter the commit message".
## Changes empty line between the commit.template and commit.status or
## commit.verbose line to be a commented line.
## Fix the init.template comment in case core.commentChar is set.
set -eu
file="$1"
#commit_source="$2"
#sha1="$3"
char="$(git config --get core.commentChar || echo "#")"
template="$(git config --get commit.template | sed "s|^~/|$HOME/|")"
if test "$char" = "auto"; then
## Try to skip the init.template comment char by getting the last match, as
## the template will be placed at the beginning of the file.
char="$(grep -E "^(#|;|@|!|$|%|^|&|\\||:) " "$file" | cut -c 1 | tail -n 1)"
fi
## Remove the default instructional message and its following empty line.
sed -i'' \
-e "/^. Please enter the commit message .*. Lines starting$/d" \
-e "/^. with '.' will be ignored, .* aborts the commit.$/ {
N; d; }" \
"$file"
## Replace init.template comment char to the core.commentChar line per line.
if test -f "$template"; then
while read -r line; do
mod_line="$(echo "$line" | sed "s/^. /${char} /")"
sed -i'' "s/^${line}$/${mod_line}/" "$file"
done < "$template"
fi