1

My team uses a common naming convention for branch names, which include the Jira task number in the branch name.

feature/ACD-1664_update-api-call

feature/VZ-1943_new-provider-template

hotfix/RV-977_fix-loading-issue


I want to create a git alias that will automatically stub out a commit message which includes the Jira task number. Ideally some bash script that will parse the branch name and echo out the commit -m command with the first part of the message pre-created.

  1. I need to regex out the commmit message.

I need to pull ACD-1664 from feature/ACD-1664_update-api-call

  1. Echo this string out into the terminal in a stubbed-out commit command like:

git commit -m "ACD-1664 | <cursor>"

Joshua Soileau
  • 2,933
  • 9
  • 40
  • 51
  • Maybe with a simple [alias](https://stackoverflow.com/questions/56977704/git-alias-git-commit-with-branch-name) would do? – Romain Valeri Jul 10 '19 at 20:38

1 Answers1

1

Although this is not the solution you requested, I'd like to hint at another way to cover this, with a commit hook :

You can put in .git/hooks a commit-msg file with these contents :

#!/bin/bash
current_branch="$(git rev-parse --abbrev-ref HEAD)"
tmp=$(mktemp) || exit
echo "$current_branch $(cat "$1")" > "$tmp"
mv "$tmp" "$1"

(Thanks guys for the improvements in bash syntax made with your help here)

Then it would automatically prepend your commit messages with the branch name, which does the trick in JIRA.

For the rare occasions when you'd prefer NOT to trigger the hook, do this :

git commit -n -m"Your message"
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • 1
    Be aware that `... $(cat "$1") > "$1"` has the potential, depending on the shell's choice of order-of-operations, to destroy the contents of the output file *before* `cat` has a chance to read those contents. It's wiser to save the contents in an explicit, separate step. (Obviously if you've tested it and it works, it's OK in *your* shell. :-) I *think* all the modern shells do it in the sensible and safe order, but I wouldn't assume it) – torek Jan 07 '19 at 16:30
  • @torek Thanks for the remark. Indeed it works well enough for my coworkers and me, but now I know I should try to make it safer along these lines :-) – Romain Valeri Jan 07 '19 at 16:38
  • @torek Edited to take your comment into account. – Romain Valeri Jan 14 '19 at 09:12