From 6cac0dc53ae380292c4184c6c0527fbb14e13b39 Mon Sep 17 00:00:00 2001 From: druckdev <63563978+druckdev@users.noreply.github.com> Date: Mon, 23 Nov 2020 01:27:51 +0100 Subject: [PATCH] git: Show patch of the changes in commit message The changes are only shown in the editor and do not land in the final commit message. For that setting the git-commit-last-msg function and the commit-msg hook had to be updated. The function is now a standalone function instead of anonymous and uses every line until the first comment in COMMIT_EDITMSG discarding the new information too. The hook breaks now when checking line lengths when the changes start since for some weird reason they are passed together with the rest of the message instead of being deleted like the comments. --- .config/git/config | 1 + .config/zsh/zshrc.d/30-alias.zsh | 7 ------- .config/zsh/zshrc.d/40-functions.zsh | 11 +++++++++++ meta/git/hooks/commit-msg | 6 ++++++ 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/.config/git/config b/.config/git/config index 0f5979b..5123d58 100644 --- a/.config/git/config +++ b/.config/git/config @@ -4,6 +4,7 @@ signingkey = 2BA20014C779A9611ED81E2163DF2C14933E928C [commit] gpgsign = true + verbose = true [difftool "meld"] path = /usr/bin/meld [color "status"] diff --git a/.config/zsh/zshrc.d/30-alias.zsh b/.config/zsh/zshrc.d/30-alias.zsh index fb6ef73..cc0ad50 100644 --- a/.config/zsh/zshrc.d/30-alias.zsh +++ b/.config/zsh/zshrc.d/30-alias.zsh @@ -28,13 +28,6 @@ alias gpush='git push' alias gpull='git pull' alias gd='git diff' - # Commit, but put the last written commit message into the editor buffer. - # Useful for example when the commit-msg hook fails but only slight - # modifications are needed. - alias git-commit-last-msg='() { - local gitdir="$(git rev-parse --git-dir)" || return - git commit -eF <(grep -v "^#" "$gitdir/COMMIT_EDITMSG") - }' # Save keystrokes and my memory alias la='ls -A' diff --git a/.config/zsh/zshrc.d/40-functions.zsh b/.config/zsh/zshrc.d/40-functions.zsh index 370c7dd..e3fb0a7 100644 --- a/.config/zsh/zshrc.d/40-functions.zsh +++ b/.config/zsh/zshrc.d/40-functions.zsh @@ -323,3 +323,14 @@ cd() { builtin cd "$toplevel" fi } + +# Commit, but put the last written commit message into the editor buffer. +# For this it uses .git/COMMIT_EDITMSG but deletes all lines after and including +# the first comment of the file. +# Useful for example when the commit-msg hook fails but only slight +# modifications are needed. +git-commit-last-msg() { + local gitdir + gitdir="$(git rev-parse --git-dir)" || return + git commit -eF <(sed -n '/^#/q;p' "$gitdir/COMMIT_EDITMSG") +} diff --git a/meta/git/hooks/commit-msg b/meta/git/hooks/commit-msg index 57c4388..708cb7f 100755 --- a/meta/git/hooks/commit-msg +++ b/meta/git/hooks/commit-msg @@ -42,7 +42,13 @@ done BKP_IFS="$IFS" IFS=' ' +# The -v flag or commit.verbose setting add the changes of the commit to the +# bottom of the commit message. But when the changes contain lines longer than +# 72 characters this hook will fail when not breaking the loop before the end +# start of the patch. +verbose_start_line="$(git diff --staged -u | head -1)" for line in $body; do + [[ "$line" != "$verbose_start_line" ]] || break [[ ${#line} -le 72 ]] || die "Body lines too long. (<= 72)\n" done IFS="$BKP_IFS"