]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1007-hash-object.sh
svn: properly escape arguments for authors-prog
[thirdparty/git.git] / t / t1007-hash-object.sh
1 #!/bin/sh
2
3 test_description="git hash-object"
4
5 . ./test-lib.sh
6
7 echo_without_newline() {
8 printf '%s' "$*"
9 }
10
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
14 "
15 }
16
17 test_blob_exists() {
18 test_expect_success 'blob exists in database' "
19 git cat-file blob $1
20 "
21 }
22
23 hello_content="Hello World"
24 hello_sha1=5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
25
26 example_content="This is an example"
27 example_sha1=ddd3f836d3e3fbb7ae289aa9ae83536f76956399
28
29 setup_repo() {
30 echo_without_newline "$hello_content" > hello
31 echo_without_newline "$example_content" > example
32 }
33
34 test_repo=test
35 push_repo() {
36 test_create_repo $test_repo
37 cd $test_repo
38
39 setup_repo
40 }
41
42 pop_repo() {
43 cd ..
44 rm -rf $test_repo
45 }
46
47 setup_repo
48
49 # Argument checking
50
51 test_expect_success "multiple '--stdin's are rejected" '
52 echo example | test_must_fail git hash-object --stdin --stdin
53 '
54
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
58 '
59
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
62 '
63
64 test_expect_success "Can't use --path with --stdin-paths" '
65 echo example | test_must_fail git hash-object --stdin-paths --path=foo
66 '
67
68 test_expect_success "Can't use --stdin-paths with --no-filters" '
69 echo example | test_must_fail git hash-object --stdin-paths --no-filters
70 '
71
72 test_expect_success "Can't use --path with --no-filters" '
73 test_must_fail git hash-object --no-filters --path=foo
74 '
75
76 # Behavior
77
78 push_repo
79
80 test_expect_success 'hash a file' '
81 test $hello_sha1 = $(git hash-object hello)
82 '
83
84 test_blob_does_not_exist $hello_sha1
85
86 test_expect_success 'hash from stdin' '
87 test $example_sha1 = $(git hash-object --stdin < example)
88 '
89
90 test_blob_does_not_exist $example_sha1
91
92 test_expect_success 'hash a file and write to database' '
93 test $hello_sha1 = $(git hash-object -w hello)
94 '
95
96 test_blob_exists $hello_sha1
97
98 test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' '
99 echo foo > file1 &&
100 obname0=$(echo bar | git hash-object --stdin) &&
101 obname1=$(git hash-object file1) &&
102 obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
103 obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
104 test "$obname0" = "$obname0new" &&
105 test "$obname1" = "$obname1new"
106 '
107
108 test_expect_success 'check that appropriate filter is invoke when --path is used' '
109 echo fooQ | tr Q "\\015" >file0 &&
110 cp file0 file1 &&
111 echo "file0 -crlf" >.gitattributes &&
112 echo "file1 crlf" >>.gitattributes &&
113 git config core.autocrlf true &&
114 file0_sha=$(git hash-object file0) &&
115 file1_sha=$(git hash-object file1) &&
116 test "$file0_sha" != "$file1_sha" &&
117 path1_sha=$(git hash-object --path=file1 file0) &&
118 path0_sha=$(git hash-object --path=file0 file1) &&
119 test "$file0_sha" = "$path0_sha" &&
120 test "$file1_sha" = "$path1_sha" &&
121 path1_sha=$(cat file0 | git hash-object --path=file1 --stdin) &&
122 path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) &&
123 test "$file0_sha" = "$path0_sha" &&
124 test "$file1_sha" = "$path1_sha" &&
125 git config --unset core.autocrlf
126 '
127
128 test_expect_success 'check that --no-filters option works' '
129 echo fooQ | tr Q "\\015" >file0 &&
130 cp file0 file1 &&
131 echo "file0 -crlf" >.gitattributes &&
132 echo "file1 crlf" >>.gitattributes &&
133 git config core.autocrlf true &&
134 file0_sha=$(git hash-object file0) &&
135 file1_sha=$(git hash-object file1) &&
136 test "$file0_sha" != "$file1_sha" &&
137 nofilters_file1=$(git hash-object --no-filters file1) &&
138 test "$file0_sha" = "$nofilters_file1" &&
139 nofilters_file1=$(cat file1 | git hash-object --stdin) &&
140 test "$file0_sha" = "$nofilters_file1" &&
141 git config --unset core.autocrlf
142 '
143
144 pop_repo
145
146 for args in "-w --stdin" "--stdin -w"; do
147 push_repo
148
149 test_expect_success "hash from stdin and write to database ($args)" '
150 test $example_sha1 = $(git hash-object $args < example)
151 '
152
153 test_blob_exists $example_sha1
154
155 pop_repo
156 done
157
158 filenames="hello
159 example"
160
161 sha1s="$hello_sha1
162 $example_sha1"
163
164 test_expect_success "hash two files with names on stdin" '
165 test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
166 '
167
168 for args in "-w --stdin-paths" "--stdin-paths -w"; do
169 push_repo
170
171 test_expect_success "hash two files with names on stdin and write to database ($args)" '
172 test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)"
173 '
174
175 test_blob_exists $hello_sha1
176 test_blob_exists $example_sha1
177
178 pop_repo
179 done
180
181 test_done