Documentation/stash: remove mention of git reset --hard
[git.git] / t / t1007-hash-object.sh
blobc5245c5cb4c1c086b6fe7205f34b9b5ca53e2e59
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 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
72 # Behavior
74 push_repo
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' '
95 echo foo > 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 'set up crlf tests' '
105 echo fooQ | tr Q "\\015" >file0 &&
106 cp file0 file1 &&
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"
115 test_expect_success 'check that appropriate filter is invoke when --path is used' '
116 path1_sha=$(git hash-object --path=file1 file0) &&
117 path0_sha=$(git hash-object --path=file0 file1) &&
118 test "$file0_sha" = "$path0_sha" &&
119 test "$file1_sha" = "$path1_sha" &&
120 path1_sha=$(cat file0 | git hash-object --path=file1 --stdin) &&
121 path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) &&
122 test "$file0_sha" = "$path0_sha" &&
123 test "$file1_sha" = "$path1_sha"
126 test_expect_success 'gitattributes also work in a subdirectory' '
127 mkdir subdir &&
129 cd subdir &&
130 subdir_sha0=$(git hash-object ../file0) &&
131 subdir_sha1=$(git hash-object ../file1) &&
132 test "$file0_sha" = "$subdir_sha0" &&
133 test "$file1_sha" = "$subdir_sha1"
137 test_expect_success 'check that --no-filters option works' '
138 nofilters_file1=$(git hash-object --no-filters file1) &&
139 test "$file0_sha" = "$nofilters_file1" &&
140 nofilters_file1=$(cat file1 | git hash-object --stdin) &&
141 test "$file0_sha" = "$nofilters_file1"
144 test_expect_success 'check that --no-filters option works with --stdin-paths' '
145 nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) &&
146 test "$file0_sha" = "$nofilters_file1"
149 pop_repo
151 for args in "-w --stdin" "--stdin -w"; do
152 push_repo
154 test_expect_success "hash from stdin and write to database ($args)" '
155 test $example_sha1 = $(git hash-object $args < example)
158 test_blob_exists $example_sha1
160 pop_repo
161 done
163 filenames="hello
164 example"
166 sha1s="$hello_sha1
167 $example_sha1"
169 test_expect_success "hash two files with names on stdin" '
170 test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
173 for args in "-w --stdin-paths" "--stdin-paths -w"; do
174 push_repo
176 test_expect_success "hash two files with names on stdin and write to database ($args)" '
177 test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)"
180 test_blob_exists $hello_sha1
181 test_blob_exists $example_sha1
183 pop_repo
184 done
186 test_expect_success 'too-short tree' '
187 echo abc >malformed-tree &&
188 test_must_fail git hash-object -t tree malformed-tree 2>err &&
189 test_i18ngrep "too-short tree object" err
192 hex2oct() {
193 perl -ne 'printf "\\%03o", hex for /../g'
196 test_expect_success 'malformed mode in tree' '
197 hex_sha1=$(echo foo | git hash-object --stdin -w) &&
198 bin_sha1=$(echo $hex_sha1 | hex2oct) &&
199 printf "9100644 \0$bin_sha1" >tree-with-malformed-mode &&
200 test_must_fail git hash-object -t tree tree-with-malformed-mode 2>err &&
201 test_i18ngrep "malformed mode in tree entry" err
204 test_expect_success 'empty filename in tree' '
205 hex_sha1=$(echo foo | git hash-object --stdin -w) &&
206 bin_sha1=$(echo $hex_sha1 | hex2oct) &&
207 printf "100644 \0$bin_sha1" >tree-with-empty-filename &&
208 test_must_fail git hash-object -t tree tree-with-empty-filename 2>err &&
209 test_i18ngrep "empty filename in tree entry" err
212 test_expect_success 'corrupt commit' '
213 test_must_fail git hash-object -t commit --stdin </dev/null
216 test_expect_success 'corrupt tag' '
217 test_must_fail git hash-object -t tag --stdin </dev/null
220 test_expect_success 'hash-object complains about bogus type name' '
221 test_must_fail git hash-object -t bogus --stdin </dev/null
224 test_expect_success 'hash-object complains about truncated type name' '
225 test_must_fail git hash-object -t bl --stdin </dev/null
228 test_expect_success '--literally' '
229 t=1234567890 &&
230 echo example | git hash-object -t $t --literally --stdin
233 test_expect_success '--literally with extra-long type' '
234 t=12345678901234567890123456789012345678901234567890 &&
235 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" &&
236 echo example | git hash-object -t $t --literally --stdin
239 test_done