(command.CommandList
  children: [
    (command.ShAssignment
      pairs: [
        (assign_pair
          lhs: (sh_lhs_expr.Name name:test_description)
          op: assign_op.Equal
          rhs: 
            {
              (SQ <'checkout <branch>\n'> <'\n'> 
                <'Ensures that checkout on an unborn branch does what the user expects'>
              )
            }
          spids: [4]
        )
      ]
    )
    (C {<.>} {<'./test-lib.sh'>})
    (command.ShFunction
      name: test_branch
      body: 
        (BraceGroup
          children: [
            (command.AndOr
              ops: [Id.Op_DAmp Id.Op_DAmp]
              children: [
                (command.Simple
                  words: [
                    {<printf>}
                    {(DQ <'%s'> <Id.Lit_Other '\\'> <n>)}
                    {(DQ <'refs/heads/'> ($ Id.VSub_Number '$1'))}
                  ]
                  redirects: [
                    (redir
                      op: <Id.Redir_Great '>'>
                      loc: (redir_loc.Fd fd:1)
                      arg: {<expect.HEAD>}
                    )
                  ]
                  do_fork: T
                )
                (command.Simple
                  words: [{<git>} {<symbolic-ref>} {<HEAD>}]
                  redirects: [
                    (redir
                      op: <Id.Redir_Great '>'>
                      loc: (redir_loc.Fd fd:1)
                      arg: {<actual.HEAD>}
                    )
                  ]
                  do_fork: T
                )
                (C {<test_cmp>} {<expect.HEAD>} {<actual.HEAD>})
              ]
            )
          ]
        )
    )
    (command.ShFunction
      name: test_branch_upstream
      body: 
        (BraceGroup
          children: [
            (command.AndOr
              ops: [Id.Op_DAmp Id.Op_DAmp]
              children: [
                (command.Simple
                  words: [
                    {<printf>}
                    {(DQ <'%s'> <Id.Lit_Other '\\'> <n>)}
                    {(DQ ($ Id.VSub_Number '$2'))}
                    {(DQ <'refs/heads/'> ($ Id.VSub_Number '$3'))}
                  ]
                  redirects: [
                    (redir
                      op: <Id.Redir_Great '>'>
                      loc: (redir_loc.Fd fd:1)
                      arg: {<expect.upstream>}
                    )
                  ]
                  do_fork: T
                )
                (BraceGroup
                  children: [
                    (command.AndOr
                      ops: [Id.Op_DAmp]
                      children: [
                        (C {<git>} {<config>} {(DQ <branch.> ($ Id.VSub_Number '$1') <.remote>)})
                        (C {<git>} {<config>} {(DQ <branch.> ($ Id.VSub_Number '$1') <.merge>)})
                      ]
                    )
                  ]
                  redirects: [
                    (redir
                      op: <Id.Redir_Great '>'>
                      loc: (redir_loc.Fd fd:1)
                      arg: {<actual.upstream>}
                    )
                  ]
                )
                (C {<test_cmp>} {<expect.upstream>} {<actual.upstream>})
              ]
            )
          ]
        )
    )
    (C {<test_expect_success>} {(SQ <setup>)} 
      {
        (SQ <'\n'> <'\ttest_commit my_master &&\n'> <'\tgit init repo_a &&\n'> <'\t(\n'> 
          <'\t\tcd repo_a &&\n'> <'\t\ttest_commit a_master &&\n'> <'\t\tgit checkout -b foo &&\n'> <'\t\ttest_commit a_foo &&\n'> 
          <'\t\tgit checkout -b bar &&\n'> <'\t\ttest_commit a_bar\n'> <'\t) &&\n'> <'\tgit init repo_b &&\n'> <'\t(\n'> <'\t\tcd repo_b &&\n'> 
          <'\t\ttest_commit b_master &&\n'> <'\t\tgit checkout -b foo &&\n'> <'\t\ttest_commit b_foo &&\n'> <'\t\tgit checkout -b baz &&\n'> 
          <'\t\ttest_commit b_baz\n'> <'\t) &&\n'> <'\tgit remote add repo_a repo_a &&\n'> <'\tgit remote add repo_b repo_b &&\n'> 
          <'\tgit config remote.repo_b.fetch \\\n'> <'\t\t"+refs/heads/*:refs/remotes/other_b/*" &&\n'> <'\tgit fetch --all\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'checkout of non-existing branch fails'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D xyzzy &&\n'> 
          <'\n'> <'\ttest_must_fail git checkout xyzzy &&\n'> 
          <'\ttest_must_fail git rev-parse --verify refs/heads/xyzzy &&\n'> <'\ttest_branch master\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'checkout of branch from multiple remotes fails #1'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D foo &&\n'> <'\n'> 
          <'\ttest_must_fail git checkout foo &&\n'> <'\ttest_must_fail git rev-parse --verify refs/heads/foo &&\n'> <'\ttest_branch master\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'checkout of branch from a single remote succeeds #1'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D bar &&\n'> <'\n'> 
          <'\tgit checkout bar &&\n'> <'\ttest_branch bar &&\n'> <'\ttest_cmp_rev remotes/repo_a/bar HEAD &&\n'> 
          <'\ttest_branch_upstream bar repo_a bar\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'checkout of branch from a single remote succeeds #2'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D baz &&\n'> <'\n'> 
          <'\tgit checkout baz &&\n'> <'\ttest_branch baz &&\n'> <'\ttest_cmp_rev remotes/other_b/baz HEAD &&\n'> 
          <'\ttest_branch_upstream baz repo_b baz\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'--no-guess suppresses branch auto-vivification'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D bar &&\n'> <'\n'> 
          <'\ttest_must_fail git checkout --no-guess bar &&\n'> <'\ttest_must_fail git rev-parse --verify refs/heads/bar &&\n'> <'\ttest_branch master\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'setup more remotes with unconventional refspecs'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\tgit init repo_c &&\n'> <'\t(\n'> 
          <'\t\tcd repo_c &&\n'> <'\t\ttest_commit c_master &&\n'> <'\t\tgit checkout -b bar &&\n'> <'\t\ttest_commit c_bar &&\n'> 
          <'\t\tgit checkout -b spam &&\n'> <'\t\ttest_commit c_spam\n'> <'\t) &&\n'> <'\tgit init repo_d &&\n'> <'\t(\n'> <'\t\tcd repo_d &&\n'> 
          <'\t\ttest_commit d_master &&\n'> <'\t\tgit checkout -b baz &&\n'> <'\t\ttest_commit d_baz &&\n'> <'\t\tgit checkout -b eggs &&\n'> 
          <'\t\ttest_commit d_eggs\n'> <'\t) &&\n'> <'\tgit remote add repo_c repo_c &&\n'> <'\tgit config remote.repo_c.fetch \\\n'> 
          <'\t\t"+refs/heads/*:refs/remotes/extra_dir/repo_c/extra_dir/*" &&\n'> <'\tgit remote add repo_d repo_d &&\n'> <'\tgit config remote.repo_d.fetch \\\n'> 
          <'\t\t"+refs/heads/*:refs/repo_d/*" &&\n'> <'\tgit fetch --all\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'checkout of branch from multiple remotes fails #2'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D bar &&\n'> <'\n'> 
          <'\ttest_must_fail git checkout bar &&\n'> <'\ttest_must_fail git rev-parse --verify refs/heads/bar &&\n'> <'\ttest_branch master\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'checkout of branch from multiple remotes fails #3'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D baz &&\n'> <'\n'> 
          <'\ttest_must_fail git checkout baz &&\n'> <'\ttest_must_fail git rev-parse --verify refs/heads/baz &&\n'> <'\ttest_branch master\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'checkout of branch from a single remote succeeds #3'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D spam &&\n'> 
          <'\n'> <'\tgit checkout spam &&\n'> <'\ttest_branch spam &&\n'> 
          <'\ttest_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&\n'> <'\ttest_branch_upstream spam repo_c spam\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'checkout of branch from a single remote succeeds #4'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D eggs &&\n'> 
          <'\n'> <'\tgit checkout eggs &&\n'> <'\ttest_branch eggs &&\n'> <'\ttest_cmp_rev refs/repo_d/eggs HEAD &&\n'> 
          <'\ttest_branch_upstream eggs repo_d eggs\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'checkout of branch with a file having the same name fails'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D spam &&\n'> 
          <'\n'> <'\t>spam &&\n'> <'\ttest_must_fail git checkout spam &&\n'> 
          <'\ttest_must_fail git rev-parse --verify refs/heads/spam &&\n'> <'\ttest_branch master\n'>
        )
      }
    )
    (C {<test_expect_success>} 
      {(SQ <'checkout of branch with a file in subdir having the same name fails'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D spam &&\n'> 
          <'\n'> <'\t>spam &&\n'> <'\tmkdir sub &&\n'> <'\tmv spam sub/spam &&\n'> 
          <'\ttest_must_fail git -C sub checkout spam &&\n'> <'\ttest_must_fail git rev-parse --verify refs/heads/spam &&\n'> <'\ttest_branch master\n'>
        )
      }
    )
    (C {<test_expect_success>} 
      {(SQ <'checkout <branch> -- succeeds, even if a file with the same name exists'>)} 
      {
        (SQ <'\n'> <'\tgit checkout -B master &&\n'> <'\ttest_might_fail git branch -D spam &&\n'> 
          <'\n'> <'\t>spam &&\n'> <'\tgit checkout spam -- &&\n'> <'\ttest_branch spam &&\n'> 
          <'\ttest_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&\n'> <'\ttest_branch_upstream spam repo_c spam\n'>
        )
      }
    )
    (C {<test_expect_success>} {(SQ <'loosely defined local base branch is reported correctly'>)} 
      {
        (SQ <'\n'> <'\n'> <'\tgit checkout master &&\n'> <'\tgit branch strict &&\n'> 
          <'\tgit branch loose &&\n'> <'\tgit commit --allow-empty -m "a bit more" &&\n'> <'\n'> 
          <'\ttest_config branch.strict.remote . &&\n'> <'\ttest_config branch.loose.remote . &&\n'> 
          <'\ttest_config branch.strict.merge refs/heads/master &&\n'> <'\ttest_config branch.loose.merge master &&\n'> <'\n'> 
          <'\tgit checkout strict | sed -e "s/strict/BRANCHNAME/g" >expect &&\n'> <'\tgit checkout loose | sed -e "s/loose/BRANCHNAME/g" >actual &&\n'> <'\n'> 
          <'\ttest_cmp expect actual\n'>
        )
      }
    )
    (C {<test_done>})
  ]
)