#!/bin/sh global test_description := ''checkout '' source ./test-lib.sh # Arguments: [] # # Runs "git checkout" to switch to , testing that # # 1) we are on the specified branch, ; # 2) HEAD is ; if is not specified, the old HEAD is used. # # If is not specified, "git checkout" is run with -b. proc do_checkout { global exp_branch := $1 && global exp_ref := ""refs/heads/$exp_branch"" && # if is not specified, use HEAD. global exp_sha := $(2:-$(git rev-parse --verify HEAD)) && # default options for git checkout: -b if test -z $3 { global opts := '"-b'" } else { global opts := $3 } git checkout $opts $exp_branch $exp_sha && test $exp_ref = $[git rev-parse --symbolic-full-name HEAD] && test $exp_sha = $[git rev-parse --verify HEAD] } proc test_dirty_unmergeable { ! git diff --exit-code >/dev/null } proc setup_dirty_unmergeable { echo >>file1 change2>>file1 change2 } proc test_dirty_mergeable { ! git diff --cached --exit-code >/dev/null } proc setup_dirty_mergeable { echo >file2 file2>file2 file2 && git add file2 } test_expect_success 'setup' ' test_commit initial file1 && HEAD1=$(git rev-parse --verify HEAD) && test_commit change1 file1 && HEAD2=$(git rev-parse --verify HEAD) && git branch -m branch1 ' test_expect_success 'checkout -b to a new branch, set to HEAD' ' do_checkout branch2 ' test_expect_success 'checkout -b to a new branch, set to an explicit ref' ' git checkout branch1 && git branch -D branch2 && do_checkout branch2 $HEAD1 ' test_expect_success 'checkout -b to a new branch with unmergeable changes fails' ' git checkout branch1 && # clean up from previous test git branch -D branch2 && setup_dirty_unmergeable && test_must_fail do_checkout branch2 $HEAD1 && test_dirty_unmergeable ' test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' ' # still dirty and on branch1 do_checkout branch2 $HEAD1 "-f -b" && test_must_fail test_dirty_unmergeable ' test_expect_success 'checkout -b to a new branch preserves mergeable changes' ' git checkout branch1 && # clean up from previous test git branch -D branch2 && setup_dirty_mergeable && do_checkout branch2 $HEAD1 && test_dirty_mergeable ' test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' ' # clean up from previous test git reset --hard && git checkout branch1 && # clean up from previous test git branch -D branch2 && setup_dirty_mergeable && do_checkout branch2 $HEAD1 "-f -b" && test_must_fail test_dirty_mergeable ' test_expect_success 'checkout -b to an existing branch fails' ' git reset --hard HEAD && test_must_fail do_checkout branch2 $HEAD2 ' test_expect_success 'checkout -b to @{-1} fails with the right branch name' ' git reset --hard HEAD && git checkout branch1 && git checkout branch2 && echo >expect "fatal: A branch named '''''branch1''''' already exists." && test_must_fail git checkout -b @{-1} 2>actual && test_i18ncmp expect actual ' test_expect_success 'checkout -B to an existing branch resets branch to HEAD' ' git checkout branch1 && do_checkout branch2 "" -B ' test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' ' git checkout $(git rev-parse --verify HEAD) && do_checkout branch2 "" -B ' test_expect_success 'checkout -B to an existing branch with an explicit ref resets branch to that ref' ' git checkout branch1 && do_checkout branch2 $HEAD1 -B ' test_expect_success 'checkout -B to an existing branch with unmergeable changes fails' ' git checkout branch1 && setup_dirty_unmergeable && test_must_fail do_checkout branch2 $HEAD1 -B && test_dirty_unmergeable ' test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' ' # still dirty and on branch1 do_checkout branch2 $HEAD1 "-f -B" && test_must_fail test_dirty_unmergeable ' test_expect_success 'checkout -B to an existing branch preserves mergeable changes' ' git checkout branch1 && setup_dirty_mergeable && do_checkout branch2 $HEAD1 -B && test_dirty_mergeable ' test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' ' # clean up from previous test git reset --hard && git checkout branch1 && setup_dirty_mergeable && do_checkout branch2 $HEAD1 "-f -B" && test_must_fail test_dirty_mergeable ' test_expect_success 'checkout -b ' ' git tag -f -m "First commit" initial initial && git checkout -f change1 && name=$(git describe) && git checkout -b $name && git diff --exit-code change1 && echo "refs/heads/$name" >expect && git symbolic-ref HEAD >actual && test_cmp expect actual ' test_expect_success 'checkout -B to the current branch works' ' git checkout branch1 && git checkout -B branch1-scratch && setup_dirty_mergeable && git checkout -B branch1-scratch initial && test_dirty_mergeable ' test_done (CommandList children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:test_description) op: Equal rhs: {(SQ <"checkout ">)} spids: [4] ) ] spids: [4] ) (C {(.)} {(./test-lib.sh)}) (FuncDef name: do_checkout body: (BraceGroup children: [ (AndOr children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:exp_branch) op: Equal rhs: {($ VSub_Number "$1")} spids: [46] ) ] spids: [46] ) (AndOr children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:exp_ref) op: Equal rhs: {(DQ (refs/heads/) ($ VSub_Name "$exp_branch"))} spids: [52] ) ] spids: [52] ) (AndOr children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:exp_sha) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonHyphen arg_word: { (CommandSubPart command_list: (CommandList children: [ (C {(git)} {(rev-parse)} {(--verify)} {(HEAD)}) ] ) left_token: spids: [70 78] ) } ) spids: [67 79] ) } spids: [66] ) ] spids: [66] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-z)} {(DQ ($ VSub_Number "$3"))} {(Lit_Other "]")} ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:opts) op: Equal rhs: {(DQ (-b))} spids: [105] ) ] spids: [105] ) ] spids: [-1 102] ) ] else_action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:opts) op: Equal rhs: {(DQ ($ VSub_Number "$3"))} spids: [114] ) ] spids: [114] ) ] spids: [111 120] ) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) (AndOr children: [ (C {(git)} {(checkout)} {($ VSub_Name "$opts")} {($ VSub_Name "$exp_branch")} {($ VSub_Name "$exp_sha")} ) (AndOr children: [ (C {(test)} {($ VSub_Name "$exp_ref")} {(Lit_Other "=")} { (CommandSubPart command_list: (CommandList children: [(C {(git)} {(rev-parse)} {(--symbolic-full-name)} {(HEAD)})] ) left_token: spids: [144 152] ) } ) (C {(test)} {($ VSub_Name "$exp_sha")} {(Lit_Other "=")} { (CommandSubPart command_list: (CommandList children: [(C {(git)} {(rev-parse)} {(--verify)} {(HEAD)})] ) left_token: spids: [163 171] ) } ) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) ] spids: [43] ) spids: [39 42] ) (FuncDef name: test_dirty_unmergeable body: (BraceGroup children: [ (Pipeline children: [ (SimpleCommand words: [{(git)} {(diff)} {(--exit-code)}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(/dev/null)} spids:[191])] ) ] negated: True ) ] spids: [180] ) spids: [176 179] ) (FuncDef name: setup_dirty_unmergeable body: (BraceGroup children: [ (SimpleCommand words: [{(echo)} {(change2)}] redirects: [(Redir op_id:Redir_DGreat fd:-1 arg_word:{(file1)} spids:[206])] ) ] spids: [201] ) spids: [197 200] ) (FuncDef name: test_dirty_mergeable body: (BraceGroup children: [ (Pipeline children: [ (SimpleCommand words: [{(git)} {(diff)} {(--cached)} {(--exit-code)}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(/dev/null)} spids:[231])] ) ] negated: True ) ] spids: [218] ) spids: [214 217] ) (FuncDef name: setup_dirty_mergeable body: (BraceGroup children: [ (AndOr children: [ (SimpleCommand words: [{(echo)} {(file2)}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(file2)} spids:[246])] ) (C {(git)} {(add)} {(file2)}) ] op_id: Op_DAmp ) ] spids: [241] ) spids: [237 240] ) (C {(test_expect_success)} {(SQ )} { (SQ <"\n"> <"\ttest_commit initial file1 &&\n"> <"\tHEAD1=$(git rev-parse --verify HEAD) &&\n"> <"\n"> <"\ttest_commit change1 file1 &&\n"> <"\tHEAD2=$(git rev-parse --verify HEAD) &&\n"> <"\n"> <"\tgit branch -m branch1\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -b to a new branch, set to HEAD">)} {(SQ <"\n"> <"\tdo_checkout branch2\n">)} ) (C {(test_expect_success)} {(SQ <"checkout -b to a new branch, set to an explicit ref">)} { (SQ <"\n"> <"\tgit checkout branch1 &&\n"> <"\tgit branch -D branch2 &&\n"> <"\n"> <"\tdo_checkout branch2 $HEAD1\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -b to a new branch with unmergeable changes fails">)} { (SQ <"\n"> <"\tgit checkout branch1 &&\n"> <"\n"> <"\t# clean up from previous test\n"> <"\tgit branch -D branch2 &&\n"> <"\n"> <"\tsetup_dirty_unmergeable &&\n"> <"\ttest_must_fail do_checkout branch2 $HEAD1 &&\n"> <"\ttest_dirty_unmergeable\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -f -b to a new branch with unmergeable changes discards changes">)} { (SQ <"\n"> <"\t# still dirty and on branch1\n"> <"\tdo_checkout branch2 $HEAD1 \"-f -b\" &&\n"> <"\ttest_must_fail test_dirty_unmergeable\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -b to a new branch preserves mergeable changes">)} { (SQ <"\n"> <"\tgit checkout branch1 &&\n"> <"\n"> <"\t# clean up from previous test\n"> <"\tgit branch -D branch2 &&\n"> <"\n"> <"\tsetup_dirty_mergeable &&\n"> <"\tdo_checkout branch2 $HEAD1 &&\n"> <"\ttest_dirty_mergeable\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -f -b to a new branch with mergeable changes discards changes">)} { (SQ <"\n"> <"\t# clean up from previous test\n"> <"\tgit reset --hard &&\n"> <"\n"> <"\tgit checkout branch1 &&\n"> <"\n"> <"\t# clean up from previous test\n"> <"\tgit branch -D branch2 &&\n"> <"\n"> <"\tsetup_dirty_mergeable &&\n"> <"\tdo_checkout branch2 $HEAD1 \"-f -b\" &&\n"> <"\ttest_must_fail test_dirty_mergeable\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -b to an existing branch fails">)} { (SQ <"\n"> <"\tgit reset --hard HEAD &&\n"> <"\n"> <"\ttest_must_fail do_checkout branch2 $HEAD2\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -b to @{-1} fails with the right branch name">)} { (SQ <"\n"> <"\tgit reset --hard HEAD &&\n"> <"\tgit checkout branch1 &&\n"> <"\tgit checkout branch2 &&\n"> <"\techo >expect \"fatal: A branch named "> ) (EscapedLiteralPart token:) (SQ ) (EscapedLiteralPart token:) (SQ <" already exists.\" &&\n"> <"\ttest_must_fail git checkout -b @{-1} 2>actual &&\n"> <"\ttest_i18ncmp expect actual\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -B to an existing branch resets branch to HEAD">)} {(SQ <"\n"> <"\tgit checkout branch1 &&\n"> <"\n"> <"\tdo_checkout branch2 \"\" -B\n">)} ) (C {(test_expect_success)} {(SQ <"checkout -B to an existing branch from detached HEAD resets branch to HEAD">)} { (SQ <"\n"> <"\tgit checkout $(git rev-parse --verify HEAD) &&\n"> <"\n"> <"\tdo_checkout branch2 \"\" -B\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -B to an existing branch with an explicit ref resets branch to that ref">)} {(SQ <"\n"> <"\tgit checkout branch1 &&\n"> <"\n"> <"\tdo_checkout branch2 $HEAD1 -B\n">)} ) (C {(test_expect_success)} {(SQ <"checkout -B to an existing branch with unmergeable changes fails">)} { (SQ <"\n"> <"\tgit checkout branch1 &&\n"> <"\n"> <"\tsetup_dirty_unmergeable &&\n"> <"\ttest_must_fail do_checkout branch2 $HEAD1 -B &&\n"> <"\ttest_dirty_unmergeable\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -f -B to an existing branch with unmergeable changes discards changes">)} { (SQ <"\n"> <"\t# still dirty and on branch1\n"> <"\tdo_checkout branch2 $HEAD1 \"-f -B\" &&\n"> <"\ttest_must_fail test_dirty_unmergeable\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -B to an existing branch preserves mergeable changes">)} { (SQ <"\n"> <"\tgit checkout branch1 &&\n"> <"\n"> <"\tsetup_dirty_mergeable &&\n"> <"\tdo_checkout branch2 $HEAD1 -B &&\n"> <"\ttest_dirty_mergeable\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -f -B to an existing branch with mergeable changes discards changes">)} { (SQ <"\n"> <"\t# clean up from previous test\n"> <"\tgit reset --hard &&\n"> <"\n"> <"\tgit checkout branch1 &&\n"> <"\n"> <"\tsetup_dirty_mergeable &&\n"> <"\tdo_checkout branch2 $HEAD1 \"-f -B\" &&\n"> <"\ttest_must_fail test_dirty_mergeable\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -b ">)} { (SQ <"\n"> <"\tgit tag -f -m \"First commit\" initial initial &&\n"> <"\tgit checkout -f change1 &&\n"> <"\tname=$(git describe) &&\n"> <"\tgit checkout -b $name &&\n"> <"\tgit diff --exit-code change1 &&\n"> <"\techo \"refs/heads/$name\" >expect &&\n"> <"\tgit symbolic-ref HEAD >actual &&\n"> <"\ttest_cmp expect actual\n"> ) } ) (C {(test_expect_success)} {(SQ <"checkout -B to the current branch works">)} { (SQ <"\n"> <"\tgit checkout branch1 &&\n"> <"\tgit checkout -B branch1-scratch &&\n"> <"\n"> <"\tsetup_dirty_mergeable &&\n"> <"\tgit checkout -B branch1-scratch initial &&\n"> <"\ttest_dirty_mergeable\n"> ) } ) (C {(test_done)}) ] )