Skip to content

Instantly share code, notes, and snippets.

@padawin
Last active August 29, 2015 14:11
Show Gist options
  • Save padawin/9899f82913d237c1fd5c to your computer and use it in GitHub Desktop.
Save padawin/9899f82913d237c1fd5c to your computer and use it in GitHub Desktop.
migrate svn branch to git
#!/bin/sh
BRANCH=$1
TARGET=$2
BRANCH_FIRST_COMMIT=$3
# Checks
[ ! -d .git ] && echo "You must be in a git project" 1>&2 && exit 1
[ -z "$BRANCH" ] && echo "A branch is needed" 1>&2 && exit 1
[ -z "$TARGET" ] && echo "A target is needed" 1>&2 && exit 1
# Go on the desired branch
git checkout $BRANCH
# If fails, exits
[ ! $? -eq 0 ] && echo "$BRANCH is not a valid git branch" 1>&2 && exit 1
# If a start commit is provided, use this one as oldest commit of the branch
# other wise use the first commit of the branch
if [ -z $3 ]
then
BRANCH_FIRST_COMMIT=`git rev-list $BRANCH | tail -n 1`
fi
COMMIT=$TARGET
DIFF='1'
# Go through all the commits of the target to find one where the diff
# between this commit and the branch first commit is null
# which means the branch has been created from this commit
while [ ! -z "$DIFF" ]
do
COMMIT=`git rev-list $COMMIT^ | head -n 1`
DIFF=`git diff $BRANCH_FIRST_COMMIT $COMMIT | head -n 1`
done
# Create a new branch from the found commit
git branch tmp-$BRANCH $COMMIT
git checkout tmp-$BRANCH
# Cherry-pick all the commits between BRANCH_FIRST_COMMIT and the branch's last commit
git rev-list $BRANCH_FIRST_COMMIT..$BRANCH | tac | awk 'NR > 0 { print }' | xargs -I [] sh -c "git cherry-pick []"
# Rename the branches
git checkout $BRANCH
git branch -m $BRANCH-old
git checkout tmp-$BRANCH
git branch -m $BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment