This repository has been archived by the owner on Sep 3, 2019. It is now read-only.
forked from msysgit/msysgit
/
merging-rebase.sh
executable file
·155 lines (143 loc) · 3.32 KB
/
merging-rebase.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
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/sh
#
# Rebase 'master' on top of an upstream branch (defaults to 'junio/next'),
# retaining "fast-forwardability" by "merging" (with the "ours" strategy) the
# previous state on top of the current upstream state.
#
# options:
# --dry-run
# do not perform the rebase but only display the revisions selected
#
# --graph
# as for dry-run but display the commits as a graph
#
# --cherry
# as for --dry-run but display the commits using cherry notation to mark
# commits that are suitable for upstream consideration.
force=
dryrun=
graph=
cherry=
while test $# -gt 0
do
case "$1" in
-f|--force)
force=t
;;
-s|--show|-d|--dry-run)
dryrun=t
;;
-g|--graph)
dryrun=t
graph=--graph
;;
-c|--cherry)
dryrun=t
cherry=--cherry
;;
-h|--help)
cat >&2 << EOF
Usage: $0 [options] [<upstream>]
Options:
-s|--show show which commits would be cherry-picked
-g|--graph like --show, but with the commit graph
-c|--cherry try to leave out commits that were applied upstream
EOF
exit 1
;;
-*)
echo "Unknown option: $1" >&2
exit 1
;;
*)
break
;;
esac
shift
done
TO=${1:-junio/next}
if test -z "$force" && test -z "$(git rev-list .."$TO")"
then
echo "Nothing new in $TO; To force a rebase, use the --force, Luke!" >&2
exit 1
fi
HEAD_NAME="$(git rev-parse --symbolic-full-name HEAD)"
case "$HEAD_NAME" in
refs/heads/*)
UPSTREAM=$(git rev-parse --symbolic-full-name HEAD@{u}) || {
echo "Not tracking any remote branch!" >&2
exit 1
}
test "$(git rev-parse HEAD)" = "$(git rev-parse $UPSTREAM)" ||
test "$(git rev-parse $HEAD_NAME@{1})" = "$(git rev-parse $UPSTREAM)" || {
echo "Your '$HEAD_NAME' is not up-to-date!" >&2
exit 1
}
;; # okay
HEAD) ;; # okay
*)
echo "Not on any branch!" >&2
exit 1
;;
esac
FROM_SHA1=$(git rev-parse --short HEAD)
TO_SHA1=$(git rev-parse --short $TO)
is_ours_merge () {
test "$(git rev-parse $1:)" = "$(git rev-parse "$1^:")"
}
list_merges () {
git rev-list --parents "$@" | sed -n 's/ .* .*//p'
}
# Find old merging rebase, if any
REBASING_BASE=
for commit in $(list_merges $TO..)
do
if is_ours_merge $commit
then
subject="$(git show -s --format=%s $commit)"
case "$subject" in
*merging-rebase*) ;;
*)
printf "%s\n\n%s\n%s\n\n(y/n) " \
"Is this the latest merging rebase?" \
$commit "$subject"
read answer
case "$answer" in
y*|Y*) ;;
*) continue;;
esac;;
esac
REBASING_BASE=$commit
break
fi
done
BASE_MESSAGE=
if test -n "$REBASING_BASE"
then
BASE_MESSAGE="using $REBASING_BASE as base."
MESSAGE="$(git cat-file commit $REBASING_BASE |
sed '1,/^$/d')"
# old style rebasing merge?
case "$MESSAGE" in
"Rebasing merge to "*)
BASE_MESSAGE="using the rebasing merge $REBASING_BASE."
# Fake a commit such that 'git log <commit>..' shows all the
# commits we want to rebase
PREVIOUS_REBASING="$(git rev-parse $REBASING_BASE^2)"
PREVIOUS_ONTO="$(echo "$MESSAGE" |
sed -n '1s/Rebasing merge .*(\(.*\))/\1/p')"
# To avoid checking out unneeded files/file versions, the
# throw-away base points to the tree we want to rebase onto
REBASING_BASE="$(echo Dummy |
git commit-tree $TO_SHA1: \
-p $PREVIOUS_ONTO -p $PREVIOUS_REBASING)"
;;
esac
fi
RANGE=$REBASING_BASE..
if test -n "$dryrun"
then
git log --oneline $graph $cherry --boundary $RANGE
exit
fi
exec "$(dirname "$0")"/shears.sh --merging --onto=$TO ${REBASING_BASE:-$TO}