Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 #
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 #
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 . ./common.sh
19 test_cat_basic() {
20 local testroot=`test_init cat_basic`
21 local commit_id=`git_show_head $testroot/repo`
22 local author_time=`git_show_author_time $testroot/repo`
23 local gmtoff=`date +%z`
24 local alpha_id=`got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1`
25 local gamma_id=`got tree -r $testroot/repo -i | grep 'gamma/$' | cut -d' ' -f 1`
26 local delta_id=`got tree -r $testroot/repo -i gamma | grep 'delta$' | cut -d' ' -f 1`
28 # cat blob
29 echo "alpha" > $testroot/stdout.expected
30 got cat -r $testroot/repo $alpha_id > $testroot/stdout
31 cmp -s $testroot/stdout.expected $testroot/stdout
32 ret=$?
33 if [ $ret -ne 0 ]; then
34 diff -u $testroot/stdout.expected $testroot/stdout
35 test_done "$testroot" "$ret"
36 return 1
37 fi
39 # cat tree
40 echo "$delta_id 0100644 delta" > $testroot/stdout.expected
41 got cat -r $testroot/repo $gamma_id > $testroot/stdout
42 cmp -s $testroot/stdout.expected $testroot/stdout
43 ret=$?
44 if [ $ret -ne 0 ]; then
45 diff -u $testroot/stdout.expected $testroot/stdout
46 test_done "$testroot" "$ret"
47 return 1
48 fi
50 # cat commit
51 echo -n "tree " > $testroot/stdout.expected
52 git_show_tree $testroot/repo >> $testroot/stdout.expected
53 echo >> $testroot/stdout.expected
54 echo "numparents 0" >> $testroot/stdout.expected
55 echo "author $GOT_AUTHOR $author_time $gmtoff" >> $testroot/stdout.expected
56 echo "committer $GOT_AUTHOR $author_time $gmtoff" \
57 >> $testroot/stdout.expected
58 echo "messagelen 22" >> $testroot/stdout.expected
59 printf "\nadding the test tree\n" >> $testroot/stdout.expected
61 got cat -r $testroot/repo $commit_id > $testroot/stdout
62 cmp -s $testroot/stdout.expected $testroot/stdout
63 ret=$?
64 if [ $ret -ne 0 ]; then
65 diff -u $testroot/stdout.expected $testroot/stdout
66 test_done "$testroot" "$ret"
67 return 1
68 fi
70 # TODO: test cat tag
72 test_done "$testroot" "$ret"
73 }
75 test_cat_path() {
76 local testroot=`test_init cat_path`
77 local commit_id=`git_show_head $testroot/repo`
78 local author_time=`git_show_author_time $testroot/repo`
79 local alpha_id=`got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1`
80 local gamma_id=`got tree -r $testroot/repo -i | grep 'gamma/$' | cut -d' ' -f 1`
81 local delta_id=`got tree -r $testroot/repo -i gamma | grep 'delta$' | cut -d' ' -f 1`
83 # cat blob by path
84 echo "alpha" > $testroot/stdout.expected
85 got cat -r $testroot/repo alpha > $testroot/stdout
86 cmp -s $testroot/stdout.expected $testroot/stdout
87 ret=$?
88 if [ $ret -ne 0 ]; then
89 diff -u $testroot/stdout.expected $testroot/stdout
90 test_done "$testroot" "$ret"
91 return 1
92 fi
94 # cat tree by path
95 echo "$delta_id 0100644 delta" > $testroot/stdout.expected
96 got cat -r $testroot/repo gamma > $testroot/stdout
97 cmp -s $testroot/stdout.expected $testroot/stdout
98 ret=$?
99 if [ $ret -ne 0 ]; then
100 diff -u $testroot/stdout.expected $testroot/stdout
101 test_done "$testroot" "$ret"
102 return 1
103 fi
105 (cd $testroot && got checkout repo wt > /dev/null)
106 echo "modified alpha" > $testroot/wt/alpha
107 (cd $testroot/wt && got commit -m "changed alpha" > /dev/null)
108 local commit_id2=`git_show_head $testroot/repo`
109 local author_time2=`git_show_author_time $testroot/repo`
110 local tree_commit2=`git_show_tree $testroot/repo`
112 # cat blob by path in specific commit
113 echo "alpha" > $testroot/stdout.expected
114 got cat -r $testroot/repo -c $commit_id alpha > $testroot/stdout
115 cmp -s $testroot/stdout.expected $testroot/stdout
116 ret=$?
117 if [ $ret -ne 0 ]; then
118 diff -u $testroot/stdout.expected $testroot/stdout
119 test_done "$testroot" "$ret"
120 return 1
121 fi
122 echo "modified alpha" > $testroot/stdout.expected
123 got cat -r $testroot/repo -c $commit_id2 alpha > $testroot/stdout
124 cmp -s $testroot/stdout.expected $testroot/stdout
125 ret=$?
126 if [ $ret -ne 0 ]; then
127 diff -u $testroot/stdout.expected $testroot/stdout
128 test_done "$testroot" "$ret"
129 return 1
130 fi
132 # resolve ambiguities between paths and other arguments
133 echo "new file called master" > $testroot/wt/master
134 echo "new file called $commit_id2" > $testroot/wt/$commit_id2
135 (cd $testroot/wt && got add master $commit_id2 > /dev/null)
136 (cd $testroot/wt && got commit -m "added clashing paths" > /dev/null)
137 local commit_id3=`git_show_head $testroot/repo`
138 local author_time3=`git_show_author_time $testroot/repo`
140 # references and object IDs override paths:
141 echo -n "tree " > $testroot/stdout.expected
142 git_show_tree $testroot/repo >> $testroot/stdout.expected
143 echo >> $testroot/stdout.expected
144 echo "numparents 1" >> $testroot/stdout.expected
145 echo "parent $commit_id2" >> $testroot/stdout.expected
146 echo "author $GOT_AUTHOR $author_time3 +0000" >> $testroot/stdout.expected
147 echo "committer $GOT_AUTHOR $author_time3 +0000" \
148 >> $testroot/stdout.expected
149 echo "messagelen 22" >> $testroot/stdout.expected
150 printf "\nadded clashing paths\n" >> $testroot/stdout.expected
152 for arg in master $commit_id3; do
153 got cat -r $testroot/repo $arg > $testroot/stdout
154 cmp -s $testroot/stdout.expected $testroot/stdout
155 ret=$?
156 if [ $ret -ne 0 ]; then
157 diff -u $testroot/stdout.expected $testroot/stdout
158 test_done "$testroot" "$ret"
159 return 1
160 fi
161 done
163 echo "tree $tree_commit2" > $testroot/stdout.expected
164 echo "numparents 1" >> $testroot/stdout.expected
165 echo "parent $commit_id" >> $testroot/stdout.expected
166 echo "author $GOT_AUTHOR $author_time2 +0000" >> $testroot/stdout.expected
167 echo "committer $GOT_AUTHOR $author_time2 +0000" \
168 >> $testroot/stdout.expected
169 echo "messagelen 15" >> $testroot/stdout.expected
170 printf "\nchanged alpha\n" >> $testroot/stdout.expected
172 got cat -r $testroot/repo $commit_id2 > $testroot/stdout
173 cmp -s $testroot/stdout.expected $testroot/stdout
174 ret=$?
175 if [ $ret -ne 0 ]; then
176 diff -u $testroot/stdout.expected $testroot/stdout
177 test_done "$testroot" "$ret"
178 return 1
179 fi
181 # force resolution of path 'master'
182 echo "new file called master" > $testroot/stdout.expected
183 got cat -r $testroot/repo -P master > $testroot/stdout
184 cmp -s $testroot/stdout.expected $testroot/stdout
185 ret=$?
186 if [ $ret -ne 0 ]; then
187 diff -u $testroot/stdout.expected $testroot/stdout
188 test_done "$testroot" "$ret"
189 return 1
190 fi
192 # force resolution of path "$commit_id2"
193 echo "new file called $commit_id2" > $testroot/stdout.expected
194 got cat -r $testroot/repo -P $commit_id2 > $testroot/stdout
195 cmp -s $testroot/stdout.expected $testroot/stdout
196 ret=$?
197 if [ $ret -ne 0 ]; then
198 diff -u $testroot/stdout.expected $testroot/stdout
199 test_done "$testroot" "$ret"
200 return 1
201 fi
202 test_done "$testroot" "$ret"
205 test_cat_submodule() {
206 local testroot=`test_init cat_submodule`
208 make_single_file_repo $testroot/repo2 foo
210 (cd $testroot/repo && git -c protocol.file.allow=always \
211 submodule -q add ../repo2)
212 (cd $testroot/repo && git commit -q -m 'adding submodule')
214 got cat -r $testroot/repo repo2 > $testroot/stdout \
215 > $testroot/stdout 2> $testroot/stderr
216 ret=$?
217 if [ $ret -eq 0 ]; then
218 echo "cat command succeeded unexpectedly" >&2
219 test_done "$testroot" "1"
220 return 1
221 fi
222 local submodule_id=$(got tree -r $testroot/repo -i | \
223 grep 'repo2\$$' | cut -d ' ' -f1)
224 echo "got: object $submodule_id not found" > $testroot/stderr.expected
226 cmp -s $testroot/stderr.expected $testroot/stderr
227 ret=$?
228 if [ $ret -ne 0 ]; then
229 diff -u $testroot/stderr.expected $testroot/stderr
230 fi
231 test_done "$testroot" "$ret"
234 test_cat_submodule_of_same_repo() {
235 local testroot=`test_init cat_submodule_of_same_repo`
236 local commit_id0=`git_show_head $testroot/repo`
237 local author_time=`git_show_author_time $testroot/repo`
238 local gmtoff=`date +%z`
240 (cd $testroot && git clone -q repo repo2 >/dev/null)
241 (cd $testroot/repo && git -c protocol.file.allow=always \
242 submodule -q add ../repo2)
243 (cd $testroot/repo && git commit -q -m 'adding submodule')
245 # 'got cat' shows the commit object which the submodule points to
246 # because a commit with the same ID exists in the outer repository
247 got cat -r $testroot/repo $commit_id0 | grep ^tree > $testroot/stdout.expected
248 echo "numparents 0" >> $testroot/stdout.expected
249 echo "author $GOT_AUTHOR $author_time $gmtoff" >> $testroot/stdout.expected
250 echo "committer $GOT_AUTHOR $author_time $gmtoff" \
251 >> $testroot/stdout.expected
252 echo "messagelen 22" >> $testroot/stdout.expected
253 printf "\nadding the test tree\n" >> $testroot/stdout.expected
255 got cat -r $testroot/repo repo2 > $testroot/stdout
256 cmp -s $testroot/stdout.expected $testroot/stdout
257 ret=$?
258 if [ $ret -ne 0 ]; then
259 diff -u $testroot/stdout.expected $testroot/stdout
260 fi
262 test_done "$testroot" "$ret"
265 test_cat_symlink() {
266 local testroot=`test_init cat_symlink`
267 local commit_id=`git_show_head $testroot/repo`
268 local author_time=`git_show_author_time $testroot/repo`
270 (cd $testroot/repo && ln -s alpha alpha.link)
271 (cd $testroot/repo && ln -s epsilon epsilon.link)
272 (cd $testroot/repo && ln -s /etc/passwd passwd.link)
273 (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
274 (cd $testroot/repo && ln -s nonexistent nonexistent.link)
275 (cd $testroot/repo && git add .)
276 git_commit $testroot/repo -m "add symlinks"
278 local alpha_link_id=`got tree -r $testroot/repo -i | grep 'alpha.link@ -> alpha$' | cut -d' ' -f 1`
279 local epsilon_link_id=`got tree -r $testroot/repo -i | grep 'epsilon.link@ -> epsilon$' | cut -d' ' -f 1`
280 local passwd_link_id=`got tree -r $testroot/repo -i | grep 'passwd.link@ -> /etc/passwd$' | cut -d' ' -f 1`
281 local epsilon_beta_link_id=`got tree -r $testroot/repo -i epsilon | grep 'beta.link@ -> ../beta$' | cut -d' ' -f 1`
282 local nonexistent_link_id=`got tree -r $testroot/repo -i | grep 'nonexistent.link@ -> nonexistent$' | cut -d' ' -f 1`
284 # cat symlink to regular file
285 echo -n "alpha" > $testroot/stdout.expected
286 got cat -r $testroot/repo $alpha_link_id > $testroot/stdout
287 cmp -s $testroot/stdout.expected $testroot/stdout
288 ret=$?
289 if [ $ret -ne 0 ]; then
290 diff -u $testroot/stdout.expected $testroot/stdout
291 test_done "$testroot" "$ret"
292 return 1
293 fi
295 # cat symlink with relative path to regular file
296 echo -n "../beta" > $testroot/stdout.expected
297 got cat -r $testroot/repo $epsilon_beta_link_id > $testroot/stdout
298 cmp -s $testroot/stdout.expected $testroot/stdout
299 ret=$?
300 if [ $ret -ne 0 ]; then
301 diff -u $testroot/stdout.expected $testroot/stdout
302 test_done "$testroot" "$ret"
303 return 1
304 fi
306 # cat symlink to a tree
307 echo -n "epsilon" > $testroot/stdout.expected
308 got cat -r $testroot/repo $epsilon_link_id > $testroot/stdout
309 cmp -s $testroot/stdout.expected $testroot/stdout
310 ret=$?
311 if [ $ret -ne 0 ]; then
312 diff -u $testroot/stdout.expected $testroot/stdout
313 test_done "$testroot" "$ret"
314 return 1
315 fi
317 # cat symlink to paths which don't exist in repository
318 echo -n "/etc/passwd" > $testroot/stdout.expected
319 got cat -r $testroot/repo $passwd_link_id > $testroot/stdout
320 cmp -s $testroot/stdout.expected $testroot/stdout
321 ret=$?
322 if [ $ret -ne 0 ]; then
323 diff -u $testroot/stdout.expected $testroot/stdout
324 test_done "$testroot" "$ret"
325 return 1
326 fi
328 echo -n "nonexistent" > $testroot/stdout.expected
329 got cat -r $testroot/repo $nonexistent_link_id > $testroot/stdout
330 cmp -s $testroot/stdout.expected $testroot/stdout
331 ret=$?
332 if [ $ret -ne 0 ]; then
333 diff -u $testroot/stdout.expected $testroot/stdout
334 test_done "$testroot" "$ret"
335 return 1
336 fi
338 test_done "$testroot" "$ret"
341 test_parseargs "$@"
342 run_test test_cat_basic
343 run_test test_cat_path
344 run_test test_cat_submodule
345 run_test test_cat_submodule_of_same_repo
346 run_test test_cat_symlink