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 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
24 ret="$?"
25 if [ "$ret" != "0" ]; then
26 test_done "$testroot" "$ret"
27 return 1
28 fi
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
62 ret="$?"
63 if [ "$ret" != "0" ]; then
64 diff -u $testroot/stdout.expected $testroot/stdout
65 test_done "$testroot" "$ret"
66 return 1
67 fi
69 # diff non-existent path
70 (cd $testroot/wt && got diff nonexistent > $testroot/stdout \
71 2> $testroot/stderr)
73 echo -n > $testroot/stdout.expected
74 cmp -s $testroot/stdout.expected $testroot/stdout
75 ret="$?"
76 if [ "$ret" != "0" ]; then
77 diff -u $testroot/stdout.expected $testroot/stdout
78 test_done "$testroot" "$ret"
79 return 1
80 fi
82 echo "got: nonexistent: No such file or directory" \
83 > $testroot/stderr.expected
84 cmp -s $testroot/stderr.expected $testroot/stderr
85 ret="$?"
86 if [ "$ret" != "0" ]; then
87 diff -u $testroot/stderr.expected $testroot/stderr
88 fi
89 test_done "$testroot" "$ret"
90 }
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"
106 got checkout $testroot/repo $testroot/wt > /dev/null
107 ret="$?"
108 if [ "$ret" != "0" ]; then
109 test_done "$testroot" "$ret"
110 return 1
111 fi
113 sed -i 's/2/22/' $testroot/repo/numbers
114 git_commit $testroot/repo -m "modified line 2"
115 local head_rev=`git_show_head $testroot/repo`
117 # modify line 2 in a conflicting way
118 sed -i 's/2/77/' $testroot/wt/numbers
120 echo "C numbers" > $testroot/stdout.expected
121 echo -n "Updated to commit $head_rev" >> $testroot/stdout.expected
122 echo >> $testroot/stdout.expected
124 (cd $testroot/wt && got update > $testroot/stdout)
126 cmp -s $testroot/stdout.expected $testroot/stdout
127 ret="$?"
128 if [ "$ret" != "0" ]; then
129 diff -u $testroot/stdout.expected $testroot/stdout
130 test_done "$testroot" "$ret"
131 return 1
132 fi
134 echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
135 echo -n 'blob - ' >> $testroot/stdout.expected
136 got tree -r $testroot/repo -i | grep 'numbers$' | cut -d' ' -f 1 \
137 >> $testroot/stdout.expected
138 echo 'file + numbers' >> $testroot/stdout.expected
139 echo '--- numbers' >> $testroot/stdout.expected
140 echo '+++ numbers' >> $testroot/stdout.expected
141 echo '@@ -1,5 +1,9 @@' >> $testroot/stdout.expected
142 echo ' 1' >> $testroot/stdout.expected
143 echo "+<<<<<<< commit $head_rev" >> $testroot/stdout.expected
144 echo ' 22' >> $testroot/stdout.expected
145 echo '+=======' >> $testroot/stdout.expected
146 echo '+77' >> $testroot/stdout.expected
147 echo '+>>>>>>> numbers' >> $testroot/stdout.expected
148 echo ' 3' >> $testroot/stdout.expected
149 echo ' 4' >> $testroot/stdout.expected
150 echo ' 5' >> $testroot/stdout.expected
152 (cd $testroot/wt && got diff > $testroot/stdout)
154 cmp -s $testroot/stdout.expected $testroot/stdout
155 ret="$?"
156 if [ "$ret" != "0" ]; then
157 diff -u $testroot/stdout.expected $testroot/stdout
158 fi
159 test_done "$testroot" "$ret"
162 function test_diff_tag {
163 local testroot=`test_init diff_tag`
164 local commit_id0=`git_show_head $testroot/repo`
165 local tag1=1.0.0
166 local tag2=2.0.0
168 echo "modified alpha" > $testroot/repo/alpha
169 git_commit $testroot/repo -m "changed alpha"
170 local commit_id1=`git_show_head $testroot/repo`
172 (cd $testroot/repo && git tag -m "test" $tag1)
174 echo "new file" > $testroot/repo/new
175 (cd $testroot/repo && git add new)
176 git_commit $testroot/repo -m "new file"
177 local commit_id2=`git_show_head $testroot/repo`
179 (cd $testroot/repo && git tag -m "test" $tag2)
181 echo "diff $commit_id0 refs/tags/$tag1" > $testroot/stdout.expected
182 echo -n 'blob - ' >> $testroot/stdout.expected
183 got tree -r $testroot/repo -c $commit_id0 -i | grep 'alpha$' | \
184 cut -d' ' -f 1 >> $testroot/stdout.expected
185 echo -n 'blob + ' >> $testroot/stdout.expected
186 got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1 \
187 >> $testroot/stdout.expected
188 echo '--- alpha' >> $testroot/stdout.expected
189 echo '+++ alpha' >> $testroot/stdout.expected
190 echo '@@ -1 +1 @@' >> $testroot/stdout.expected
191 echo '-alpha' >> $testroot/stdout.expected
192 echo '+modified alpha' >> $testroot/stdout.expected
194 got diff -r $testroot/repo $commit_id0 $tag1 > $testroot/stdout
195 cmp -s $testroot/stdout.expected $testroot/stdout
196 ret="$?"
197 if [ "$ret" != "0" ]; then
198 diff -u $testroot/stdout.expected $testroot/stdout
199 test_done "$testroot" "$ret"
200 return 1
201 fi
203 echo "diff refs/tags/$tag1 refs/tags/$tag2" > $testroot/stdout.expected
204 echo "blob - /dev/null" >> $testroot/stdout.expected
205 echo -n 'blob + ' >> $testroot/stdout.expected
206 got tree -r $testroot/repo -i -c $commit_id2 | grep 'new$' | \
207 cut -d' ' -f 1 >> $testroot/stdout.expected
208 echo '--- /dev/null' >> $testroot/stdout.expected
209 echo '+++ new' >> $testroot/stdout.expected
210 echo '@@ -0,0 +1 @@' >> $testroot/stdout.expected
211 echo '+new file' >> $testroot/stdout.expected
213 got diff -r $testroot/repo $tag1 $tag2 > $testroot/stdout
214 cmp -s $testroot/stdout.expected $testroot/stdout
215 ret="$?"
216 if [ "$ret" != "0" ]; then
217 diff -u $testroot/stdout.expected $testroot/stdout
218 fi
219 test_done "$testroot" "$ret"
222 run_test test_diff_basic
223 run_test test_diff_shows_conflict
224 run_test test_diff_tag