Skip to content

fix(git): respect core.commentChar when stripping commit message comments#308432

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/git-respect-core-commentchar
Open

fix(git): respect core.commentChar when stripping commit message comments#308432
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/git-respect-core-commentchar

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

Problem

The Git extension strips comment lines from commit message templates and in-progress merge/squash messages before displaying them as the initial commit message. This is done by stripCommitMessageComments(), which always assumes the comment character is #.

However, Git supports configuring an alternative comment character via core.commentChar (e.g. git config core.commentChar ";"). When a user has set this, lines beginning with ; (or whatever they chose) are comments in Git's eyes, but VS Code's extension was not stripping them — they would leak into the commit message input box.

This also means that if a user has core.commentChar set to something other than #, and their commit template or message content contains #, lines with # would be stripped even though they are not comments.

Fix

  • Add getCommentChar() which reads core.commentChar from git config. Handles the auto value (falls back to #) and missing/empty config (defaults to #).
  • Update stripCommitMessageComments() to accept an optional commentChar parameter (defaults to # for backward compatibility) and build the regex dynamically with proper escaping.
  • Update all three call sites (getSquashMessage, getMergeMessage, getCommitTemplate) to fetch the comment char in parallel with the file read and pass it through.

Testing

  1. Set git config core.commentChar ";" in a local repo
  2. Create a commit template with ;-prefixed comment lines
  3. Open VS Code and start a commit — comment lines should be stripped
  4. Verify that # lines (not comments with this config) are preserved

…ents

The `stripCommitMessageComments` helper always stripped lines starting
with `#`, ignoring any `core.commentChar` setting the user may have
configured in their Git config.

Git allows changing the comment character via `core.commentChar` (e.g.
`git config core.commentChar ";"`) so that lines beginning with the
configured character are treated as comments in commit messages and
templates. VS Code's Git extension was not aware of this setting and
always assumed `#`, causing commit template and SQUASH/MERGE message
prefilling to incorrectly include comment lines when a non-default
comment character was configured.

Fix: read `core.commentChar` from Git config and pass it through to the
stripping regex. When the setting is `auto` (let Git pick automatically)
or is not configured, fall back to the default `#`. The comment
character is escaped before use in the regex to handle any special
characters a user might configure.
@vs-code-engineering
Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

@lszomoru

Matched files:

  • extensions/git/src/git.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants