t / t1006-cat-file.shon commit subtree: fix AsciiDoc list item continuation (e0a1f09)
   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    test -z "$content" ||
  90    test_expect_success "--batch without type ($type)" '
  91        {
  92                echo "$size" &&
  93                maybe_remove_timestamp "$content" $no_ts
  94        } >expect &&
  95        echo $sha1 | git cat-file --batch="%(objectsize)" >actual.full &&
  96        maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
  97        test_cmp expect actual
  98    '
  99
 100    test -z "$content" ||
 101    test_expect_success "--batch without size ($type)" '
 102        {
 103                echo "$type" &&
 104                maybe_remove_timestamp "$content" $no_ts
 105        } >expect &&
 106        echo $sha1 | git cat-file --batch="%(objecttype)" >actual.full &&
 107        maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
 108        test_cmp expect actual
 109    '
 110}
 111
 112hello_content="Hello World"
 113hello_size=$(strlen "$hello_content")
 114hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
 115
 116test_expect_success "setup" '
 117        echo_without_newline "$hello_content" > hello &&
 118        git update-index --add hello
 119'
 120
 121run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
 122
 123test_expect_success '--batch-check without %(rest) considers whole line' '
 124        echo "$hello_sha1 blob $hello_size" >expect &&
 125        git update-index --add --cacheinfo 100644 $hello_sha1 "white space" &&
 126        test_when_finished "git update-index --remove \"white space\"" &&
 127        echo ":white space" | git cat-file --batch-check >actual &&
 128        test_cmp expect actual
 129'
 130
 131tree_sha1=$(git write-tree)
 132tree_size=33
 133tree_pretty_content="100644 blob $hello_sha1    hello"
 134
 135run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
 136
 137commit_message="Initial commit"
 138commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
 139commit_size=177
 140commit_content="tree $tree_sha1
 141author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
 142committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
 143
 144$commit_message"
 145
 146run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
 147
 148tag_header_without_timestamp="object $hello_sha1
 149type blob
 150tag hellotag
 151tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
 152tag_description="This is a tag"
 153tag_content="$tag_header_without_timestamp 0000000000 +0000
 154
 155$tag_description"
 156
 157tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
 158tag_size=$(strlen "$tag_content")
 159
 160run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
 161
 162test_expect_success \
 163    "Reach a blob from a tag pointing to it" \
 164    "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
 165
 166for batch in batch batch-check
 167do
 168    for opt in t s e p
 169    do
 170        test_expect_success "Passing -$opt with --$batch fails" '
 171            test_must_fail git cat-file --$batch -$opt $hello_sha1
 172        '
 173
 174        test_expect_success "Passing --$batch with -$opt fails" '
 175            test_must_fail git cat-file -$opt --$batch $hello_sha1
 176        '
 177    done
 178
 179    test_expect_success "Passing <type> with --$batch fails" '
 180        test_must_fail git cat-file --$batch blob $hello_sha1
 181    '
 182
 183    test_expect_success "Passing --$batch with <type> fails" '
 184        test_must_fail git cat-file blob --$batch $hello_sha1
 185    '
 186
 187    test_expect_success "Passing sha1 with --$batch fails" '
 188        test_must_fail git cat-file --$batch $hello_sha1
 189    '
 190done
 191
 192test_expect_success "--batch-check for a non-existent named object" '
 193    test "foobar42 missing
 194foobar84 missing" = \
 195    "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
 196'
 197
 198test_expect_success "--batch-check for a non-existent hash" '
 199    test "0000000000000000000000000000000000000042 missing
 2000000000000000000000000000000000000000084 missing" = \
 201    "$( ( echo 0000000000000000000000000000000000000042;
 202         echo_without_newline 0000000000000000000000000000000000000084; ) \
 203       | git cat-file --batch-check)"
 204'
 205
 206test_expect_success "--batch for an existent and a non-existent hash" '
 207    test "$tag_sha1 tag $tag_size
 208$tag_content
 2090000000000000000000000000000000000000000 missing" = \
 210    "$( ( echo $tag_sha1;
 211         echo_without_newline 0000000000000000000000000000000000000000; ) \
 212       | git cat-file --batch)"
 213'
 214
 215test_expect_success "--batch-check for an emtpy line" '
 216    test " missing" = "$(echo | git cat-file --batch-check)"
 217'
 218
 219test_expect_success 'empty --batch-check notices missing object' '
 220        echo "$_z40 missing" >expect &&
 221        echo "$_z40" | git cat-file --batch-check="" >actual &&
 222        test_cmp expect actual
 223'
 224
 225batch_input="$hello_sha1
 226$commit_sha1
 227$tag_sha1
 228deadbeef
 229
 230"
 231
 232batch_output="$hello_sha1 blob $hello_size
 233$hello_content
 234$commit_sha1 commit $commit_size
 235$commit_content
 236$tag_sha1 tag $tag_size
 237$tag_content
 238deadbeef missing
 239 missing"
 240
 241test_expect_success '--batch with multiple sha1s gives correct format' '
 242        test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
 243'
 244
 245batch_check_input="$hello_sha1
 246$tree_sha1
 247$commit_sha1
 248$tag_sha1
 249deadbeef
 250
 251"
 252
 253batch_check_output="$hello_sha1 blob $hello_size
 254$tree_sha1 tree $tree_size
 255$commit_sha1 commit $commit_size
 256$tag_sha1 tag $tag_size
 257deadbeef missing
 258 missing"
 259
 260test_expect_success "--batch-check with multiple sha1s gives correct format" '
 261    test "$batch_check_output" = \
 262    "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
 263'
 264
 265test_expect_success 'setup blobs which are likely to delta' '
 266        test-genrandom foo 10240 >foo &&
 267        { cat foo; echo plus; } >foo-plus &&
 268        git add foo foo-plus &&
 269        git commit -m foo &&
 270        cat >blobs <<-\EOF
 271        HEAD:foo
 272        HEAD:foo-plus
 273        EOF
 274'
 275
 276test_expect_success 'confirm that neither loose blob is a delta' '
 277        cat >expect <<-EOF
 278        $_z40
 279        $_z40
 280        EOF
 281        git cat-file --batch-check="%(deltabase)" <blobs >actual &&
 282        test_cmp expect actual
 283'
 284
 285# To avoid relying too much on the current delta heuristics,
 286# we will check only that one of the two objects is a delta
 287# against the other, but not the order. We can do so by just
 288# asking for the base of both, and checking whether either
 289# sha1 appears in the output.
 290test_expect_success '%(deltabase) reports packed delta bases' '
 291        git repack -ad &&
 292        git cat-file --batch-check="%(deltabase)" <blobs >actual &&
 293        {
 294                grep "$(git rev-parse HEAD:foo)" actual ||
 295                grep "$(git rev-parse HEAD:foo-plus)" actual
 296        }
 297'
 298
 299test_done