diff mbox series

[v2] git-gui: remove lines starting with the comment character

Message ID 20210218181937.83419-1-me@yadavpratyush.com (mailing list archive)
State New, archived
Headers show
Series [v2] git-gui: remove lines starting with the comment character | expand

Commit Message

Pratyush Yadav Feb. 18, 2021, 6:19 p.m. UTC
The comment character is specified by the config variable
'core.commentchar'. Any lines starting with this character is considered
a comment and should not be included in the final commit message.

Teach git-gui to filter out lines in the commit message that start with
the comment character using git-stripspace. If the config is not set,
'#' is taken as the default. Also add a message educating users about
the comment character.

Signed-off-by: Pratyush Yadav <me@yadavpratyush.com>
---

Changes in v2:
- Use git-stripspace to sanitize the commit message.
- Add a message above the commit message buffer mentioning what the
  comment character is.

 git-gui.sh     |  5 +++++
 lib/commit.tcl | 18 ++++++++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)

--
2.30.0

Comments

Pratyush Yadav Feb. 22, 2021, 2:51 p.m. UTC | #1
On 18/02/21 11:49PM, Pratyush Yadav wrote:
> The comment character is specified by the config variable
> 'core.commentchar'. Any lines starting with this character is considered
> a comment and should not be included in the final commit message.
> 
> Teach git-gui to filter out lines in the commit message that start with
> the comment character using git-stripspace. If the config is not set,
> '#' is taken as the default. Also add a message educating users about
> the comment character.
> 
> Signed-off-by: Pratyush Yadav <me@yadavpratyush.com>

Applied to git-gui/master.
Eric Sunshine Feb. 28, 2021, 11 p.m. UTC | #2
[Junio: please do not pull the latest git-gui changes yet; they break
on macOS, making git-gui unusable]

On Thu, Feb 18, 2021 at 1:20 PM Pratyush Yadav <me@yadavpratyush.com> wrote:
> The comment character is specified by the config variable
> 'core.commentchar'. Any lines starting with this character is considered
> a comment and should not be included in the final commit message.
>
> Teach git-gui to filter out lines in the commit message that start with
> the comment character using git-stripspace. If the config is not set,
> '#' is taken as the default. Also add a message educating users about
> the comment character.

Thanks for working on this. I've been looking forward to the
improvement; the old behavior of not stripping comment lines has been
a long-time annoyance.

Unfortunately, however, the changes break git-gui badly on macOS. See below...

> Signed-off-by: Pratyush Yadav <me@yadavpratyush.com>
> ---
> diff --git a/git-gui.sh b/git-gui.sh
> @@ -3436,6 +3437,10 @@ proc trace_commit_type {varname args} {
> +       set comment_char [get_config core.commentchar]
> +       set txt [string cat $txt \
> +                                [mc " (Lines starting with '$comment_char' will be ignored)"]]

The old Tcl 8.5.9 on macOS does not have a string `cat` method, so
`string cat` crashes:

    can't set "commit_type": unknown or ambiguous subcommand "cat":
        must be bytelength, compare, equal, first, index, is, last,
        length, map, match, range, repeat, replace, reverse, tolower,
        totitle, toupper, trim, trimleft, trimright, wordend, or
        wordstart
    unknown or ambiguous subcommand "cat": must be bytelength,
        compare, equal, first, index, is, last, length, map, match,
        range, repeat, replace, reverse, tolower, totitle, toupper,
        trim, trimleft, trimright, wordend, or wordstart while
        executing
    "string cat $txt [mc " (Lines starting with '$comment_char' will
        be ignored)"]"

> diff --git a/lib/commit.tcl b/lib/commit.tcl
> @@ -141,6 +141,20 @@ proc setup_commit_encoding {msg_wt {quiet 0}} {
> +proc strip_msg {msg} {
> +       set cmd [concat [list | ] [_git_cmd stripspace] --strip-comments]
> +       _trace_exec $cmd
> +       set fd [open $cmd r+]
> +       fconfigure $fd -translation binary -encoding utf-8
> +       puts -nonewline $fd $msg
> +       close $fd w
> +       set result [read $fd]
> +       close $fd
> +       return $result
> +}

The old Tcl on macOS does not support closing one end of a
bidirectional pipe, so `close $fd w` errors out:

    wrong # args: should be "close channelId"
    wrong # args: should be "close channelId"
        while executing
    "close $fd w"

I'll send a patch which resolves both problems.
diff mbox series

Patch

diff --git a/git-gui.sh b/git-gui.sh
index 201524c..236bc4e 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -875,6 +875,7 @@  set default_config(merge.summary) false
 set default_config(merge.verbosity) 2
 set default_config(user.name) {}
 set default_config(user.email) {}
+set default_config(core.commentchar) "#"

 set default_config(gui.encoding) [encoding system]
 set default_config(gui.matchtrackingbranch) false
@@ -3436,6 +3437,10 @@  proc trace_commit_type {varname args} {
 	merge         {set txt [mc "Merge Commit Message:"]}
 	*             {set txt [mc "Commit Message:"]}
 	}
+
+	set comment_char [get_config core.commentchar]
+	set txt [string cat $txt \
+				 [mc " (Lines starting with '$comment_char' will be ignored)"]]
 	$ui_coml conf -text $txt
 }
 trace add variable commit_type write trace_commit_type
diff --git a/lib/commit.tcl b/lib/commit.tcl
index 11379f8..23d67d4 100644
--- a/lib/commit.tcl
+++ b/lib/commit.tcl
@@ -141,6 +141,20 @@  proc setup_commit_encoding {msg_wt {quiet 0}} {
 	}
 }

+proc strip_msg {msg} {
+	set cmd [concat [list | ] [_git_cmd stripspace] --strip-comments]
+	_trace_exec $cmd
+	set fd [open $cmd r+]
+	fconfigure $fd -translation binary -encoding utf-8
+
+	puts -nonewline $fd $msg
+	close $fd w
+	set result [read $fd]
+	close $fd
+
+	return $result
+}
+
 proc commit_tree {} {
 	global HEAD commit_type file_states ui_comm repo_config
 	global pch_error
@@ -207,8 +221,8 @@  You must stage at least 1 file before you can commit.

 	# -- A message is required.
 	#
-	set msg [string trim [$ui_comm get 1.0 end]]
-	regsub -all -line {[ \t\r]+$} $msg {} msg
+	set msg [strip_msg [$ui_comm get 1.0 end]]
+
 	if {$msg eq {}} {
 		error_popup [mc "Please supply a commit message.