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"
74 }
76 test_cat_path() {
77 local testroot=`test_init cat_path`
78 local commit_id=`git_show_head $testroot/repo`
79 local author_time=`git_show_author_time $testroot/repo`
80 local alpha_id=`got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1`
81 local gamma_id=`got tree -r $testroot/repo -i | grep 'gamma/$' | cut -d' ' -f 1`
82 local delta_id=`got tree -r $testroot/repo -i gamma | grep 'delta$' | cut -d' ' -f 1`
84 # cat blob by path
85 echo "alpha" > $testroot/stdout.expected
86 got cat -r $testroot/repo alpha > $testroot/stdout
87 cmp -s $testroot/stdout.expected $testroot/stdout
88 ret=$?
89 if [ $ret -ne 0 ]; then
90 diff -u $testroot/stdout.expected $testroot/stdout
91 test_done "$testroot" "$ret"
92 return 1
93 fi
95 # cat tree by path
96 echo "$delta_id 0100644 delta" > $testroot/stdout.expected
97 got cat -r $testroot/repo gamma > $testroot/stdout
98 cmp -s $testroot/stdout.expected $testroot/stdout
99 ret=$?
100 if [ $ret -ne 0 ]; then
101 diff -u $testroot/stdout.expected $testroot/stdout
102 test_done "$testroot" "$ret"
103 return 1
104 fi
106 (cd $testroot && got checkout repo wt > /dev/null)
107 echo "modified alpha" > $testroot/wt/alpha
108 (cd $testroot/wt && got commit -m "changed alpha" > /dev/null)
109 local commit_id2=`git_show_head $testroot/repo`
110 local author_time2=`git_show_author_time $testroot/repo`
111 local tree_commit2=`git_show_tree $testroot/repo`
113 # cat blob by path in specific commit
114 echo "alpha" > $testroot/stdout.expected
115 got cat -r $testroot/repo -c $commit_id alpha > $testroot/stdout
116 cmp -s $testroot/stdout.expected $testroot/stdout
117 ret=$?
118 if [ $ret -ne 0 ]; then
119 diff -u $testroot/stdout.expected $testroot/stdout
120 test_done "$testroot" "$ret"
121 return 1
122 fi
123 echo "modified alpha" > $testroot/stdout.expected
124 got cat -r $testroot/repo -c $commit_id2 alpha > $testroot/stdout
125 cmp -s $testroot/stdout.expected $testroot/stdout
126 ret=$?
127 if [ $ret -ne 0 ]; then
128 diff -u $testroot/stdout.expected $testroot/stdout
129 test_done "$testroot" "$ret"
130 return 1
131 fi
133 # resolve ambiguities between paths and other arguments
134 echo "new file called master" > $testroot/wt/master
135 echo "new file called $commit_id2" > $testroot/wt/$commit_id2
136 (cd $testroot/wt && got add master $commit_id2 > /dev/null)
137 (cd $testroot/wt && got commit -m "added clashing paths" > /dev/null)
138 local commit_id3=`git_show_head $testroot/repo`
139 local author_time3=`git_show_author_time $testroot/repo`
141 # references and object IDs override paths:
142 echo -n "tree " > $testroot/stdout.expected
143 git_show_tree $testroot/repo >> $testroot/stdout.expected
144 echo >> $testroot/stdout.expected
145 echo "numparents 1" >> $testroot/stdout.expected
146 echo "parent $commit_id2" >> $testroot/stdout.expected
147 echo "author $GOT_AUTHOR $author_time3 +0000" >> $testroot/stdout.expected
148 echo "committer $GOT_AUTHOR $author_time3 +0000" \
149 >> $testroot/stdout.expected
150 echo "messagelen 22" >> $testroot/stdout.expected
151 printf "\nadded clashing paths\n" >> $testroot/stdout.expected
153 for arg in master $commit_id3; do
154 got cat -r $testroot/repo $arg > $testroot/stdout
155 cmp -s $testroot/stdout.expected $testroot/stdout
156 ret=$?
157 if [ $ret -ne 0 ]; then
158 diff -u $testroot/stdout.expected $testroot/stdout
159 test_done "$testroot" "$ret"
160 return 1
161 fi
162 done
164 echo "tree $tree_commit2" > $testroot/stdout.expected
165 echo "numparents 1" >> $testroot/stdout.expected
166 echo "parent $commit_id" >> $testroot/stdout.expected
167 echo "author $GOT_AUTHOR $author_time2 +0000" >> $testroot/stdout.expected
168 echo "committer $GOT_AUTHOR $author_time2 +0000" \
169 >> $testroot/stdout.expected
170 echo "messagelen 15" >> $testroot/stdout.expected
171 printf "\nchanged alpha\n" >> $testroot/stdout.expected
173 got cat -r $testroot/repo $commit_id2 > $testroot/stdout
174 cmp -s $testroot/stdout.expected $testroot/stdout
175 ret=$?
176 if [ $ret -ne 0 ]; then
177 diff -u $testroot/stdout.expected $testroot/stdout
178 test_done "$testroot" "$ret"
179 return 1
180 fi
182 # force resolution of path 'master'
183 echo "new file called master" > $testroot/stdout.expected
184 got cat -r $testroot/repo -P master > $testroot/stdout
185 cmp -s $testroot/stdout.expected $testroot/stdout
186 ret=$?
187 if [ $ret -ne 0 ]; then
188 diff -u $testroot/stdout.expected $testroot/stdout
189 test_done "$testroot" "$ret"
190 return 1
191 fi
193 # force resolution of path "$commit_id2"
194 echo "new file called $commit_id2" > $testroot/stdout.expected
195 got cat -r $testroot/repo -P $commit_id2 > $testroot/stdout
196 cmp -s $testroot/stdout.expected $testroot/stdout
197 ret=$?
198 if [ $ret -ne 0 ]; then
199 diff -u $testroot/stdout.expected $testroot/stdout
200 test_done "$testroot" "$ret"
201 return 1
202 fi
203 test_done "$testroot" "$ret"
206 test_cat_submodule() {
207 local testroot=`test_init cat_submodule`
209 make_single_file_repo $testroot/repo2 foo
211 (cd $testroot/repo && git 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 submodule -q add ../repo2)
242 (cd $testroot/repo && git commit -q -m 'adding submodule')
244 # 'got cat' shows the commit object which the submodule points to
245 # because a commit with the same ID exists in the outer repository
246 got cat -r $testroot/repo $commit_id0 | grep ^tree > $testroot/stdout.expected
247 echo "numparents 0" >> $testroot/stdout.expected
248 echo "author $GOT_AUTHOR $author_time $gmtoff" >> $testroot/stdout.expected
249 echo "committer $GOT_AUTHOR $author_time $gmtoff" \
250 >> $testroot/stdout.expected
251 echo "messagelen 22" >> $testroot/stdout.expected
252 printf "\nadding the test tree\n" >> $testroot/stdout.expected
254 got cat -r $testroot/repo repo2 > $testroot/stdout
255 cmp -s $testroot/stdout.expected $testroot/stdout
256 ret=$?
257 if [ $ret -ne 0 ]; then
258 diff -u $testroot/stdout.expected $testroot/stdout
259 fi
261 test_done "$testroot" "$ret"
264 test_cat_symlink() {
265 local testroot=`test_init cat_symlink`
266 local commit_id=`git_show_head $testroot/repo`
267 local author_time=`git_show_author_time $testroot/repo`
269 (cd $testroot/repo && ln -s alpha alpha.link)
270 (cd $testroot/repo && ln -s epsilon epsilon.link)
271 (cd $testroot/repo && ln -s /etc/passwd passwd.link)
272 (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
273 (cd $testroot/repo && ln -s nonexistent nonexistent.link)
274 (cd $testroot/repo && git add .)
275 git_commit $testroot/repo -m "add symlinks"
277 local alpha_link_id=`got tree -r $testroot/repo -i | grep 'alpha.link@ -> alpha$' | cut -d' ' -f 1`
278 local epsilon_link_id=`got tree -r $testroot/repo -i | grep 'epsilon.link@ -> epsilon$' | cut -d' ' -f 1`
279 local passwd_link_id=`got tree -r $testroot/repo -i | grep 'passwd.link@ -> /etc/passwd$' | cut -d' ' -f 1`
280 local epsilon_beta_link_id=`got tree -r $testroot/repo -i epsilon | grep 'beta.link@ -> ../beta$' | cut -d' ' -f 1`
281 local nonexistent_link_id=`got tree -r $testroot/repo -i | grep 'nonexistent.link@ -> nonexistent$' | cut -d' ' -f 1`
283 # cat symlink to regular file
284 echo -n "alpha" > $testroot/stdout.expected
285 got cat -r $testroot/repo $alpha_link_id > $testroot/stdout
286 cmp -s $testroot/stdout.expected $testroot/stdout
287 ret=$?
288 if [ $ret -ne 0 ]; then
289 diff -u $testroot/stdout.expected $testroot/stdout
290 test_done "$testroot" "$ret"
291 return 1
292 fi
294 # cat symlink with relative path to regular file
295 echo -n "../beta" > $testroot/stdout.expected
296 got cat -r $testroot/repo $epsilon_beta_link_id > $testroot/stdout
297 cmp -s $testroot/stdout.expected $testroot/stdout
298 ret=$?
299 if [ $ret -ne 0 ]; then
300 diff -u $testroot/stdout.expected $testroot/stdout
301 test_done "$testroot" "$ret"
302 return 1
303 fi
305 # cat symlink to a tree
306 echo -n "epsilon" > $testroot/stdout.expected
307 got cat -r $testroot/repo $epsilon_link_id > $testroot/stdout
308 cmp -s $testroot/stdout.expected $testroot/stdout
309 ret=$?
310 if [ $ret -ne 0 ]; then
311 diff -u $testroot/stdout.expected $testroot/stdout
312 test_done "$testroot" "$ret"
313 return 1
314 fi
316 # cat symlink to paths which don't exist in repository
317 echo -n "/etc/passwd" > $testroot/stdout.expected
318 got cat -r $testroot/repo $passwd_link_id > $testroot/stdout
319 cmp -s $testroot/stdout.expected $testroot/stdout
320 ret=$?
321 if [ $ret -ne 0 ]; then
322 diff -u $testroot/stdout.expected $testroot/stdout
323 test_done "$testroot" "$ret"
324 return 1
325 fi
327 echo -n "nonexistent" > $testroot/stdout.expected
328 got cat -r $testroot/repo $nonexistent_link_id > $testroot/stdout
329 cmp -s $testroot/stdout.expected $testroot/stdout
330 ret=$?
331 if [ $ret -ne 0 ]; then
332 diff -u $testroot/stdout.expected $testroot/stdout
333 test_done "$testroot" "$ret"
334 return 1
335 fi
337 test_done "$testroot" "$ret"
340 test_parseargs "$@"
341 run_test test_cat_basic
342 run_test test_cat_path
343 run_test test_cat_submodule
344 run_test test_cat_submodule_of_same_repo
345 run_test test_cat_symlink