Subject: Re: [PATCH] git-gui: do not end the commit message with an empty line

From: Junio C Hamano <[email-masked]>
Date: Wed, 14 May 2025 14:14:30 -0700

Johannes Sixt <[email-masked]> writes:

> The commit message is processed to remove unnecessary empty lines.
> In particular, it is ensured that the text ends with at most one LF
> character. This one is always present, because the Tk text widget
> ensures that is present.
>
> However, we forgot that the processed text is written to the commit
> message file using 'puts', which also appends a LF character, so that
> the final commit message ends with two LF. Trim all trailing LF
> characters, and while we are here, use `string trim`, which lets us
> remove the leading LF in the same command.

The above reasoning look sensible.

As git-gui uses plumbing commit-tree to create the
commit object, it has to be more careful not to have extra blank
lines, as it cannot assume stripspace internal to "git commit"?

>
> Reported-by: Gareth Fenn <[email-masked]>
> Signed-off-by: Johannes Sixt <[email-masked]>
> ---
>  lib/commit.tcl | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)

> diff --git a/lib/commit.tcl b/lib/commit.tcl
> index a570f9cdc6a4..f3c714e600ac 100644
> --- a/lib/commit.tcl
> +++ b/lib/commit.tcl
> @@ -214,12 +214,10 @@ You must stage at least 1 file before you can commit.
>  	global comment_string
>  	set cmt_rx [strcat {(^|\n)} [regsub -all {\W} $comment_string {\\&}] {[^\n]*}]
>  	regsub -all $cmt_rx $msg {\1} msg
> -	# Strip leading empty lines
> -	regsub {^\n*} $msg {} msg
> +	# Strip leading and trailing empty lines
> +	set msg [string trim $msg \n]
>  	# Compress consecutive empty lines
>  	regsub -all {\n{3,}} $msg "\n\n" msg
> -	# Strip trailing empty line
> -	regsub {\n\n$} $msg "\n" msg
>  	if {$msg eq {}} {
>  		error_popup [mc "Please supply a commit message.