git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Feb 2, 2021, 22:26 UTC
Message-ID
<CAPig+cT+n3=o9AVyK6FUQ=88aqdnoCfSAxmAO0Xe9S84a9RBqw@mail.gmail.com>
In-Reply-To
<20210202200301.44282-1-me@yadavpratyush.com>
On Tue, Feb 2, 2021 at 3:07 PM Pratyush Yadav <me@yadavpratyush.com> wrote:
Show 7 quoted lines
> 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...
Show 27 quoted lines
> 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.

Previous: Pratyush YadavNext: Pratyush Yadav
Message 2 of 8 in “git-gui: remove lines starting with the comment character”
  1. git-gui: remove lines starting with the comment characterPratyush Yadav, Feb 2, 2021
  2. Eric SunshineFeb 2, 2021
  3. Pratyush YadavFeb 3, 2021
  4. Johannes SixtFeb 3, 2021
  5. Eric SunshineFeb 3, 2021
  6. Eric SunshineFeb 3, 2021
  7. Johannes SixtFeb 3, 2021
  8. Pratyush YadavFeb 3, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.