Git分支在shell脚本变量中合并

时间:2015-04-07 16:20:01

标签: git bash shell git-merge

我正在创建一个合并shell脚本变量中定义的分支的脚本。这是脚本的一部分

# 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

这是输出

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

为什么这不起作用?

信息:当我复制并粘贴命令时(由echo "C = ..."打印,它按预期工作

更多信息:当我运行eval "git merge -q --no-edit $branches"时,我收到了另一个错误

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

1 个答案:

答案 0 :(得分:3)

设置export IFS=,时,此行

git merge -q --no-edit $branches

不再将两个单独的分支引用传递给git merge,因为空格不再用于分词。这就好像你输入了

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

我认为解决此问题的最快方法是在设置IFS后恢复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

这可能会更好:

# 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