-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-redo-merge
More file actions
executable file
·104 lines (81 loc) · 2.66 KB
/
git-redo-merge
File metadata and controls
executable file
·104 lines (81 loc) · 2.66 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
#!/usr/bin/env bash
prog="$(basename $0)"
if [ $# -gt 0 ]; then
cat <<EOF
Usage:
\$ $prog
Redo a merge after a failed attempt to push it due to
the tracked upstream branch being ahead.
Example:
(master)\$ git merge topic
(master)\$ git push upstream master
FAIL.
! [rejected] master -> master (non-fast-forward)
(master)\$ $prog
(master)\$ git push upstream master
Ok
EOF
exit 1
fi
curr_branch="$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)"
if [ "$curr_branch" == "" ]; then
echo "$prog ERROR: No branch checked out. You are on a detached HEAD."
exit 1
fi
if ! git diff --quiet --exit-code; then
echo "$prog ERROR: Repo is dirty. You have unstaged changes."
exit 1
fi
if ! git diff --staged --quiet --exit-code HEAD; then
echo "$prog ERROR: Repo is dirty. You have staged changes."
exit 1
fi
remote_branch="$(git rev-parse --symbolic-full-name @{u} 2> /dev/null)"
if [ $? -ne 0 -o "$remote_branch" == "" ]; then
echo "$prog ERROR: Branch '$curr_branch' is not tracking any remote branch."
exit 1
fi
echo "$prog: Using remote bransh '$remote_branch'"
old_ours="$(git rev-parse --verify --quiet HEAD^1)"
if [ $? -ne 0 -o "$old_ours" == "" ]; then
echo "$prog ERROR: HEAD does not even have a first parent???"
exit 1
fi
old_theirs="$(git rev-parse --verify --quiet HEAD^2)"
if [ $? -ne 0 -o "$old_theirs" == "" ]; then
echo "$prog ERROR: HEAD is not a merge commit."
exit 1
fi
if git rev-parse --verify --quiet HEAD^3; then
echo "$prog ERROR: HEAD is an octopus merge."
exit 1
fi
if ! git merge-base --is-ancestor $old_ours $remote_branch; then
echo "$prog ERROR: First parent HEAD^1 is not an ancestor of $remote_branch."
exit 1
fi
if git merge-base --is-ancestor $remote_branch $curr_branch; then
echo "$prog ERROR: $remote_branch is reachable from $curr_branch. They have not diverged."
exit 1
fi
if git merge-base --is-ancestor $curr_branch $remote_branch; then
echo "$prog ERROR: $curr_branch is reachable from $remote_branch. They have not diverged."
exit 1
fi
old_head="$(git rev-parse --verify --quiet HEAD)"
if [ $? -ne 0 -o "$old_head" == "" ]; then
echo "$prog ERROR: Cannot rev-parse HEAD???"
exit 1
fi
if ! git merge --no-edit $remote_branch; then
echo
echo "$prog:"
echo -e "\tFailed to merge $remote_branch due to conflicts."
echo -e "\tEither solve conflicts, commit merge and do the fixup-merge yourself"
echo -e "\tor do \"git merge --abort\" to get back where you started."
exit 1
fi
# Fixup merge
git reset --soft $remote_branch
git update-ref MERGE_HEAD $old_theirs
git commit --reuse-message=$old_head