-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-trees.sh
142 lines (132 loc) · 4.53 KB
/
merge-trees.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
PROJECT1=$1
PROJECT2=$2
NEW_PROJECT=$3
BRANCH1=$4
BRANCH2=$5
NEW_BRANCH=$6
FORMAT_PATCH_OPTIONS=""
AM_OPTION="--ignore-white-space"
fromdos="fromdos -a -d"
pushd $PROJECT1 >/dev/null
git reset --hard HEAD >/dev/null
git checkout $BRANCH1
git reset --hard HEAD >/dev/null
popd >/dev/null
pushd $PROJECT2 >/dev/null
git reset --hard HEAD >/dev/null
git checkout $BRANCH2
git reset --hard HEAD >/dev/null
popd >/dev/null
mkdir -p $NEW_PROJECT
pushd $NEW_PROJECT >/dev/null
if [ ! -d "$NEW_PROJECT/.git" ]; then
git init >/dev/null
else
echo ref: refs/heads/$NEW_BRANCH >.git/HEAD
ls -a1 | egrep -v '^(\.|\.\.|\.git)$' | xargs rm -rf
fi
popd >/dev/null
remove_carriage_returns() {
for FILE in `git ls-files`; do
$fromdos $FILE
done
}
pushd $PROJECT1 >/dev/null
for GIT_COMMIT in `{ echo "after"; git log --pretty='format:%H'; } | cat -n | sort -nr | awk '{print $2}'`; do
if [ "$GIT_COMMIT" = "after" ]; then
ANCESTORS=1
else
ANCESTORS=`git log -2 --pretty='format:%H' $GIT_COMMIT | wc -l`
fi
if [ "$ANCESTORS" -eq 0 ]; then
git archive $GIT_COMMIT | tar -x -C $NEW_PROJECT/
TAG_TIME=`git log $GIT_COMMIT -1 --date=raw | grep '^Date:' | awk '{ print $2; }'`
TAG_MESSAGE_FILE=/tmp/tag-message-$$
git log $GIT_COMMIT -1 --pretty='format:%s' >$TAG_MESSAGE_FILE
tag_name=` git log $GIT_COMMIT -1 --pretty="format:%an"`
tag_email=`git log $GIT_COMMIT -1 --pretty="format:%ae"`
tag_date=`git log $GIT_COMMIT -1 --pretty="format:%ai"`
pushd $NEW_PROJECT >/dev/null
git add -A
remove_carriage_returns
git add -A
git commit -F $TAG_MESSAGE_FILE
git filter-branch -f --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ];
then
GIT_AUTHOR_NAME="'"$tag_name"'";
GIT_AUTHOR_EMAIL="'"$tag_email"'";
GIT_AUTHOR_DATE="'"$tag_date"'";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD
popd >/dev/null
else
if [ "$GIT_COMMIT" != "after" ]; then
PREVIOUS_COMMIT="$GIT_COMMIT^1"
PREVIOUS_TIME=`git log $PREVIOUS_COMMIT -1 --date=raw | grep '^Date:' | awk '{print $2; }'`
COMMIT_TIME=`git log $GIT_COMMIT -1 --date=raw | grep '^Date:' | awk '{print $2; }'`
fi
pushd $PROJECT2 >/dev/null
if [ "$GIT_COMMIT" != "after" ]; then
INTRODUCE_COMMITS=`git log --after=$PREVIOUS_TIME --before=$COMMIT_TIME --pretty='format:%H' | cat -n | sort -nr | awk '{print $2}'`
else
INTRODUCE_COMMITS=`git log --after=$COMMIT_TIME --pretty='format:%H' | cat -n | sort -nr | awk '{print $2}'`
fi
if [ -n "$INTRODUCE_COMMITS" ]; then
for INTRODUCE_COMMIT in $INTRODUCE_COMMITS; do
ANCESTORS=`git log -2 --pretty='format:%H' $INTRODUCE_COMMIT | wc -l`
if [ "$ANCESTORS" -eq 0 ]; then
git archive $INTRODUCE_COMMIT | tar -x -C $NEW_PROJECT/
TAG_TIME=`git log $INTRODUCE_COMMIT -1 --date=raw | grep '^Date:' | awk '{ print $2; }'`
TAG_MESSAGE_FILE=/tmp/tag-message-$$
git log $INTRODUCE_COMMIT -1 --pretty='format:%s' >$TAG_MESSAGE_FILE
tag_name=` git log $INTRODUCE_COMMIT -1 --pretty="format:%an"`
tag_email=`git log $INTRODUCE_COMMIT -1 --pretty="format:%ae"`
tag_date=`git log $INTRODUCE_COMMIT -1 --pretty="format:%ai"`
pushd $NEW_PROJECT >/dev/null
git add -A
remove_carriage_returns
git add -A
git commit -F $TAG_MESSAGE_FILE
git filter-branch -f --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ];
then
GIT_AUTHOR_NAME="'"$tag_name"'";
GIT_AUTHOR_EMAIL="'"$tag_email"'";
GIT_AUTHOR_DATE="'"$tag_date"'";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD
popd >/dev/null
else
git format-patch $FORMAT_PATCH_OPTIONS --stdout $INTRODUCE_COMMIT^1..$INTRODUCE_COMMIT >/tmp/patch
pushd $NEW_PROJECT >/dev/null
$fromdos /tmp/patch
git am $AM_OPTIONS /tmp/patch || exit 1
popd >/dev/null
fi
done
fi
popd >/dev/null
if [ "$GIT_COMMIT" != "after" ]; then
git format-patch $FORMAT_PATCH_OPTIONS --stdout $GIT_COMMIT^1..$GIT_COMMIT >/tmp/patch
pushd $NEW_PROJECT >/dev/null
$fromdos /tmp/patch
git am $AM_OPTIONS /tmp/patch || exit 1
popd >/dev/null
fi
fi
done
popd >/dev/null
pushd $NEW_PROJECT >/dev/null
git filter-branch -f --commit-filter '
GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME";
GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL";
GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE";
git commit-tree "$@";
' HEAD
popd