git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Eric Sunshine <sunshine@sunshineco.com>
To: Pratyush Yadav <me@yadavpratyush.com>
Cc: Git List <git@vger.kernel.org>
Subject: Re: [PATCH] git-gui: remove lines starting with the comment character
Date: Tue, 2 Feb 2021 17:26:48 -0500	[thread overview]
Message-ID: <CAPig+cT+n3=o9AVyK6FUQ=88aqdnoCfSAxmAO0Xe9S84a9RBqw@mail.gmail.com> (raw)
In-Reply-To: <20210202200301.44282-1-me@yadavpratyush.com>

On Tue, Feb 2, 2021 at 3:07 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. If the config is not set, '#' is taken as the
> default.

Thanks. This shortcoming has bugged me for a long time. A few comments below...

> Signed-off-by: Pratyush Yadav <me@yadavpratyush.com>
> ---
> diff --git a/lib/commit.tcl b/lib/commit.tcl
> @@ -209,6 +209,28 @@ You must stage at least 1 file before you can commit.
>         set msg [string trim [$ui_comm get 1.0 end]]
>         regsub -all -line {[ \t\r]+$} $msg {} msg
> +
> +       # Remove lines starting with the comment character.
> +       set comment_char [get_config core.commentchar]
> +       if {[string length $comment_char] > 1} {
> +               error_popup [mc "core.commitchar should only be one character."]
> +               unlock_index
> +               return
> +       }
> +
> +       if {$comment_char eq {}} {
> +               set comment_char "#"
> +       }
> +
> +       # If the comment character is not alphabetical, then we need to escape it
> +       # with a backslash to make sure it is not interpreted as a special character
> +       # in the regex.
> +       if {![string is alpha $comment_char]} {
> +               set comment_char "\\$comment_char"
> +       }
> +
> +       regsub -all -line "$comment_char.*(\\n|\\Z)" $msg {} msg

This regular expression is too loose. It will incorrectly change:

    line one
    line # two
    # line three
    line four

into:

    line one
    line
    line four

You could fix it by anchoring the start of the match while being
careful not to lose the newline at the start of line. Perhaps like
this:

    regsub -all -line "(^|\\A)$comment_char.*(\\n|\\Z)" $msg {} msg

However, an even better approach than doing this manipulation manually
might be to pass the commit message through `git stripspace
--strip-comments` which will do the exact normalization that Git
itself does. That way, you don't have to worry about weird corner
cases. Also, using git-stripspace may allow you to get rid of the
existing `trim` and `regsub` which precede the new code you added.

  reply	other threads:[~2021-02-02 22:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-02 20:03 [PATCH] git-gui: remove lines starting with the comment character Pratyush Yadav
2021-02-02 22:26 ` Eric Sunshine [this message]
2021-02-03 11:54   ` Pratyush Yadav
2021-02-03 17:33 ` Johannes Sixt
2021-02-03 17:48   ` Eric Sunshine
2021-02-03 17:58     ` Eric Sunshine
2021-02-03 21:42       ` Johannes Sixt
2021-02-03 20:39     ` Pratyush Yadav

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAPig+cT+n3=o9AVyK6FUQ=88aqdnoCfSAxmAO0Xe9S84a9RBqw@mail.gmail.com' \
    --to=sunshine@sunshineco.com \
    --cc=git@vger.kernel.org \
    --cc=me@yadavpratyush.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).