Skip to content

Commit bfacf83

Browse files
committed
git-gui: strip the commit message after running commit-msg hook
When commit-msg writes the file using CRLF, the lines in the final message include trailing spaces. Postpone stripping until after hooks execute. This aligns with Git's behavior, which passes the original message to commit-msg, then strips comments and whitespace. Signed-off-by: Orgad Shaneh <[email protected]>
1 parent 3f07230 commit bfacf83

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

lib/commit.tcl

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -208,28 +208,6 @@ You must stage at least 1 file before you can commit.
208208
# -- A message is required.
209209
#
210210
set msg [$ui_comm get 1.0 end]
211-
# Strip trailing whitespace
212-
regsub -all -line {[ \t\r]+$} $msg {} msg
213-
# Strip comment lines
214-
global comment_string
215-
set cmt_rx [strcat {(^|\n)} [regsub -all {\W} $comment_string {\\&}] {[^\n]*}]
216-
regsub -all $cmt_rx $msg {\1} msg
217-
# Strip leading and trailing empty lines (puts adds one \n)
218-
set msg [string trim $msg \n]
219-
# Compress consecutive empty lines
220-
regsub -all {\n{3,}} $msg "\n\n" msg
221-
if {$msg eq {}} {
222-
error_popup [mc "Please supply a commit message.
223-
224-
A good commit message has the following format:
225-
226-
- First line: Describe in one sentence what you did.
227-
- Second line: Blank
228-
- Remaining lines: Describe why this change is good.
229-
"]
230-
unlock_index
231-
return
232-
}
233211

234212
# -- Build the message file.
235213
#
@@ -332,7 +310,52 @@ proc commit_commitmsg_wait {fd_ph curHEAD msg_p} {
332310
fconfigure $fd_ph -blocking 0
333311
}
334312

313+
proc wash_commit_message {msg} {
314+
# Strip trailing whitespace
315+
regsub -all -line {[ \t\r]+$} $msg {} msg
316+
# Strip comment lines
317+
global comment_string
318+
set cmt_rx [strcat {(^|\n)} [regsub -all {\W} $comment_string {\\&}] {[^\n]*}]
319+
regsub -all $cmt_rx $msg {\1} msg
320+
# Strip leading and trailing empty lines (puts adds one \n)
321+
set msg [string trim $msg \n]
322+
# Compress consecutive empty lines
323+
regsub -all {\n{3,}} $msg \n\n msg
324+
325+
return $msg
326+
}
327+
335328
proc commit_writetree {curHEAD msg_p} {
329+
# -- Process the commit message after hooks have run.
330+
#
331+
set msg_fd [safe_open_file $msg_p r]
332+
setup_commit_encoding $msg_fd 1
333+
set msg [read $msg_fd]
334+
close $msg_fd
335+
336+
# Process the message (strip whitespace, comments, etc.)
337+
set msg [wash_commit_message $msg]
338+
339+
if {$msg eq {}} {
340+
error_popup [mc "Please supply a commit message.
341+
342+
A good commit message has the following format:
343+
344+
- First line: Describe in one sentence what you did.
345+
- Second line: Blank
346+
- Remaining lines: Describe why this change is good.
347+
"]
348+
unlock_index
349+
return
350+
}
351+
352+
# Write the processed message back to the file
353+
set msg_wt [safe_open_file $msg_p w]
354+
fconfigure $msg_wt -translation lf
355+
setup_commit_encoding $msg_wt
356+
puts $msg_wt $msg
357+
close $msg_wt
358+
336359
ui_status [mc "Committing changes..."]
337360
set fd_wt [git_read [list write-tree]]
338361
fileevent $fd_wt readable \

0 commit comments

Comments
 (0)