rebase --root: print usage on too many args

Just like git rebase --onto newbase upstream branch error displays the usage message, so should clearly git rebase --onto newbase --root branch error , but it doesn't. Instead, it ignores both "branch" and "error" and rebases the current HEAD. This is because we try to match the number of remainging arguments "$#", which fails to match "1" argument and matches the "*" that really should have been a "0". Make sure we display usage information when too many arguments are given. Also fail-fast in case of similar bugs in the future by matching on exactly 0 arguments and failing on unknown numbers. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Martin von Zweigbergk committed Jun 26, 2012 at 07:51 UTC f2b6a19907ccb366790c7ec7b640f9111ac7ad32
2 files changed +12 -2
git-rebase.sh
+5 -1
@@ -400,6 +400,7 @@ else
400 test -z "$onto" && die "You must specify --onto when using --root"
401 unset upstream_name
402 unset upstream
403 + test $# -gt 1 && usage
404 upstream_arg=--root
405 fi
406
@@ -450,7 +451,7 @@ case "$#" in
451 die "fatal: no such branch: $1"
452 fi
453 ;;
453 -*)
454 +0)
455 # Do not need to switch branches, we are already on it.
456 if branch_name=`git symbolic-ref -q HEAD`
457 then
@@ -462,6 +463,9 @@ case "$#" in
463 fi
464 orig_head=$(git rev-parse --verify "${branch_name}^0") || exit
465 ;;
466 +*)
467 + die "BUG: unexpected number of arguments left to parse"
468 + ;;
469 esac
470
471 require_clean_work_tree "rebase" "Please commit or stash them."
t/t3412-rebase-root.sh
+7 -1
@@ -23,9 +23,15 @@ test_expect_success 'prepare repository' '
23 '
24
25 test_expect_success 'rebase --root expects --onto' '
26 + git checkout -B fail other &&
27 test_must_fail git rebase --root
28 '
29
30 +test_expect_success 'rebase --root fails with too many args' '
31 + git checkout -B fail other &&
32 + test_must_fail git rebase --onto master --root fail fail
33 +'
34 +
35 test_expect_success 'setup pre-rebase hook' '
36 mkdir -p .git/hooks &&
37 cat >.git/hooks/pre-rebase <<EOF &&
@@ -42,7 +48,7 @@ cat > expect <<EOF
48 EOF
49
50 test_expect_success 'rebase --root --onto <newbase>' '
45 - git checkout -b work &&
51 + git checkout -b work other &&
52 git rebase --root --onto master &&
53 git log --pretty=tformat:"%s" > rebased &&
54 test_cmp expect rebased