-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove-subtree.sh
90 lines (84 loc) · 3.57 KB
/
move-subtree.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
SUBTREE=$1
NEW_PROJECT=$2
CURRENT_PROJECT=$PWD
PROCESSORS=$3
mkdir -p $NEW_PROJECT
pushd $NEW_PROJECT
git init
popd
if [ -n "$SUBTREE" ]; then
for BRANCH in `git branch -r`; do
echo "############################################################"
echo "## Moving branch '$BRANCH' from subtree '$SUBTREE'"
echo "############################################################"
{ git branch | grep "^\s*$SUBTREE-local-$BRANCH$"; } && git branch -D $SUBTREE-local-$BRANCH
{ git branch | grep "^\s*$SUBTREE-checkout-$BRANCH$"; } && git branch -D $SUBTREE-checkout-$BRANCH
{ git branch | grep "^\s*$SUBTREE-export-$BRANCH$"; } && git branch -D $SUBTREE-export-$BRANCH
git reset --hard HEAD
git checkout -b $SUBTREE-local-$BRANCH remotes/$BRANCH
if [ -d "$SUBTREE" ]; then
git subtree split -P $SUBTREE -b $SUBTREE-export-$BRANCH
pushd $NEW_PROJECT
git fetch $CURRENT_PROJECT $SUBTREE-export-$BRANCH
git checkout -b $BRANCH FETCH_HEAD
popd
fi
git reset --hard HEAD
git checkout master
{ git branch | grep "^\s*$SUBTREE-local-$BRANCH$"; } && git branch -D $SUBTREE-local-$BRANCH
{ git branch | grep "^\s*$SUBTREE-checkout-$BRANCH$"; } && git branch -D $SUBTREE-checkout-$BRANCH
{ git branch | grep "^\s*$SUBTREE-export-$BRANCH$"; } && git branch -D $SUBTREE-export-$BRANCH
done
else
for BRANCH in `git branch -r`; do
echo "############################################################"
echo "## Moving branch '$BRANCH' from subtree '$SUBTREE'"
echo "############################################################"
{ git branch | grep "^\s*local-$BRANCH$"; } && git branch -D local-$BRANCH
git checkout -b local-$BRANCH remotes/$BRANCH
pushd $NEW_PROJECT
git fetch $CURRENT_PROJECT local-$BRANCH
git checkout -b $BRANCH FETCH_HEAD
popd
{ git branch | grep "^\s*local-$BRANCH$"; } && git branch -D local-$BRANCH
done
fi
for PROCESSOR in $PROCESSORS; do
if [ -n "$PROCESSOR" -a -f "$PROCESSOR" ]; then
pushd $NEW_PROJECT
for BRANCH in `git branch -l | sed -r 's/^.{2}//'`; do
echo "############################################################"
echo "## Processing script '$PROCESSOR' on branch '$BRANCH' from subtree '$SUBTREE'"
echo "############################################################"
git filter-branch --force --prune-empty --tree-filter ". $PROCESSOR" $BRANCH
done
popd
fi
done
for TAG in `git tag -l`; do
echo "##################################################"
echo "## Tagging '$TAG' from subtree '$SUBTREE'"
echo "##################################################"
if [ -d "$SUBTREE" -o -z "$SUBTREE" ]; then
TAG_TIME=`git log tags/$TAG -1 --date=raw | grep '^Date:' | awk '{print $2; }'`
TAG_REF=`git log tags/$TAG -1 | grep '^commit' | awk '{ print $2; }'`
TAG_BRANCH=`git branch -r --contains $TAG_REF`
TAG_MESSAGE_FILE=/tmp/move-subtree-tag-message-$$
git log tags/$TAG -1 --pretty='format:%s' >$TAG_MESSAGE_FILE
tag_name=$( git log tags/$TAG -1 --pretty="format:%an" )
tag_email=$( git log tags/$TAG -1 --pretty="format:%ae" )
tag_date=$( git log tags/$TAG -1 --pretty="format:%ai" )
pushd $NEW_PROJECT
TAG_TARGET=`git log $TAG_BRANCH -1 --until=$TAG_TIME | grep '^commit' | awk '{ print $2; }'`
GIT_COMMITTER_NAME="$tag_name" GIT_COMMITTER_EMAIL="$tag_email" GIT_COMMITTER_DATE="$tag_date" git tag -a -F $TAG_MESSAGE_FILE $TAG $TAG_TARGET
popd
rm $TAG_MESSAGE_FILE
fi
done
echo "##################################################"
echo "## Moving trunk to master on subtree '$SUBTREE'"
echo "##################################################"
pushd $NEW_PROJECT
git branch -m trunk master
popd