Merge git://git.bogomips.org/git-svn
[git/git-p4.git] / t / t1007-hash-object.sh
blob1ec0535138c72bbd1e497c35c21bc5ea46b0315f
1 #!/bin/sh
3 test_description="git-hash-object"
5 . ./test-lib.sh
7 echo_without_newline() {
8 printf '%s' "$*"
11 test_blob_does_not_exist() {
12 test_expect_success 'blob does not exist in database' "
13 test_must_fail git cat-file blob $1
17 test_blob_exists() {
18 test_expect_success 'blob exists in database' "
19 git cat-file blob $1
23 hello_content="Hello World"
24 hello_sha1=5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
26 example_content="This is an example"
27 example_sha1=ddd3f836d3e3fbb7ae289aa9ae83536f76956399
29 setup_repo() {
30 echo_without_newline "$hello_content" > hello
31 echo_without_newline "$example_content" > example
34 test_repo=test
35 push_repo() {
36 test_create_repo $test_repo
37 cd $test_repo
39 setup_repo
42 pop_repo() {
43 cd ..
44 rm -rf $test_repo
47 setup_repo
49 # Argument checking
51 test_expect_success "multiple '--stdin's are rejected" '
52 test_must_fail git hash-object --stdin --stdin < example
55 test_expect_success "Can't use --stdin and --stdin-paths together" '
56 test_must_fail git hash-object --stdin --stdin-paths &&
57 test_must_fail git hash-object --stdin-paths --stdin
60 test_expect_success "Can't pass filenames as arguments with --stdin-paths" '
61 test_must_fail git hash-object --stdin-paths hello < example
64 # Behavior
66 push_repo
68 test_expect_success 'hash a file' '
69 test $hello_sha1 = $(git hash-object hello)
72 test_blob_does_not_exist $hello_sha1
74 test_expect_success 'hash from stdin' '
75 test $example_sha1 = $(git hash-object --stdin < example)
78 test_blob_does_not_exist $example_sha1
80 test_expect_success 'hash a file and write to database' '
81 test $hello_sha1 = $(git hash-object -w hello)
84 test_blob_exists $hello_sha1
86 test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' '
87 echo foo > file1 &&
88 obname0=$(echo bar | git hash-object --stdin) &&
89 obname1=$(git hash-object file1) &&
90 obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
91 obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
92 test "$obname0" = "$obname0new" &&
93 test "$obname1" = "$obname1new"
96 pop_repo
98 for args in "-w --stdin" "--stdin -w"; do
99 push_repo
101 test_expect_success "hash from stdin and write to database ($args)" '
102 test $example_sha1 = $(git hash-object $args < example)
105 test_blob_exists $example_sha1
107 pop_repo
108 done
110 filenames="hello
111 example"
113 sha1s="$hello_sha1
114 $example_sha1"
116 test_expect_success "hash two files with names on stdin" '
117 test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
120 for args in "-w --stdin-paths" "--stdin-paths -w"; do
121 push_repo
123 test_expect_success "hash two files with names on stdin and write to database ($args)" '
124 test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)"
127 test_blob_exists $hello_sha1
128 test_blob_exists $example_sha1
130 pop_repo
131 done
133 test_done