Skip to content

Instantly share code, notes, and snippets.

@truongvinht
Created November 18, 2021 20:13
Show Gist options
  • Save truongvinht/60cc2957c2c7ec3457d50b02e3eb3836 to your computer and use it in GitHub Desktop.
Save truongvinht/60cc2957c2c7ec3457d50b02e3eb3836 to your computer and use it in GitHub Desktop.
Git SVN Migration script with tagging structure
#!/bin/bash
# Script checkout SVN as git repository,
# move trunk to develop branch
# and create a master branch based on latest tag
#constants
SVN_TMP_FILE=svnlog.txt
BRANCH_FILE=branch.txt
LATEST_ANNUAL=""
#variables
revision=""
project=""
#ARGUMENTS: SVN-URL BRANCHES
SVN_URL=$1
FILTER_YEAR=$2
#BRANCHES=$3
if [[ $1 == "" ]]; then
echo "Missing SVN-URL: svnmigration.sh SVN-URL"
exit 0
fi
#get folder / project name
delimiter=/
s=$SVN_URL$delimiter
project=""
while [[ $s ]]; do
project=( "${s%%"$delimiter"*}" );
s=${s#*"$delimiter"};
done;
echo "###### ${project} #####"
echo "Checking for latest revision in ${SVN_URL}..."
svn log $SVN_URL > $SVN_TMP_FILE
while read row; do
# filter only commits (commit/user/date/details)
if [[ $row == *"|"* ]]; then
# split REV | USER | DATE | LINE
# echo "${row}"
IFS="|" read -ra list <<< "$row"
#revision
svn_revision="${list[0]}"
#date
commit_date="${list[2]}"
year=${commit_date:0:5}
#echo "${svn_revision} - ${year}"
if [[ "$year" -ge "$FILTER_YEAR" ]]; then
#echo "${year} - ${FILTER_YEAR}"
revision="${svn_revision}"
fi
fi
done < $SVN_TMP_FILE
#remote tmp file
rm -f $SVN_TMP_FILE
#trim spaces
revision="$(echo -e "${revision}" | tr -d '[:space:]')"
echo "Latest SVN revision: ${revision} (${FILTER_YEAR})"
#clone svn repository
echo "Clone ${SVN_URL}..."
clone_argument="-${revision}:HEAD"
git svn clone -s $clone_argument $SVN_URL
cd $project
#rename master to develop branch
echo "rename branch trunk/master as develop"
git branch -m master develop
##### MOVE LAST TAG TO MASTER ###############
# get all branches
git branch -r --sort=-committerdate > $BRANCH_FILE
#latest tag
latestTag=""
while read row; do
if [[ $row == *"/tags/"* ]]; then
latestTag=$row
break;
fi
done < $BRANCH_FILE
if [[ $latestTag == "" ]]
then
# no tags -> no master
echo "No Tags available"
else
#rename tagged branch to master
echo "created branch ${latestTag} as master"
git checkout -b master $latestTag
# move tag branch to master
IFS="/" read -ra branchTags <<< "$latestTag"
# origin/tags/App.1.0.0 => App.1.0.0
tagName="${branchTags[-1]}"
#create Tag
echo "created tag ${tagName}"
git tag -a $tagName -m "migrated tag"
fi
#remote tmp file
rm -f $SVN_TMP_FILE
rm -f $BRANCH_FILE
echo "######################################"
echo "SVN preparation finished"
echo "Next step:"
echo "git remote add origin URL\ngit push -u origin --all\ngit push origin --tags"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment