rafael.braga
rafael.braga

Reputation: 402

How to use git alias with shell function to build a commit message

I'm trying to create a git alias to automate my PR merge and approval.

The complete git command is this:

git checkout master && git sync && git merge --no-ff -m 'Merged in hotfix/X-3144 (pull request #1112)' remotes/origin/hotfix/X-3144

Where X is the BitBucket prefix for the issue. Reading other stack overflow questions, I came up with this:

mergeto = "!f() { git checkout $1 && git sync && git merge --no-ff -m 'Merged in {$2}/B2B-{$3} (pull request #{$4})' remotes/origin/$2/X-$3;}; f "

So, naturally I'm using this to merge a PR that has conflicts. After I resolve these conflicts, I issue a git commit and git brings up nano with my previously typed commit message. My goal here is that the commit message is constructed using my parameters. So, for an example, if I use:

git mergeto master hotfix 123 1120

My final commit message was supposed to be "Merged in hotfix/X-123 (pull request #1120)" but instead I get a "Merged in {$2}/X-{$3} (pull request #{$4})"

How can I specify the parameters in such a way that this can work?

I Read a few stack over flow posts that show how to use a shell function inside a git alias, but couldn't find an example of escaping the text with parameters.

Upvotes: 3

Views: 155

Answers (1)

rafael.braga
rafael.braga

Reputation: 402

Using John Szakmeister and eftshift0 suggestions, I got it working by escaping double quotes with backlash:

mergeto = "!f() { git checkout $1 && git sync && git merge --no-ff -m \"Merged in $2/B2B-$3 (pull request #$4)\" remotes/origin/$2/X-$3;}; f "

Thanks for your help!

Upvotes: 4

Related Questions