1#!/bin/sh
23
test_description=git-hash-object
45
. ./test-lib.sh
67
echo_without_newline() {
8printf '%s' "$*"
9}
1011
test_blob_does_not_exist() {
12test_expect_success 'blob does not exist in database' "
13test_must_fail git cat-file blob $1
14"
15}
1617
test_blob_exists() {
18test_expect_success 'blob exists in database' "
19git cat-file blob $1
20"
21}
2223
hello_content="Hello World"
24hello_sha1=5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
2526
example_content="This is an example"
27example_sha1=ddd3f836d3e3fbb7ae289aa9ae83536f76956399
2829
setup_repo() {
30echo_without_newline "$hello_content" > hello
31echo_without_newline "$example_content" > example
32}
3334
test_repo=test
35push_repo() {
36test_create_repo $test_repo
37cd $test_repo
3839
setup_repo
40}
4142
pop_repo() {
43cd ..
44rm -rf $test_repo
45}
4647
setup_repo
4849
# Argument checking
5051
test_expect_success "multiple '--stdin's are rejected" '
52test_must_fail git hash-object --stdin --stdin < example
53'
5455
# Behavior
5657
push_repo
5859
test_expect_success 'hash a file' '
60test $hello_sha1 = $(git hash-object hello)
61'
6263
test_blob_does_not_exist $hello_sha1
6465
test_expect_success 'hash from stdin' '
66test $example_sha1 = $(git hash-object --stdin < example)
67'
6869
test_blob_does_not_exist $example_sha1
7071
test_expect_success 'hash a file and write to database' '
72test $hello_sha1 = $(git hash-object -w hello)
73'
7475
test_blob_exists $hello_sha1
7677
test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' '
78echo foo > file1 &&
79obname0=$(echo bar | git hash-object --stdin) &&
80obname1=$(git hash-object file1) &&
81obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
82obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
83test "$obname0" = "$obname0new" &&
84test "$obname1" = "$obname1new"
85'
8687
pop_repo
8889
for args in "-w --stdin" "--stdin -w"; do
90push_repo
9192
test_expect_success "hash from stdin and write to database ($args)" '
93test $example_sha1 = $(git hash-object $args < example)
94'
9596
test_blob_exists $example_sha1
9798
pop_repo
99done
100101
test_done