开发者

How can I create a custom cleanup mode for 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 ad开发者_运维问答ded 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.


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. The commit-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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜