-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgit-rebase-via-merge.sh
More file actions
executable file
·177 lines (142 loc) · 4.76 KB
/
git-rebase-via-merge.sh
File metadata and controls
executable file
·177 lines (142 loc) · 4.76 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
#
# https://github.com/capslocky/git-rebase-via-merge
default_base_branch="origin/develop"
base_branch=${1:-$default_base_branch}
export GIT_ADVICE=0
set -e
main() {
echo "This script will perform a rebase via merge."
echo
init
git checkout --quiet "$current_branch_hash" # checkout to detached head (no branch, only commit)
git merge "$base_branch" -m "Hidden orphaned commit with merge result." || true
echo
if [ -n "$(get_unstaged_files)" ]; then
prompt_user_to_fix_conflicts
fi
hidden_result_hash=$(get_hash HEAD)
echo "Merge succeeded. The target state is: $hidden_result_hash"
echo "Starting rebase. Any conflicts will be resolved automatically."
echo
git checkout --quiet "$current_branch"
git rebase "$base_branch" -X theirs 2>/dev/null || true # here option 'theirs' means choosing our changes.
while [ -n "$(get_unstaged_files)" ]; do
echo "File-level conflict detected. Removing their file, keeping ours." # e.g. parallel file rename
git status --porcelain
git status --porcelain | grep -E "^(DD|AU|UD) " | cut -c4- | xargs -r git rm --
git add -A
echo
git -c core.editor=true rebase --continue 2>/dev/null || true # suppressing opening commit message editor
done
current_tree=$(git cat-file -p HEAD | grep "^tree")
result_tree=$(git cat-file -p "$hidden_result_hash" | grep "^tree")
if [ "$current_tree" != "$result_tree" ]; then
echo "Restoring the project state from the hidden result with one additional commit."
echo
additional_commit_message="Rebase via merge. '$current_branch' rebased on '$base_branch'."
additional_commit_hash=$(git commit-tree $hidden_result_hash^{tree} -p HEAD -m "$additional_commit_message")
git merge --ff "$additional_commit_hash"
# uncomment if you want to exclude additional commits completely:
# git reset --soft HEAD~1
# git commit --amend --no-edit
echo
fi
echo "Done. Current branch:"
echo "$(git branch --show-current) ($(get_hash HEAD))"
show_commit HEAD
exit 0
}
init() {
if [ -d "$(git rev-parse --git-path rebase-merge)" ]; then
echo "Can't rebase. Rebase in progress detected. Continue or abort existing rebase."
exit 1
fi
if [ -f "$(git rev-parse --git-path MERGE_HEAD)" ]; then
echo "Can't rebase. Merge in progress detected. Continue or abort existing merge."
exit 1
fi
current_branch=$(git branch --show-current)
current_branch_hash=$(get_hash "$current_branch")
base_branch_hash=$(get_hash "$base_branch")
if [ -z "$current_branch" ]; then
echo "Can't rebase. There is no current branch: detached head."
exit 1
fi
if [ -z "$base_branch_hash" ]; then
echo "Can't rebase. Base branch '$base_branch' not found."
exit 1
fi
echo "Current branch:"
echo "$current_branch ($current_branch_hash)"
show_commit "$current_branch_hash"
echo
echo "Base branch:"
echo "$base_branch ($base_branch_hash)"
show_commit "$base_branch_hash"
echo
if [ -n "$(get_any_changed_files)" ]; then
echo "Can't rebase. You need to commit changes in the following files:"
echo
get_any_changed_files
exit 1
fi
if [ "$base_branch_hash" = "$current_branch_hash" ]; then
echo "Can't rebase. Current branch is equal to the base branch."
exit 1
fi
if [ -z "$(git rev-list "$base_branch" ^"$current_branch")" ]; then
echo "Can't rebase. Current branch is already rebased."
exit 1
fi
if [ -z "$(git rev-list ^"$base_branch" "$current_branch")" ]; then
echo "Can't rebase. Current branch has no any unique commits. You can do fast-forward merge."
exit 1
fi
echo "Continue (c) / Abort (a)"
read input
if [ "$input" != "c" ]; then
echo "Aborted."
exit 1
fi
}
prompt_user_to_fix_conflicts() {
echo "Fix all conflicts in the following files, stage all changes, do not commit, and type 'c':"
get_unstaged_files
echo
while true; do
echo "Continue merge (c) / Abort merge (a)"
read input
echo
if [ "$input" = "c" ]; then
if [ -n "$(get_unstaged_files)" ]; then
echo "There are still unstaged files:"
get_unstaged_files
echo
else
git -c core.editor=true merge --continue # suppressing opening commit message editor
break
fi
elif [ "$input" = "a" ]; then
echo "Aborting merge."
git merge --abort
git checkout "$current_branch"
exit 2
else
echo "Invalid option: $input"
fi
done
}
get_any_changed_files() {
git status --porcelain --ignore-submodules=dirty | cut -c4-
}
get_unstaged_files() {
git status --porcelain --ignore-submodules=dirty | grep -v "^. " | cut -c4-
}
get_hash() {
git rev-parse --short "$1" 2>/dev/null || true
}
show_commit() {
git log -n 1 --pretty=format:"%<(20)%an | %<(14)%ar | %s" "$1"
}
main