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 submodule -q add ../repo2)
211 (cd $testroot/repo && git commit -q -m 'adding submodule')
213 got cat -r $testroot/repo repo2 > $testroot/stdout \
214 > $testroot/stdout 2> $testroot/stderr
215 ret=$?
216 if [ $ret -eq 0 ]; then
217 echo "cat command succeeded unexpectedly" >&2
218 test_done "$testroot" "1"
219 return 1
220 fi
221 local submodule_id=$(got tree -r $testroot/repo -i | \
222 grep 'repo2\$$' | cut -d ' ' -f1)
223 echo "got: object $submodule_id not found" > $testroot/stderr.expected
225 cmp -s $testroot/stderr.expected $testroot/stderr
226 ret=$?
227 if [ $ret -ne 0 ]; then
228 diff -u $testroot/stderr.expected $testroot/stderr
229 fi
230 test_done "$testroot" "$ret"
233 test_cat_submodule_of_same_repo() {
234 local testroot=`test_init cat_submodule_of_same_repo`
235 local commit_id0=`git_show_head $testroot/repo`
236 local author_time=`git_show_author_time $testroot/repo`
237 local gmtoff=`date +%z`
239 (cd $testroot && git clone -q repo repo2 >/dev/null)
240 (cd $testroot/repo && git submodule -q add ../repo2)
241 (cd $testroot/repo && git commit -q -m 'adding submodule')
243 # 'got cat' shows the commit object which the submodule points to
244 # because a commit with the same ID exists in the outer repository
245 got cat -r $testroot/repo $commit_id0 | grep ^tree > $testroot/stdout.expected
246 echo "numparents 0" >> $testroot/stdout.expected
247 echo "author $GOT_AUTHOR $author_time $gmtoff" >> $testroot/stdout.expected
248 echo "committer $GOT_AUTHOR $author_time $gmtoff" \
249 >> $testroot/stdout.expected
250 echo "messagelen 22" >> $testroot/stdout.expected
251 printf "\nadding the test tree\n" >> $testroot/stdout.expected
253 got cat -r $testroot/repo repo2 > $testroot/stdout
254 cmp -s $testroot/stdout.expected $testroot/stdout
255 ret=$?
256 if [ $ret -ne 0 ]; then
257 diff -u $testroot/stdout.expected $testroot/stdout
258 fi
260 test_done "$testroot" "$ret"
263 test_cat_symlink() {
264 local testroot=`test_init cat_symlink`
265 local commit_id=`git_show_head $testroot/repo`
266 local author_time=`git_show_author_time $testroot/repo`
268 (cd $testroot/repo && ln -s alpha alpha.link)
269 (cd $testroot/repo && ln -s epsilon epsilon.link)
270 (cd $testroot/repo && ln -s /etc/passwd passwd.link)
271 (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
272 (cd $testroot/repo && ln -s nonexistent nonexistent.link)
273 (cd $testroot/repo && git add .)
274 git_commit $testroot/repo -m "add symlinks"
276 local alpha_link_id=`got tree -r $testroot/repo -i | grep 'alpha.link@ -> alpha$' | cut -d' ' -f 1`
277 local epsilon_link_id=`got tree -r $testroot/repo -i | grep 'epsilon.link@ -> epsilon$' | cut -d' ' -f 1`
278 local passwd_link_id=`got tree -r $testroot/repo -i | grep 'passwd.link@ -> /etc/passwd$' | cut -d' ' -f 1`
279 local epsilon_beta_link_id=`got tree -r $testroot/repo -i epsilon | grep 'beta.link@ -> ../beta$' | cut -d' ' -f 1`
280 local nonexistent_link_id=`got tree -r $testroot/repo -i | grep 'nonexistent.link@ -> nonexistent$' | cut -d' ' -f 1`
282 # cat symlink to regular file
283 echo -n "alpha" > $testroot/stdout.expected
284 got cat -r $testroot/repo $alpha_link_id > $testroot/stdout
285 cmp -s $testroot/stdout.expected $testroot/stdout
286 ret=$?
287 if [ $ret -ne 0 ]; then
288 diff -u $testroot/stdout.expected $testroot/stdout
289 test_done "$testroot" "$ret"
290 return 1
291 fi
293 # cat symlink with relative path to regular file
294 echo -n "../beta" > $testroot/stdout.expected
295 got cat -r $testroot/repo $epsilon_beta_link_id > $testroot/stdout
296 cmp -s $testroot/stdout.expected $testroot/stdout
297 ret=$?
298 if [ $ret -ne 0 ]; then
299 diff -u $testroot/stdout.expected $testroot/stdout
300 test_done "$testroot" "$ret"
301 return 1
302 fi
304 # cat symlink to a tree
305 echo -n "epsilon" > $testroot/stdout.expected
306 got cat -r $testroot/repo $epsilon_link_id > $testroot/stdout
307 cmp -s $testroot/stdout.expected $testroot/stdout
308 ret=$?
309 if [ $ret -ne 0 ]; then
310 diff -u $testroot/stdout.expected $testroot/stdout
311 test_done "$testroot" "$ret"
312 return 1
313 fi
315 # cat symlink to paths which don't exist in repository
316 echo -n "/etc/passwd" > $testroot/stdout.expected
317 got cat -r $testroot/repo $passwd_link_id > $testroot/stdout
318 cmp -s $testroot/stdout.expected $testroot/stdout
319 ret=$?
320 if [ $ret -ne 0 ]; then
321 diff -u $testroot/stdout.expected $testroot/stdout
322 test_done "$testroot" "$ret"
323 return 1
324 fi
326 echo -n "nonexistent" > $testroot/stdout.expected
327 got cat -r $testroot/repo $nonexistent_link_id > $testroot/stdout
328 cmp -s $testroot/stdout.expected $testroot/stdout
329 ret=$?
330 if [ $ret -ne 0 ]; then
331 diff -u $testroot/stdout.expected $testroot/stdout
332 test_done "$testroot" "$ret"
333 return 1
334 fi
336 test_done "$testroot" "$ret"
339 test_parseargs "$@"
340 run_test test_cat_basic
341 run_test test_cat_path
342 run_test test_cat_submodule
343 run_test test_cat_submodule_of_same_repo
344 run_test test_cat_symlink