t / t1006-cat-file.shon commit Merge branch 'tg/index-struct-sizes' (b0d974d)
   1#!/bin/sh
   2
   3test_description='git cat-file'
   4
   5. ./test-lib.sh
   6
   7echo_without_newline () {
   8    printf '%s' "$*"
   9}
  10
  11strlen () {
  12    echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
  13}
  14
  15maybe_remove_timestamp () {
  16    if test -z "$2"; then
  17        echo_without_newline "$1"
  18    else
  19        echo_without_newline "$(printf '%s\n' "$1" | sed -e 's/ [0-9][0-9]* [-+][0-9][0-9][0-9][0-9]$//')"
  20    fi
  21}
  22
  23run_tests () {
  24    type=$1
  25    sha1=$2
  26    size=$3
  27    content=$4
  28    pretty_content=$5
  29    no_ts=$6
  30
  31    batch_output="$sha1 $type $size
  32$content"
  33
  34    test_expect_success "$type exists" '
  35        git cat-file -e $sha1
  36    '
  37
  38    test_expect_success "Type of $type is correct" '
  39        echo $type >expect &&
  40        git cat-file -t $sha1 >actual &&
  41        test_cmp expect actual
  42    '
  43
  44    test_expect_success "Size of $type is correct" '
  45        echo $size >expect &&
  46        git cat-file -s $sha1 >actual &&
  47        test_cmp expect actual
  48    '
  49
  50    test -z "$content" ||
  51    test_expect_success "Content of $type is correct" '
  52        maybe_remove_timestamp "$content" $no_ts >expect &&
  53        maybe_remove_timestamp "$(git cat-file $type $sha1)" $no_ts >actual &&
  54        test_cmp expect actual
  55    '
  56
  57    test_expect_success "Pretty content of $type is correct" '
  58        maybe_remove_timestamp "$pretty_content" $no_ts >expect &&
  59        maybe_remove_timestamp "$(git cat-file -p $sha1)" $no_ts >actual &&
  60        test_cmp expect actual
  61    '
  62
  63    test -z "$content" ||
  64    test_expect_success "--batch output of $type is correct" '
  65        maybe_remove_timestamp "$batch_output" $no_ts >expect &&
  66        maybe_remove_timestamp "$(echo $sha1 | git cat-file --batch)" $no_ts >actual &&
  67        test_cmp expect actual
  68    '
  69
  70    test_expect_success "--batch-check output of $type is correct" '
  71        echo "$sha1 $type $size" >expect &&
  72        echo_without_newline $sha1 | git cat-file --batch-check >actual &&
  73        test_cmp expect actual
  74    '
  75
  76    test_expect_success "custom --batch-check format" '
  77        echo "$type $sha1" >expect &&
  78        echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
  79        test_cmp expect actual
  80    '
  81
  82    test_expect_success '--batch-check with %(rest)' '
  83        echo "$type this is some extra content" >expect &&
  84        echo "$sha1    this is some extra content" |
  85                git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
  86        test_cmp expect actual
  87    '
  88}
  89
  90hello_content="Hello World"
  91hello_size=$(strlen "$hello_content")
  92hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
  93
  94test_expect_success "setup" '
  95        echo_without_newline "$hello_content" > hello &&
  96        git update-index --add hello
  97'
  98
  99run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
 100
 101test_expect_success '--batch-check without %(rest) considers whole line' '
 102        echo "$hello_sha1 blob $hello_size" >expect &&
 103        git update-index --add --cacheinfo 100644 $hello_sha1 "white space" &&
 104        test_when_finished "git update-index --remove \"white space\"" &&
 105        echo ":white space" | git cat-file --batch-check >actual &&
 106        test_cmp expect actual
 107'
 108
 109tree_sha1=$(git write-tree)
 110tree_size=33
 111tree_pretty_content="100644 blob $hello_sha1    hello"
 112
 113run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
 114
 115commit_message="Initial commit"
 116commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
 117commit_size=177
 118commit_content="tree $tree_sha1
 119author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
 120committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
 121
 122$commit_message"
 123
 124run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
 125
 126tag_header_without_timestamp="object $hello_sha1
 127type blob
 128tag hellotag
 129tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
 130tag_description="This is a tag"
 131tag_content="$tag_header_without_timestamp 0000000000 +0000
 132
 133$tag_description"
 134
 135tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
 136tag_size=$(strlen "$tag_content")
 137
 138run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
 139
 140test_expect_success \
 141    "Reach a blob from a tag pointing to it" \
 142    "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
 143
 144for batch in batch batch-check
 145do
 146    for opt in t s e p
 147    do
 148        test_expect_success "Passing -$opt with --$batch fails" '
 149            test_must_fail git cat-file --$batch -$opt $hello_sha1
 150        '
 151
 152        test_expect_success "Passing --$batch with -$opt fails" '
 153            test_must_fail git cat-file -$opt --$batch $hello_sha1
 154        '
 155    done
 156
 157    test_expect_success "Passing <type> with --$batch fails" '
 158        test_must_fail git cat-file --$batch blob $hello_sha1
 159    '
 160
 161    test_expect_success "Passing --$batch with <type> fails" '
 162        test_must_fail git cat-file blob --$batch $hello_sha1
 163    '
 164
 165    test_expect_success "Passing sha1 with --$batch fails" '
 166        test_must_fail git cat-file --$batch $hello_sha1
 167    '
 168done
 169
 170test_expect_success "--batch-check for a non-existent named object" '
 171    test "foobar42 missing
 172foobar84 missing" = \
 173    "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
 174'
 175
 176test_expect_success "--batch-check for a non-existent hash" '
 177    test "0000000000000000000000000000000000000042 missing
 1780000000000000000000000000000000000000084 missing" = \
 179    "$( ( echo 0000000000000000000000000000000000000042;
 180         echo_without_newline 0000000000000000000000000000000000000084; ) \
 181       | git cat-file --batch-check)"
 182'
 183
 184test_expect_success "--batch for an existent and a non-existent hash" '
 185    test "$tag_sha1 tag $tag_size
 186$tag_content
 1870000000000000000000000000000000000000000 missing" = \
 188    "$( ( echo $tag_sha1;
 189         echo_without_newline 0000000000000000000000000000000000000000; ) \
 190       | git cat-file --batch)"
 191'
 192
 193test_expect_success "--batch-check for an emtpy line" '
 194    test " missing" = "$(echo | git cat-file --batch-check)"
 195'
 196
 197batch_input="$hello_sha1
 198$commit_sha1
 199$tag_sha1
 200deadbeef
 201
 202"
 203
 204batch_output="$hello_sha1 blob $hello_size
 205$hello_content
 206$commit_sha1 commit $commit_size
 207$commit_content
 208$tag_sha1 tag $tag_size
 209$tag_content
 210deadbeef missing
 211 missing"
 212
 213test_expect_success '--batch with multiple sha1s gives correct format' '
 214        test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
 215'
 216
 217batch_check_input="$hello_sha1
 218$tree_sha1
 219$commit_sha1
 220$tag_sha1
 221deadbeef
 222
 223"
 224
 225batch_check_output="$hello_sha1 blob $hello_size
 226$tree_sha1 tree $tree_size
 227$commit_sha1 commit $commit_size
 228$tag_sha1 tag $tag_size
 229deadbeef missing
 230 missing"
 231
 232test_expect_success "--batch-check with multiple sha1s gives correct format" '
 233    test "$batch_check_output" = \
 234    "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
 235'
 236
 237test_done