I am having a problem passing parameters to an external bash script file from git aliases. Currently, I have my bash script file in the same directory as my .gitconfig. I plan on moving all my complex !f() { }
aliases in there to avoid battling all the escaping and no commenting.
So my script file is named .gitconfig.kat.aliases.script and it looks something like this:
#!/bin/bash
openInBrowser() {
REPO_URL=$(git config remote.origin.url)
find="git@bitbucket.org:"
replace="https://bitbucket.org/"
# Replace SSH with HTTPS prefix
REPO_URL=$(echo $REPO_URL | sed -e "s%${find}%${replace}%g")
explorer $REPO_URL
exit 0;
}
checkoutRemoteBranch() {
echo "$# Parameters"
echo $0
echo $1
echo $2
if [ "$#" = 2 ]
then
echo -e "BINGO"
# git fetch
# git co -b $2 $1/$2
else
echo -e "usage:\tgit co-rb <origin> <branch>"
fi
}
Configuration 1
Inside my .gitconfig [alias] section, I have this:
co-rb = !bash -c 'source $HOME/.gitconfig.kat.aliases.script && checkoutRemoteBranch \"$@\"'
open = !bash -c 'source $HOME/.gitconfig.kat.aliases.script && openInBrowser'
I followed this question's syntax for aliases to an external script/function. Then, I followed this question's syntax for passing parameters to the function (above alias isn't passing dummy parameter like the question suggested, more info below).
The problem is, without a dummy parameter, when I execute git co-rb origin feature/test
, I get the following output:
1 Parameters
origin
feature/test
Configuration 2
If I define a dummy parameter after the $@...
co-rb = !bash -c 'source $HOME/.gitconfig.kat.aliases.script && checkoutRemoteBranch \"$@\" x'
The output is:
2 Parameters
origin
feature/test
x
Configuration 3
If I change the position of the dummy parameter to co-rb = !bash -c 'source $HOME/.gitconfig.kat.aliases.script && checkoutRemoteBranch x \"$@\"'
The output is:
2 Parameters
origin
x
feature/test
Issues:
1. Configuration 1 - The parameter count is wrong, and the positional parameters are offset by -1.
1. Configuration 2 - The parameter count is right, but the positional parameters are offset by -1.
1. Configuration 3 - The parameter count is right, but the parameters are all messed up. x
is injected in the middle and the positions aren't correct for $1.
How should I set up my git alias so that the parameter count is correct and positions 1..N are what I expect? Or should I change my shell script so that any time I check parameter count, I check against 'count-1'?