7cfeaa99ba1faca21e4e68e42f9ba4ce2e34b8a0
   1#!/bin/sh
   2
   3test_description='Test git update-ref error handling'
   4. ./test-lib.sh
   5
   6# Create some references, perhaps run pack-refs --all, then try to
   7# create some more references. Ensure that the second creation fails
   8# with the correct error message.
   9# Usage: test_update_rejected <before> <pack> <create> <error>
  10#   <before> is a ws-separated list of refs to create before the test
  11#   <pack> (true or false) tells whether to pack the refs before the test
  12#   <create> is a list of variables to attempt creating
  13#   <error> is a string to look for in the stderr of update-ref.
  14# All references are created in the namespace specified by the current
  15# value of $prefix.
  16test_update_rejected () {
  17        before="$1" &&
  18        pack="$2" &&
  19        create="$3" &&
  20        error="$4" &&
  21        printf "create $prefix/%s $C\n" $before |
  22        git update-ref --stdin &&
  23        git for-each-ref $prefix >unchanged &&
  24        if $pack
  25        then
  26                git pack-refs --all
  27        fi &&
  28        printf "create $prefix/%s $C\n" $create >input &&
  29        test_must_fail git update-ref --stdin <input 2>output.err &&
  30        grep -F "$error" output.err &&
  31        git for-each-ref $prefix >actual &&
  32        test_cmp unchanged actual
  33}
  34
  35Q="'"
  36
  37test_expect_success 'setup' '
  38
  39        git commit --allow-empty -m Initial &&
  40        C=$(git rev-parse HEAD) &&
  41        git commit --allow-empty -m Second &&
  42        D=$(git rev-parse HEAD)
  43
  44'
  45
  46test_expect_success 'existing loose ref is a simple prefix of new' '
  47
  48        prefix=refs/1l &&
  49        test_update_rejected "a c e" false "b c/x d" \
  50                "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x$Q"
  51
  52'
  53
  54test_expect_success 'existing packed ref is a simple prefix of new' '
  55
  56        prefix=refs/1p &&
  57        test_update_rejected "a c e" true "b c/x d" \
  58                "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x$Q"
  59
  60'
  61
  62test_expect_success 'existing loose ref is a deeper prefix of new' '
  63
  64        prefix=refs/2l &&
  65        test_update_rejected "a c e" false "b c/x/y d" \
  66                "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x/y$Q"
  67
  68'
  69
  70test_expect_success 'existing packed ref is a deeper prefix of new' '
  71
  72        prefix=refs/2p &&
  73        test_update_rejected "a c e" true "b c/x/y d" \
  74                "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x/y$Q"
  75
  76'
  77
  78test_expect_success 'new ref is a simple prefix of existing loose' '
  79
  80        prefix=refs/3l &&
  81        test_update_rejected "a c/x e" false "b c d" \
  82                "$Q$prefix/c/x$Q exists; cannot create $Q$prefix/c$Q"
  83
  84'
  85
  86test_expect_success 'new ref is a simple prefix of existing packed' '
  87
  88        prefix=refs/3p &&
  89        test_update_rejected "a c/x e" true "b c d" \
  90                "$Q$prefix/c/x$Q exists; cannot create $Q$prefix/c$Q"
  91
  92'
  93
  94test_expect_success 'new ref is a deeper prefix of existing loose' '
  95
  96        prefix=refs/4l &&
  97        test_update_rejected "a c/x/y e" false "b c d" \
  98                "$Q$prefix/c/x/y$Q exists; cannot create $Q$prefix/c$Q"
  99
 100'
 101
 102test_expect_success 'new ref is a deeper prefix of existing packed' '
 103
 104        prefix=refs/4p &&
 105        test_update_rejected "a c/x/y e" true "b c d" \
 106                "$Q$prefix/c/x/y$Q exists; cannot create $Q$prefix/c$Q"
 107
 108'
 109
 110test_expect_success 'one new ref is a simple prefix of another' '
 111
 112        prefix=refs/5 &&
 113        test_update_rejected "a e" false "b c c/x d" \
 114                "cannot process $Q$prefix/c$Q and $Q$prefix/c/x$Q at the same time"
 115
 116'
 117
 118test_expect_success 'empty directory should not fool rev-parse' '
 119        prefix=refs/e-rev-parse &&
 120        git update-ref $prefix/foo $C &&
 121        git pack-refs --all &&
 122        mkdir -p .git/$prefix/foo/bar/baz &&
 123        echo "$C" >expected &&
 124        git rev-parse $prefix/foo >actual &&
 125        test_cmp expected actual
 126'
 127
 128test_expect_success 'empty directory should not fool for-each-ref' '
 129        prefix=refs/e-for-each-ref &&
 130        git update-ref $prefix/foo $C &&
 131        git for-each-ref $prefix >expected &&
 132        git pack-refs --all &&
 133        mkdir -p .git/$prefix/foo/bar/baz &&
 134        git for-each-ref $prefix >actual &&
 135        test_cmp expected actual
 136'
 137
 138test_expect_success 'empty directory should not fool create' '
 139        prefix=refs/e-create &&
 140        mkdir -p .git/$prefix/foo/bar/baz &&
 141        printf "create %s $C\n" $prefix/foo |
 142        git update-ref --stdin
 143'
 144
 145test_expect_success 'empty directory should not fool verify' '
 146        prefix=refs/e-verify &&
 147        git update-ref $prefix/foo $C &&
 148        git pack-refs --all &&
 149        mkdir -p .git/$prefix/foo/bar/baz &&
 150        printf "verify %s $C\n" $prefix/foo |
 151        git update-ref --stdin
 152'
 153
 154test_expect_success 'empty directory should not fool 1-arg update' '
 155        prefix=refs/e-update-1 &&
 156        git update-ref $prefix/foo $C &&
 157        git pack-refs --all &&
 158        mkdir -p .git/$prefix/foo/bar/baz &&
 159        printf "update %s $D\n" $prefix/foo |
 160        git update-ref --stdin
 161'
 162
 163test_expect_success 'empty directory should not fool 2-arg update' '
 164        prefix=refs/e-update-2 &&
 165        git update-ref $prefix/foo $C &&
 166        git pack-refs --all &&
 167        mkdir -p .git/$prefix/foo/bar/baz &&
 168        printf "update %s $D $C\n" $prefix/foo |
 169        git update-ref --stdin
 170'
 171
 172test_expect_success 'empty directory should not fool 0-arg delete' '
 173        prefix=refs/e-delete-0 &&
 174        git update-ref $prefix/foo $C &&
 175        git pack-refs --all &&
 176        mkdir -p .git/$prefix/foo/bar/baz &&
 177        printf "delete %s\n" $prefix/foo |
 178        git update-ref --stdin
 179'
 180
 181test_expect_success 'empty directory should not fool 1-arg delete' '
 182        prefix=refs/e-delete-1 &&
 183        git update-ref $prefix/foo $C &&
 184        git pack-refs --all &&
 185        mkdir -p .git/$prefix/foo/bar/baz &&
 186        printf "delete %s $C\n" $prefix/foo |
 187        git update-ref --stdin
 188'
 189
 190test_done