Created
April 18, 2010 15:18
-
-
Save alab1001101/370277 to your computer and use it in GitHub Desktop.
script for import zend framework svn trunk to local git and vice versa and easy commiting of patches to releases
This file contains hidden or 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 | |
# This script takes four different actions | |
# | |
# import | |
# imports updatet svn_dir trunk into git_dir master | |
# and pushs git master after that rebases git_branch | |
# | |
# export | |
# calls import and then exports git_branch | |
# into updated svn_dir | |
# | |
# commit | |
# asks for ZF-Issue number, then fetches summary and components from JIRA | |
# asks for Commit Message defaults to 'ZF-Issue: summary' | |
# calls export | |
# asks for components to Unit Test on svn trunk | |
# runs Unit Tests | |
# shows svn diff and asks if to commit | |
# commits | |
# | |
# release | |
# calls commit and fetches committed revision number | |
# then a svn merge on branch release dir | |
# runs Unit Tests | |
# commits the merge with an assembled Message of Zf-Issue and revision number | |
action="$1" | |
svn_dir=./trunk | |
git_dir=./work | |
svn_branch_dir=./branches | |
default_release_version="1.10" | |
git_master=master | |
git_branch=tmp | |
# This is the prefix for committing git master | |
# followed by updated svn revision | |
sync_svn_msg="Sync svn trunk r" | |
# The prefixes outputted by $ diff -r -q ./from/dir ./into/dir | |
diff_prefix_only="Nur in " | |
diff_prefix_diff="Dateien " | |
# This is a temp file for diff output | |
diff_changes=./changes.diff | |
# This is for tee redirections | |
term_tty=$(tty) | |
# Verbose level 1,2,3 | |
verbosity=3 | |
verbose() | |
{ | |
if [[ $verbosity -ge $1 ]] ; then | |
echo "# $2" | |
fi | |
} | |
diff_copy() | |
{ | |
from="$1" | |
into="$2" | |
left="${diff_prefix_only}$from" | |
right="${diff_prefix_only}$into" | |
update="${diff_prefix_diff}$from" | |
verbose 1 "Running diff now." | |
diff -r -q -x '.svn' -x '.git' $from $into > $diff_changes | |
while read line ; do | |
if [[ "$line" =~ "$left" ]] ; then | |
ent=${line#$left} | |
ent=${ent//: //} | |
ent=${ent%.} | |
verbose 2 "cp -R -f ${from}$ent ${into}$ent" | |
cp -R -f ${from}$ent ${into}$ent | |
elif [[ "$line" =~ "$right" ]] ; then | |
ent=${line#$right} | |
ent=${ent//: //} | |
ent=${ent%.} | |
verbose 2 "rm -R -f ${into}$ent" | |
rm -R -f ${into}$ent | |
elif [[ "$line" =~ "$update" ]] ; then | |
ent=${line#$update} | |
ent=${ent%% *} | |
verbose 2 "cp -R -f ${from}$ent ${into}$ent" | |
cp -R -f ${from}$ent ${into}$ent | |
fi | |
done < $diff_changes | |
rm $diff_changes | |
} | |
import_from_svn() | |
{ | |
branch=$1 | |
verbose 1 "Preparing import from svn to git" | |
cd $svn_dir | |
verbose 1 "Updating svn trunk" | |
rev=$(svn update | tee $term_tty | tail -1) | |
rev=${rev##* } | |
rev=${rev%.} | |
cd $OLDPWD | |
cd $git_dir | |
upd=$(git log $git_master...$git_master~15 | grep "${sync_svn_msg}$rev") | |
if [[ ! -z $upd ]] ; then | |
verbose 1 "Git Committed Message: $(echo -e $upd)" | |
verbose 1 "Git $git_master allready up to date." | |
cd $OLDPWD | |
return 0 | |
fi | |
verbose 3 "git checkout $git_master" | |
git checkout $git_master | |
cd $OLDPWD | |
diff_copy $svn_dir $git_dir | |
cd $git_dir | |
verbose 3 "git add ." | |
git add . | |
verbose 3 "git commit -a -m '${sync_svn_msg}$rev'" | |
git commit -a -m "${sync_svn_msg}$rev" | |
verbose 3 "git push origin $git_master" | |
git push origin $git_master | |
verbose 3 "git checkout $branch" | |
git checkout $branch | |
verbose 3 "git rebase $git_master" | |
git rebase $git_master | |
verbose 3 "git push origin $branch" | |
git push origin $branch | |
cd $OLDPWD | |
return 0 | |
} | |
export_to_svn() | |
{ | |
verbose 1 "Preparing export from git to svn" | |
branch=$1 | |
import_from_svn $branch | |
if [[ $? -eq 1 ]] ; then | |
cd $git_dir | |
verbose 3 "git checkout $branch" | |
git checkout $branch | |
verbose 3 "git rebase $git_master" | |
git rebase $git_master | |
verbose 3 "git push origin $branch" | |
git push origin $branch | |
cd $OLDPWD | |
fi | |
verbose 1 "Now export from git to svn" | |
diff_copy $git_dir $svn_dir | |
} | |
ask_proceed() | |
{ | |
echo | |
if [[ ! -z $1 ]] ; then | |
echo "$1" | |
fi | |
read -n 1 -p "Proceed? y[n]: " proceed | |
if [[ $proceed != y ]] ; then | |
return 1 | |
fi | |
return 0 | |
} | |
issue="" | |
summary="" | |
components="" | |
ci_msg="" | |
ask_commit_infos() | |
{ | |
read -p "ZF-Issue number: " issue | |
if [[ -z $issue || ! $issue =~ [[:digit:]]* ]] ; then | |
echo "No ZF-Issue number given. Aborting." | |
exit 0 | |
fi | |
get="http://framework.zend.com/issues/si/" | |
get+="jira.issueviews:issue-xml/ZF-$issue/ZF-$issue.xml" | |
get+="?field=summary&field=components" | |
xml=$(wget -q -O- "$get") | |
summary="$(echo "$xml" | sed -n -e '/<summary>/{s/[^>]*>\([^<]*\)<.*/\1/p;q}')" | |
components="$(echo "$xml" | sed -n -e '/<component>/{s/[^>]*>\([^<]*\)<.*/\1/p}' | xargs)" | |
echo "Summary: $summary" | |
echo "Components: $components" | |
read -e -i "ZF-$issue: $summary" -p "Commit Message: " ci_msg | |
if [[ -z $ci_msg ]] ; then | |
echo "No Commit Message given. Aborting." | |
exit 0 | |
fi | |
} | |
ci_rev="" | |
commit() | |
{ | |
branch=$1 | |
ask_commit_infos | |
export_to_svn $branch | |
cd "$svn_dir/tests" | |
verbose 1 "Prepare Unit Test on svn trunk" | |
read -e -i "${components//Zend_/}" -p "Unit Test components: " unit_tests | |
./runtests.sh $unit_tests | |
ask_proceed | |
if [[ 1 -eq $? ]] ; then | |
exit 0 | |
fi | |
cd $OLDPWD | |
cd $svn_dir | |
svn diff | |
ask_proceed ">> svn commit -m '$ci_msg'" | |
if [[ 1 -eq $? ]] ; then | |
exit 0 | |
fi | |
ci_rev=$(svn commit -m "$ci_msg" | tee $term_tty | tail -1) | |
ci_rev=${rev#* } | |
ci_rev=${rev% *} | |
cd $OLDPWD | |
} | |
release() | |
{ | |
branch=$1 | |
commit $branch | |
read -e -i "$default_release_version" -p "Release Version: " release_ver | |
if [[ -z $release_ver ]] ; then | |
release_ver="$default_release_version" | |
fi | |
if [[ $version =~ [[:digit:]]*\.?.* ]] ; then | |
release_dir="$svn_branch_dir/release-$release_ver" | |
else | |
release_dir="$svn_branch_dir/$release_ver" | |
fi | |
cd $release_dir | |
svn merge -c $rev http://framework.zend.com/svn/framework/standard/trunk | |
cd $OLDPWD | |
cd "$release_dir/tests" | |
verbose 1 "Prepare Unit Test on release" | |
read -e -i "${components//Zend_/}" -p "Unit Test components: " unit_tests | |
./runtests.sh $unit_tests | |
ask_proceed | |
if [[ 1 -eq $? ]] ; then | |
exit 0 | |
fi | |
cd $OLDPWD | |
cd $release_dir | |
ci_msg="ZF-$issue: merge r$ci_rev to $release_ver release branch" | |
ask_proceed ">> svn commit -m '$ci_msg'" | |
if [[ 1 -eq $? ]] ; then | |
exit 0 | |
fi | |
svn commit -m "$ci_msg" | |
cd $OLDPWD | |
} | |
# Ok here it goes | |
if [[ $action = import ]] ; then | |
import_from_svn $git_branch | |
elif [[ $action = export ]] ; then | |
export_to_svn $git_branch | |
elif [[ $action = commit ]] ; then | |
commit $git_branch | |
elif [[ $action = release ]] ; then | |
release $git_branch | |
fi | |
exit 0 |
weierophinney, thanks for the advice, you know, git is hard to understand for a versioning newbie – and this way i am sure not to mess something in zf svn trunk.
As time passes i will be able to handle git and git-svn as well :) i will take your comment as a starting point.
You might want to look at a post I did on cloning the ZF svn repo using git-svn.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe I'm missing something important about why you're using this particular approach, but I have to ask the question, why not just use git-svn, and have some git hooks for things like issue numbers, etc? Doing so allows you to selectively pull in branches to which you need to commit, and push in changes upstream to svn, all within your git repository, using native git tools.