首页 > 解决方案 > Bash:for循环中的错误替代错误-git标签

问题描述

我正在尝试执行从 SVN 到 GIT 的迁移,git svn clone ..并且一切正常。

现在我需要使用此命令将 SVN 中的标签转换为 git 中的真实标签

for t in $(git for-each-ref --format='%(refname:short)' refs/remotes/origin/tags) 
do 
    git tag ${t/origin\/tags\//} $t - #BAD substitution error - need to fix
    git branch -D -r $t
done

如果我在命令行中运行此脚本,它可以工作,但如果我在 shell 脚本中运行此脚本,它将失败并出现“错误替换错误”。这里有什么建议吗?

完整的脚本在这里:

#!/bin/bash

## Modificed script - Fork from https://github.com/MPDFT/svn-to-git

####### Project name
PROJECT_NAME="" #Example - Digisharp
SVN_USERNAME="" #Example - adys
GIT_USERNAME="" #Example - adys
GIT_CREDENTIAL="" #Example - pz2fekhjcsq5io5xbslcuss5lspo4lcgh4cwjswge265uzxrnzxv

####### SVN
# SVN repository to be migrated
SVN_URL="" #Example - 

####### GIT
# Git repository to migrate - IMPORTANT! YOU MUST INCLUDE YOUR USERNAME AND PASSWORD(PAT TOKEN FOR AZURE) 
# We need this to automate the git push without having it asking you for password
GIT_URL="" #Example - 

###########################
#### Don't need to change from here
###########################

#STYLE_COLOR
RED='\033[0;31m'
LIGHT_GREEN='\e[1;32m'
NC='\033[0m' # No Color


echo -e "${LIGHT_GREEN} [LOG] Starting migration of ${NC}" $SVN_TRUNK
echo -e "${LIGHT_GREEN} [LOG] Using: ${NC}" $(git --version)
echo -e "${LIGHT_GREEN} [LOG] Using: ${NC}"  $(svn --version | grep svn,)
echo -e "${LIGHT_GREEN} [LOG] Step 01/05 Create Directories ${NC}"
echo -e "${LIGHT_GREEN} [RUN] Step 02/05 ${NC} £ git svn clone --stdlayout --no-minimize-url $BASE_SVN $PROJECT_NAME --user=$SVN_USERNAME"
git svn clone --stdlayout --no-minimize-url $SVN_URL $PROJECT_NAME --user=$SVN_USERNAME --authors-file=authors.txt
cd $PROJECT_NAME
echo -e "${LIGHT_GREEN} [RUN] Step 03/05 ${NC} $ git remote add origin"
git remote add origin $GIT_URL
echo -e "${LIGHT_GREEN} [RUN] Step 04/05 ${NC} - Preparing the git branches and tags"
for t in $(git for-each-ref --format='%(refname:short)' refs/remotes/origin/tags) 
do 
    #git tag ${t/origin\/tags\//} $t - BAD substitution error - need to fix
    git branch -D -r $t
done

for b in $(git for-each-ref --format='%(refname:short)' refs/remotes)
do 
    git branch $b refs/remotes/$b
    git branch -D -r $b
done
echo -e "${LIGHT_GREEN} [RUN] Step 05/05 [RUN] git push ${NC}"
git push origin --all
git push origin --tags
echo "Successful - The git repository is now available in" $GIT_URL

我运行命令sh migration.sh

标签: bashgit

解决方案


你是在用类似的东西执行你的脚本/bin/sh /path/to/script.sh吗?

${t/origin\/tags\//}在 bash 中可用,但与 POSIX shell 不兼容,您可能会成功地将其更改为:

echo "$t" | sed 's~origin/tags/~~'

我强烈建议您使用 shellcheck 对脚本进行linting,因为您遇到了一些潜在的错误。


推荐阅读