3 test_description="git hash-object"
7 echo_without_newline() {
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
18 test_expect_success 'blob exists in database' "
23 hello_content="Hello World"
24 hello_sha1=5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
26 example_content="This is an example"
27 example_sha1=ddd3f836d3e3fbb7ae289aa9ae83536f76956399
30 echo_without_newline "$hello_content" > hello
31 echo_without_newline "$example_content" > example
36 test_create_repo $test_repo
51 test_expect_success "multiple '--stdin's are rejected" '
52 echo example | test_must_fail git hash-object --stdin --stdin
55 test_expect_success "Can't use --stdin and --stdin-paths together" '
56 echo example | test_must_fail git hash-object --stdin --stdin-paths &&
57 echo example | test_must_fail git hash-object --stdin-paths --stdin
60 test_expect_success "Can't pass filenames as arguments with --stdin-paths" '
61 echo example | test_must_fail git hash-object --stdin-paths hello
64 test_expect_success "Can't use --path with --stdin-paths" '
65 echo example | test_must_fail git hash-object --stdin-paths --path=foo
68 test_expect_success "Can't use --path with --no-filters" '
69 test_must_fail git hash-object --no-filters --path=foo
76 test_expect_success 'hash a file' '
77 test $hello_sha1 = $(git hash-object hello)
80 test_blob_does_not_exist $hello_sha1
82 test_expect_success 'hash from stdin' '
83 test $example_sha1 = $(git hash-object --stdin < example)
86 test_blob_does_not_exist $example_sha1
88 test_expect_success 'hash a file and write to database' '
89 test $hello_sha1 = $(git hash-object -w hello)
92 test_blob_exists $hello_sha1
94 test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' '
96 obname0=$(echo bar | git hash-object --stdin) &&
97 obname1=$(git hash-object file1) &&
98 obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
99 obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
100 test "$obname0" = "$obname0new" &&
101 test "$obname1" = "$obname1new"
104 test_expect_success 'check that appropriate filter is invoke when --path is used' '
105 echo fooQ | tr Q "\\015" >file0 &&
107 echo "file0 -crlf" >.gitattributes &&
108 echo "file1 crlf" >>.gitattributes &&
109 git config core.autocrlf true &&
110 file0_sha=$(git hash-object file0) &&
111 file1_sha=$(git hash-object file1) &&
112 test "$file0_sha" != "$file1_sha" &&
113 path1_sha=$(git hash-object --path=file1 file0) &&
114 path0_sha=$(git hash-object --path=file0 file1) &&
115 test "$file0_sha" = "$path0_sha" &&
116 test "$file1_sha" = "$path1_sha" &&
117 path1_sha=$(cat file0 | git hash-object --path=file1 --stdin) &&
118 path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) &&
119 test "$file0_sha" = "$path0_sha" &&
120 test "$file1_sha" = "$path1_sha" &&
121 git config --unset core.autocrlf
124 test_expect_success 'gitattributes also work in a subdirectory' '
128 subdir_sha0=$(git hash-object ../file0) &&
129 subdir_sha1=$(git hash-object ../file1) &&
130 test "$file0_sha" = "$subdir_sha0" &&
131 test "$file1_sha" = "$subdir_sha1"
135 test_expect_success 'check that --no-filters option works' '
136 echo fooQ | tr Q "\\015" >file0 &&
138 echo "file0 -crlf" >.gitattributes &&
139 echo "file1 crlf" >>.gitattributes &&
140 git config core.autocrlf true &&
141 file0_sha=$(git hash-object file0) &&
142 file1_sha=$(git hash-object file1) &&
143 test "$file0_sha" != "$file1_sha" &&
144 nofilters_file1=$(git hash-object --no-filters file1) &&
145 test "$file0_sha" = "$nofilters_file1" &&
146 nofilters_file1=$(cat file1 | git hash-object --stdin) &&
147 test "$file0_sha" = "$nofilters_file1" &&
148 git config --unset core.autocrlf
151 test_expect_success 'check that --no-filters option works with --stdin-paths' '
152 echo fooQ | tr Q "\\015" >file0 &&
154 echo "file0 -crlf" >.gitattributes &&
155 echo "file1 crlf" >>.gitattributes &&
156 git config core.autocrlf true &&
157 file0_sha=$(git hash-object file0) &&
158 file1_sha=$(git hash-object file1) &&
159 test "$file0_sha" != "$file1_sha" &&
160 nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) &&
161 test "$file0_sha" = "$nofilters_file1" &&
162 git config --unset core.autocrlf
167 for args in "-w --stdin" "--stdin -w"; do
170 test_expect_success "hash from stdin and write to database ($args)" '
171 test $example_sha1 = $(git hash-object $args < example)
174 test_blob_exists $example_sha1
185 test_expect_success "hash two files with names on stdin" '
186 test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
189 for args in "-w --stdin-paths" "--stdin-paths -w"; do
192 test_expect_success "hash two files with names on stdin and write to database ($args)" '
193 test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)"
196 test_blob_exists $hello_sha1
197 test_blob_exists $example_sha1
202 test_expect_success 'corrupt tree' '
203 echo abc >malformed-tree &&
204 test_must_fail git hash-object -t tree malformed-tree
207 test_expect_success 'corrupt commit' '
208 test_must_fail git hash-object -t commit --stdin </dev/null
211 test_expect_success 'corrupt tag' '
212 test_must_fail git hash-object -t tag --stdin </dev/null
215 test_expect_success 'hash-object complains about bogus type name' '
216 test_must_fail git hash-object -t bogus --stdin </dev/null
219 test_expect_success 'hash-object complains about truncated type name' '
220 test_must_fail git hash-object -t bl --stdin </dev/null
223 test_expect_success '--literally' '
225 echo example | git hash-object -t $t --literally --stdin
228 test_expect_success '--literally with extra-long type' '
229 t=12345678901234567890123456789012345678901234567890 &&
230 t="$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t" &&
231 echo example | git hash-object -t $t --literally --stdin