Skip to content

Instantly share code, notes, and snippets.

@RunnerRick
Last active December 21, 2015 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RunnerRick/6226490 to your computer and use it in GitHub Desktop.
Save RunnerRick/6226490 to your computer and use it in GitHub Desktop.
A simple utility for using Git locally while using an external SVN repository.

A simple utility for using Git locally while using an external SVN repository.

#Git Working Copy Directory
GIT_WORKING_DIR=/your/git/working/copy
#Temporary directory for Git Export
GIT_EXPORT_DIR=$GIT_WORKING_DIR/usr/tmp/.export
#SVN Working Copy Directory
SVN_WORKING_DIR=/your/svn/working/copy
#!/usr/bin/env bash
## Functions
showUsage() {
echo
printf "git2svn - A simple utility for using Git locally while using an external SVN repository.\n"
printf "\tAssumes ./.git2svnrc or ./usr/.git2svnrc exists.\n"
printf "\t*** NOTE: Those are relative paths. ***\n\n"
printf "./git2svn import\n"
printf "\tImport the SVN working copy to the Git working copy.\n"
printf "./git2svn export\n"
printf "\tExport Git working copy to the SVN working copy with option to commit.\n"
printf "./git2svn update\n"
printf "\tUpdate the SVN working copy from the remote SVN repository.\n"
printf "./git2svn delete\n"
printf "\Delete Git-deleted/moved files from the SVN working copy from the remote SVN repository.\n"
printf "./git2svn status\n"
printf "\tShow SVN Status w/o changing directories.\n"
echo
# Show configured directories.
echo
echo CONFIG = $CONFIG
echo USR_CONFIG = $USR_CONFIG
echo CONFIG_LOADED = $CONFIG_LOADED
echo
echo GIT_WORKING_DIR = $GIT_WORKING_DIR
echo GIT_EXPORT_DIR = $GIT_EXPORT_DIR
echo SVN_WORKING_DIR = $SVN_WORKING_DIR
echo
}
showSvnStatus() {
cd $SVN_WORKING_DIR
echo
echo SVN Status
echo =====================================================================
echo $SVN_WORKING_DIR
echo =====================================================================
svn status
}
loadConfig () {
if [[ -e $1 ]]; then
source $1
if [[ $? -ne 0 ]]; then
echo "Error loading configuration file at $1"
exit
fi
CONFIG_LOADED=$1
fi
}
getConfig () {
# User-specific config.
USR_CONFIG=./usr/.git2svnrc
# Config
CONFIG=./.git2svnrc
# Read configuration.
# Check for it first in the usr subdirectory.
loadConfig $USR_CONFIG
if [[ -z $CONFIG_LOADED ]]; then
loadConfig $CONFIG
fi
#Directories are read-in from the configuration file.
if [[ "$GIT_WORKING_DIR" = "" ]]; then
echo "The SVN working copy directory is not specified."
showUsage
exit 3
fi
if [[ "$SVN_WORKING_DIR" = "" ]]; then
echo "The SVN working copy directory is not specified."
showUsage
exit 3
fi
if [[ "$GIT_EXPORT_DIR" = "" ]]; then
echo "The SVN working copy directory is not specified."
showUsage
exit 3
fi
}
importFromSvn() {
# Switch to the SVN working copy.
cd $SVN_WORKING_DIR
# Update SVN working copy from external repo.
svn update
# Show status after operation.
showSvnStatus
# Provide option to import changes.
echo "Import changes from the SVN working copy to the Git working copy?"
select yn in "Yes" "No"; do
case $yn in
Yes )
svn export $SVN_WORKING_DIR $GIT_WORKING_DIR --force
# Show status after operation.
cd $GIT_WORKING_DIR
echo
git status
break;;
No ) exit;;
esac
done
}
exportToSvn() {
# Switch to the Git working copy.
cd $GIT_WORKING_DIR
# Export git repo to GIT_EXPORT_DIR folder.
git checkout-index --prefix=$GIT_EXPORT_DIR/ -a
# Copy contents of GIT_EXPORT_DIR to SVN working folder.
cp -f -R $GIT_EXPORT_DIR/ $SVN_WORKING_DIR
# Remove GIT_EXPORT_DIR folder.
rm -rf $GIT_EXPORT_DIR
# Switch to the SVN working copy.
cd $SVN_WORKING_DIR
# Show status after operation.
showSvnStatus
# Provide option to add all new files.
echo
echo "Add **ALL** unversioned files (?) to SVN's version control?"
select yn in "Yes" "No"; do
case $yn in
Yes )
svn status | grep '?' | sed 's/^.* /svn add /' | bash
break;;
No ) break;;
esac
done
showSvnStatus
# Provide option to commit changes.
echo
echo "Commit changes to the remote SVN repository?"
select yn in "Yes" "No"; do
case $yn in
Yes )
echo Commit Message:
read MESSAGE
svn commit -m "$MESSAGE"
break;;
No ) break;;
esac
done
}
deleteFromSvn() {
# Switch to the Git working copy.
cd $GIT_WORKING_DIR
# Get list of deleted files.
FILES_TO_DELETE=$(git status --porcelain | grep 'D' | sed 's/^.* /echo /' | bash)
FILES_TO_DELETE=$(echo $FILES_TO_DELETE && git status --porcelain | grep 'R' | sed 's/ ->.*$//' | grep 'R' | sed 's/^.* /echo /' | bash)
echo $FILES_TO_DELETE
# SwigitvnStatus
}
updateFromRemoteSvn() {
# Switch to the SVN working copy.
cd $SVN_WORKING_DIR
# Update from remote SVN repository.
svn update
# Show status after operation.
showSvnStatus
}
## /Functions
# Get the script's directory.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
getConfig
case $1 in
"import" )
importFromSvn
;;
"export" )
exportToSvn
;;
"update" )
updateFromRemoteSvn
;;
"delete" )
deleteFromSvn
;;
"status" )
showSvnStatus
;;
* )
showUsage
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment