从commit-msg钩子使用Git的提交消息清理模式

Mat*_*tor 11 git githooks

git help commit 说如下:

--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.
Run Code Online (Sandbox Code Playgroud)

我想确定将从commit-msg 挂钩应用哪种清理模式(必要时正确使用commit.cleanup 配置值).我在提交消息上运行了一些验证,我想确保我看到Git正在计划使用的确切内容.

或者,我会接受一种方法来获取清理后的提交消息文本(也许我可以欺骗Git为我清理它?).这对我的用例来说很棒,因为那时我不必担心重新实现任何清理模式.

tor*_*rek 10

不幸的是,在当前(ish)git源中,清理模式不会以任何方式传递给钩子.参数to --cleanup仅存储在(static,local to builtin/commit.c)变量中,cleanup_mode而不是导出(例如,作为参数或环境变量)到各种钩子.

(添加一个包含该设置的环境变量应该很容易.如果你想自己试验一下,请参阅builtin/commit.cfunction parse_and_validate_options; setenv()使用适当的参数调用.)