From 5747b6e04cbb64f02b2ad63d86aaa9ce53469117 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Fri, 5 Jan 2024 16:10:55 +0100 Subject: [PATCH] git:commit-last-msg: Make more robust Use core.commentchar to identify commented lines and use the cut line instead of just deleting from the first comment on, as this would break for example in git generated messages (e.g. squashes). --- .config/zsh/autoload/git/git-commit-last-msg | 24 ++++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/.config/zsh/autoload/git/git-commit-last-msg b/.config/zsh/autoload/git/git-commit-last-msg index 392fa65..8eaf118 100755 --- a/.config/zsh/autoload/git/git-commit-last-msg +++ b/.config/zsh/autoload/git/git-commit-last-msg @@ -1,12 +1,22 @@ #!/usr/bin/env zsh -# 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. +# Commit, but put the last written commit message into the editor buffer. For +# this it uses .git/COMMIT_EDITMSG but deletes all commented lines and +# everything from the cut line on (i.e. `-- >8 --`). All arguments are passed to +# git-commit. +# # Useful for example when the commit-msg hook fails but only slight -# modifications are needed. -# Additional arguments (for example `-n` to bypass the hooks) can be passed. +# modifications are needed. For example, just run: +# +# git-commit-last-msg --no-verify --no-edit + +local gitdir cut_line cchar +cut_line='------------------------ >8 ------------------------' -local gitdir gitdir="$(git rev-parse --git-dir)" || return -git commit -eF <(sed -n '/^#/q;p' "$gitdir/COMMIT_EDITMSG") "$@" + +cchar="$(git config --get --default='#' core.commentchar)" +# only ^ needs escaping because of the character class used +cchar="${cchar//^/\\^}" + +git commit -eF <(sed -n "/$cut_line/q; /^[$cchar]/!p" "$gitdir/COMMIT_EDITMSG") "$@"