1

I'm not a perl user so I'm unsure of how to write a git commit-msg hook script to capitalise the first letter of the message for each commit so that

initial commit

becomes

Initial commit
h-rai
  • 3,636
  • 6
  • 52
  • 76

1 Answers1

1

First, a commit-msg hook is generaly used only to validate a commit message, not change it.
You can still try and change the content of the temporary file passed as parameter to that hook.

Second, that hook can be a simple bash sed command:

#!/bin/bash
sed -ie "1 s/\b\(.\)/\u\1/" $1

(see "Uppercasing First Letter of Words Using SED")

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I went to the git-scm.com link and it says that `This hook generally isn’t useful for normal commits; rather, it’s good for commits where the default message is auto-generated, such as templated commit messages, merge commits, squashed commits, and amended commits.` How can I use this feature for normal user entered commit message? – h-rai Jun 12 '18 at 05:02
  • @nick-s it also says, just after the passage you mention "You may use it in conjunction with a commit template to programmatically insert information." That is *precisely* what my answer does. – VonC Jun 12 '18 at 05:03
  • @nick-s I see what you mean: the default message is not the one written by the user. But I don't think the commit-msg hook allows to change the content of the message, only to validate it. You can try the same sed command I mention with a commit-msg hook. I have edited the answer accordingly. – VonC Jun 12 '18 at 05:06
  • So if I were to add a custom message using `-m`, it can't be changed/manipulated programmatically by git hook? – h-rai Jun 12 '18 at 05:16
  • @nick-s if the commit-msg does not change the content after your sed command (or an even simpler command just for testing: echo test>$1), then no. Did you test a simple content modification done with that hook? – VonC Jun 12 '18 at 05:20
  • It wasn't working on the repo I wanted to set it up in. But it works on the other repo. So accepting the answer. – h-rai Jun 12 '18 at 08:43