t / t2020-checkout-detach.shon commit Merge branch 'jk/maint-config-param' (fe01ef3)
   1#!/bin/sh
   2
   3test_description='checkout into detached HEAD state'
   4. ./test-lib.sh
   5
   6check_detached () {
   7        test_must_fail git symbolic-ref -q HEAD >/dev/null
   8}
   9
  10check_not_detached () {
  11        git symbolic-ref -q HEAD >/dev/null
  12}
  13
  14ORPHAN_WARNING='you are leaving .* commit.*behind'
  15check_orphan_warning() {
  16        test_i18ngrep "$ORPHAN_WARNING" "$1"
  17}
  18check_no_orphan_warning() {
  19        test_i18ngrep ! "$ORPHAN_WARNING" "$1"
  20}
  21
  22reset () {
  23        git checkout master &&
  24        check_not_detached
  25}
  26
  27test_expect_success 'setup' '
  28        test_commit one &&
  29        test_commit two &&
  30        test_commit three && git tag -d three &&
  31        test_commit four && git tag -d four &&
  32        git branch branch &&
  33        git tag tag
  34'
  35
  36test_expect_success 'checkout branch does not detach' '
  37        reset &&
  38        git checkout branch &&
  39        check_not_detached
  40'
  41
  42test_expect_success 'checkout tag detaches' '
  43        reset &&
  44        git checkout tag &&
  45        check_detached
  46'
  47
  48test_expect_success 'checkout branch by full name detaches' '
  49        reset &&
  50        git checkout refs/heads/branch &&
  51        check_detached
  52'
  53
  54test_expect_success 'checkout non-ref detaches' '
  55        reset &&
  56        git checkout branch^ &&
  57        check_detached
  58'
  59
  60test_expect_success 'checkout ref^0 detaches' '
  61        reset &&
  62        git checkout branch^0 &&
  63        check_detached
  64'
  65
  66test_expect_success 'checkout --detach detaches' '
  67        reset &&
  68        git checkout --detach branch &&
  69        check_detached
  70'
  71
  72test_expect_success 'checkout --detach without branch name' '
  73        reset &&
  74        git checkout --detach &&
  75        check_detached
  76'
  77
  78test_expect_success 'checkout --detach errors out for non-commit' '
  79        reset &&
  80        test_must_fail git checkout --detach one^{tree} &&
  81        check_not_detached
  82'
  83
  84test_expect_success 'checkout --detach errors out for extra argument' '
  85        reset &&
  86        git checkout master &&
  87        test_must_fail git checkout --detach tag one.t &&
  88        check_not_detached
  89'
  90
  91test_expect_success 'checkout --detached and -b are incompatible' '
  92        reset &&
  93        test_must_fail git checkout --detach -b newbranch tag &&
  94        check_not_detached
  95'
  96
  97test_expect_success 'checkout --detach moves HEAD' '
  98        reset &&
  99        git checkout one &&
 100        git checkout --detach two &&
 101        git diff --exit-code HEAD &&
 102        git diff --exit-code two
 103'
 104
 105test_expect_success 'checkout warns on orphan commits' '
 106        reset &&
 107        git checkout --detach two &&
 108        echo content >orphan &&
 109        git add orphan &&
 110        git commit -a -m orphan &&
 111        git checkout master 2>stderr
 112'
 113
 114test_expect_success 'checkout warns on orphan commits: output' '
 115        check_orphan_warning stderr
 116'
 117
 118test_expect_success 'checkout does not warn leaving ref tip' '
 119        reset &&
 120        git checkout --detach two &&
 121        git checkout master 2>stderr
 122'
 123
 124test_expect_success 'checkout does not warn leaving ref tip' '
 125        check_no_orphan_warning stderr
 126'
 127
 128test_expect_success 'checkout does not warn leaving reachable commit' '
 129        reset &&
 130        git checkout --detach HEAD^ &&
 131        git checkout master 2>stderr
 132'
 133
 134test_expect_success 'checkout does not warn leaving reachable commit' '
 135        check_no_orphan_warning stderr
 136'
 137
 138cat >expect <<'EOF'
 139Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
 140EOF
 141test_expect_success 'tracking count is accurate after orphan check' '
 142        reset &&
 143        git branch child master^ &&
 144        git config branch.child.remote . &&
 145        git config branch.child.merge refs/heads/master &&
 146        git checkout child^ &&
 147        git checkout child >stdout &&
 148        test_cmp expect stdout
 149'
 150
 151test_done