views:

55

answers:

1

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.

+2  A: 

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.

Jefromi
This is probably unusual, but all the git repos I work with would be best to have this hook. Thusly, can the hooks be specified globally in .gitconfig? I didn't notice anything in the githooks manpage you linked, but given the oddness of having a hook operate on any arbitrary repo I can see it not being implemented, it is a somewhat specific situation I believe.
Danny
The hooks aren't part of the config. If you'd like to place them in new repositories you create, you can use the `--template` option to `git init` and `git clone`. That lets you specify a template directory instead of the system-wide default one; the default hooks are all copied from the template directory's hooks directory. (See the git init man page http://www.kernel.org/pub/software/scm/git/docs/git-init.html ) You could also edit the system-wide template (generally in `/usr/share/git-core/templates`) if you really wanted.
Jefromi
To make it easier to remember to use the template, you could place aliases in your .gitconfig - something like `myinit = init --template=<template directory>`.
Jefromi
Thats a good idea, I will probably go that route, thanks!
Danny
Aside from using an alias, is there a way to set a default cleanup mode? I've looked around in the documentation to no avail. I'm either blind or it is simply not available
Danny