The company I work for is imposing some requirements on our git messages. Our branch names follow the format <branch_type>/<jira_ticket><arbitrary>. We are required to prepend our commit messages with <jira_ticket>.
I would like to add a custom option to git commit, -p, that makes a commit with the current branch's jira_ticket prepended to the commit message. Ideally I would like the option to work such that:
git commit -p "my commit message" [OTHER_ARGS]
Gets executed as:
git commit -m "<jira_ticket> my commit message" [OTHER_ARGS]
Furthermore, if I simply execute git commit -p [OTHER_ARGS] where -m is not in OTHER_ARGS, I'd like my configured text editor to pop up with "<jira_ticket> " prefilled and ready for me to append to (akin to the functionality of executing git commit with no -m).
Does git have built in functionality for adding custom options like this? If not, what approach should I take to this problem?
The best way to handle this is with a commit-msg hook. It allows you to customize the commit message in any way you like, and I have successfully used such a hook in the past to insert a ticket number. The documentation for how it's invoked can be found with
git help githooks.Note that the commit-msg hook operates on the file in place, and thus if you write it as a shell script, you may want to use the
-ioption for sed, Perl, or Ruby, or use an scriptable editor such as ed or ex. You may also wish to avoid modifying the value if it isn't already empty so that you don't affectgit commit --amend.As for a custom option, Git doesn't provide functionality for that. You can use an alias or create a custom command (e.g., if
git-foois in yourPATH, you can rungit foo), but there's little need to do so when you have a hook that is specifically designed for this purpose.