Created
March 10, 2012 16:37
-
-
Save vincentbernat/2011995 to your computer and use it in GitHub Desktop.
My own git2svn
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/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