Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Git's commit message cleanup mode from commit-msg hook

Tags:

git

githooks

git help commit says the following:

--cleanup=<mode>
   This option determines how the supplied commit message should be
   cleaned up before committing. The <mode> can be strip, whitespace,
   verbatim, or default.

   strip
       Strip leading and trailing empty lines, trailing whitespace,
       and #commentary and collapse consecutive empty lines.

   whitespace
       Same as strip except #commentary is not removed.

   verbatim
       Do not change the message at all.

   default
       Same as strip if the message is to be edited. Otherwise
       whitespace.

I'd like to determine which cleanup mode is going to be applied from the commit-msg hook (correctly using the commit.cleanup config value when necessary). I run some validations on my commit messages and I want to make sure I'm seeing exactly what Git is planning on using.

Alternatively, I'll accept a way to grab the commit message text post-cleanup (maybe I can trick Git into cleaning it up for me?). This would be great for my use case, since then I wouldn't have to worry about re-implementing any cleanup modes.

like image 798
Matt Kantor Avatar asked Apr 29 '13 06:04

Matt Kantor


People also ask

How do you edit old commit messages?

Commit has not been pushed online On the command line, navigate to the repository that contains the commit you want to amend. Type git commit --amend and press Enter. In your text editor, edit the commit message, and save the commit.

Can I edit commit message after push?

Changing the latest Git commit message If the message to be changed is for the latest commit to the repository, then the following commands are to be executed: git commit --amend -m "New message" git push --force repository-name branch-name.


1 Answers

Unfortunately, in current(ish) git source, the cleanup mode is not passed through to the hook in any way. The argument to --cleanup is stored only in the (static, local to builtin/commit.c) variable cleanup_mode and not exported (e.g., as an argument or environment variable) to the various hooks.

(It should be easy to add an environment variable containing the setting. If you want to experiment with this yourself, see builtin/commit.c function parse_and_validate_options; put in a call to setenv() with appropriate arguments.)

like image 112
torek Avatar answered Sep 19 '22 06:09

torek