Search code examples
bashgitrepo

How to parse correctly an argument with foward slash in bash?


I wrote a script in which I try to wrap some AOSP repo commands. One of the arguments I pass to the script is a branch in the following format: refs/tags/NAME.

When I pass this directly to repo init, it resolves just fine. However, when I pass to my script, I get the error: fatal: Couldn't find remote ref refs/tags/NAME

Below is my script:

#!/bin/bash

URL='private url ommited'
# initially, I tried just to BRANCH=$3 which didn't work either
BRANCH=`echo $3 | sed 's!/!\/!g'`
MANIFEST="$5"
REPO_URL='another private url ommited'
REPO_BRANCH='ommited'

# point to objects
repo init -u $URL -b $BRANCH -m $MANIFEST --repo-url=$REPO_URL --repo-branch=$REPO_BRANCH --depth=1

# download code
time repo sync --no-tags

First I assumed the issue was related to "/" and tried to sed it with "\/", but I get the same error. Now I assume there is some bash logic I don't understand correctly.

For now, we can assume that branch will always be the third argument and manifest the fifth. In the future, I may refactor this. Also, this is pure bash and will just run on this particular machine.

EDIT:

I've just run the script with /bin/bash -x and my variable BRANCH is returning refs/tags/NAME as it should, but the repo init command doesn't seem to recognize when the variable BRANCH is expanded to refs/tags/NAME. In fact, if I pass the "$3" directly to repo init, it doesn't work either.

I also tried to pass the whole arguments array "$@" as the other variables allocated in the script, such as URL, MANIFEST and others also passed to the scripts, but the repo init command doesn't interpret it correctly also (which seems related to the URL and what led me to believe that foward slashes where the one responsible for this issue).

All my tries where made by the following steps:

  • create a new directory
  • cd to it
  • try to run the script and notice the fail
  • rm -rf .repo
  • repo init with the same arguments

Solution

  • I've just found the solution for this issue. I think that repo might have some issue with bash expansion or I just don't really get what was going wrong.

    However, to solve this I used the below:

    INIT=$(eval echo $@)
    
    repo init -u $INIT
    

    Instead passing the $@ directly to repo init command or setting the variables by hand. Just echo $@ didn't work either.