git-rebase.sh 8.5 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 54 55 56 57

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

	unmerged=$(git-ls-files -u)
	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
	if ! git-diff-index --quiet HEAD
63
	then
64 65 66 67 68 69
		if ! git-commit -C "`cat $dotest/current`"
		then
			echo "Commit failed, please do not call \"git commit\""
			echo "directly, but instead do one of the following: "
			die "$RESOLVEMSG"
		fi
70
		printf "Committed: %0${prec}d" $msgnum
71
	else
72
		printf "Already applied: %0${prec}d" $msgnum
73
	fi
74 75
	echo ' '`git-rev-list --pretty=oneline -1 HEAD | \
				sed 's/^[a-f0-9]\+ //'`
76 77 78 79 80 81 82

	prev_head=`git-rev-parse HEAD^0`
	# 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 91 92 93 94 95 96
	hd=$(git-rev-parse --verify HEAD)
	cmt_name=$(git-symbolic-ref HEAD)
	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 104
		;;
	1)
		test -d "$GIT_DIR/rr-cache" && 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 138
		git-diff-files --quiet || {
			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 164 165 166
			if test -d "$GIT_DIR/rr-cache"
			then
				git-rerere clear
			fi
167 168 169 170 171 172 173 174 175 176 177 178
			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
179
		fi
180
		git am -3 --skip --resolvemsg="$RESOLVEMSG"
181 182 183
		exit
		;;
	--abort)
184 185 186 187
		if test -d "$GIT_DIR/rr-cache"
		then
			git-rerere clear
		fi
188 189 190 191 192 193 194 195 196
		if test -d "$dotest"
		then
			rm -r "$dotest"
		elif test -d .dotest
		then
			rm -r .dotest
		else
			die "No rebase in progress?"
		fi
197 198 199
		git reset --hard ORIG_HEAD
		exit
		;;
200 201 202 203 204
	--onto)
		test 2 -le "$#" || usage
		newbase="$2"
		shift
		;;
205 206 207 208 209 210 211 212
	-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 已提交
213
			strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
214 215 216 217 218 219 220 221
		1,*)
			usage ;;
		*)
			strategy="$2"
			shift ;;
		esac
		do_merge=t
		;;
222 223 224
	-v|--verbose)
		verbose=t
		;;
M
Michael S. Tsirkin 已提交
225 226 227 228
	-C*)
		git_am_opt=$1
		shift
		;;
229 230 231 232 233 234 235 236 237
	-*)
		usage
		;;
	*)
		break
		;;
	esac
	shift
done
238

239
# Make sure we do not have .dotest
240
if test -z "$do_merge"
241
then
242 243 244 245 246
	if mkdir .dotest
	then
		rmdir .dotest
	else
		echo >&2 '
247 248 249 250
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.'
251 252 253 254 255 256 257 258
		exit 1
	fi
else
	if test -d "$dotest"
	then
		die "previous dotest directory $dotest still exists." \
			'try git-rebase < --continue | --abort >'
	fi
259 260
fi

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

271 272 273
# 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 已提交
274
    die "invalid upstream $upstream_name"
L
Lukas Sandström 已提交
275

276 277 278 279
# Make sure the branch to rebase onto is valid.
onto_name=${newbase-"$upstream_name"}
onto=$(git-rev-parse --verify "${onto_name}^0") || exit

280 281 282 283 284 285 286 287 288
# 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

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

306 307 308 309
# Now we are rebasing commits $upstream..$branch on top of $onto

# Check if we are already based on $onto, but this should be
# done only when upstream and onto are the same.
310 311
mb=$(git-merge-base "$onto" "$branch")
if test "$upstream" = "$onto" && test "$mb" = "$onto"
312
then
313 314
	echo >&2 "Current branch $branch_name is up to date."
	exit 0
315 316
fi

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

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

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

336 337
if test -z "$do_merge"
then
338
	git-format-patch -k --stdout --full-index --ignore-if-in-upstream "$upstream"..ORIG_HEAD |
M
Michael S. Tsirkin 已提交
339
	git am $git_am_opt --binary -3 -k --resolvemsg="$RESOLVEMSG"
340 341 342 343 344 345 346 347
	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"
348
echo "$onto_name" > "$dotest/onto_name"
349 350 351 352
prev_head=`git-rev-parse HEAD^0`
echo "$prev_head" > "$dotest/prev_head"

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

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

end=$msgnum
msgnum=1

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

371
finish_rb_merge