git core.commentChar=auto 有什么作用?

What does git core.commentChar=auto do?

我正在用 git-commit 测试 git commentChar=auto 的库,出于某些目的必须模拟 git-commit

如果我运行 git commit -m "#message",然后进行第二次提交,打开编辑器编辑消息, 评论仍然用 # 标记。这是为什么?

根据 docs:

core.commentChar

Commands such as commit and tag that let you edit messages consider a line that begins with this character commented, and removes them after the editor returns (default #).

If set to "auto", git-commit would select a character that is not the beginning character of any line in existing commit messages.

但是当我运行一个测试脚本时:

git config --global core.commentChar auto
touch foo
git add foo
git commit -m '#message'
touch bar
git add bar
git commit # opens editor, And I see:

# Please enter the commit message // Wrong! This should not use the # char.

some tests in the git repo 开始,如果 "commenting" 关于的消息已经包含注释字符,则它仅应用此 "auto" 方法。文档中的 existing commit messages 指的是(在这种情况下)修改后的消息(或者在 rebase-squash 的情况下,指的是几个提交消息)。

您可以在 commit.c: here

中查看处理方式

它查看即将显示的当前缓冲区,并选择 "#;@!$%^&|:" 中的第一个作为潜在的注释字符,否则会出错。

当它说 "existing commit messages" 时,它并不意味着 previous 提交的消息;检查所有以前的消息是不切实际的。这意味着在调用编辑器之前 current 消息中尚未使用的字符。这似乎是多余的——如果您还没有编写消息,为什么它需要检查任何内容?但是有很多方法可以获得现有消息,包括:

  • 你正在做 git commit --amend
  • 您正在执行 git merge 并且消息已预先填充。
  • 您设置了 commit.template 配置变量或正在使用 git commit--template 选项。

无论如何,core.commentChar = auto 的目的是扫描预先存在的消息中的行首字符,然后选择一个不会冲突的字符。