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 fi
66 test_done "$testroot" "$ret"
67 }
69 function test_diff_shows_conflict {
70 local testroot=`test_init diff_shows_conflict 1`
72 echo "1" > $testroot/repo/numbers
73 echo "2" >> $testroot/repo/numbers
74 echo "3" >> $testroot/repo/numbers
75 echo "4" >> $testroot/repo/numbers
76 echo "5" >> $testroot/repo/numbers
77 echo "6" >> $testroot/repo/numbers
78 echo "7" >> $testroot/repo/numbers
79 echo "8" >> $testroot/repo/numbers
80 (cd $testroot/repo && git add numbers)
81 git_commit $testroot/repo -m "added numbers file"
83 got checkout $testroot/repo $testroot/wt > /dev/null
84 ret="$?"
85 if [ "$ret" != "0" ]; then
86 test_done "$testroot" "$ret"
87 return 1
88 fi
90 sed -i 's/2/22/' $testroot/repo/numbers
91 git_commit $testroot/repo -m "modified line 2"
92 local head_rev=`git_show_head $testroot/repo`
94 # modify line 2 in a conflicting way
95 sed -i 's/2/77/' $testroot/wt/numbers
97 echo "C numbers" > $testroot/stdout.expected
98 echo -n "Updated to commit $head_rev" >> $testroot/stdout.expected
99 echo >> $testroot/stdout.expected
101 (cd $testroot/wt && got update > $testroot/stdout)
103 cmp -s $testroot/stdout.expected $testroot/stdout
104 ret="$?"
105 if [ "$ret" != "0" ]; then
106 diff -u $testroot/stdout.expected $testroot/stdout
107 test_done "$testroot" "$ret"
108 return 1
109 fi
111 echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
112 echo -n 'blob - ' >> $testroot/stdout.expected
113 got tree -r $testroot/repo -i | grep 'numbers$' | cut -d' ' -f 1 \
114 >> $testroot/stdout.expected
115 echo 'file + numbers' >> $testroot/stdout.expected
116 echo '--- numbers' >> $testroot/stdout.expected
117 echo '+++ numbers' >> $testroot/stdout.expected
118 echo '@@ -1,5 +1,9 @@' >> $testroot/stdout.expected
119 echo ' 1' >> $testroot/stdout.expected
120 echo "+<<<<<<< commit $head_rev" >> $testroot/stdout.expected
121 echo ' 22' >> $testroot/stdout.expected
122 echo '+=======' >> $testroot/stdout.expected
123 echo '+77' >> $testroot/stdout.expected
124 echo '+>>>>>>> numbers' >> $testroot/stdout.expected
125 echo ' 3' >> $testroot/stdout.expected
126 echo ' 4' >> $testroot/stdout.expected
127 echo ' 5' >> $testroot/stdout.expected
129 (cd $testroot/wt && got diff > $testroot/stdout)
131 cmp -s $testroot/stdout.expected $testroot/stdout
132 ret="$?"
133 if [ "$ret" != "0" ]; then
134 diff -u $testroot/stdout.expected $testroot/stdout
135 fi
136 test_done "$testroot" "$ret"
139 function test_diff_tag {
140 local testroot=`test_init diff_tag`
141 local commit_id0=`git_show_head $testroot/repo`
142 local tag1=1.0.0
143 local tag2=2.0.0
145 echo "modified alpha" > $testroot/repo/alpha
146 git_commit $testroot/repo -m "changed alpha"
147 local commit_id1=`git_show_head $testroot/repo`
149 (cd $testroot/repo && git tag -m "test" $tag1)
151 echo "new file" > $testroot/repo/new
152 (cd $testroot/repo && git add new)
153 git_commit $testroot/repo -m "new file"
154 local commit_id2=`git_show_head $testroot/repo`
156 (cd $testroot/repo && git tag -m "test" $tag2)
158 echo "diff $commit_id0 refs/tags/$tag1" > $testroot/stdout.expected
159 echo -n 'blob - ' >> $testroot/stdout.expected
160 got tree -r $testroot/repo -c $commit_id0 -i | grep 'alpha$' | \
161 cut -d' ' -f 1 >> $testroot/stdout.expected
162 echo -n 'blob + ' >> $testroot/stdout.expected
163 got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1 \
164 >> $testroot/stdout.expected
165 echo '--- alpha' >> $testroot/stdout.expected
166 echo '+++ alpha' >> $testroot/stdout.expected
167 echo '@@ -1 +1 @@' >> $testroot/stdout.expected
168 echo '-alpha' >> $testroot/stdout.expected
169 echo '+modified alpha' >> $testroot/stdout.expected
171 got diff -r $testroot/repo $commit_id0 $tag1 > $testroot/stdout
172 cmp -s $testroot/stdout.expected $testroot/stdout
173 ret="$?"
174 if [ "$ret" != "0" ]; then
175 diff -u $testroot/stdout.expected $testroot/stdout
176 test_done "$testroot" "$ret"
177 return 1
178 fi
180 echo "diff refs/tags/$tag1 refs/tags/$tag2" > $testroot/stdout.expected
181 echo "blob - /dev/null" >> $testroot/stdout.expected
182 echo -n 'blob + ' >> $testroot/stdout.expected
183 got tree -r $testroot/repo -i -c $commit_id2 | grep 'new$' | \
184 cut -d' ' -f 1 >> $testroot/stdout.expected
185 echo '--- /dev/null' >> $testroot/stdout.expected
186 echo '+++ new' >> $testroot/stdout.expected
187 echo '@@ -0,0 +1 @@' >> $testroot/stdout.expected
188 echo '+new file' >> $testroot/stdout.expected
190 got diff -r $testroot/repo $tag1 $tag2 > $testroot/stdout
191 cmp -s $testroot/stdout.expected $testroot/stdout
192 ret="$?"
193 if [ "$ret" != "0" ]; then
194 diff -u $testroot/stdout.expected $testroot/stdout
195 fi
196 test_done "$testroot" "$ret"
199 run_test test_diff_basic
200 run_test test_diff_shows_conflict
201 run_test test_diff_tag