Merge branch 'ds/mailmap'
[git/debian.git] / t / t1007-hash-object.sh
blobac5ad8c7402d2bd3f43d38e5a676d864ea415d37
1 #!/bin/sh
3 test_description="git hash-object"
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 echo_without_newline() {
9 printf '%s' "$*"
12 test_blob_does_not_exist() {
13 test_expect_success 'blob does not exist in database' "
14 test_must_fail git cat-file blob $1
18 test_blob_exists() {
19 test_expect_success 'blob exists in database' "
20 git cat-file blob $1
24 hello_content="Hello World"
25 example_content="This is an example"
27 setup_repo() {
28 echo_without_newline "$hello_content" > hello
29 echo_without_newline "$example_content" > example
32 test_repo=test
33 push_repo() {
34 test_create_repo $test_repo
35 cd $test_repo
37 setup_repo
40 pop_repo() {
41 cd ..
42 rm -rf $test_repo
45 test_expect_success 'setup' '
46 setup_repo &&
47 test_oid_cache <<-EOF
48 hello sha1:5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
49 hello sha256:1e3b6c04d2eeb2b3e45c8a330445404c0b7cc7b257e2b097167d26f5230090c4
51 example sha1:ddd3f836d3e3fbb7ae289aa9ae83536f76956399
52 example sha256:b44fe1fe65589848253737db859bd490453510719d7424daab03daf0767b85ae
53 EOF
56 # Argument checking
58 test_expect_success "multiple '--stdin's are rejected" '
59 echo example | test_must_fail git hash-object --stdin --stdin
62 test_expect_success "Can't use --stdin and --stdin-paths together" '
63 echo example | test_must_fail git hash-object --stdin --stdin-paths &&
64 echo example | test_must_fail git hash-object --stdin-paths --stdin
67 test_expect_success "Can't pass filenames as arguments with --stdin-paths" '
68 echo example | test_must_fail git hash-object --stdin-paths hello
71 test_expect_success "Can't use --path with --stdin-paths" '
72 echo example | test_must_fail git hash-object --stdin-paths --path=foo
75 test_expect_success "Can't use --path with --no-filters" '
76 test_must_fail git hash-object --no-filters --path=foo
79 # Behavior
81 push_repo
83 test_expect_success 'hash a file' '
84 test "$(test_oid hello)" = $(git hash-object hello)
87 test_blob_does_not_exist "$(test_oid hello)"
89 test_expect_success 'hash from stdin' '
90 test "$(test_oid example)" = $(git hash-object --stdin < example)
93 test_blob_does_not_exist "$(test_oid example)"
95 test_expect_success 'hash a file and write to database' '
96 test "$(test_oid hello)" = $(git hash-object -w hello)
99 test_blob_exists "$(test_oid hello)"
101 test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' '
102 echo foo > file1 &&
103 obname0=$(echo bar | git hash-object --stdin) &&
104 obname1=$(git hash-object file1) &&
105 obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
106 obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
107 test "$obname0" = "$obname0new" &&
108 test "$obname1" = "$obname1new"
111 test_expect_success 'set up crlf tests' '
112 echo fooQ | tr Q "\\015" >file0 &&
113 cp file0 file1 &&
114 echo "file0 -crlf" >.gitattributes &&
115 echo "file1 crlf" >>.gitattributes &&
116 git config core.autocrlf true &&
117 file0_sha=$(git hash-object file0) &&
118 file1_sha=$(git hash-object file1) &&
119 test "$file0_sha" != "$file1_sha"
122 test_expect_success 'check that appropriate filter is invoke when --path is used' '
123 path1_sha=$(git hash-object --path=file1 file0) &&
124 path0_sha=$(git hash-object --path=file0 file1) &&
125 test "$file0_sha" = "$path0_sha" &&
126 test "$file1_sha" = "$path1_sha" &&
127 path1_sha=$(cat file0 | git hash-object --path=file1 --stdin) &&
128 path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) &&
129 test "$file0_sha" = "$path0_sha" &&
130 test "$file1_sha" = "$path1_sha"
133 test_expect_success 'gitattributes also work in a subdirectory' '
134 mkdir subdir &&
136 cd subdir &&
137 subdir_sha0=$(git hash-object ../file0) &&
138 subdir_sha1=$(git hash-object ../file1) &&
139 test "$file0_sha" = "$subdir_sha0" &&
140 test "$file1_sha" = "$subdir_sha1"
144 test_expect_success '--path works in a subdirectory' '
146 cd subdir &&
147 path1_sha=$(git hash-object --path=../file1 ../file0) &&
148 path0_sha=$(git hash-object --path=../file0 ../file1) &&
149 test "$file0_sha" = "$path0_sha" &&
150 test "$file1_sha" = "$path1_sha"
154 test_expect_success 'check that --no-filters option works' '
155 nofilters_file1=$(git hash-object --no-filters file1) &&
156 test "$file0_sha" = "$nofilters_file1" &&
157 nofilters_file1=$(cat file1 | git hash-object --stdin) &&
158 test "$file0_sha" = "$nofilters_file1"
161 test_expect_success 'check that --no-filters option works with --stdin-paths' '
162 nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) &&
163 test "$file0_sha" = "$nofilters_file1"
166 pop_repo
168 for args in "-w --stdin" "--stdin -w"; do
169 push_repo
171 test_expect_success "hash from stdin and write to database ($args)" '
172 test "$(test_oid example)" = $(git hash-object $args < example)
175 test_blob_exists "$(test_oid example)"
177 pop_repo
178 done
180 filenames="hello
181 example"
183 oids="$(test_oid hello)
184 $(test_oid example)"
186 test_expect_success "hash two files with names on stdin" '
187 test "$oids" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
190 for args in "-w --stdin-paths" "--stdin-paths -w"; do
191 push_repo
193 test_expect_success "hash two files with names on stdin and write to database ($args)" '
194 test "$oids" = "$(echo_without_newline "$filenames" | git hash-object $args)"
197 test_blob_exists "$(test_oid hello)"
198 test_blob_exists "$(test_oid example)"
200 pop_repo
201 done
203 test_expect_success 'too-short tree' '
204 echo abc >malformed-tree &&
205 test_must_fail git hash-object -t tree malformed-tree 2>err &&
206 test_i18ngrep "too-short tree object" err
209 test_expect_success 'malformed mode in tree' '
210 hex_sha1=$(echo foo | git hash-object --stdin -w) &&
211 bin_sha1=$(echo $hex_sha1 | hex2oct) &&
212 printf "9100644 \0$bin_sha1" >tree-with-malformed-mode &&
213 test_must_fail git hash-object -t tree tree-with-malformed-mode 2>err &&
214 test_i18ngrep "malformed mode in tree entry" err
217 test_expect_success 'empty filename in tree' '
218 hex_sha1=$(echo foo | git hash-object --stdin -w) &&
219 bin_sha1=$(echo $hex_sha1 | hex2oct) &&
220 printf "100644 \0$bin_sha1" >tree-with-empty-filename &&
221 test_must_fail git hash-object -t tree tree-with-empty-filename 2>err &&
222 test_i18ngrep "empty filename in tree entry" err
225 test_expect_success 'corrupt commit' '
226 test_must_fail git hash-object -t commit --stdin </dev/null
229 test_expect_success 'corrupt tag' '
230 test_must_fail git hash-object -t tag --stdin </dev/null
233 test_expect_success 'hash-object complains about bogus type name' '
234 test_must_fail git hash-object -t bogus --stdin </dev/null
237 test_expect_success 'hash-object complains about truncated type name' '
238 test_must_fail git hash-object -t bl --stdin </dev/null
241 test_expect_success '--literally' '
242 t=1234567890 &&
243 echo example | git hash-object -t $t --literally --stdin
246 test_expect_success '--literally with extra-long type' '
247 t=12345678901234567890123456789012345678901234567890 &&
248 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" &&
249 echo example | git hash-object -t $t --literally --stdin
252 test_done