Created
April 17, 2012 15:21
-
-
Save toke/2406723 to your computer and use it in GitHub Desktop.
relocation of git-svn repositories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Helper to relocate git-svn urls. | |
OLD_REPO=`git config --local --get svn-remote.svn.url` | |
if [[ $1 ]] ; then | |
NEW_REPO=$1 | |
else | |
NEW_REPO='https://defaultrepourl/lalal/' | |
fi | |
escape_val() { | |
return_val=$(echo "$1" | sed 's/[^-A-Za-z0-9_]/\\&/g') | |
return 0 | |
} | |
build_filter() { | |
escape_val $1 | |
val1=${return_val} | |
escape_val $2 | |
val2=${return_val} | |
return_val="sed \"s/git-svn-id: ${val1}/git-svn-id: ${val2}/g\"" | |
return 0 | |
} | |
prepare_switch() { | |
echo "* Stashing orphaned files (use git stash pop later to get them back)" | |
git stash save | |
echo "* Running garbage collection" | |
git gc | |
return 0 | |
} | |
do_switch() { | |
if [[ $2 ]] ; then | |
echo "* Migrating from ${1} to ${2}" | |
else | |
exit 1 | |
fi | |
build_filter ${1} ${2} | |
svn_filter=$return_val | |
echo -e "\n* Rewrite the git history to reflect new repository URL" | |
git filter-branch -f --msg-filter "${svn_filter}" $(cat .git/packed-refs | awk '// {print $2}' | grep -v 'pack-refs') | |
echo -e "\n* Setting new svn-remote url" | |
git config --local --replace-all svn-remote.svn.url ${2} | |
return 0 | |
} | |
rebuild_index() { | |
echo "* Removing old svn meta info." | |
rm -rf .git/svn | |
echo -e "\n* Rebuild svn index" | |
git svn rebase | |
return 0 | |
} | |
echo "-------------------------------------" | |
echo " Use at your own risk " | |
echo "-------------------------------------" | |
echo "Migrating from ${OLD_REPO} to ${NEW_REPO}." | |
if [[ $OLD_REPO == $NEW_REPO ]] ; then | |
echo "Old and new repository URL is the same!" | |
echo "Already switched to new URL?" | |
echo "I'm stopping now." | |
exit 1 | |
fi | |
echo "Proceed? (y/n)" | |
read dorun | |
if [[ $dorun == "y" ]] ; then | |
echo "Prepare..." | |
prepare_switch | |
echo -e "\n\nSwitching..." | |
do_switch $OLD_REPO $NEW_REPO | |
echo -e "\n\nRebuilding..." | |
rebuild_index | |
echo -e "\n* DONE: now the result" | |
git svn info | |
exit 0 | |
else | |
echo "Canceled." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment