2

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'?

Philippe
  • 28,207
  • 6
  • 54
  • 78
Terry
  • 2,148
  • 2
  • 32
  • 53

1 Answers1

0
$ bash -c 'echo $@' 1 2 3
2 3

$ bash -c 'echo $0 $@' 1 2 3
1 2 3

That is, "$@" passes parameters started from the 1st but in this situation there is also 0th parameter.

So pass $0:

co-rb = !bash -c 'source $HOME/.gitconfig.kat.aliases.script && checkoutRemoteBranch "$0" "$@"'

$0 is not added to $@ because in 99 use cases out of 100 it's not needed. Usually it's the shell name or the script name, neither one is needed in $@.

phd
  • 82,685
  • 13
  • 120
  • 165
  • What if one of the parameters I'm trying to forward is a path? I seem to be getting one parameter for each folder in the path. Is there a way to package those up into one parameter? `git myalias 5 "Path/To/File.txt" delete` I'd like to see three parameters for this and not 5. – Terry Mar 15 '19 at 11:35
  • The path must be a single parameter: `$ bash -c 'for param in $0 "$@"; do echo "$param"; done' 1 /etc/passwd 3` => output 3 lines: 1 /etc/passwd 3 – phd Mar 15 '19 at 13:54
  • I recieved `bash -c 'source $HOME/.gitconfig.kat.aliases.script && undoFile for param in $0 $@: -c: line 1: syntax error: unexpected end of file` – Terry Mar 15 '19 at 16:35