t / t1402-check-ref-format.shon commit archive.c: use OPT_BOOL() (f858c64)
   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'
  33invalid_ref "$(printf 'heads/foo\t')"
  34invalid_ref "$(printf 'heads/foo\177')"
  35valid_ref "$(printf 'heads/fu\303\237')"
  36
  37test_expect_success "check-ref-format --branch @{-1}" '
  38        T=$(git write-tree) &&
  39        sha1=$(echo A | git commit-tree $T) &&
  40        git update-ref refs/heads/master $sha1 &&
  41        git update-ref refs/remotes/origin/master $sha1 &&
  42        git checkout master &&
  43        git checkout origin/master &&
  44        git checkout master &&
  45        refname=$(git check-ref-format --branch @{-1}) &&
  46        test "$refname" = "$sha1" &&
  47        refname2=$(git check-ref-format --branch @{-2}) &&
  48        test "$refname2" = master'
  49
  50test_expect_success 'check-ref-format --branch from subdir' '
  51        mkdir subdir &&
  52
  53        T=$(git write-tree) &&
  54        sha1=$(echo A | git commit-tree $T) &&
  55        git update-ref refs/heads/master $sha1 &&
  56        git update-ref refs/remotes/origin/master $sha1 &&
  57        git checkout master &&
  58        git checkout origin/master &&
  59        git checkout master &&
  60        refname=$(
  61                cd subdir &&
  62                git check-ref-format --branch @{-1}
  63        ) &&
  64        test "$refname" = "$sha1"
  65'
  66
  67valid_ref_normalized() {
  68        test_expect_success "ref name '$1' simplifies to '$2'" "
  69                refname=\$(git check-ref-format --print '$1') &&
  70                test \"\$refname\" = '$2'"
  71}
  72invalid_ref_normalized() {
  73        test_expect_success "check-ref-format --print rejects '$1'" "
  74                test_must_fail git check-ref-format --print '$1'"
  75}
  76
  77valid_ref_normalized 'heads/foo' 'heads/foo'
  78valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
  79valid_ref_normalized '/heads/foo' 'heads/foo'
  80valid_ref_normalized '///heads/foo' 'heads/foo'
  81invalid_ref_normalized 'foo'
  82invalid_ref_normalized '/foo'
  83invalid_ref_normalized 'heads/foo/../bar'
  84invalid_ref_normalized 'heads/./foo'
  85invalid_ref_normalized 'heads\foo'
  86
  87test_done