0

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?

justaguy
  • 29
  • 3
  • You can write a shell wrapper for `git` that intercepts the calls, modifies the command line and then passes them through. I believe I've already written a Q&A entry deciding how to do this, albeit in a slightly different context (the OP there was asking how to add custom subcommands to an arbitrary command). – Charles Duffy Jun 24 '22 at 21:37
  • Ahh -- the place where I demonstrated a technique that could be adopted here is [Can I alias a subcommand? Shortening the output of `docker ps`](https://stackoverflow.com/questions/34748747/can-i-alias-a-subcommand-shortening-the-output-of-docker-ps) – Charles Duffy Jun 24 '22 at 21:39
  • One alternative is to force users to include your JIRA key in the commit message. Otherwise fail the commit. You can do this with a simple commit-msg Git Hook that scans (grep) the commit message for the JIRA key and fails the commit if it is missing. – Edwin Torres Jun 24 '22 at 22:04
  • 1
    Instead of asking for how to edit git commands/options (seems like an XY problem), you can instead see solutions using git commit hooks: [Append ticket number using git commit hooks?](https://stackoverflow.com/q/5823772/2745495), [git hooks to enforce JIRA ticket number in commit message](https://stackoverflow.com/q/42327673/2745495) – Gino Mempin Jun 25 '22 at 02:47

1 Answers1

2

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 -i option 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 affect git 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-foo is in your PATH, you can run git foo), but there's little need to do so when you have a hook that is specifically designed for this purpose.

bk2204
  • 64,793
  • 6
  • 84
  • 100