简体   繁体   中英

Git branches to be merged in shell script variable

I'm creating a an script that merge the branches defined in a shell script variable. Here is part of the script

# Here is how we are setting the variable 'branches'
ALL_BRANCHES=`git ls-remote . | cut -d $'\t' -f 2` # THIS WILL INCLUDE THE FOLDER AS PREFIX
includePattern=
if [[ -n "$INCLUDE_PATTERN" ]] ; then
  export IFS=","
  for pattern in $INCLUDE_PATTERN; do
    includePattern="$includePattern -e $REMOTE_FOLDER$pattern"  
  done
fi
branches=`echo "$ALL_BRANCHES" | eval "grep $includePattern"`

echo "B = $branches"
echo "C = git merge -q --no-edit $branches"
git merge -q --no-edit $branches

This is the output

B = refs/remotes/origin/XXX refs/remotes/origin/XXY
C = git merge -q --no-edit refs/remotes/origin/XXX refs/remotes/origin/XXY
merge: refs/remotes/origin/XXX refs/remotes/origin/XXY - not something we can merge

Why this is not working?

INFO: When I do a copy&paste of the command (printed by the echo "C = ..." it works as expected

MORE INFO: When I run eval "git merge -q --no-edit $branches" I got another error

/usr/lib/git-core/git-merge-octopus: 1: eval: Bad substitution
Merge with strategy octopus failed.

When you set export IFS=, , this line

git merge -q --no-edit $branches

no longer passes two separate branch references to git merge , because whitespace is no longer used for word-splitting. It's as if you typed

git merge -q --no-edit "refs/remotes/origin/XXX refs/remotes/origin/XXY"

I think the quickest way to fix this is to restore the original value of IFS after you are done setting INCLUDE_PATTERN :

if [[ -n "$INCLUDE_PATTERN" ]] ; then
  old_ifs=$IFS
  export IFS=","
  for pattern in $INCLUDE_PATTERN; do
    includePattern="$includePattern -e $REMOTE_FOLDER$pattern"  
  done
  IFS=$old_ifs
fi

This might work better:

# Using a here-doc instead of a here string
# to avoid a bug present prior to bash 4.3
IFS=, read -a patterns <<EOF
$INCLUDE_PATTERN
EOF
for pattern in "${patterns[@]}"; do
  includePattern+=" -e $REMOTE_FOLDER$pattern"
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM