1#!/bin/sh
   2test_description='show-ref'
   4. ./test-lib.sh
   5test_expect_success setup '
   7        test_commit A &&
   8        git tag -f -a -m "annotated A" A &&
   9        git checkout -b side &&
  10        test_commit B &&
  11        git tag -f -a -m "annotated B" B &&
  12        git checkout master &&
  13        test_commit C &&
  14        git branch B A^0
  15'
  16test_expect_success 'show-ref' '
  18        echo $(git rev-parse refs/tags/A) refs/tags/A >expect &&
  19        git show-ref A >actual &&
  21        test_cmp expect actual &&
  22        git show-ref tags/A >actual &&
  24        test_cmp expect actual &&
  25        git show-ref refs/tags/A >actual &&
  27        test_cmp expect actual &&
  28        test_must_fail git show-ref D >actual &&
  30        test_must_be_empty actual
  31'
  32test_expect_success 'show-ref -q' '
  34        git show-ref -q A >actual &&
  35        test_must_be_empty actual &&
  36        git show-ref -q tags/A >actual &&
  38        test_must_be_empty actual &&
  39        git show-ref -q refs/tags/A >actual &&
  41        test_must_be_empty actual &&
  42        test_must_fail git show-ref -q D >actual &&
  44        test_must_be_empty actual
  45'
  46test_expect_success 'show-ref --verify' '
  48        echo $(git rev-parse refs/tags/A) refs/tags/A >expect &&
  49        git show-ref --verify refs/tags/A >actual &&
  51        test_cmp expect actual &&
  52        test_must_fail git show-ref --verify A >actual &&
  54        test_must_be_empty actual &&
  55        test_must_fail git show-ref --verify tags/A >actual &&
  57        test_must_be_empty actual &&
  58        test_must_fail git show-ref --verify D >actual &&
  60        test_must_be_empty actual
  61'
  62test_expect_success 'show-ref --verify -q' '
  64        git show-ref --verify -q refs/tags/A >actual &&
  65        test_must_be_empty actual &&
  66        test_must_fail git show-ref --verify -q A >actual &&
  68        test_must_be_empty actual &&
  69        test_must_fail git show-ref --verify -q tags/A >actual &&
  71        test_must_be_empty actual &&
  72        test_must_fail git show-ref --verify -q D >actual &&
  74        test_must_be_empty actual
  75'
  76test_expect_success 'show-ref -d' '
  78        {
  79                echo $(git rev-parse refs/tags/A) refs/tags/A &&
  80                echo $(git rev-parse refs/tags/A^0) "refs/tags/A^{}"
  81                echo $(git rev-parse refs/tags/C) refs/tags/C
  82        } >expect &&
  83        git show-ref -d A C >actual &&
  84        test_cmp expect actual &&
  85        git show-ref -d tags/A tags/C >actual &&
  87        test_cmp expect actual &&
  88        git show-ref -d refs/tags/A refs/tags/C >actual &&
  90        test_cmp expect actual &&
  91        git show-ref --verify -d refs/tags/A refs/tags/C >actual &&
  93        test_cmp expect actual &&
  94        echo $(git rev-parse refs/heads/master) refs/heads/master >expect &&
  96        git show-ref -d master >actual &&
  97        test_cmp expect actual &&
  98        git show-ref -d heads/master >actual &&
 100        test_cmp expect actual &&
 101        git show-ref -d refs/heads/master >actual &&
 103        test_cmp expect actual &&
 104        git show-ref -d --verify refs/heads/master >actual &&
 106        test_cmp expect actual &&
 107        test_must_fail git show-ref -d --verify master >actual &&
 109        test_must_be_empty actual &&
 110        test_must_fail git show-ref -d --verify heads/master >actual &&
 112        test_must_be_empty actual &&
 113        test_must_fail git show-ref --verify -d A C >actual &&
 115        test_must_be_empty actual &&
 116        test_must_fail git show-ref --verify -d tags/A tags/C >actual &&
 118        test_must_be_empty actual
 119'
 121test_expect_success 'show-ref --heads, --tags, --head, pattern' '
 123        for branch in B master side
 124        do
 125                echo $(git rev-parse refs/heads/$branch) refs/heads/$branch
 126        done >expect.heads &&
 127        git show-ref --heads >actual &&
 128        test_cmp expect.heads actual &&
 129        for tag in A B C
 131        do
 132                echo $(git rev-parse refs/tags/$tag) refs/tags/$tag
 133        done >expect.tags &&
 134        git show-ref --tags >actual &&
 135        test_cmp expect.tags actual &&
 136        cat expect.heads expect.tags >expect &&
 138        git show-ref --heads --tags >actual &&
 139        test_cmp expect actual &&
 140        {
 142                echo $(git rev-parse HEAD) HEAD &&
 143                cat expect.heads expect.tags
 144        } >expect &&
 145        git show-ref --heads --tags --head >actual &&
 146        test_cmp expect actual &&
 147        {
 149                echo $(git rev-parse HEAD) HEAD &&
 150                echo $(git rev-parse refs/heads/B) refs/heads/B
 151                echo $(git rev-parse refs/tags/B) refs/tags/B
 152        } >expect &&
 153        git show-ref --head B >actual &&
 154        test_cmp expect actual &&
 155        {
 157                echo $(git rev-parse HEAD) HEAD &&
 158                echo $(git rev-parse refs/heads/B) refs/heads/B
 159                echo $(git rev-parse refs/tags/B) refs/tags/B
 160                echo $(git rev-parse refs/tags/B^0) "refs/tags/B^{}"
 161        } >expect &&
 162        git show-ref --head -d B >actual &&
 163        test_cmp expect actual
 164'
 165test_expect_success 'show-ref --verify HEAD' '
 167        echo $(git rev-parse HEAD) HEAD >expect &&
 168        git show-ref --verify HEAD >actual &&
 169        test_cmp expect actual &&
 170        git show-ref --verify -q HEAD >actual &&
 172        test_must_be_empty actual
 173'
 174test_expect_success 'show-ref --verify with dangling ref' '
 176        sha1_file() {
 177                echo "$*" | sed "s#..#.git/objects/&/#"
 178        } &&
 179        remove_object() {
 181                file=$(sha1_file "$*") &&
 182                test -e "$file" &&
 183                rm -f "$file"
 184        } &&
 185        test_when_finished "rm -rf dangling" &&
 187        (
 188                git init dangling &&
 189                cd dangling &&
 190                test_commit dangling &&
 191                sha=$(git rev-parse refs/tags/dangling) &&
 192                remove_object $sha &&
 193                test_must_fail git show-ref --verify refs/tags/dangling
 194        )
 195'
 196test_done