3
# Copyright (c) 2005 Junio C Hamano.
4
#
5
6
-USAGE='[--interactive | -i] [-v] [--onto <newbase>] <upstream> [<branch>]'
6
+USAGE='[--interactive | -i] [-v] [--onto <newbase>] [<upstream>|--root] [<branch>]'
7
LONG_USAGE='git-rebase replaces <branch> with a new branch of the
8
same name. When the --onto option is provided the new branch starts
9
out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
47
prec=4
48
verbose=
49
git_am_opt=
50
+rebase_root=
51
52
continue_merge () {
53
test -n "$prev_head" || die "prev_head must be defined"
298
-C*)
299
git_am_opt="$git_am_opt $1"
300
;;
301
+ --root)
302
+ rebase_root=t
303
+ ;;
304
-*)
305
usage
306
;;
348
;;
349
esac
350
347
-# The upstream head must be given. Make sure it is valid.
348
-upstream_name="$1"
349
-upstream=`git rev-parse --verify "${upstream_name}^0"` ||
350
- die "invalid upstream $upstream_name"
351
+if test -z "$rebase_root"
352
+then
353
+ # The upstream head must be given. Make sure it is valid.
354
+ upstream_name="$1"
355
+ shift
356
+ upstream=`git rev-parse --verify "${upstream_name}^0"` ||
357
+ die "invalid upstream $upstream_name"
358
+ unset root_flag
359
+ upstream_arg="$upstream_name"
360
+else
361
+ test -z "$newbase" && die "--root must be used with --onto"
362
+ unset upstream_name
363
+ unset upstream
364
+ root_flag="--root"
365
+ upstream_arg="$root_flag"
366
+fi
367
368
# Make sure the branch to rebase onto is valid.
369
onto_name=${newbase-"$upstream_name"}
370
onto=$(git rev-parse --verify "${onto_name}^0") || exit
371
372
# If a hook exists, give it a chance to interrupt
357
-run_pre_rebase_hook ${1+"$@"}
373
+run_pre_rebase_hook "$upstream_arg" "$@"
374
375
# If the branch to rebase is given, that is the branch we will rebase
376
# $branch_name -- branch being rebased, or HEAD (already detached)
378
# $head_name -- refs/heads/<that-branch> or "detached HEAD"
379
switch_to=
380
case "$#" in
365
-2)
381
+1)
382
# Is it "rebase other $branchname" or "rebase other $commit"?
367
- branch_name="$2"
368
- switch_to="$2"
383
+ branch_name="$1"
384
+ switch_to="$1"
385
370
- if git show-ref --verify --quiet -- "refs/heads/$2" &&
371
- branch=$(git rev-parse -q --verify "refs/heads/$2")
386
+ if git show-ref --verify --quiet -- "refs/heads/$1" &&
387
+ branch=$(git rev-parse -q --verify "refs/heads/$1")
388
then
373
- head_name="refs/heads/$2"
374
- elif branch=$(git rev-parse -q --verify "$2")
389
+ head_name="refs/heads/$1"
390
+ elif branch=$(git rev-parse -q --verify "$1")
391
then
392
head_name="detached HEAD"
393
else
409
esac
410
orig_head=$branch
411
396
-# Now we are rebasing commits $upstream..$branch on top of $onto
412
+# Now we are rebasing commits $upstream..$branch (or with --root,
413
+# everything leading up to $branch) on top of $onto
414
415
# Check if we are already based on $onto with linear history,
416
# but this should be done only when upstream and onto are the same.
446
exit 0
447
fi
448
449
+if test -n "$rebase_root"
450
+then
451
+ revisions="$onto..$orig_head"
452
+else
453
+ revisions="$upstream..$orig_head"
454
+fi
455
+
456
if test -z "$do_merge"
457
then
458
git format-patch -k --stdout --full-index --ignore-if-in-upstream \
435
- "$upstream..$orig_head" |
459
+ $root_flag "$revisions" |
460
git am $git_am_opt --rebasing --resolvemsg="$RESOLVEMSG" &&
461
move_to_original_branch
462
ret=$?
479
echo "$head_name" > "$dotest/head-name"
480
481
msgnum=0
458
-for cmt in `git rev-list --reverse --no-merges "$upstream..$orig_head"`
482
+for cmt in `git rev-list --reverse --no-merges "$revisions"`
483
do
484
msgnum=$(($msgnum + 1))
485
echo "$cmt" > "$dotest/cmt.$msgnum"