7563043c53f09cd28b77bcc4f227ecf3a855f163
   1#!/bin/sh
   2
   3test_description='Test git check-ref-format'
   4
   5. ./test-lib.sh
   6
   7valid_ref() {
   8        test_expect_success "ref name '$1' is valid" \
   9                "git check-ref-format '$1'"
  10}
  11invalid_ref() {
  12        test_expect_success "ref name '$1' is not valid" \
  13                "test_must_fail git check-ref-format '$1'"
  14}
  15
  16valid_ref 'heads/foo'
  17invalid_ref 'foo'
  18valid_ref 'foo/bar/baz'
  19valid_ref 'refs///heads/foo'
  20invalid_ref 'heads/foo/'
  21valid_ref '/heads/foo'
  22valid_ref '///heads/foo'
  23invalid_ref '/foo'
  24invalid_ref './foo'
  25invalid_ref '.refs/foo'
  26invalid_ref 'heads/foo..bar'
  27invalid_ref 'heads/foo?bar'
  28valid_ref 'foo./bar'
  29invalid_ref 'heads/foo.lock'
  30valid_ref 'heads/foo@bar'
  31invalid_ref 'heads/v@{ation'
  32invalid_ref 'heads/foo\bar'
  33
  34test_expect_success "check-ref-format --branch @{-1}" '
  35        T=$(git write-tree) &&
  36        sha1=$(echo A | git commit-tree $T) &&
  37        git update-ref refs/heads/master $sha1 &&
  38        git update-ref refs/remotes/origin/master $sha1 &&
  39        git checkout master &&
  40        git checkout origin/master &&
  41        git checkout master &&
  42        refname=$(git check-ref-format --branch @{-1}) &&
  43        test "$refname" = "$sha1" &&
  44        refname2=$(git check-ref-format --branch @{-2}) &&
  45        test "$refname2" = master'
  46
  47test_expect_success 'check-ref-format --branch from subdir' '
  48        mkdir subdir &&
  49
  50        T=$(git write-tree) &&
  51        sha1=$(echo A | git commit-tree $T) &&
  52        git update-ref refs/heads/master $sha1 &&
  53        git update-ref refs/remotes/origin/master $sha1 &&
  54        git checkout master &&
  55        git checkout origin/master &&
  56        git checkout master &&
  57        refname=$(
  58                cd subdir &&
  59                git check-ref-format --branch @{-1}
  60        ) &&
  61        test "$refname" = "$sha1"
  62'
  63
  64valid_ref_normalized() {
  65        test_expect_success "ref name '$1' simplifies to '$2'" "
  66                refname=\$(git check-ref-format --print '$1') &&
  67                test \"\$refname\" = '$2'"
  68}
  69invalid_ref_normalized() {
  70        test_expect_success "check-ref-format --print rejects '$1'" "
  71                test_must_fail git check-ref-format --print '$1'"
  72}
  73
  74valid_ref_normalized 'heads/foo' 'heads/foo'
  75valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
  76valid_ref_normalized '/heads/foo' 'heads/foo'
  77valid_ref_normalized '///heads/foo' 'heads/foo'
  78invalid_ref_normalized 'foo'
  79invalid_ref_normalized '/foo'
  80invalid_ref_normalized 'heads/foo/../bar'
  81invalid_ref_normalized 'heads/./foo'
  82invalid_ref_normalized 'heads\foo'
  83
  84test_done