Skip to content

Instantly share code, notes, and snippets.

@cas--
Last active January 27, 2022 12:34
Show Gist options
  • Save cas--/b2c2e507d0639d1f7286720853ee1ea8 to your computer and use it in GitHub Desktop.
Save cas--/b2c2e507d0639d1f7286720853ee1ea8 to your computer and use it in GitHub Desktop.

These steps will create a git alias command to parse a Jira issue reference from a branch and automatically append it to a commit message.

Create a Sureswift specific git config e.g. ~/sureswift/.gitconfig. The path will be the parent directory where you keep all the repositories e.g. ~/sureswift. Add your details and a 'jira' trailer for the Jira reference:

[user]
	name = Your Name
	email = your.name@sureswiftcapital.com
[trailer "jira"]
	key = "Jira-ref"
	command = "echo $(git branch --show-current | grep -o '[A-Z]\\{2,10\\}-[0-9]\\+' | xargs -i echo 'https://sureswift.atlassian.net/browse/{}')"

In the global git config ~/.gitconfig add an includeIf that will always use the above config for any repository under ~/sureswift/

[includeIf "gitdir:~/sureswift/"]
	path = ~/sureswift/.gitconfig

Include a new git alias to under the [alias] key in global config ~/.gitconfig that will take an existing commit message pass it through intepret-trailers and update your commit message

[alias]
	trailer-append = "!git commit --amend -m \"$(echo \"$(git log --format=%B -n1)\" | git interpret-trailers --trim-empty)\""

To try it out checkout a branch with a Jira reference in the name and run the trailer-append command:

git checkout DEV-1234
git trailer-append
git show

commit d9b61ea529e40062c1d8d698ab3eef97489ff3e3 (HEAD -> DEV-1234-test-trailer-append)
Author: Your Name <your.name@sureswiftcapital.com>
Date:   Thu Jan 27 09:15:11 2022 +0000

    Add Jira reference to git trailer
    
    Jira-ref: https://sureswift.atlassian.net/browse/DEV-1234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment