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