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