-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-squash-merge
More file actions
executable file
·103 lines (88 loc) · 2.2 KB
/
git-squash-merge
File metadata and controls
executable file
·103 lines (88 loc) · 2.2 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
#!/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 "Convert merge commit at HEAD into a single merge commit"
echo "on top of <onto-commit> (by squashing all commits between HEAD and <onto-commit>)."
echo
echo "Syntax: git-squash-merge <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
onto=$1
onto_sha=$(git rev-parse "$onto")
HEAD1=$(git rev-parse HEAD^1)
HEAD2=$(git rev-parse HEAD^2)
if [ $? -ne 0 ]; then
echo
echo "ERROR: HEAD is not a merge commit"
echo
exit 1
fi
if git rev-parse HEAD^3 >& /dev/null; then
echo
echo "ERROR: HEAD has more than 2 parents"
echo
exit 1
fi
if [ "$onto_sha" = "$HEAD1" ]; then
echo
echo "ERROR: '$onto' already (first) parent of HEAD."
echo " No commits to squash."
echo
exit 1
fi
if [ "$onto_sha" = "$HEAD2" ]; then
echo
echo "ERROR: '$onto' already (second) parent of HEAD."
echo " No commits to squash."
echo
exit 1
fi
HEAD_MSG=$(git log -1 --format="%B" HEAD)
if git merge-base --is-ancestor $onto $HEAD1; then
if git merge-base --is-ancestor $onto $HEAD2; then
echo
echo "ERROR: '$onto' is reachable from both HEAD^1 and HEAD^2."
echo " Don't know what to do."
echo
exit 1
fi
SQUASH_LOG=$(git log --format="%n%H %an%n%B" --ancestry-path $onto..$HEAD1)
#git-create-merge $onto $HEAD2
run git reset --soft $onto
run git update-ref MERGE_HEAD $HEAD2
git commit --edit -m "$HEAD_MSG" -m "SQUASHED commits:" -m "$SQUASH_LOG"
elif git merge-base --is-ancestor $onto $HEAD2; then
SQUASH_LOG=$(git log --format="%n%H %an%n%B" --ancestry-path $onto..$HEAD2)
#git-create-merge $HEAD1 $onto
run git reset --soft $HEAD1
run git update-ref MERGE_HEAD $onto
git commit --edit -m "$HEAD_MSG" -m "SQUASHED commits:" -m "$SQUASH_LOG"
else
echo
echo "ERROR: '$onto' is not reachable from either HEAD^1 or HEAD^2."
echo
exit 1
fi