Skip to content

Instantly share code, notes, and snippets.

@vincentbernat
Created March 10, 2012 16:37
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 vincentbernat/2011995 to your computer and use it in GitHub Desktop.
Save vincentbernat/2011995 to your computer and use it in GitHub Desktop.
My own git2svn
#!/bin/zsh
set -e
# Usage: $0 git-repos svn-repos
#
# Both repositories should be initialized. A tag will be added to the
# git repository to remember the last synced position.
GIT_DIR=$1
SVN_DIR=$2
# Grab current position (will be empty the first time)
GIT_TAG=$(cd ${GIT_DIR} && git tag -l git2svn)
for commit in $(cd ${GIT_DIR} && git rev-list --no-merges --reverse ${GIT_TAG:+${GIT_TAG}...HEAD}); do
# Grab author and commit
author=$( cd ${GIT_DIR} && git log -n 1 --pretty=format:%an ${commit})
message=$(cd ${GIT_DIR} && git log -n 1 --pretty=format:%B ${commit})
date=$( cd ${GIT_DIR} && git log -n 1 --pretty=format:%aD ${commit})
log=$(cd ${GIT_DIR} && git log -n 1 --pretty=format:%s ${commit})
# Message
echo
echo "8<----------------------------------------"
echo "[+] Prepare to push this commit:"
echo " SHA1: ${commit}"
echo " Author: ${author}"
echo " Date: ${date}"
echo " Log: ${log}"
# Copy the commit to the SVN directory
rm -f ${SVN_DIR}/**/*(N^/)
(cd ${GIT_DIR} && git archive ${commit}) | tar -C ${SVN_DIR} -xf -
# Add new files, remove old ones
for file in $(svn st ${SVN_DIR} | awk -F" " '{print $1 ":::" $2}'); do
fstatus=${file%:::*}
fname=${file#*:::}
case ${fstatus} in
"?")
case ${fname} in
*@*)
svn add ${fname}@
;;
*)
svn add ${fname}
;;
esac
;;
"!")
case ${fname} in
*@*)
svn rm ${fname}@
;;
*)
svn rm ${fname}
;;
esac
;;
"M")
;;
*)
# Better safe than sorry
echo "[!] Don't know how to handle status \"${fstatus}\" for ${fname}"
exit 1
;;
esac
done
# Let's commit!
echo -n "[?] Ready to commit? "
read
(cd ${SVN_DIR} && svn commit -F =(echo "${message}" ;
echo
echo "Original author: ${author}"
echo "Original date: ${date}"
echo "Original SHA1: ${commit}"))
(cd ${GIT_DIR} && git tag -f git2svn ${commit})
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment