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 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`
29 echo "alpha" > $testroot/stdout.expected
30 got
cat -r $testroot/repo
$alpha_id > $testroot/stdout
31 cmp -s $testroot/stdout.expected
$testroot/stdout
33 if [ $ret -ne 0 ]; then
34 diff -u $testroot/stdout.expected
$testroot/stdout
35 test_done
"$testroot" "$ret"
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
44 if [ $ret -ne 0 ]; then
45 diff -u $testroot/stdout.expected
$testroot/stdout
46 test_done
"$testroot" "$ret"
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
64 if [ $ret -ne 0 ]; then
65 diff -u $testroot/stdout.expected
$testroot/stdout
66 test_done
"$testroot" "$ret"
72 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 -ne 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 -ne 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 -ne 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 -ne 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 -ne 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 -ne 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 -ne 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 -ne 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 git
-C $testroot/repo
-c protocol.
file.allow
=always \
211 submodule
-q add ..
/repo2
212 git
-C $testroot/repo commit
-q -m 'adding submodule'
214 got
cat -r $testroot/repo repo2
> $testroot/stdout \
215 > $testroot/stdout
2> $testroot/stderr
217 if [ $ret -eq 0 ]; then
218 echo "cat command succeeded unexpectedly" >&2
219 test_done
"$testroot" "1"
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
228 if [ $ret -ne 0 ]; then
229 diff -u $testroot/stderr.expected
$testroot/stderr
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 git
-C $testroot clone
-q repo repo2
>/dev
/null
241 git
-C $testroot/repo
-c protocol.
file.allow
=always \
242 submodule
-q add ..
/repo2
243 git
-C $testroot/repo 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
258 if [ $ret -ne 0 ]; then
259 diff -u $testroot/stdout.expected
$testroot/stdout
262 test_done
"$testroot" "$ret"
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 git
-C $testroot/repo 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
289 if [ $ret -ne 0 ]; then
290 diff -u $testroot/stdout.expected
$testroot/stdout
291 test_done
"$testroot" "$ret"
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
300 if [ $ret -ne 0 ]; then
301 diff -u $testroot/stdout.expected
$testroot/stdout
302 test_done
"$testroot" "$ret"
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
311 if [ $ret -ne 0 ]; then
312 diff -u $testroot/stdout.expected
$testroot/stdout
313 test_done
"$testroot" "$ret"
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
322 if [ $ret -ne 0 ]; then
323 diff -u $testroot/stdout.expected
$testroot/stdout
324 test_done
"$testroot" "$ret"
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
332 if [ $ret -ne 0 ]; then
333 diff -u $testroot/stdout.expected
$testroot/stdout
334 test_done
"$testroot" "$ret"
338 test_done
"$testroot" "$ret"
341 test_cat_commit_keywords
() {
342 local testroot
=$
(test_init cat_commit_keywords
)
343 local repo
="$testroot/repo"
344 local wt
="$testroot/wt"
346 # :base requires work tree
347 echo "got: '-c :base' requires work tree" > "$testroot/stderr.expected"
348 got
cat -r "$repo" -c:base alpha
2> "$testroot/stderr"
350 if [ $ret -eq 0 ]; then
351 echo "cat command succeeded unexpectedly" >&2
352 test_done
"$testroot" "1"
356 cmp -s "$testroot/stderr.expected" "$testroot/stderr"
358 if [ $ret -ne 0 ]; then
359 diff -u "$testroot/stderr.expected" "$testroot/stderr"
360 test_done
"$testroot" "$ret"
364 got checkout
"$repo" "$wt" > /dev
/null
366 if [ $ret -ne 0 ]; then
367 test_done
"$testroot" "$ret"
371 for i
in $
(seq 8); do
372 echo "change $i" > "$wt/alpha"
373 echo "delta $i" > "$wt/gamma/delta"
375 (cd "$wt" && got ci
-m "commit $i" > /dev
/null
)
377 if [ $ret -ne 0 ]; then
378 echo "commit failed unexpectedly" >&2
379 test_done
"$testroot" "$ret"
383 local delta_id
=$
(got tree
-r "$repo" -i gamma | \
384 grep 'delta$' | cut
-d' ' -f 1)
385 set -- "$@" "$delta_id"
389 echo "change 6" > "$testroot/stdout.expected"
390 (cd "$wt" && got
cat -c:head:-2 alpha
> "$testroot/stdout")
391 cmp -s "$testroot/stdout.expected" "$testroot/stdout"
393 if [ $ret -ne 0 ]; then
394 diff -u "$testroot/stdout.expected" "$testroot/stdout"
395 test_done
"$testroot" "$ret"
399 # cat blob by path with -r repo
400 echo "delta 7" > "$testroot/stdout.expected"
401 got
cat -r "$repo" -c:head:- gamma
/delta
> "$testroot/stdout"
402 cmp -s "$testroot/stdout.expected" "$testroot/stdout"
404 if [ $ret -ne 0 ]; then
405 diff -u "$testroot/stdout.expected" "$testroot/stdout"
406 test_done
"$testroot" "$ret"
411 echo "$(pop_idx 4 $@) 0100644 delta" > \
412 "$testroot/stdout.expected"
413 (cd "$wt" && got
cat -c:base
:-4 gamma
> "$testroot/stdout")
414 cmp -s "$testroot/stdout.expected" "$testroot/stdout"
416 if [ $ret -ne 0 ]; then
417 diff -u "$testroot/stdout.expected" "$testroot/stdout"
418 test_done
"$testroot" "$ret"
422 # cat blob by path with -P
423 echo "delta 4" > "$testroot/stdout.expected"
424 (cd "$wt" && got up
-c:base
:-8 > /dev
/null
)
425 (cd "$wt" && got
cat -c:base
:+4 -P gamma
/delta
> "$testroot/stdout")
426 cmp -s "$testroot/stdout.expected" "$testroot/stdout"
428 if [ $ret -ne 0 ]; then
429 diff -u "$testroot/stdout.expected" "$testroot/stdout"
430 test_done
"$testroot" "$ret"
434 test_done
"$testroot" "$ret"
438 run_test test_cat_basic
439 run_test test_cat_path
440 run_test test_cat_submodule
441 run_test test_cat_submodule_of_same_repo
442 run_test test_cat_symlink
443 run_test test_cat_commit_keywords