I'm trying to write a script that will be called as a git alias.
The script is a wrapper around git merge
, so I want to pass all it's parameters to git merge
as-is when I call it in the script.
The alias is set like this:
git config alias.my-merge-wrapper '!f(){ path/to/script.sh $@; }; f'
My first try was this:
#!/bin/sh
git merge "$@"
Sadly, this does not work with stuck options (see here):
For example, if I call my script like this:
git my-merge-wrapper -m"message (b2 into b1)"
It fails, because git merge
is called like this:
git merge -mmessage (b2 into b1)
So I get merge: (b2 - not something we can merge
I also tried with $*
in both the alias and the script, without success.
Are there any solution to this?
git my-merge-wrapper -m "message (b2 into b1)"
. Apparently the argument-m"message (b2 into b1)"
gets split into words. To debug this problem, change youralias
definition togit config alias.my-merge-wrapper '!f(){ set -x; path/to/script.sh "$@"; }; f'
and add a lineset -x
in your script after#!/bin/sh
and copy&paste the output to your question you get from runninggit my-merge-wrapper -m"message (b2 into b1)"
printf
to debug this, and it misled me,set -x
is way better... I'll clarify my question and answer it as soon as I canprintf
orecho
for debugging as well, e.g. something likeecho "args:"; for i in "$@"; do echo "$i"; done
. In this case you should show the exact code, the command line and the resulting output. If your script is big you can also useset -x
/set +x
selectively to limit the output to a certain part of your script.printf
's fault but mine only: when I tested my script I first declared the alias like this:git config alias.my-merge-wrapper '!f(){ path/to/script.sh $@; }; f'
, which gave me themerge: (b2 - not something we can merge
error. To understand what happened I usedprintf 'Arg: %s\n' $@
andprintf 'Arg: %s\n' "$@"
but mixed them up, and since I thought that I had to separate-m
and the message, even when I fixed the alias (just to be sure, I didn't see that the error disappeared), since the printf didn't change I thought I was stuck...