如何为 git 创建自定义清理模式?
我希望进行提交消息清理,但不一定会删除消息本身中以 #
开头的行。默认的 --cleanup=strip
会删除以 #
字符开头的所有行。
不幸的是,Trac 引擎的 wiki 格式化程序在代码块的开头使用哈希值来表示语法类型。这在我的提交消息中使用引擎的语法时会产生困难。
示例:
Created demo of perl added to helloworld.pl
{{{
#!/usr/bin/perl
use strict;
# say hi to the user.
print "hello world\n";
}}}
# 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:
# (use "git reset HEAD^1 <file>..." to unstage)
#
# added: helloworld.pl
# and so on and so forth...
我希望在最终记录的提交消息中出现以下结果:
commit 1234567890123456789012345678901234567890
Author: Danny <...>
Date: Wed Apr 7 13:34:29 2010 -0400
Created demo of perl added to helloworld.pl
{{{
#!/usr/bin/perl
use strict;
# say hi to the user.
print "hello world\n";
}}}
我想使用自定义过滤器来删除从提交消息底部向上的以哈希开头的所有行。留下我单独添加的消息中的行。我在哪里或如何在 git 中指定它?
请注意,创建 sed 或 perl 脚本来执行该操作不是问题,只需知道将其挂接到 git 的位置即可。
我对我的问题感到困惑表示歉意,我没有意识到它有多么含糊。
I'd like to have a commit message cleanup that will not necessarily remove lines beginning with a #
in the message itself. The default --cleanup=strip
removes all lines starting with a #
character.
The reason being is unfortunately, the Trac engine's wiki formatter uses hashes in the beginning of a code block to denote the syntax type. This creates difficulty when using the engine's syntax in my commit messages.
Example:
Created demo of perl added to helloworld.pl
{{{
#!/usr/bin/perl
use strict;
# say hi to the user.
print "hello world\n";
}}}
# 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:
# (use "git reset HEAD^1 <file>..." to unstage)
#
# added: helloworld.pl
# and so on and so forth...
I would like the following result in the final logged commit message:
commit 1234567890123456789012345678901234567890
Author: Danny <...>
Date: Wed Apr 7 13:34:29 2010 -0400
Created demo of perl added to helloworld.pl
{{{
#!/usr/bin/perl
use strict;
# say hi to the user.
print "hello world\n";
}}}
I'd like to use a custom filter that removes all lines beginning with a hash from the bottom of the commit message upwards. Leaving the lines in the message I have added alone. Where or how can I specify this in git?
Note, creating a sed or perl script to perform the operation is not a problem, just knowing where to hook it into git is the question.
My apologies on the confusion of my question, I hadn't realized how vague it was.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要使用两件事来完成此操作:
指定清理模式。
git commit
采用--cleanup=
选项;您可以使用--cleanup=whitespace
来防止 git 删除任何注释。您可能想要的另一个选择是逐字记录,这(令人惊讶)不会触及消息。有关其他选项,请参阅 手册页 ,如果您有兴趣。使用
commit-msg
钩子就地编辑消息。如果您以前没有使用过钩子,则手册页名为 githooks,它们是放置在.git/hooks
中的脚本,会在适当的时间执行。 commit-msg 钩子使用单个参数执行,该文件包含建议的提交消息。它可以就地编辑消息,如果返回失败(非零),则提交将被中止。因此,将您的自定义剥离脚本编写为.git/hooks/commit-msg
,确保它是可执行的,然后您就可以设置了。请注意,hooks 目录不会被跟踪;如果您想与其他人共享此内容,常见的方法是将其放置在存储库中的某个位置,然后将.git/hooks/commit-msg
符号链接到它。I think you'll want to use two things to accomplish this:
Specify the cleanup mode.
git commit
takes a--cleanup=<mode>
option; you can use--cleanup=whitespace
to prevent git from stripping any comments. The other choice you might want is verbatim, which (surprise) doesn't touch the message. See the man page for the other choices, if you're interested.Use the
commit-msg
hook to edit the message in place. In case you haven't used hooks before, the man page is called githooks, and they're scripts placed in.git/hooks
which are executed at the appropriate times. Thecommit-msg
hook is executed with a single argument, the file containing the proposed commit message. It can edit the message in place, and if it returns failure (non-zero) the commit will be aborted. So, write your custom stripping script as.git/hooks/commit-msg
, make sure it's executable, and you'll be set. Note that the hooks directory isn't tracked; if you want to share this with others, a common method is to place it somewhere inside your repository, then symlink.git/hooks/commit-msg
to it.