t / t1006-cat-file.shon commit cat-file: teach --batch to stream blob objects (98e2092)
   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
  77hello_content="Hello World"
  78hello_size=$(strlen "$hello_content")
  79hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
  80
  81test_expect_success "setup" '
  82        echo_without_newline "$hello_content" > hello &&
  83        git update-index --add hello
  84'
  85
  86run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
  87
  88tree_sha1=$(git write-tree)
  89tree_size=33
  90tree_pretty_content="100644 blob $hello_sha1    hello"
  91
  92run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
  93
  94commit_message="Initial commit"
  95commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
  96commit_size=177
  97commit_content="tree $tree_sha1
  98author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
  99committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
 100
 101$commit_message"
 102
 103run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
 104
 105tag_header_without_timestamp="object $hello_sha1
 106type blob
 107tag hellotag
 108tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
 109tag_description="This is a tag"
 110tag_content="$tag_header_without_timestamp 0000000000 +0000
 111
 112$tag_description"
 113
 114tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
 115tag_size=$(strlen "$tag_content")
 116
 117run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
 118
 119test_expect_success \
 120    "Reach a blob from a tag pointing to it" \
 121    "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
 122
 123for batch in batch batch-check
 124do
 125    for opt in t s e p
 126    do
 127        test_expect_success "Passing -$opt with --$batch fails" '
 128            test_must_fail git cat-file --$batch -$opt $hello_sha1
 129        '
 130
 131        test_expect_success "Passing --$batch with -$opt fails" '
 132            test_must_fail git cat-file -$opt --$batch $hello_sha1
 133        '
 134    done
 135
 136    test_expect_success "Passing <type> with --$batch fails" '
 137        test_must_fail git cat-file --$batch blob $hello_sha1
 138    '
 139
 140    test_expect_success "Passing --$batch with <type> fails" '
 141        test_must_fail git cat-file blob --$batch $hello_sha1
 142    '
 143
 144    test_expect_success "Passing sha1 with --$batch fails" '
 145        test_must_fail git cat-file --$batch $hello_sha1
 146    '
 147done
 148
 149test_expect_success "--batch-check for a non-existent named object" '
 150    test "foobar42 missing
 151foobar84 missing" = \
 152    "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
 153'
 154
 155test_expect_success "--batch-check for a non-existent hash" '
 156    test "0000000000000000000000000000000000000042 missing
 1570000000000000000000000000000000000000084 missing" = \
 158    "$( ( echo 0000000000000000000000000000000000000042;
 159         echo_without_newline 0000000000000000000000000000000000000084; ) \
 160       | git cat-file --batch-check)"
 161'
 162
 163test_expect_success "--batch for an existent and a non-existent hash" '
 164    test "$tag_sha1 tag $tag_size
 165$tag_content
 1660000000000000000000000000000000000000000 missing" = \
 167    "$( ( echo $tag_sha1;
 168         echo_without_newline 0000000000000000000000000000000000000000; ) \
 169       | git cat-file --batch)"
 170'
 171
 172test_expect_success "--batch-check for an emtpy line" '
 173    test " missing" = "$(echo | git cat-file --batch-check)"
 174'
 175
 176batch_input="$hello_sha1
 177$commit_sha1
 178$tag_sha1
 179deadbeef
 180
 181"
 182
 183batch_output="$hello_sha1 blob $hello_size
 184$hello_content
 185$commit_sha1 commit $commit_size
 186$commit_content
 187$tag_sha1 tag $tag_size
 188$tag_content
 189deadbeef missing
 190 missing"
 191
 192test_expect_success '--batch with multiple sha1s gives correct format' '
 193        test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
 194'
 195
 196batch_check_input="$hello_sha1
 197$tree_sha1
 198$commit_sha1
 199$tag_sha1
 200deadbeef
 201
 202"
 203
 204batch_check_output="$hello_sha1 blob $hello_size
 205$tree_sha1 tree $tree_size
 206$commit_sha1 commit $commit_size
 207$tag_sha1 tag $tag_size
 208deadbeef missing
 209 missing"
 210
 211test_expect_success "--batch-check with multiple sha1s gives correct format" '
 212    test "$batch_check_output" = \
 213    "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
 214'
 215
 216test_done