t / t2024-checkout-dwim.shon commit t2024: Add tests verifying current DWIM behavior of 'git checkout <branch>' (399e4a1)
   1#!/bin/sh
   2
   3test_description='checkout <branch>
   4
   5Ensures that checkout on an unborn branch does what the user expects'
   6
   7. ./test-lib.sh
   8
   9# Is the current branch "refs/heads/$1"?
  10test_branch () {
  11        printf "%s\n" "refs/heads/$1" >expect.HEAD &&
  12        git symbolic-ref HEAD >actual.HEAD &&
  13        test_cmp expect.HEAD actual.HEAD
  14}
  15
  16# Is branch "refs/heads/$1" set to pull from "$2/$3"?
  17test_branch_upstream () {
  18        printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
  19        {
  20                git config "branch.$1.remote" &&
  21                git config "branch.$1.merge"
  22        } >actual.upstream &&
  23        test_cmp expect.upstream actual.upstream
  24}
  25
  26test_expect_success 'setup' '
  27        git init repo_a &&
  28        (
  29                cd repo_a &&
  30                test_commit a_master &&
  31                git checkout -b foo &&
  32                test_commit a_foo &&
  33                git checkout -b bar &&
  34                test_commit a_bar
  35        ) &&
  36        git init repo_b &&
  37        (
  38                cd repo_b &&
  39                test_commit b_master &&
  40                git checkout -b foo &&
  41                test_commit b_foo &&
  42                git checkout -b baz &&
  43                test_commit b_baz
  44        ) &&
  45        git remote add repo_a repo_a &&
  46        git remote add repo_b repo_b &&
  47        git config remote.repo_b.fetch \
  48                "+refs/heads/*:refs/remotes/other_b/*" &&
  49        git fetch --all
  50'
  51
  52test_expect_success 'checkout of non-existing branch fails' '
  53        git checkout -B master &&
  54        test_might_fail git branch -D xyzzy &&
  55
  56        test_must_fail git checkout xyzzy &&
  57        test_must_fail git rev-parse --verify refs/heads/xyzzy &&
  58        test_branch master
  59'
  60
  61test_expect_success 'checkout of branch from multiple remotes fails' '
  62        git checkout -B master &&
  63        test_might_fail git branch -D foo &&
  64
  65        test_must_fail git checkout foo &&
  66        test_must_fail git rev-parse --verify refs/heads/foo &&
  67        test_branch master
  68'
  69
  70test_expect_success 'checkout of branch from a single remote succeeds #1' '
  71        git checkout -B master &&
  72        test_might_fail git branch -D bar &&
  73
  74        git checkout bar &&
  75        test_branch bar &&
  76        test_cmp_rev remotes/repo_a/bar HEAD &&
  77        test_branch_upstream bar repo_a bar
  78'
  79
  80test_expect_success 'checkout of branch from a single remote succeeds #2' '
  81        git checkout -B master &&
  82        test_might_fail git branch -D baz &&
  83
  84        git checkout baz &&
  85        test_branch baz &&
  86        test_cmp_rev remotes/other_b/baz HEAD &&
  87        test_branch_upstream baz repo_b baz
  88'
  89
  90test_expect_success '--no-guess suppresses branch auto-vivification' '
  91        git checkout -B master &&
  92        test_might_fail git branch -D bar &&
  93
  94        test_must_fail git checkout --no-guess bar &&
  95        test_must_fail git rev-parse --verify refs/heads/bar &&
  96        test_branch master
  97'
  98
  99test_done