英文:
Inject additional hints into `COMMIT_EDITMSG` during `git-commit`
问题
以下是翻译的内容:
问题描述
当我运行 git commit
而没有使用 -m
选项时,Git 会启动一个提交消息编辑器,其中包含一些提示信息,类似于以下内容:
# 请输入您的更改的提交消息。以 '#' 开头的行将被忽略,空消息将中止提交。
#
# 在分支 master 上
# 要提交的更改:
# 新文件: bar
#
我经常在提交之前运行 git log --oneline -n 10
,只是为了查看提交消息的样式,以便我可以以类似的方式编写。因此,如果这些信息作为额外的提示注入到提交消息编辑器中,那将是很好的。理想情况下,类似于以下内容:
# 请输入您的更改的提交消息。以 '#' 开头的行将被忽略,空消息将中止提交。
#
# 在分支 master 上
# 要提交的更改:
# 新文件: bar
#
# 最近的 10 个提交:
# bbbbbbb(HEAD -> master)另一个提交
# aaaaaaa 初始提交
#
这样做的正确方式是什么?
我尝试过的方法
自然而然,我想到了 Git 钩子,特别是 prepare-commit-msg
钩子。这是我编写的内容:
#!/usr/bin/env bash
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
# 显示最近的 10 个提交(最大值)
echo '# 最近的 10 个提交:' >> "$COMMIT_MSG_FILE"
git log --oneline -n 10 --decorate=short | sed 's/^/# /' >> "$COMMIT_MSG_FILE"
echo '#' >> "$COMMIT_MSG_FILE"
这基本上可以正常工作,除了一个问题 - 它也有一个意外的副作用,会破坏像 git cherry-pick
和 git rebase
这样的命令。例如,如果我有一个看起来像这样的提交:
ccccccc 添加 baz
并尝试 git cherry-pick ccccccc
,它会变成这样:
ddddddd 添加 baz # 最近的 10 个提交: # bbbbbbb(HEAD -> master)另一个提交 # aaaaaaa 初始提交 #
如您所见,我插入的提示未正确解释,而没有交互式编辑器,它们会破坏提交消息。显然,这不是我想要的。我尝试调查是否可以使用脚本参数仅在交互模式下注入提示,但不清楚如何做到这一点,或者是否可能做到这一点。
英文:
The problem
When I run git commit
without -m
, git launches a commit message editor with some hints, something like this:
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Changes to be committed:
# new file: bar
#
I often find myself running git log --oneline -n 10
before committing, just to see what the commit messages look like so I can write in a similar style. So it would be great if this information is injected into the commit message editor as additional hints. Something like this would be ideal:
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Changes to be committed:
# new file: bar
#
# Last 10 commits:
# bbbbbbb (HEAD -> master) Another commit
# aaaaaaa Initial commit
#
What is the proper way to do this?
What I've tried
Naturally I thought of git hooks, in particular prepare-commit-msg
. This is what I wrote:
#!/usr/bin/env bash
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
# Show last 10 (max) commits
echo '# Last 10 commits:' >> "$COMMIT_MSG_FILE"
git log --oneline -n 10 --decorate=short | sed 's/^/# /' >> "$COMMIT_MSG_FILE"
echo '#' >> "$COMMIT_MSG_FILE"
This mostly works fine except for one thing - it doesn't work fine. While this info is now injected as hints correctly when running git commit
, it also has the unintended side effect of breaking commands like git cherry-pick
and git rebase
. For example, if I have a commit that looks like this:
ccccccc Add baz
And I try to git cherry-pick ccccccc
, it turns into this instead:
ddddddd Add baz # Last 10 commits: # bbbbbbb (HEAD -> master) Another commit # aaaaaaa Initial commit #
As you can see, the hints I inserted are not interpreted correctly without an interactive editor and clobber the commit message instead. Obviously this is not what I want. I did try to investigate whether I can use the script arguments to only inject hints in interactive mode, but it's not obvious how to do that or whether it's possible.
答案1
得分: 3
The behavior is controlled by --cleanup=$mode or the configuration variable commit.cleanup.
git commit --cleanup=strip
# Or
git -c commit.cleanup=strip commit
With strip
, the commentary is removed no matter the commit message is edited or not.
With default
, the commentary is removed if the commit message is edited, and not removed if it's not edited.
strip
去除前导和尾随空行,尾随空格,注释以及合并连续的空行。
whitespace
与strip相同,但不会删除#注释。
default
如果消息要被编辑,则与strip相同。否则保留空格。
So, I think you can use git config --global commit.cleanup strip
.
英文:
The behavior is controlled by --cleanup=$mode or the configuration variable commit.cleanup.
git commit --cleanup=strip
# Or
git -c commit.cleanup=strip commit
With strip
, the commentary is removed no matter the commit message is edited or not.
With default
, the commentary is removed if the commit message is edited, and not removed if it's not edited.
> strip
>
>Strip leading and trailing empty lines, trailing whitespace, commentary and collapse consecutive empty lines.
>
> whitespace
>
> Same as strip except #commentary is not removed.
>
> default
>
>Same as strip if the message is to be edited. Otherwise whitespace.
So, I think you can use git config --global commit.cleanup strip
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论