0

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?

7
  • I doubt that the problem won't occur with 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 your alias definition to git config alias.my-merge-wrapper '!f(){ set -x; path/to/script.sh "$@"; }; f' and add a line set -x in your script after #!/bin/sh and copy&paste the output to your question you get from running git my-merge-wrapper -m"message (b2 into b1)"
    – Bodo
    Commented Jun 16, 2021 at 11:41
  • Thank you... I saw elsewhere that I had to use 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 can
    – CidTori
    Commented Jun 16, 2021 at 13:39
  • It is possible to use printf or echo for debugging as well, e.g. something like echo "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 use set -x/set +x selectively to limit the output to a certain part of your script.
    – Bodo
    Commented Jun 16, 2021 at 13:51
  • Thank you for your help, actually it's not 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 the merge: (b2 - not something we can merge error. To understand what happened I used printf 'Arg: %s\n' $@ and printf '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...
    – CidTori
    Commented Jun 16, 2021 at 16:53
  • Anyway, now I don't know what to do with this question...
    – CidTori
    Commented Jun 16, 2021 at 16:53

1 Answer 1

0

The solution was to quote $@ in the alias too:

git config alias.my-merge-wrapper  '!f(){ path/to/script.sh "$@"; }; f'

Then the git stuck option's short name and its quoted argument are grouped as a unique shell script argument (as if it was '-mmessage (b2 into b1)'), but git supports it without problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .