builtin/rebase.c: make sure the active branch isn't moved when autostashing

Consider the following scenario: git checkout not-the-master work work work git rebase --autostash upstream master Here 'rebase --autostash <upstream> <branch>' incorrectly moves the active branch (not-the-master) to master (before the rebase). The expected behavior: (58794775:/git-rebase.sh:526) AUTOSTASH=$(git stash create autostash) git reset --hard git checkout master git rebase upstream git stash apply $AUTOSTASH The actual behavior: (6defce2b:/builtin/rebase.c:1062) AUTOSTASH=$(git stash create autostash) git reset --hard master git checkout master git rebase upstream git stash apply $AUTOSTASH This commit reinstates the 'legacy script' behavior as introduced with 58794775: rebase: implement --[no-]autostash and rebase.autostash Signed-off-by: Ben Wijen <ben@wijen.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ben Wijen committed Aug 30, 2019 at 17:16 UTC d2172ef02dee468b85bc4567332cbf58cd6b4b0a
2 files changed +14 -3
builtin/rebase.c
+6 -3
@@ -1968,9 +1968,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1968 state_dir_path("autostash", &options);
1969 struct child_process stash = CHILD_PROCESS_INIT;
1970 struct object_id oid;
1971 - struct commit *head =
1972 - lookup_commit_reference(the_repository,
1973 - &options.orig_head);
1971 + struct object_id head_oid;
1972 + struct commit *head;
1973 +
1974 + if (get_oid("HEAD", &head_oid))
1975 + die(_("could not determine HEAD revision"));
1976 + head = lookup_commit_reference(the_repository, &head_oid);
1977
1978 argv_array_pushl(&stash.args,
1979 "stash", "create", "autostash", NULL);
t/t3420-rebase-autostash.sh
+8
@@ -306,4 +306,12 @@ test_expect_success 'branch is left alone when possible' '
306 test unchanged-branch = "$(git rev-parse --abbrev-ref HEAD)"
307 '
308
309 +test_expect_success 'never change active branch' '
310 + git checkout -b not-the-feature-branch unrelated-onto-branch &&
311 + test_when_finished "git reset --hard && git checkout master" &&
312 + echo changed >file0 &&
313 + git rebase --autostash not-the-feature-branch feature-branch &&
314 + test_cmp_rev not-the-feature-branch unrelated-onto-branch
315 +'
316 +
317 test_done