git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pratyush Yadav <me@yadavpratyush.com>
To: Eric Sunshine <sunshine@sunshineco.com>
Cc: Git List <git@vger.kernel.org>
Subject: Re: [PATCH] git-gui: remove lines starting with the comment character
Date: Wed, 3 Feb 2021 17:24:53 +0530	[thread overview]
Message-ID: <20210203115453.5ahqba63kvxtdfyb@yadavpratyush.com> (raw)
In-Reply-To: <CAPig+cT+n3=o9AVyK6FUQ=88aqdnoCfSAxmAO0Xe9S84a9RBqw@mail.gmail.com>

On 02/02/21 05:26PM, Eric Sunshine wrote:
> 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

Good catch!

> 
> 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.

This is exactly what I was looking for when I was writing the patch but 
didn't manage to find it. Will re-roll. Thanks.

-- 
Regards,
Pratyush Yadav

  reply	other threads:[~2021-02-03 11:55 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
2021-02-03 11:54   ` Pratyush Yadav [this message]
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

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

  git send-email \
    --in-reply-to=20210203115453.5ahqba63kvxtdfyb@yadavpratyush.com \
    --to=me@yadavpratyush.com \
    --cc=git@vger.kernel.org \
    --cc=sunshine@sunshineco.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.
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 NNTP newsgroup(s).