3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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 alpha_id
=`got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1`
24 local gamma_id
=`got tree -r $testroot/repo -i | grep 'gamma/$' | cut -d' ' -f 1`
25 local delta_id
=`got tree -r $testroot/repo -i gamma | grep 'delta$' | cut -d' ' -f 1`
28 echo "alpha" > $testroot/stdout.expected
29 got
cat -r $testroot/repo
$alpha_id > $testroot/stdout
30 cmp -s $testroot/stdout.expected
$testroot/stdout
32 if [ "$ret" != "0" ]; then
33 diff -u $testroot/stdout.expected
$testroot/stdout
34 test_done
"$testroot" "$ret"
39 echo "$delta_id 0100644 delta" > $testroot/stdout.expected
40 got
cat -r $testroot/repo
$gamma_id > $testroot/stdout
41 cmp -s $testroot/stdout.expected
$testroot/stdout
43 if [ "$ret" != "0" ]; then
44 diff -u $testroot/stdout.expected
$testroot/stdout
45 test_done
"$testroot" "$ret"
50 echo -n "tree " > $testroot/stdout.expected
51 git_show_tree
$testroot/repo
>> $testroot/stdout.expected
52 echo >> $testroot/stdout.expected
53 echo "numparents 0" >> $testroot/stdout.expected
54 echo "author $GOT_AUTHOR $author_time +0000" >> $testroot/stdout.expected
55 echo "committer $GOT_AUTHOR $author_time +0000" \
56 >> $testroot/stdout.expected
57 echo "messagelen 22" >> $testroot/stdout.expected
58 printf "\nadding the test tree\n" >> $testroot/stdout.expected
60 got
cat -r $testroot/repo
$commit_id > $testroot/stdout
61 cmp -s $testroot/stdout.expected
$testroot/stdout
63 if [ "$ret" != "0" ]; then
64 diff -u $testroot/stdout.expected
$testroot/stdout
65 test_done
"$testroot" "$ret"
71 test_done
"$testroot" "$ret"
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`
84 echo "alpha" > $testroot/stdout.expected
85 got
cat -r $testroot/repo alpha
> $testroot/stdout
86 cmp -s $testroot/stdout.expected
$testroot/stdout
88 if [ "$ret" != "0" ]; then
89 diff -u $testroot/stdout.expected
$testroot/stdout
90 test_done
"$testroot" "$ret"
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
99 if [ "$ret" != "0" ]; then
100 diff -u $testroot/stdout.expected
$testroot/stdout
101 test_done
"$testroot" "$ret"
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
117 if [ "$ret" != "0" ]; then
118 diff -u $testroot/stdout.expected
$testroot/stdout
119 test_done
"$testroot" "$ret"
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
126 if [ "$ret" != "0" ]; then
127 diff -u $testroot/stdout.expected
$testroot/stdout
128 test_done
"$testroot" "$ret"
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
156 if [ "$ret" != "0" ]; then
157 diff -u $testroot/stdout.expected
$testroot/stdout
158 test_done
"$testroot" "$ret"
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
175 if [ "$ret" != "0" ]; then
176 diff -u $testroot/stdout.expected
$testroot/stdout
177 test_done
"$testroot" "$ret"
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
186 if [ "$ret" != "0" ]; then
187 diff -u $testroot/stdout.expected
$testroot/stdout
188 test_done
"$testroot" "$ret"
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
197 if [ "$ret" != "0" ]; then
198 diff -u $testroot/stdout.expected
$testroot/stdout
199 test_done
"$testroot" "$ret"
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
216 if [ "$ret" = "0" ]; then
217 echo "cat command succeeded unexpectedly" >&2
218 test_done
"$testroot" "1"
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
227 if [ "$ret" != "0" ]; then
228 diff -u $testroot/stderr.expected
$testroot/stderr
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`
238 (cd $testroot && git clone
-q repo repo2
>/dev
/null
)
239 (cd $testroot/repo
&& git submodule
-q add ..
/repo2
)
240 (cd $testroot/repo
&& git commit
-q -m 'adding submodule')
242 # 'got cat' shows the commit object which the submodule points to
243 # because a commit with the same ID exists in the outer repository
244 got
cat -r $testroot/repo
$commit_id0 |
grep ^tree
> $testroot/stdout.expected
245 echo "numparents 0" >> $testroot/stdout.expected
246 echo "author $GOT_AUTHOR $author_time +0000" >> $testroot/stdout.expected
247 echo "committer $GOT_AUTHOR $author_time +0000" \
248 >> $testroot/stdout.expected
249 echo "messagelen 22" >> $testroot/stdout.expected
250 printf "\nadding the test tree\n" >> $testroot/stdout.expected
252 got
cat -r $testroot/repo repo2
> $testroot/stdout
253 cmp -s $testroot/stdout.expected
$testroot/stdout
255 if [ "$ret" != "0" ]; then
256 diff -u $testroot/stdout.expected
$testroot/stdout
259 test_done
"$testroot" "$ret"
263 local testroot
=`test_init cat_symlink`
264 local commit_id
=`git_show_head $testroot/repo`
265 local author_time
=`git_show_author_time $testroot/repo`
267 (cd $testroot/repo
&& ln -s alpha alpha.link
)
268 (cd $testroot/repo
&& ln -s epsilon epsilon.link
)
269 (cd $testroot/repo
&& ln -s /etc
/passwd passwd.link
)
270 (cd $testroot/repo
&& ln -s ..
/beta epsilon
/beta.link
)
271 (cd $testroot/repo
&& ln -s nonexistent nonexistent.link
)
272 (cd $testroot/repo
&& git add .
)
273 git_commit
$testroot/repo
-m "add symlinks"
275 local alpha_link_id
=`got tree -r $testroot/repo -i | grep 'alpha.link@ -> alpha$' | cut -d' ' -f 1`
276 local epsilon_link_id
=`got tree -r $testroot/repo -i | grep 'epsilon.link@ -> epsilon$' | cut -d' ' -f 1`
277 local passwd_link_id
=`got tree -r $testroot/repo -i | grep 'passwd.link@ -> /etc/passwd$' | cut -d' ' -f 1`
278 local epsilon_beta_link_id
=`got tree -r $testroot/repo -i epsilon | grep 'beta.link@ -> ../beta$' | cut -d' ' -f 1`
279 local nonexistent_link_id
=`got tree -r $testroot/repo -i | grep 'nonexistent.link@ -> nonexistent$' | cut -d' ' -f 1`
281 # cat symlink to regular file
282 echo -n "alpha" > $testroot/stdout.expected
283 got
cat -r $testroot/repo
$alpha_link_id > $testroot/stdout
284 cmp -s $testroot/stdout.expected
$testroot/stdout
286 if [ "$ret" != "0" ]; then
287 diff -u $testroot/stdout.expected
$testroot/stdout
288 test_done
"$testroot" "$ret"
292 # cat symlink with relative path to regular file
293 echo -n "../beta" > $testroot/stdout.expected
294 got
cat -r $testroot/repo
$epsilon_beta_link_id > $testroot/stdout
295 cmp -s $testroot/stdout.expected
$testroot/stdout
297 if [ "$ret" != "0" ]; then
298 diff -u $testroot/stdout.expected
$testroot/stdout
299 test_done
"$testroot" "$ret"
303 # cat symlink to a tree
304 echo -n "epsilon" > $testroot/stdout.expected
305 got
cat -r $testroot/repo
$epsilon_link_id > $testroot/stdout
306 cmp -s $testroot/stdout.expected
$testroot/stdout
308 if [ "$ret" != "0" ]; then
309 diff -u $testroot/stdout.expected
$testroot/stdout
310 test_done
"$testroot" "$ret"
314 # cat symlink to paths which don't exist in repository
315 echo -n "/etc/passwd" > $testroot/stdout.expected
316 got
cat -r $testroot/repo
$passwd_link_id > $testroot/stdout
317 cmp -s $testroot/stdout.expected
$testroot/stdout
319 if [ "$ret" != "0" ]; then
320 diff -u $testroot/stdout.expected
$testroot/stdout
321 test_done
"$testroot" "$ret"
325 echo -n "nonexistent" > $testroot/stdout.expected
326 got
cat -r $testroot/repo
$nonexistent_link_id > $testroot/stdout
327 cmp -s $testroot/stdout.expected
$testroot/stdout
329 if [ "$ret" != "0" ]; then
330 diff -u $testroot/stdout.expected
$testroot/stdout
331 test_done
"$testroot" "$ret"
335 test_done
"$testroot" "$ret"
339 run_test test_cat_basic
340 run_test test_cat_path
341 run_test test_cat_submodule
342 run_test test_cat_submodule_of_same_repo
343 run_test test_cat_symlink