Gerrit Pape | 8a2f5e5 | 2008-02-21 10:06:47 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Nanako Shiraishi | 0cb0e14 | 2008-09-03 17:59:27 +0900 | [diff] [blame] | 3 | test_description="git hash-object" |
Gerrit Pape | 8a2f5e5 | 2008-02-21 10:06:47 +0000 | [diff] [blame] | 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 7 | echo_without_newline() { |
| 8 | printf '%s' "$*" |
| 9 | } |
Gerrit Pape | 8a2f5e5 | 2008-02-21 10:06:47 +0000 | [diff] [blame] | 10 | |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 11 | test_blob_does_not_exist() { |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 12 | test_expect_success 'blob does not exist in database' " |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 13 | test_must_fail git cat-file blob $1 |
| 14 | " |
| 15 | } |
Gerrit Pape | 8a2f5e5 | 2008-02-21 10:06:47 +0000 | [diff] [blame] | 16 | |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 17 | test_blob_exists() { |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 18 | test_expect_success 'blob exists in database' " |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 19 | git cat-file blob $1 |
| 20 | " |
| 21 | } |
| 22 | |
| 23 | hello_content="Hello World" |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 24 | example_content="This is an example" |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 25 | |
| 26 | setup_repo() { |
| 27 | echo_without_newline "$hello_content" > hello |
| 28 | echo_without_newline "$example_content" > example |
| 29 | } |
| 30 | |
| 31 | test_repo=test |
| 32 | push_repo() { |
| 33 | test_create_repo $test_repo |
| 34 | cd $test_repo |
| 35 | |
| 36 | setup_repo |
| 37 | } |
| 38 | |
| 39 | pop_repo() { |
| 40 | cd .. |
| 41 | rm -rf $test_repo |
| 42 | } |
| 43 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 44 | test_expect_success 'setup' ' |
| 45 | setup_repo && |
| 46 | test_oid_cache <<-EOF |
| 47 | hello sha1:5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689 |
| 48 | hello sha256:1e3b6c04d2eeb2b3e45c8a330445404c0b7cc7b257e2b097167d26f5230090c4 |
| 49 | |
| 50 | example sha1:ddd3f836d3e3fbb7ae289aa9ae83536f76956399 |
| 51 | example sha256:b44fe1fe65589848253737db859bd490453510719d7424daab03daf0767b85ae |
| 52 | EOF |
| 53 | ' |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 54 | |
| 55 | # Argument checking |
| 56 | |
| 57 | test_expect_success "multiple '--stdin's are rejected" ' |
Dmitry Potapov | 8101407 | 2008-08-03 18:36:18 +0400 | [diff] [blame] | 58 | echo example | test_must_fail git hash-object --stdin --stdin |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 59 | ' |
| 60 | |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 61 | test_expect_success "Can't use --stdin and --stdin-paths together" ' |
Dmitry Potapov | 8101407 | 2008-08-03 18:36:18 +0400 | [diff] [blame] | 62 | echo example | test_must_fail git hash-object --stdin --stdin-paths && |
| 63 | echo example | test_must_fail git hash-object --stdin-paths --stdin |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 64 | ' |
| 65 | |
| 66 | test_expect_success "Can't pass filenames as arguments with --stdin-paths" ' |
Dmitry Potapov | 8101407 | 2008-08-03 18:36:18 +0400 | [diff] [blame] | 67 | echo example | test_must_fail git hash-object --stdin-paths hello |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 68 | ' |
| 69 | |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 70 | test_expect_success "Can't use --path with --stdin-paths" ' |
| 71 | echo example | test_must_fail git hash-object --stdin-paths --path=foo |
| 72 | ' |
| 73 | |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 74 | test_expect_success "Can't use --path with --no-filters" ' |
| 75 | test_must_fail git hash-object --no-filters --path=foo |
| 76 | ' |
| 77 | |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 78 | # Behavior |
| 79 | |
| 80 | push_repo |
| 81 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 82 | test_expect_success 'hash a file' ' |
| 83 | test "$(test_oid hello)" = $(git hash-object hello) |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 84 | ' |
| 85 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 86 | test_blob_does_not_exist "$(test_oid hello)" |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 87 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 88 | test_expect_success 'hash from stdin' ' |
| 89 | test "$(test_oid example)" = $(git hash-object --stdin < example) |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 90 | ' |
| 91 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 92 | test_blob_does_not_exist "$(test_oid example)" |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 93 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 94 | test_expect_success 'hash a file and write to database' ' |
| 95 | test "$(test_oid hello)" = $(git hash-object -w hello) |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 96 | ' |
| 97 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 98 | test_blob_exists "$(test_oid hello)" |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 99 | |
| 100 | test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' ' |
| 101 | echo foo > file1 && |
| 102 | obname0=$(echo bar | git hash-object --stdin) && |
| 103 | obname1=$(git hash-object file1) && |
| 104 | obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) && |
| 105 | obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) && |
| 106 | test "$obname0" = "$obname0new" && |
| 107 | test "$obname1" = "$obname1new" |
| 108 | ' |
| 109 | |
Jeff King | 4d0efa1 | 2016-09-12 20:23:12 -0700 | [diff] [blame] | 110 | test_expect_success 'set up crlf tests' ' |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 111 | echo fooQ | tr Q "\\015" >file0 && |
| 112 | cp file0 file1 && |
| 113 | echo "file0 -crlf" >.gitattributes && |
| 114 | echo "file1 crlf" >>.gitattributes && |
| 115 | git config core.autocrlf true && |
| 116 | file0_sha=$(git hash-object file0) && |
| 117 | file1_sha=$(git hash-object file1) && |
Jeff King | 4d0efa1 | 2016-09-12 20:23:12 -0700 | [diff] [blame] | 118 | test "$file0_sha" != "$file1_sha" |
| 119 | ' |
| 120 | |
| 121 | test_expect_success 'check that appropriate filter is invoke when --path is used' ' |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 122 | path1_sha=$(git hash-object --path=file1 file0) && |
| 123 | path0_sha=$(git hash-object --path=file0 file1) && |
| 124 | test "$file0_sha" = "$path0_sha" && |
| 125 | test "$file1_sha" = "$path1_sha" && |
| 126 | path1_sha=$(cat file0 | git hash-object --path=file1 --stdin) && |
| 127 | path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) && |
| 128 | test "$file0_sha" = "$path0_sha" && |
Jeff King | 4d0efa1 | 2016-09-12 20:23:12 -0700 | [diff] [blame] | 129 | test "$file1_sha" = "$path1_sha" |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 130 | ' |
| 131 | |
Jeff King | 0e94ee9 | 2016-09-12 20:23:17 -0700 | [diff] [blame] | 132 | test_expect_success 'gitattributes also work in a subdirectory' ' |
| 133 | mkdir subdir && |
| 134 | ( |
| 135 | cd subdir && |
| 136 | subdir_sha0=$(git hash-object ../file0) && |
| 137 | subdir_sha1=$(git hash-object ../file1) && |
| 138 | test "$file0_sha" = "$subdir_sha0" && |
| 139 | test "$file1_sha" = "$subdir_sha1" |
| 140 | ) |
| 141 | ' |
| 142 | |
Jeff King | a1be47e | 2017-03-20 21:20:42 -0400 | [diff] [blame] | 143 | test_expect_success '--path works in a subdirectory' ' |
| 144 | ( |
| 145 | cd subdir && |
| 146 | path1_sha=$(git hash-object --path=../file1 ../file0) && |
| 147 | path0_sha=$(git hash-object --path=../file0 ../file1) && |
| 148 | test "$file0_sha" = "$path0_sha" && |
| 149 | test "$file1_sha" = "$path1_sha" |
| 150 | ) |
| 151 | ' |
| 152 | |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 153 | test_expect_success 'check that --no-filters option works' ' |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 154 | nofilters_file1=$(git hash-object --no-filters file1) && |
| 155 | test "$file0_sha" = "$nofilters_file1" && |
| 156 | nofilters_file1=$(cat file1 | git hash-object --stdin) && |
Jeff King | 4d0efa1 | 2016-09-12 20:23:12 -0700 | [diff] [blame] | 157 | test "$file0_sha" = "$nofilters_file1" |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 158 | ' |
| 159 | |
Erik Faye-Lund | a9f9790 | 2010-03-03 21:10:21 +0100 | [diff] [blame] | 160 | test_expect_success 'check that --no-filters option works with --stdin-paths' ' |
Erik Faye-Lund | a9f9790 | 2010-03-03 21:10:21 +0100 | [diff] [blame] | 161 | nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) && |
Jeff King | 4d0efa1 | 2016-09-12 20:23:12 -0700 | [diff] [blame] | 162 | test "$file0_sha" = "$nofilters_file1" |
Erik Faye-Lund | a9f9790 | 2010-03-03 21:10:21 +0100 | [diff] [blame] | 163 | ' |
| 164 | |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 165 | pop_repo |
| 166 | |
| 167 | for args in "-w --stdin" "--stdin -w"; do |
| 168 | push_repo |
| 169 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 170 | test_expect_success "hash from stdin and write to database ($args)" ' |
| 171 | test "$(test_oid example)" = $(git hash-object $args < example) |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 172 | ' |
| 173 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 174 | test_blob_exists "$(test_oid example)" |
Adam Roben | 3ea5a1b | 2008-05-23 16:19:37 +0200 | [diff] [blame] | 175 | |
| 176 | pop_repo |
| 177 | done |
Gerrit Pape | 8a2f5e5 | 2008-02-21 10:06:47 +0000 | [diff] [blame] | 178 | |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 179 | filenames="hello |
| 180 | example" |
| 181 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 182 | oids="$(test_oid hello) |
| 183 | $(test_oid example)" |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 184 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 185 | test_expect_success "hash two files with names on stdin" ' |
| 186 | test "$oids" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)" |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 187 | ' |
| 188 | |
| 189 | for args in "-w --stdin-paths" "--stdin-paths -w"; do |
| 190 | push_repo |
| 191 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 192 | test_expect_success "hash two files with names on stdin and write to database ($args)" ' |
| 193 | test "$oids" = "$(echo_without_newline "$filenames" | git hash-object $args)" |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 194 | ' |
| 195 | |
brian m. carlson | 2e306f6 | 2019-06-28 22:59:26 +0000 | [diff] [blame] | 196 | test_blob_exists "$(test_oid hello)" |
| 197 | test_blob_exists "$(test_oid example)" |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 198 | |
| 199 | pop_repo |
| 200 | done |
| 201 | |
Jeff King | 2edffef | 2016-09-27 16:59:50 -0400 | [diff] [blame] | 202 | test_expect_success 'too-short tree' ' |
Ramkumar Ramachandra | 0238038 | 2011-12-08 18:40:17 +0530 | [diff] [blame] | 203 | echo abc >malformed-tree && |
Jeff King | 2edffef | 2016-09-27 16:59:50 -0400 | [diff] [blame] | 204 | test_must_fail git hash-object -t tree malformed-tree 2>err && |
| 205 | test_i18ngrep "too-short tree object" err |
| 206 | ' |
| 207 | |
Jeff King | 2edffef | 2016-09-27 16:59:50 -0400 | [diff] [blame] | 208 | test_expect_success 'malformed mode in tree' ' |
| 209 | hex_sha1=$(echo foo | git hash-object --stdin -w) && |
| 210 | bin_sha1=$(echo $hex_sha1 | hex2oct) && |
| 211 | printf "9100644 \0$bin_sha1" >tree-with-malformed-mode && |
| 212 | test_must_fail git hash-object -t tree tree-with-malformed-mode 2>err && |
| 213 | test_i18ngrep "malformed mode in tree entry" err |
| 214 | ' |
| 215 | |
| 216 | test_expect_success 'empty filename in tree' ' |
| 217 | hex_sha1=$(echo foo | git hash-object --stdin -w) && |
| 218 | bin_sha1=$(echo $hex_sha1 | hex2oct) && |
| 219 | printf "100644 \0$bin_sha1" >tree-with-empty-filename && |
| 220 | test_must_fail git hash-object -t tree tree-with-empty-filename 2>err && |
| 221 | test_i18ngrep "empty filename in tree entry" err |
Nguyễn Thái Ngọc Duy | c879daa | 2011-02-05 17:52:21 +0700 | [diff] [blame] | 222 | ' |
| 223 | |
| 224 | test_expect_success 'corrupt commit' ' |
| 225 | test_must_fail git hash-object -t commit --stdin </dev/null |
| 226 | ' |
| 227 | |
| 228 | test_expect_success 'corrupt tag' ' |
| 229 | test_must_fail git hash-object -t tag --stdin </dev/null |
| 230 | ' |
| 231 | |
Jeff King | b7994af | 2015-04-17 10:52:48 -0400 | [diff] [blame] | 232 | test_expect_success 'hash-object complains about bogus type name' ' |
| 233 | test_must_fail git hash-object -t bogus --stdin </dev/null |
| 234 | ' |
| 235 | |
| 236 | test_expect_success 'hash-object complains about truncated type name' ' |
| 237 | test_must_fail git hash-object -t bl --stdin </dev/null |
| 238 | ' |
| 239 | |
Eric Sunshine | 383c342 | 2015-05-04 03:25:14 -0400 | [diff] [blame] | 240 | test_expect_success '--literally' ' |
| 241 | t=1234567890 && |
| 242 | echo example | git hash-object -t $t --literally --stdin |
| 243 | ' |
| 244 | |
| 245 | test_expect_success '--literally with extra-long type' ' |
| 246 | t=12345678901234567890123456789012345678901234567890 && |
| 247 | 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" && |
| 248 | echo example | git hash-object -t $t --literally --stdin |
| 249 | ' |
| 250 | |
Gerrit Pape | 8a2f5e5 | 2008-02-21 10:06:47 +0000 | [diff] [blame] | 251 | test_done |