Skip to content

Instantly share code, notes, and snippets.

@approximatenumber
Created June 26, 2018 09:37
Show Gist options
  • Save approximatenumber/194dad69c300b67f412b589cbd611933 to your computer and use it in GitHub Desktop.
Save approximatenumber/194dad69c300b67f412b589cbd611933 to your computer and use it in GitHub Desktop.
Bash script to migrate SVN repo to Git (Bitbucket)
#!/bin/bash
project_name="my-project"
# Full path to your svn repo or path in repo
svn_repo="https://server.svn/repos/my-project"
trunk_path="/trunk"
tags_path="/tags"
branches_path="/branches"
default_email_suffix="@my.company"
git_repo="ssh://git@bitbucket.git:7999/my-project.git"
# get authors from svn history and write them to users.txt
# example: 'linus' from svn commits becomes 'linus = linus <linus@my.company>' in git commits
authors=$(svn log --quiet $svn_repo | grep "^r" | awk '{print $3}' | sort -u)
for login in $authors; do
echo "$login = $login <$default_email_suffix>" >> users.txt
done
git svn clone $svn_repo --authors-file=users.txt \
--no-metadata \
--trunk="$trunk_path" \
--tags="$tags_path" \
--branches="$branches_path" $project_name
rm users.txt
# pushing trunk to master
cd $project_name
git remote add origin $git_repo
git push --force -u origin master
# filtering and pushing branches
if [ $branches_path ]; then
branches=$(git branch -r | grep -v "@" | grep -v "trunk" | grep -v "tags")
for branch in $branches; do
# convert origin/branchname to branchname
cleared_branchname=$(sed -e 's|origin/||g' -e 's|%20|_|g'<<< $branch)
git branch "$branch" refs/remotes/"$branch"
git branch -m "$branch" "$cleared_branchname"
git push -u origin "$cleared_branchname"
done
fi
# filtering and pushing tags
if [ $tags_path ]; then
tags=$(git branch -r | grep -v "@" | grep "tags")
for tag in $tags; do
# convert origin/tags/my-tag to my-tag
cleared_tagname=$(sed -e 's|origin/tags/||g' -e 's|%20|_|g' <<< $tag)
git tag "$tag" "$tag"
git tag "$cleared_tagname" "$tag"
git push -u origin "$cleared_tagname"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment