#!/bin/sh global test_description := ''checkout can switch to last branch and merge base'' source ./test-lib.sh test_expect_success 'setup' ' echo hello >world && git add world && git commit -m initial && git branch other && echo "hello again" >>world && git add world && git commit -m second ' test_expect_success '"checkout -" does not work initially' ' test_must_fail git checkout - ' test_expect_success 'first branch switch' ' git checkout other ' test_expect_success '"checkout -" switches back' ' git checkout - && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/master" ' test_expect_success '"checkout -" switches forth' ' git checkout - && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/other" ' test_expect_success 'detach HEAD' ' git checkout $(git rev-parse HEAD) ' test_expect_success '"checkout -" attaches again' ' git checkout - && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/other" ' test_expect_success '"checkout -" detaches again' ' git checkout - && test "z$(git rev-parse HEAD)" = "z$(git rev-parse other)" && test_must_fail git symbolic-ref HEAD ' test_expect_success 'more switches' ' for i in 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 do git checkout -b branch$i done ' proc more_switches { for i in [16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1] { git checkout branch$i } } test_expect_success 'switch to the last' ' more_switches && git checkout @{-1} && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/branch2" ' test_expect_success 'switch to second from the last' ' more_switches && git checkout @{-2} && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/branch3" ' test_expect_success 'switch to third from the last' ' more_switches && git checkout @{-3} && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/branch4" ' test_expect_success 'switch to fourth from the last' ' more_switches && git checkout @{-4} && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/branch5" ' test_expect_success 'switch to twelfth from the last' ' more_switches && git checkout @{-12} && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/branch13" ' test_expect_success 'merge base test setup' ' git checkout -b another other && echo "hello again" >>world && git add world && git commit -m third ' test_expect_success 'another...master' ' git checkout another && git checkout another...master && test "z$(git rev-parse --verify HEAD)" = "z$(git rev-parse --verify master^)" ' test_expect_success '...master' ' git checkout another && git checkout ...master && test "z$(git rev-parse --verify HEAD)" = "z$(git rev-parse --verify master^)" ' test_expect_success 'master...' ' git checkout another && git checkout master... && test "z$(git rev-parse --verify HEAD)" = "z$(git rev-parse --verify master^)" ' test_expect_success '"checkout -" works after a rebase A' ' git checkout master && git checkout other && git rebase master && git checkout - && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/master" ' test_expect_success '"checkout -" works after a rebase A B' ' git branch moodle master~1 && git checkout master && git checkout other && git rebase master moodle && git checkout - && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/master" ' test_expect_success '"checkout -" works after a rebase -i A' ' git checkout master && git checkout other && git rebase -i master && git checkout - && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/master" ' test_expect_success '"checkout -" works after a rebase -i A B' ' git branch foodle master~1 && git checkout master && git checkout other && git rebase master foodle && git checkout - && test "z$(git symbolic-ref HEAD)" = "zrefs/heads/master" ' test_done (CommandList children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:test_description) op: Equal rhs: {(SQ <"checkout can switch to last branch and merge base">)} spids: [4] ) ] spids: [4] ) (C {(.)} {(./test-lib.sh)}) (C {(test_expect_success)} {(SQ )} { (SQ <"\n"> <"\techo hello >world &&\n"> <"\tgit add world &&\n"> <"\tgit commit -m initial &&\n"> <"\tgit branch other &&\n"> <"\techo \"hello again\" >>world &&\n"> <"\tgit add world &&\n"> <"\tgit commit -m second\n"> ) } ) (C {(test_expect_success)} {(SQ <"\"checkout -\" does not work initially">)} {(SQ <"\n"> <"\ttest_must_fail git checkout -\n">)} ) (C {(test_expect_success)} {(SQ <"first branch switch">)} {(SQ <"\n"> <"\tgit checkout other\n">)}) (C {(test_expect_success)} {(SQ <"\"checkout -\" switches back">)} { (SQ <"\n"> <"\tgit checkout - &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/master\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"\"checkout -\" switches forth">)} { (SQ <"\n"> <"\tgit checkout - &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/other\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"detach HEAD">)} {(SQ <"\n"> <"\tgit checkout $(git rev-parse HEAD)\n">)} ) (C {(test_expect_success)} {(SQ <"\"checkout -\" attaches again">)} { (SQ <"\n"> <"\tgit checkout - &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/other\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"\"checkout -\" detaches again">)} { (SQ <"\n"> <"\tgit checkout - &&\n"> <"\ttest \"z$(git rev-parse HEAD)\" = \"z$(git rev-parse other)\" &&\n"> <"\ttest_must_fail git symbolic-ref HEAD\n"> ) } ) (C {(test_expect_success)} {(SQ <"more switches">)} { (SQ <"\n"> <"\tfor i in 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\n"> <"\tdo\n"> <"\t\tgit checkout -b branch$i\n"> <"\tdone\n"> ) } ) (FuncDef name: more_switches body: (BraceGroup children: [ (ForEach iter_name: i iter_words: [ {(16)} {(15)} {(14)} {(13)} {(12)} {(11)} {(10)} {(9)} {(8)} {(7)} {(6)} {(5)} {(4)} {(3)} {(2)} {(1)} ] do_arg_iter: False body: (DoGroup children: [(C {(git)} {(checkout)} {(branch) ($ VSub_Name "$i")})] spids: [184 195] ) spids: [150 -1] ) ] spids: [142] ) spids: [137 141] ) (C {(test_expect_success)} {(SQ <"switch to the last">)} { (SQ <"\n"> <"\tmore_switches &&\n"> <"\tgit checkout @{-1} &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/branch2\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"switch to second from the last">)} { (SQ <"\n"> <"\tmore_switches &&\n"> <"\tgit checkout @{-2} &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/branch3\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"switch to third from the last">)} { (SQ <"\n"> <"\tmore_switches &&\n"> <"\tgit checkout @{-3} &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/branch4\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"switch to fourth from the last">)} { (SQ <"\n"> <"\tmore_switches &&\n"> <"\tgit checkout @{-4} &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/branch5\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"switch to twelfth from the last">)} { (SQ <"\n"> <"\tmore_switches &&\n"> <"\tgit checkout @{-12} &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/branch13\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"merge base test setup">)} { (SQ <"\n"> <"\tgit checkout -b another other &&\n"> <"\techo \"hello again\" >>world &&\n"> <"\tgit add world &&\n"> <"\tgit commit -m third\n"> ) } ) (C {(test_expect_success)} {(SQ )} { (SQ <"\n"> <"\tgit checkout another &&\n"> <"\tgit checkout another...master &&\n"> <"\ttest \"z$(git rev-parse --verify HEAD)\" = \"z$(git rev-parse --verify master^)\"\n"> ) } ) (C {(test_expect_success)} {(SQ <...master>)} { (SQ <"\n"> <"\tgit checkout another &&\n"> <"\tgit checkout ...master &&\n"> <"\ttest \"z$(git rev-parse --verify HEAD)\" = \"z$(git rev-parse --verify master^)\"\n"> ) } ) (C {(test_expect_success)} {(SQ )} { (SQ <"\n"> <"\tgit checkout another &&\n"> <"\tgit checkout master... &&\n"> <"\ttest \"z$(git rev-parse --verify HEAD)\" = \"z$(git rev-parse --verify master^)\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"\"checkout -\" works after a rebase A">)} { (SQ <"\n"> <"\tgit checkout master &&\n"> <"\tgit checkout other &&\n"> <"\tgit rebase master &&\n"> <"\tgit checkout - &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/master\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"\"checkout -\" works after a rebase A B">)} { (SQ <"\n"> <"\tgit branch moodle master~1 &&\n"> <"\tgit checkout master &&\n"> <"\tgit checkout other &&\n"> <"\tgit rebase master moodle &&\n"> <"\tgit checkout - &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/master\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"\"checkout -\" works after a rebase -i A">)} { (SQ <"\n"> <"\tgit checkout master &&\n"> <"\tgit checkout other &&\n"> <"\tgit rebase -i master &&\n"> <"\tgit checkout - &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/master\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"\"checkout -\" works after a rebase -i A B">)} { (SQ <"\n"> <"\tgit branch foodle master~1 &&\n"> <"\tgit checkout master &&\n"> <"\tgit checkout other &&\n"> <"\tgit rebase master foodle &&\n"> <"\tgit checkout - &&\n"> <"\ttest \"z$(git symbolic-ref HEAD)\" = \"zrefs/heads/master\"\n"> ) } ) (C {(test_done)}) ] )