Skip to content

Instantly share code, notes, and snippets.

@tomasfejfar
Last active July 29, 2019 07:51
Show Gist options
  • Save tomasfejfar/1c7c0159092fceeca02520d3654deebe to your computer and use it in GitHub Desktop.
Save tomasfejfar/1c7c0159092fceeca02520d3654deebe to your computer and use it in GitHub Desktop.
"rebase" onto codebase that moved to different directory
#! /bin/bash
# usage: rebase-to-moved.sh my-branch
# will create tag backup of current branch and rebase it on a moved directory
if [ -n "$(git status --porcelain)" ]; then
echo "You need to have clean working copy"
exit 1
fi;
git checkout $1
git merge-base --is-ancestor 250d09f7e5ef64f7e8c449e2ea3f5636b0f8ac57 $1
if [[ $? -ne 0 ]]; then
echo "Please run 'git rebase -i 250d09f7e5ef64f7e8c449e2ea3f5636b0f8ac57' first"
exit 1
fi
git merge-base --is-ancestor 8195138a5cab7a00715503fb86cb4c769e48be91 $1
if [[ $? -eq 0 ]]; then
echo "Everything is already OK"
exit 1
fi
set -e
CD=$( dirname "${BASH_SOURCE[0]}" )
rm -rf $CD/patches
git format-patch -M -C --output-directory "$CD/patches" 8195138a5cab7a00715503fb86cb4c769e48be91..$1
ls -d $CD/patches/* | xargs -n1 -I {} sh -c "sed -i -r 's@diff --git a/([^ ]*) b/([^ ]*)@diff --git a/legacy-app/\1 b/legacy-app/\2@' {}"
ls -d $CD/patches/* | xargs -n1 -I {} sh -c "sed -i -r 's@--- a/([^ ]*)@--- a/legacy-app/\1@' {}"
ls -d $CD/patches/* | xargs -n1 -I {} sh -c "sed -i -r 's@\+\+\+ b/([^ ]*)@+++ b/legacy-app/\1@' {}"
ls -d $CD/patches/* | xargs -n1 -I {} sh -c "sed -i -r 's@([^ ]*) mode ([^ ]*) ([^ ]*)@\1 mode \2 legacy-app/\3@' {}"
ls -d $CD/patches/* | xargs -n1 -I {} sh -c "sed -i -r 's@rename (from|to)? ?([^ ]*)@rename \1 legacy-app/\2@' {}"
git tag $1-backup $1 || true
git checkout $1
git reset --hard 8195138a5cab7a00715503fb86cb4c769e48be91
git am $CD/patches/*
@tomasfejfar
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment