t / t1401-symbolic-ref.shon commit git_config_set: reuse empty sections (c71d8bb)
   1#!/bin/sh
   2
   3test_description='basic symbolic-ref tests'
   4. ./test-lib.sh
   5
   6# If the tests munging HEAD fail, they can break detection of
   7# the git repo, meaning that further tests will operate on
   8# the surrounding git repo instead of the trash directory.
   9reset_to_sane() {
  10        echo ref: refs/heads/foo >.git/HEAD
  11}
  12
  13test_expect_success 'symbolic-ref writes HEAD' '
  14        git symbolic-ref HEAD refs/heads/foo &&
  15        echo ref: refs/heads/foo >expect &&
  16        test_cmp expect .git/HEAD
  17'
  18
  19test_expect_success 'symbolic-ref reads HEAD' '
  20        echo refs/heads/foo >expect &&
  21        git symbolic-ref HEAD >actual &&
  22        test_cmp expect actual
  23'
  24
  25test_expect_success 'symbolic-ref refuses non-ref for HEAD' '
  26        test_must_fail git symbolic-ref HEAD foo
  27'
  28reset_to_sane
  29
  30test_expect_success 'symbolic-ref refuses bare sha1' '
  31        echo content >file && git add file && git commit -m one &&
  32        test_must_fail git symbolic-ref HEAD $(git rev-parse HEAD)
  33'
  34reset_to_sane
  35
  36test_expect_success 'HEAD cannot be removed' '
  37        test_must_fail git symbolic-ref -d HEAD
  38'
  39
  40reset_to_sane
  41
  42test_expect_success 'symbolic-ref can be deleted' '
  43        git symbolic-ref NOTHEAD refs/heads/foo &&
  44        git symbolic-ref -d NOTHEAD &&
  45        test_path_is_file .git/refs/heads/foo &&
  46        test_path_is_missing .git/NOTHEAD
  47'
  48reset_to_sane
  49
  50test_expect_success 'symbolic-ref can delete dangling symref' '
  51        git symbolic-ref NOTHEAD refs/heads/missing &&
  52        git symbolic-ref -d NOTHEAD &&
  53        test_path_is_missing .git/refs/heads/missing &&
  54        test_path_is_missing .git/NOTHEAD
  55'
  56reset_to_sane
  57
  58test_expect_success 'symbolic-ref fails to delete missing FOO' '
  59        echo "fatal: Cannot delete FOO, not a symbolic ref" >expect &&
  60        test_must_fail git symbolic-ref -d FOO >actual 2>&1 &&
  61        test_cmp expect actual
  62'
  63reset_to_sane
  64
  65test_expect_success 'symbolic-ref fails to delete real ref' '
  66        echo "fatal: Cannot delete refs/heads/foo, not a symbolic ref" >expect &&
  67        test_must_fail git symbolic-ref -d refs/heads/foo >actual 2>&1 &&
  68        test_path_is_file .git/refs/heads/foo &&
  69        test_cmp expect actual
  70'
  71reset_to_sane
  72
  73test_expect_success 'create large ref name' '
  74        # make 256+ character ref; some systems may not handle that,
  75        # so be gentle
  76        long=0123456789abcdef &&
  77        long=$long/$long/$long/$long &&
  78        long=$long/$long/$long/$long &&
  79        long_ref=refs/heads/$long &&
  80        tree=$(git write-tree) &&
  81        commit=$(echo foo | git commit-tree $tree) &&
  82        if git update-ref $long_ref $commit; then
  83                test_set_prereq LONG_REF
  84        else
  85                echo >&2 "long refs not supported"
  86        fi
  87'
  88
  89test_expect_success LONG_REF 'symbolic-ref can point to large ref name' '
  90        git symbolic-ref HEAD $long_ref &&
  91        echo $long_ref >expect &&
  92        git symbolic-ref HEAD >actual &&
  93        test_cmp expect actual
  94'
  95
  96test_expect_success LONG_REF 'we can parse long symbolic ref' '
  97        echo $commit >expect &&
  98        git rev-parse --verify HEAD >actual &&
  99        test_cmp expect actual
 100'
 101
 102test_expect_success 'symbolic-ref reports failure in exit code' '
 103        test_when_finished "rm -f .git/HEAD.lock" &&
 104        >.git/HEAD.lock &&
 105        test_must_fail git symbolic-ref HEAD refs/heads/whatever
 106'
 107
 108test_expect_success 'symbolic-ref writes reflog entry' '
 109        git checkout -b log1 &&
 110        test_commit one &&
 111        git checkout -b log2  &&
 112        test_commit two &&
 113        git checkout --orphan orphan &&
 114        git symbolic-ref -m create HEAD refs/heads/log1 &&
 115        git symbolic-ref -m update HEAD refs/heads/log2 &&
 116        cat >expect <<-\EOF &&
 117        update
 118        create
 119        EOF
 120        git log --format=%gs -g -2 >actual &&
 121        test_cmp expect actual
 122'
 123
 124test_expect_success 'symbolic-ref does not create ref d/f conflicts' '
 125        git checkout -b df &&
 126        test_commit df &&
 127        test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df &&
 128        git pack-refs --all --prune &&
 129        test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df
 130'
 131
 132test_expect_success 'symbolic-ref can overwrite pointer to invalid name' '
 133        test_when_finished reset_to_sane &&
 134        head=$(git rev-parse HEAD) &&
 135        git symbolic-ref HEAD refs/heads/outer &&
 136        test_when_finished "git update-ref -d refs/heads/outer/inner" &&
 137        git update-ref refs/heads/outer/inner $head &&
 138        git symbolic-ref HEAD refs/heads/unrelated
 139'
 140
 141test_expect_success 'symbolic-ref can resolve d/f name (EISDIR)' '
 142        test_when_finished reset_to_sane &&
 143        head=$(git rev-parse HEAD) &&
 144        git symbolic-ref HEAD refs/heads/outer/inner &&
 145        test_when_finished "git update-ref -d refs/heads/outer" &&
 146        git update-ref refs/heads/outer $head &&
 147        echo refs/heads/outer/inner >expect &&
 148        git symbolic-ref HEAD >actual &&
 149        test_cmp expect actual
 150'
 151
 152test_expect_success 'symbolic-ref can resolve d/f name (ENOTDIR)' '
 153        test_when_finished reset_to_sane &&
 154        head=$(git rev-parse HEAD) &&
 155        git symbolic-ref HEAD refs/heads/outer &&
 156        test_when_finished "git update-ref -d refs/heads/outer/inner" &&
 157        git update-ref refs/heads/outer/inner $head &&
 158        echo refs/heads/outer >expect &&
 159        git symbolic-ref HEAD >actual &&
 160        test_cmp expect actual
 161'
 162
 163test_done