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.
19 function test_diff_basic {
20 local testroot=`test_init diff_basic`
21 local head_rev=`git_show_head $testroot/repo`
23 got checkout $testroot/repo $testroot/wt > /dev/null
25 if [ "$ret" != "0" ]; then
26 test_done "$testroot" "$ret"
30 echo "modified alpha" > $testroot/wt/alpha
31 (cd $testroot/wt && got rm beta >/dev/null)
32 echo "new file" > $testroot/wt/new
33 (cd $testroot/wt && got add new >/dev/null)
35 echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
36 echo -n 'blob - ' >> $testroot/stdout.expected
37 got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1 \
38 >> $testroot/stdout.expected
39 echo 'file + alpha' >> $testroot/stdout.expected
40 echo '--- alpha' >> $testroot/stdout.expected
41 echo '+++ alpha' >> $testroot/stdout.expected
42 echo '@@ -1 +1 @@' >> $testroot/stdout.expected
43 echo '-alpha' >> $testroot/stdout.expected
44 echo '+modified alpha' >> $testroot/stdout.expected
45 echo -n 'blob - ' >> $testroot/stdout.expected
46 got tree -r $testroot/repo -i | grep 'beta$' | cut -d' ' -f 1 \
47 >> $testroot/stdout.expected
48 echo 'file + /dev/null' >> $testroot/stdout.expected
49 echo '--- beta' >> $testroot/stdout.expected
50 echo '+++ beta' >> $testroot/stdout.expected
51 echo '@@ -1 +0,0 @@' >> $testroot/stdout.expected
52 echo '-beta' >> $testroot/stdout.expected
53 echo 'blob - /dev/null' >> $testroot/stdout.expected
54 echo 'file + new' >> $testroot/stdout.expected
55 echo '--- new' >> $testroot/stdout.expected
56 echo '+++ new' >> $testroot/stdout.expected
57 echo '@@ -0,0 +1 @@' >> $testroot/stdout.expected
58 echo '+new file' >> $testroot/stdout.expected
60 (cd $testroot/wt && got diff > $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"
69 # diff non-existent path
70 (cd $testroot/wt && got diff nonexistent > $testroot/stdout \
73 echo -n > $testroot/stdout.expected
74 cmp -s $testroot/stdout.expected $testroot/stdout
76 if [ "$ret" != "0" ]; then
77 diff -u $testroot/stdout.expected $testroot/stdout
78 test_done "$testroot" "$ret"
82 echo "got: nonexistent: No such file or directory" \
83 > $testroot/stderr.expected
84 cmp -s $testroot/stderr.expected $testroot/stderr
86 if [ "$ret" != "0" ]; then
87 diff -u $testroot/stderr.expected $testroot/stderr
89 test_done "$testroot" "$ret"
92 function test_diff_shows_conflict {
93 local testroot=`test_init diff_shows_conflict 1`
95 echo "1" > $testroot/repo/numbers
96 echo "2" >> $testroot/repo/numbers
97 echo "3" >> $testroot/repo/numbers
98 echo "4" >> $testroot/repo/numbers
99 echo "5" >> $testroot/repo/numbers
100 echo "6" >> $testroot/repo/numbers
101 echo "7" >> $testroot/repo/numbers
102 echo "8" >> $testroot/repo/numbers
103 (cd $testroot/repo && git add numbers)
104 git_commit $testroot/repo -m "added numbers file"
105 local base_commit=`git_show_head $testroot/repo`
107 got checkout $testroot/repo $testroot/wt > /dev/null
109 if [ "$ret" != "0" ]; then
110 test_done "$testroot" "$ret"
114 sed -i 's/2/22/' $testroot/repo/numbers
115 sed -i 's/8/33/' $testroot/repo/numbers
116 git_commit $testroot/repo -m "modified line 2"
117 local head_rev=`git_show_head $testroot/repo`
119 # modify lines 2 and 8 in conflicting ways
120 sed -i 's/2/77/' $testroot/wt/numbers
121 sed -i 's/8/88/' $testroot/wt/numbers
123 echo "C numbers" > $testroot/stdout.expected
124 echo -n "Updated to commit $head_rev" >> $testroot/stdout.expected
125 echo >> $testroot/stdout.expected
127 (cd $testroot/wt && got update > $testroot/stdout)
129 cmp -s $testroot/stdout.expected $testroot/stdout
131 if [ "$ret" != "0" ]; then
132 diff -u $testroot/stdout.expected $testroot/stdout
133 test_done "$testroot" "$ret"
137 echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
138 echo -n 'blob - ' >> $testroot/stdout.expected
139 got tree -r $testroot/repo -i | grep 'numbers$' | cut -d' ' -f 1 \
140 >> $testroot/stdout.expected
141 echo 'file + numbers' >> $testroot/stdout.expected
142 echo '--- numbers' >> $testroot/stdout.expected
143 echo '+++ numbers' >> $testroot/stdout.expected
144 echo '@@ -1,8 +1,20 @@' >> $testroot/stdout.expected
145 echo ' 1' >> $testroot/stdout.expected
146 echo "+<<<<<<< merged change: commit $head_rev" \
147 >> $testroot/stdout.expected
148 echo ' 22' >> $testroot/stdout.expected
149 echo "+||||||| 3-way merge base: commit $base_commit" \
150 >> $testroot/stdout.expected
151 echo '+2' >> $testroot/stdout.expected
152 echo '+=======' >> $testroot/stdout.expected
153 echo '+77' >> $testroot/stdout.expected
154 echo '+>>>>>>>' >> $testroot/stdout.expected
155 echo ' 3' >> $testroot/stdout.expected
156 echo ' 4' >> $testroot/stdout.expected
157 echo ' 5' >> $testroot/stdout.expected
158 echo ' 6' >> $testroot/stdout.expected
159 echo ' 7' >> $testroot/stdout.expected
160 echo "+<<<<<<< merged change: commit $head_rev" \
161 >> $testroot/stdout.expected
162 echo ' 33' >> $testroot/stdout.expected
163 echo "+||||||| 3-way merge base: commit $base_commit" \
164 >> $testroot/stdout.expected
165 echo '+8' >> $testroot/stdout.expected
166 echo '+=======' >> $testroot/stdout.expected
167 echo '+88' >> $testroot/stdout.expected
168 echo '+>>>>>>>' >> $testroot/stdout.expected
170 (cd $testroot/wt && got diff > $testroot/stdout)
172 cmp -s $testroot/stdout.expected $testroot/stdout
174 if [ "$ret" != "0" ]; then
175 diff -u $testroot/stdout.expected $testroot/stdout
177 test_done "$testroot" "$ret"
180 function test_diff_tag {
181 local testroot=`test_init diff_tag`
182 local commit_id0=`git_show_head $testroot/repo`
186 echo "modified alpha" > $testroot/repo/alpha
187 git_commit $testroot/repo -m "changed alpha"
188 local commit_id1=`git_show_head $testroot/repo`
190 (cd $testroot/repo && git tag -m "test" $tag1)
192 echo "new file" > $testroot/repo/new
193 (cd $testroot/repo && git add new)
194 git_commit $testroot/repo -m "new file"
195 local commit_id2=`git_show_head $testroot/repo`
197 (cd $testroot/repo && git tag -m "test" $tag2)
199 echo "diff $commit_id0 refs/tags/$tag1" > $testroot/stdout.expected
200 echo -n 'blob - ' >> $testroot/stdout.expected
201 got tree -r $testroot/repo -c $commit_id0 -i | grep 'alpha$' | \
202 cut -d' ' -f 1 >> $testroot/stdout.expected
203 echo -n 'blob + ' >> $testroot/stdout.expected
204 got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1 \
205 >> $testroot/stdout.expected
206 echo '--- alpha' >> $testroot/stdout.expected
207 echo '+++ alpha' >> $testroot/stdout.expected
208 echo '@@ -1 +1 @@' >> $testroot/stdout.expected
209 echo '-alpha' >> $testroot/stdout.expected
210 echo '+modified alpha' >> $testroot/stdout.expected
212 got diff -r $testroot/repo $commit_id0 $tag1 > $testroot/stdout
213 cmp -s $testroot/stdout.expected $testroot/stdout
215 if [ "$ret" != "0" ]; then
216 diff -u $testroot/stdout.expected $testroot/stdout
217 test_done "$testroot" "$ret"
221 echo "diff refs/tags/$tag1 refs/tags/$tag2" > $testroot/stdout.expected
222 echo "blob - /dev/null" >> $testroot/stdout.expected
223 echo -n 'blob + ' >> $testroot/stdout.expected
224 got tree -r $testroot/repo -i -c $commit_id2 | grep 'new$' | \
225 cut -d' ' -f 1 | tr -d '\n' >> $testroot/stdout.expected
226 echo " (mode 644)" >> $testroot/stdout.expected
227 echo '--- /dev/null' >> $testroot/stdout.expected
228 echo '+++ new' >> $testroot/stdout.expected
229 echo '@@ -0,0 +1 @@' >> $testroot/stdout.expected
230 echo '+new file' >> $testroot/stdout.expected
232 got diff -r $testroot/repo $tag1 $tag2 > $testroot/stdout
233 cmp -s $testroot/stdout.expected $testroot/stdout
235 if [ "$ret" != "0" ]; then
236 diff -u $testroot/stdout.expected $testroot/stdout
238 test_done "$testroot" "$ret"
241 function test_diff_lightweight_tag {
242 local testroot=`test_init diff_tag`
243 local commit_id0=`git_show_head $testroot/repo`
247 echo "modified alpha" > $testroot/repo/alpha
248 git_commit $testroot/repo -m "changed alpha"
249 local commit_id1=`git_show_head $testroot/repo`
251 (cd $testroot/repo && git tag $tag1)
253 echo "new file" > $testroot/repo/new
254 (cd $testroot/repo && git add new)
255 git_commit $testroot/repo -m "new file"
256 local commit_id2=`git_show_head $testroot/repo`
258 (cd $testroot/repo && git tag $tag2)
260 echo "diff $commit_id0 refs/tags/$tag1" > $testroot/stdout.expected
261 echo -n 'blob - ' >> $testroot/stdout.expected
262 got tree -r $testroot/repo -c $commit_id0 -i | grep 'alpha$' | \
263 cut -d' ' -f 1 >> $testroot/stdout.expected
264 echo -n 'blob + ' >> $testroot/stdout.expected
265 got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1 \
266 >> $testroot/stdout.expected
267 echo '--- alpha' >> $testroot/stdout.expected
268 echo '+++ alpha' >> $testroot/stdout.expected
269 echo '@@ -1 +1 @@' >> $testroot/stdout.expected
270 echo '-alpha' >> $testroot/stdout.expected
271 echo '+modified alpha' >> $testroot/stdout.expected
273 got diff -r $testroot/repo $commit_id0 $tag1 > $testroot/stdout
274 cmp -s $testroot/stdout.expected $testroot/stdout
276 if [ "$ret" != "0" ]; then
277 diff -u $testroot/stdout.expected $testroot/stdout
278 test_done "$testroot" "$ret"
282 echo "diff refs/tags/$tag1 refs/tags/$tag2" > $testroot/stdout.expected
283 echo "blob - /dev/null" >> $testroot/stdout.expected
284 echo -n 'blob + ' >> $testroot/stdout.expected
285 got tree -r $testroot/repo -i -c $commit_id2 | grep 'new$' | \
286 cut -d' ' -f 1 | tr -d '\n' >> $testroot/stdout.expected
287 echo " (mode 644)" >> $testroot/stdout.expected
288 echo '--- /dev/null' >> $testroot/stdout.expected
289 echo '+++ new' >> $testroot/stdout.expected
290 echo '@@ -0,0 +1 @@' >> $testroot/stdout.expected
291 echo '+new file' >> $testroot/stdout.expected
293 got diff -r $testroot/repo $tag1 $tag2 > $testroot/stdout
294 cmp -s $testroot/stdout.expected $testroot/stdout
296 if [ "$ret" != "0" ]; then
297 diff -u $testroot/stdout.expected $testroot/stdout
299 test_done "$testroot" "$ret"
302 function test_diff_ignore_whitespace {
303 local testroot=`test_init diff_ignore_whitespace`
304 local commit_id0=`git_show_head $testroot/repo`
306 got checkout $testroot/repo $testroot/wt > /dev/null
308 if [ "$ret" != "0" ]; then
309 test_done "$testroot" "$ret"
313 echo "alpha " > $testroot/wt/alpha
315 (cd $testroot/wt && got diff -w > $testroot/stdout)
317 echo "diff $commit_id0 $testroot/wt" > $testroot/stdout.expected
318 echo -n 'blob - ' >> $testroot/stdout.expected
319 got tree -r $testroot/repo -c $commit_id0 -i | grep 'alpha$' | \
320 cut -d' ' -f 1 >> $testroot/stdout.expected
321 echo 'file + alpha' >> $testroot/stdout.expected
323 cmp -s $testroot/stdout.expected $testroot/stdout
325 if [ "$ret" != "0" ]; then
326 diff -u $testroot/stdout.expected $testroot/stdout
328 test_done "$testroot" "$ret"
331 run_test test_diff_basic
332 run_test test_diff_shows_conflict
333 run_test test_diff_tag
334 run_test test_diff_lightweight_tag
335 run_test test_diff_ignore_whitespace