-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-remerge
More file actions
executable file
·129 lines (109 loc) · 2.82 KB
/
git-remerge
File metadata and controls
executable file
·129 lines (109 loc) · 2.82 KB
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
#!/usr/bin/env bash
run() {
cmd=
while [ $# -gt 0 ]; do
cmd="$cmd $1"
shift
done
echo $cmd
eval $cmd
if [ $? -ne 0 ]; then
echo
echo "ERROR: '$cmd' failed!"
echo
exit 1
fi
}
if [ "$#" != 1 ]; then
echo
echo "Rebase current branch on <onto-commit> while keeping merge commits."
echo
echo "Syntax: git-remerge <onto-commit>"
echo
exit 1
fi
if ! git rev-parse $1 >& /dev/null; then
echo
echo "ERROR: Argument '$1' is not a known commit"
echo
exit 1
fi
curr_branch="$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)"
if [ "$curr_branch" = "HEAD" ]; then
echo "ERROR: Detached head"
exit 1
fi
onto=$1
if git merge-base --is-ancestor HEAD $onto; then
echo
echo "ERROR: '$curr_branch' is already reachable from '$onto'. Nothing to remerge."
echo
exit 1
fi
if git merge-base --is-ancestor $onto HEAD; then
echo
echo "ERROR: '$onto' is reachable from '$curr_branch'. Nothing to remerge."
echo
exit 1
fi
if ! git diff --quiet --exit-code; then
echo "Cannot remerge: You have unstaged changes."
echo "Please commit or stash them."
exit 1
fi
if ! git diff --cached --quiet --exit-code; then
echo "Cannot remerge: Your index contains uncommitted changes."
echo "Please commit or stash them."
exit 1
fi
git branch -D remerge/tmp1 >& /dev/null
git branch -D remerge/tmp2 >& /dev/null
git branch -D remerge/child1 >& /dev/null
#
# Loop down to find the common ancestor
#
parent="$(git rev-parse HEAD)"
old_commits=$parent
while true; do
echo "Commit to remerge: '$parent'"
child1=$(git rev-parse $parent^1)
if git merge-base --is-ancestor $child1 $onto; then
echo "Found remerge base '$child1'"
break;
fi
old_commits="$child1 $old_commits"
parent=$child1
done
#
# Loop up and do new merge commits
#
run git checkout -b remerge/tmp1 $onto
for sha in $old_commits; do
echo "Remerging commit $sha ..."
run git merge --no-commit $sha
run git commit --reuse-message=$sha
tmp_commits="$tmp_commits $(git rev-parse HEAD)"
done
#
# Loop up through new merge commits and fix children
#
run git checkout -b remerge/tmp2
child1=$onto
for sha in $tmp_commits; do
echo "Re-childing commit $sha ..."
run git branch -f remerge/child1 $child1
run git reset --hard $sha
run git reset --soft remerge/child1
if git rev-parse --verify --quiet $sha^2 >/dev/null; then
# A merge commit. Retain second parent of original merge commit.
run git update-ref MERGE_HEAD $sha^2^2
#else A normal commit. Omit second parent
fi
run git commit --reuse-message=$sha
child1="$(git rev-parse HEAD)"
done
run git branch -f $curr_branch
run git checkout $curr_branch
git branch -D remerge/tmp1 >& /dev/null
git branch -D remerge/tmp2 >& /dev/null
git branch -D remerge/child1 >& /dev/null