t / t2017-checkout-orphan.shon commit t1430: clean up broken refs/tags/shadow (45669a7)
   1#!/bin/sh
   2#
   3# Copyright (c) 2010 Erick Mattos
   4#
   5
   6test_description='git checkout --orphan
   7
   8Main Tests for --orphan functionality.'
   9
  10. ./test-lib.sh
  11
  12TEST_FILE=foo
  13
  14test_expect_success 'Setup' '
  15        echo "Initial" >"$TEST_FILE" &&
  16        git add "$TEST_FILE" &&
  17        git commit -m "First Commit" &&
  18        test_tick &&
  19        echo "State 1" >>"$TEST_FILE" &&
  20        git add "$TEST_FILE" &&
  21        test_tick &&
  22        git commit -m "Second Commit"
  23'
  24
  25test_expect_success '--orphan creates a new orphan branch from HEAD' '
  26        git checkout --orphan alpha &&
  27        test_must_fail git rev-parse --verify HEAD &&
  28        test "refs/heads/alpha" = "$(git symbolic-ref HEAD)" &&
  29        test_tick &&
  30        git commit -m "Third Commit" &&
  31        test_must_fail git rev-parse --verify HEAD^ &&
  32        git diff-tree --quiet master alpha
  33'
  34
  35test_expect_success '--orphan creates a new orphan branch from <start_point>' '
  36        git checkout master &&
  37        git checkout --orphan beta master^ &&
  38        test_must_fail git rev-parse --verify HEAD &&
  39        test "refs/heads/beta" = "$(git symbolic-ref HEAD)" &&
  40        test_tick &&
  41        git commit -m "Fourth Commit" &&
  42        test_must_fail git rev-parse --verify HEAD^ &&
  43        git diff-tree --quiet master^ beta
  44'
  45
  46test_expect_success '--orphan must be rejected with -b' '
  47        git checkout master &&
  48        test_must_fail git checkout --orphan new -b newer &&
  49        test refs/heads/master = "$(git symbolic-ref HEAD)"
  50'
  51
  52test_expect_success '--orphan must be rejected with -t' '
  53        git checkout master &&
  54        test_must_fail git checkout --orphan new -t master &&
  55        test refs/heads/master = "$(git symbolic-ref HEAD)"
  56'
  57
  58test_expect_success '--orphan ignores branch.autosetupmerge' '
  59        git checkout master &&
  60        git config branch.autosetupmerge always &&
  61        git checkout --orphan gamma &&
  62        test -z "$(git config branch.gamma.merge)" &&
  63        test refs/heads/gamma = "$(git symbolic-ref HEAD)" &&
  64        test_must_fail git rev-parse --verify HEAD^
  65'
  66
  67test_expect_success '--orphan makes reflog by default' '
  68        git checkout master &&
  69        git config --unset core.logAllRefUpdates &&
  70        git checkout --orphan delta &&
  71        test_must_fail git rev-parse --verify delta@{0} &&
  72        git commit -m Delta &&
  73        git rev-parse --verify delta@{0}
  74'
  75
  76test_expect_success '--orphan does not make reflog when core.logAllRefUpdates = false' '
  77        git checkout master &&
  78        git config core.logAllRefUpdates false &&
  79        git checkout --orphan epsilon &&
  80        test_must_fail git rev-parse --verify epsilon@{0} &&
  81        git commit -m Epsilon &&
  82        test_must_fail git rev-parse --verify epsilon@{0}
  83'
  84
  85test_expect_success '--orphan with -l makes reflog when core.logAllRefUpdates = false' '
  86        git checkout master &&
  87        git checkout -l --orphan zeta &&
  88        test_must_fail git rev-parse --verify zeta@{0} &&
  89        git commit -m Zeta &&
  90        git rev-parse --verify zeta@{0}
  91'
  92
  93test_expect_success 'giving up --orphan not committed when -l and core.logAllRefUpdates = false deletes reflog' '
  94        git checkout master &&
  95        git checkout -l --orphan eta &&
  96        test_must_fail git rev-parse --verify eta@{0} &&
  97        git checkout master &&
  98        test_must_fail git rev-parse --verify eta@{0}
  99'
 100
 101test_expect_success '--orphan is rejected with an existing name' '
 102        git checkout master &&
 103        test_must_fail git checkout --orphan master &&
 104        test refs/heads/master = "$(git symbolic-ref HEAD)"
 105'
 106
 107test_expect_success '--orphan refuses to switch if a merge is needed' '
 108        git checkout master &&
 109        git reset --hard &&
 110        echo local >>"$TEST_FILE" &&
 111        cat "$TEST_FILE" >"$TEST_FILE.saved" &&
 112        test_must_fail git checkout --orphan new master^ &&
 113        test refs/heads/master = "$(git symbolic-ref HEAD)" &&
 114        test_cmp "$TEST_FILE" "$TEST_FILE.saved" &&
 115        git diff-index --quiet --cached HEAD &&
 116        git reset --hard
 117'
 118
 119test_expect_success 'cannot --detach on an unborn branch' '
 120        git checkout master &&
 121        git checkout --orphan new &&
 122        test_must_fail git checkout --detach
 123'
 124
 125test_done