Git 个分支要合并到 shell 个脚本变量中
Git branches to be merged in shell script variable
我正在创建一个脚本,用于合并 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
为什么这不起作用?
INFO: 当我复制并粘贴命令时(由 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.
当你设置export IFS=,
时,这一行
git merge -q --no-edit $branches
不再将两个单独的分支引用传递给 git merge
,因为空格不再用于分词。就好像你输入了
git merge -q --no-edit "refs/remotes/origin/XXX refs/remotes/origin/XXY"
我认为解决此问题的最快方法是在完成 INCLUDE_PATTERN
:
设置后恢复 IFS
的原始值
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
我正在创建一个脚本,用于合并 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
为什么这不起作用?
INFO: 当我复制并粘贴命令时(由 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.
当你设置export IFS=,
时,这一行
git merge -q --no-edit $branches
不再将两个单独的分支引用传递给 git merge
,因为空格不再用于分词。就好像你输入了
git merge -q --no-edit "refs/remotes/origin/XXX refs/remotes/origin/XXY"
我认为解决此问题的最快方法是在完成 INCLUDE_PATTERN
:
IFS
的原始值
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