t / t1006-cat-file.shon commit Merge branch 'db/clone-in-c' (b84c343)
   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        test $type = "$(git cat-file -t $sha1)"
  40    '
  41
  42    test_expect_success "Size of $type is correct" '
  43        test $size = "$(git cat-file -s $sha1)"
  44    '
  45
  46    test -z "$content" ||
  47    test_expect_success "Content of $type is correct" '
  48        expect="$(maybe_remove_timestamp "$content" $no_ts)"
  49        actual="$(maybe_remove_timestamp "$(git cat-file $type $sha1)" $no_ts)"
  50
  51        if test "z$expect" = "z$actual"
  52        then
  53                : happy
  54        else
  55                echo "Oops: expected $expect"
  56                echo "but got $actual"
  57                false
  58        fi
  59    '
  60
  61    test_expect_success "Pretty content of $type is correct" '
  62        expect="$(maybe_remove_timestamp "$pretty_content" $no_ts)"
  63        actual="$(maybe_remove_timestamp "$(git cat-file -p $sha1)" $no_ts)"
  64        if test "z$expect" = "z$actual"
  65        then
  66                : happy
  67        else
  68                echo "Oops: expected $expect"
  69                echo "but got $actual"
  70                false
  71        fi
  72    '
  73
  74    test -z "$content" ||
  75    test_expect_success "--batch output of $type is correct" '
  76        expect="$(maybe_remove_timestamp "$batch_output" $no_ts)"
  77        actual="$(maybe_remove_timestamp "$(echo $sha1 | git cat-file --batch)" no_ts)"
  78        if test "z$expect" = "z$actual"
  79        then
  80                : happy
  81        else
  82                echo "Oops: expected $expect"
  83                echo "but got $actual"
  84                false
  85        fi
  86    '
  87
  88    test_expect_success "--batch-check output of $type is correct" '
  89        expect="$sha1 $type $size"
  90        actual="$(echo_without_newline $sha1 | git cat-file --batch-check)"
  91        if test "z$expect" = "z$actual"
  92        then
  93                : happy
  94        else
  95                echo "Oops: expected $expect"
  96                echo "but got $actual"
  97                false
  98        fi
  99    '
 100}
 101
 102hello_content="Hello World"
 103hello_size=$(strlen "$hello_content")
 104hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
 105
 106test_expect_success "setup" '
 107        echo_without_newline "$hello_content" > hello &&
 108        git update-index --add hello
 109'
 110
 111run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
 112
 113tree_sha1=$(git write-tree)
 114tree_size=33
 115tree_pretty_content="100644 blob $hello_sha1    hello"
 116
 117run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
 118
 119commit_message="Intial commit"
 120commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
 121commit_size=176
 122commit_content="tree $tree_sha1
 123author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
 124committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
 125
 126$commit_message"
 127
 128run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
 129
 130tag_header_without_timestamp="object $hello_sha1
 131type blob
 132tag hellotag
 133tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
 134tag_description="This is a tag"
 135tag_content="$tag_header_without_timestamp 0000000000 +0000
 136
 137$tag_description"
 138tag_pretty_content="$tag_header_without_timestamp Thu Jan 1 00:00:00 1970 +0000
 139
 140$tag_description"
 141
 142tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
 143tag_size=$(strlen "$tag_content")
 144
 145run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_pretty_content" 1
 146
 147test_expect_success \
 148    "Reach a blob from a tag pointing to it" \
 149    "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
 150
 151for batch in batch batch-check
 152do
 153    for opt in t s e p
 154    do
 155        test_expect_success "Passing -$opt with --$batch fails" '
 156            test_must_fail git cat-file --$batch -$opt $hello_sha1
 157        '
 158
 159        test_expect_success "Passing --$batch with -$opt fails" '
 160            test_must_fail git cat-file -$opt --$batch $hello_sha1
 161        '
 162    done
 163
 164    test_expect_success "Passing <type> with --$batch fails" '
 165        test_must_fail git cat-file --$batch blob $hello_sha1
 166    '
 167
 168    test_expect_success "Passing --$batch with <type> fails" '
 169        test_must_fail git cat-file blob --$batch $hello_sha1
 170    '
 171
 172    test_expect_success "Passing sha1 with --$batch fails" '
 173        test_must_fail git cat-file --$batch $hello_sha1
 174    '
 175done
 176
 177test_expect_success "--batch-check for a non-existent object" '
 178    test "deadbeef missing" = \
 179    "$(echo_without_newline deadbeef | git cat-file --batch-check)"
 180'
 181
 182test_expect_success "--batch-check for an emtpy line" '
 183    test " missing" = "$(echo | git cat-file --batch-check)"
 184'
 185
 186batch_input="$hello_sha1
 187$commit_sha1
 188$tag_sha1
 189deadbeef
 190
 191"
 192
 193batch_output="$hello_sha1 blob $hello_size
 194$hello_content
 195$commit_sha1 commit $commit_size
 196$commit_content
 197$tag_sha1 tag $tag_size
 198$tag_content
 199deadbeef missing
 200 missing"
 201
 202test_expect_success '--batch with multiple sha1s gives correct format' '
 203        test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
 204'
 205
 206batch_check_input="$hello_sha1
 207$tree_sha1
 208$commit_sha1
 209$tag_sha1
 210deadbeef
 211
 212"
 213
 214batch_check_output="$hello_sha1 blob $hello_size
 215$tree_sha1 tree $tree_size
 216$commit_sha1 commit $commit_size
 217$tag_sha1 tag $tag_size
 218deadbeef missing
 219 missing"
 220
 221test_expect_success "--batch-check with multiple sha1s gives correct format" '
 222    test "$batch_check_output" = \
 223    "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
 224'
 225
 226test_done