git-rebase.sh 8.6 KB
Newer Older
1 2 3 4 5
#!/bin/sh
#
# Copyright (c) 2005 Junio C Hamano.
#

6
USAGE='[--interactive | -i] [-v] [--onto <newbase>] <upstream> [<branch>]'
7 8 9 10 11
LONG_USAGE='git-rebase replaces <branch> with a new branch of the
same name.  When the --onto option is provided the new branch starts
out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
It then attempts to create a new commit for each commit from the original
<branch> that does not exist in the <upstream> branch.
12

13 14
It is possible that a merge failure will prevent this process from being
completely automatic.  You will have to resolve any such merge failure
15 16 17 18
and run git rebase --continue.  Another option is to bypass the commit
that caused the merge failure with git rebase --skip.  To restore the
original <branch> and remove the .dotest working files, use the command
git rebase --abort instead.
19

20 21 22
Note that if <branch> is not specified on the command line, the
currently checked out branch is used.  You must be in the top
directory of your project to start (or continue) a rebase.
23

24
Example:       git-rebase master~1 topic
25

26 27 28
        A---B---C topic                   A'\''--B'\''--C'\'' topic
       /                   -->           /
  D---E---F---G master          D---E---F---G master
29
'
30 31

SUBDIRECTORY_OK=Yes
32
. git-sh-setup
33
set_reflog_action rebase
34
require_work_tree
35
cd_to_toplevel
36

37 38 39 40 41
RESOLVEMSG="
When you have resolved this problem run \"git rebase --continue\".
If you would prefer to skip this patch, instead run \"git rebase --skip\".
To restore the original branch and stop rebasing run \"git rebase --abort\".
"
42
unset newbase
J
Junio C Hamano 已提交
43
strategy=recursive
44 45 46
do_merge=
dotest=$GIT_DIR/.dotest-merge
prec=4
47
verbose=
M
Michael S. Tsirkin 已提交
48
git_am_opt=
49 50 51 52 53

continue_merge () {
	test -n "$prev_head" || die "prev_head must be defined"
	test -d "$dotest" || die "$dotest directory does not exist"

54
	unmerged=$(git ls-files -u)
55 56 57
	if test -n "$unmerged"
	then
		echo "You still have unmerged paths in your index"
58
		echo "did you forget to use git add?"
59
		die "$RESOLVEMSG"
60 61
	fi

62
	cmt=`cat $dotest/current`
63
	if ! git diff-index --quiet HEAD
64
	then
65
		if ! git-commit -C "$cmt"
66 67 68 69 70
		then
			echo "Commit failed, please do not call \"git commit\""
			echo "directly, but instead do one of the following: "
			die "$RESOLVEMSG"
		fi
71
		printf "Committed: %0${prec}d " $msgnum
72
	else
73
		printf "Already applied: %0${prec}d " $msgnum
74
	fi
75
	git rev-list --pretty=oneline -1 "$cmt" | sed -e 's/^[^ ]* //'
76

77
	prev_head=`git rev-parse HEAD^0`
78 79 80 81 82
	# save the resulting commit so we can read-tree on it later
	echo "$prev_head" > "$dotest/prev_head"

	# onto the next patch:
	msgnum=$(($msgnum + 1))
83
	echo "$msgnum" >"$dotest/msgnum"
84 85 86
}

call_merge () {
87
	cmt="$(cat $dotest/cmt.$1)"
88
	echo "$cmt" > "$dotest/current"
89 90
	hd=$(git rev-parse --verify HEAD)
	cmt_name=$(git symbolic-ref HEAD)
91 92 93 94 95 96
	msgnum=$(cat $dotest/msgnum)
	end=$(cat $dotest/end)
	eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
	eval GITHEAD_$hd='"$(cat $dotest/onto_name)"'
	export GITHEAD_$cmt GITHEAD_$hd
	git-merge-$strategy "$cmt^" -- "$hd" "$cmt"
97 98 99
	rv=$?
	case "$rv" in
	0)
100
		unset GITHEAD_$cmt GITHEAD_$hd
101
		return
102 103
		;;
	1)
104
		git rerere
105
		die "$RESOLVEMSG"
106 107 108
		;;
	2)
		echo "Strategy: $rv $strategy failed, try another" 1>&2
109
		die "$RESOLVEMSG"
110 111 112 113 114 115 116 117 118 119 120 121 122
		;;
	*)
		die "Unknown exit code ($rv) from command:" \
			"git-merge-$strategy $cmt^ -- HEAD $cmt"
		;;
	esac
}

finish_rb_merge () {
	rm -r "$dotest"
	echo "All done."
}

123 124 125 126 127 128 129 130 131 132
is_interactive () {
	test -f "$dotest"/interactive ||
	while case $#,"$1" in 0,|*,-i|*,--interactive) break ;; esac
	do
		shift
	done && test -n "$1"
}

is_interactive "$@" && exec git-rebase--interactive "$@"

133 134 135
while case "$#" in 0) break ;; esac
do
	case "$1" in
136
	--continue)
137
		git diff-files --quiet || {
138
			echo "You must edit all merge conflicts and then"
139
			echo "mark them as resolved using git add"
140
			exit 1
141
		}
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		if test -d "$dotest"
		then
			prev_head="`cat $dotest/prev_head`"
			end="`cat $dotest/end`"
			msgnum="`cat $dotest/msgnum`"
			onto="`cat $dotest/onto`"
			continue_merge
			while test "$msgnum" -le "$end"
			do
				call_merge "$msgnum"
				continue_merge
			done
			finish_rb_merge
			exit
		fi
157
		git am --resolved --3way --resolvemsg="$RESOLVEMSG"
158 159 160
		exit
		;;
	--skip)
161 162
		if test -d "$dotest"
		then
163
			git rerere clear
164 165 166 167 168 169 170 171 172 173 174 175
			prev_head="`cat $dotest/prev_head`"
			end="`cat $dotest/end`"
			msgnum="`cat $dotest/msgnum`"
			msgnum=$(($msgnum + 1))
			onto="`cat $dotest/onto`"
			while test "$msgnum" -le "$end"
			do
				call_merge "$msgnum"
				continue_merge
			done
			finish_rb_merge
			exit
176
		fi
177
		git am -3 --skip --resolvemsg="$RESOLVEMSG"
178 179 180
		exit
		;;
	--abort)
181
		git rerere clear
182 183 184 185 186 187 188 189 190
		if test -d "$dotest"
		then
			rm -r "$dotest"
		elif test -d .dotest
		then
			rm -r .dotest
		else
			die "No rebase in progress?"
		fi
191 192 193
		git reset --hard ORIG_HEAD
		exit
		;;
194 195 196 197 198
	--onto)
		test 2 -le "$#" || usage
		newbase="$2"
		shift
		;;
199 200 201 202 203 204 205 206
	-M|-m|--m|--me|--mer|--merg|--merge)
		do_merge=t
		;;
	-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
		--strateg=*|--strategy=*|\
	-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
		case "$#,$1" in
		*,*=*)
D
Dennis Stosberg 已提交
207
			strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
208 209 210 211 212 213 214 215
		1,*)
			usage ;;
		*)
			strategy="$2"
			shift ;;
		esac
		do_merge=t
		;;
216 217 218
	-v|--verbose)
		verbose=t
		;;
219 220 221
	--whitespace=*)
		git_am_opt="$git_am_opt $1"
		;;
M
Michael S. Tsirkin 已提交
222
	-C*)
223
		git_am_opt="$git_am_opt $1"
M
Michael S. Tsirkin 已提交
224
		;;
225 226 227 228 229 230 231 232 233
	-*)
		usage
		;;
	*)
		break
		;;
	esac
	shift
done
234

235
# Make sure we do not have .dotest
236
if test -z "$do_merge"
237
then
238 239 240 241 242
	if mkdir .dotest
	then
		rmdir .dotest
	else
		echo >&2 '
243 244 245 246
It seems that I cannot create a .dotest directory, and I wonder if you
are in the middle of patch application or another rebase.  If that is not
the case, please rm -fr .dotest and run me again.  I am stopping in case
you still have something valuable there.'
247 248 249 250 251 252 253 254
		exit 1
	fi
else
	if test -d "$dotest"
	then
		die "previous dotest directory $dotest still exists." \
			'try git-rebase < --continue | --abort >'
	fi
255 256
fi

257
# The tree must be really really clean.
258 259
git update-index --refresh || exit
diff=$(git diff-index --cached --name-status -r HEAD)
L
Lukas Sandström 已提交
260
case "$diff" in
261 262
?*)	echo "cannot rebase: your index is not up-to-date"
	echo "$diff"
263 264 265
	exit 1
	;;
esac
J
Junio C Hamano 已提交
266

267 268 269
# The upstream head must be given.  Make sure it is valid.
upstream_name="$1"
upstream=`git rev-parse --verify "${upstream_name}^0"` ||
J
Jason Riedy 已提交
270
    die "invalid upstream $upstream_name"
L
Lukas Sandström 已提交
271

272 273
# Make sure the branch to rebase onto is valid.
onto_name=${newbase-"$upstream_name"}
274
onto=$(git rev-parse --verify "${onto_name}^0") || exit
275

276 277 278 279 280 281 282 283 284
# If a hook exists, give it a chance to interrupt
if test -x "$GIT_DIR/hooks/pre-rebase"
then
	"$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
		echo >&2 "The pre-rebase hook refused to rebase."
		exit 1
	}
fi

285
# If the branch to rebase is given, first switch to it.
286
case "$#" in
287
2)
288
	branch_name="$2"
289
	git-checkout "$2" || usage
290 291
	;;
*)
292 293 294 295 296 297
	if branch_name=`git symbolic-ref -q HEAD`
	then
		branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
	else
		branch_name=HEAD ;# detached
	fi
298
	;;
299
esac
300
branch=$(git rev-parse --verify "${branch_name}^0") || exit
301

302 303
# Now we are rebasing commits $upstream..$branch on top of $onto

304 305
# Check if we are already based on $onto with linear history,
# but this should be done only when upstream and onto are the same.
306
mb=$(git merge-base "$onto" "$branch")
307 308 309
if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
	# linear history?
	! git rev-list --parents "$onto".."$branch" | grep " .* " > /dev/null
310
then
311 312
	echo >&2 "Current branch $branch_name is up to date."
	exit 0
313 314
fi

315 316 317
if test -n "$verbose"
then
	echo "Changes from $mb to $onto:"
318
	# We want color (if set), but no pager
319
	GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
320 321
fi

322
# Rewind the head to "$onto"; this saves our current head in ORIG_HEAD.
323
echo "First, rewinding head to replay your work on top of it..."
324
git-reset --hard "$onto"
L
Lukas Sandström 已提交
325

326
# If the $onto is a proper descendant of the tip of the branch, then
L
Lukas Sandström 已提交
327
# we just fast forwarded.
328
if test "$mb" = "$branch"
L
Lukas Sandström 已提交
329
then
330
	echo >&2 "Fast-forwarded $branch_name to $onto_name."
L
Lukas Sandström 已提交
331 332 333
	exit 0
fi

334 335
if test -z "$do_merge"
then
336
	git format-patch -k --stdout --full-index --ignore-if-in-upstream "$upstream"..ORIG_HEAD |
M
Michael S. Tsirkin 已提交
337
	git am $git_am_opt --binary -3 -k --resolvemsg="$RESOLVEMSG"
338 339 340 341 342 343 344 345
	exit $?
fi

# start doing a rebase with git-merge
# this is rename-aware if the recursive (default) strategy is used

mkdir -p "$dotest"
echo "$onto" > "$dotest/onto"
346
echo "$onto_name" > "$dotest/onto_name"
347
prev_head=`git rev-parse HEAD^0`
348 349 350
echo "$prev_head" > "$dotest/prev_head"

msgnum=0
351
for cmt in `git rev-list --reverse --no-merges "$upstream"..ORIG_HEAD`
352 353
do
	msgnum=$(($msgnum + 1))
354
	echo "$cmt" > "$dotest/cmt.$msgnum"
355 356
done

357 358
echo 1 >"$dotest/msgnum"
echo $msgnum >"$dotest/end"
359 360 361 362 363 364 365 366 367

end=$msgnum
msgnum=1

while test "$msgnum" -le "$end"
do
	call_merge "$msgnum"
	continue_merge
done
368

369
finish_rb_merge