files_pack_refs(): use a reference transaction to write packed refs
[git.git] / contrib / diff-highlight / t / t9400-diff-highlight.sh
blob3b43dbed7488c5f4a5f05809725ffd0bcd7e61b5
1 #!/bin/sh
3 test_description='Test diff-highlight'
5 CURR_DIR=$(pwd)
6 TEST_OUTPUT_DIRECTORY=$(pwd)
7 TEST_DIRECTORY="$CURR_DIR"/../../../t
8 DIFF_HIGHLIGHT="$CURR_DIR"/../diff-highlight
10 CW="$(printf "\033[7m")" # white
11 CR="$(printf "\033[27m")" # reset
13 . "$TEST_DIRECTORY"/test-lib.sh
15 if ! test_have_prereq PERL
16 then
17 skip_all='skipping diff-highlight tests; perl not available'
18 test_done
21 # dh_test is a test helper function which takes 3 file names as parameters. The
22 # first 2 files are used to generate diff and commit output, which is then
23 # piped through diff-highlight. The 3rd file should contain the expected output
24 # of diff-highlight (minus the diff/commit header, ie. everything after and
25 # including the first @@ line).
26 dh_test () {
27 a="$1" b="$2" &&
29 cat >patch.exp &&
32 cat "$a" >file &&
33 git add file &&
34 git commit -m "Add a file" &&
36 cat "$b" >file &&
37 git diff file >diff.raw &&
38 git commit -a -m "Update a file" &&
39 git show >commit.raw
40 } >/dev/null &&
42 "$DIFF_HIGHLIGHT" <diff.raw | test_strip_patch_header >diff.act &&
43 "$DIFF_HIGHLIGHT" <commit.raw | test_strip_patch_header >commit.act &&
44 test_cmp patch.exp diff.act &&
45 test_cmp patch.exp commit.act
48 test_strip_patch_header () {
49 sed -n '/^@@/,$p' $*
52 # dh_test_setup_history generates a contrived graph such that we have at least
53 # 1 nesting (E) and 2 nestings (F).
55 # A branch
56 # /
57 # D---E---F master
59 # git log --all --graph
60 # * commit
61 # | A
62 # | * commit
63 # | | F
64 # | * commit
65 # |/
66 # | E
67 # * commit
68 # D
70 dh_test_setup_history () {
71 echo "file1" >file1 &&
72 echo "file2" >file2 &&
73 echo "file3" >file3 &&
75 cat file1 >file &&
76 git add file &&
77 git commit -m "D" &&
79 git checkout -b branch &&
80 cat file2 >file &&
81 git commit -a -m "A" &&
83 git checkout master &&
84 cat file2 >file &&
85 git commit -a -m "E" &&
87 cat file3 >file &&
88 git commit -a -m "F"
91 left_trim () {
92 "$PERL_PATH" -pe 's/^\s+//'
95 trim_graph () {
96 # graphs start with * or |
97 # followed by a space or / or \
98 "$PERL_PATH" -pe 's@^((\*|\|)( |/|\\))+@@'
101 test_expect_success 'diff-highlight highlights the beginning of a line' '
102 cat >a <<-\EOF &&
108 cat >b <<-\EOF &&
114 dh_test a b <<-EOF
115 @@ -1,3 +1,3 @@
117 -${CW}b${CR}bb
118 +${CW}0${CR}bb
123 test_expect_success 'diff-highlight highlights the end of a line' '
124 cat >a <<-\EOF &&
130 cat >b <<-\EOF &&
136 dh_test a b <<-EOF
137 @@ -1,3 +1,3 @@
139 -bb${CW}b${CR}
140 +bb${CW}0${CR}
145 test_expect_success 'diff-highlight highlights the middle of a line' '
146 cat >a <<-\EOF &&
152 cat >b <<-\EOF &&
158 dh_test a b <<-EOF
159 @@ -1,3 +1,3 @@
161 -b${CW}b${CR}b
162 +b${CW}0${CR}b
167 test_expect_success 'diff-highlight does not highlight whole line' '
168 cat >a <<-\EOF &&
174 cat >b <<-\EOF &&
180 dh_test a b <<-EOF
181 @@ -1,3 +1,3 @@
183 -bbb
184 +000
189 test_expect_failure 'diff-highlight highlights mismatched hunk size' '
190 cat >a <<-\EOF &&
195 cat >b <<-\EOF &&
201 dh_test a b <<-EOF
202 @@ -1,3 +1,3 @@
204 -b${CW}b${CR}b
205 +b${CW}0${CR}b
206 +ccc
210 # These two code points share the same leading byte in UTF-8 representation;
211 # a naive byte-wise diff would highlight only the second byte.
213 # - U+00f3 ("o" with acute)
214 o_accent=$(printf '\303\263')
215 # - U+00f8 ("o" with stroke)
216 o_stroke=$(printf '\303\270')
218 test_expect_success 'diff-highlight treats multibyte utf-8 as a unit' '
219 echo "unic${o_accent}de" >a &&
220 echo "unic${o_stroke}de" >b &&
221 dh_test a b <<-EOF
222 @@ -1 +1 @@
223 -unic${CW}${o_accent}${CR}de
224 +unic${CW}${o_stroke}${CR}de
228 # Unlike the UTF-8 above, these are combining code points which are meant
229 # to modify the character preceding them:
231 # - U+0301 (combining acute accent)
232 combine_accent=$(printf '\314\201')
233 # - U+0302 (combining circumflex)
234 combine_circum=$(printf '\314\202')
236 test_expect_failure 'diff-highlight treats combining code points as a unit' '
237 echo "unico${combine_accent}de" >a &&
238 echo "unico${combine_circum}de" >b &&
239 dh_test a b <<-EOF
240 @@ -1 +1 @@
241 -unic${CW}o${combine_accent}${CR}de
242 +unic${CW}o${combine_circum}${CR}de
246 test_expect_success 'diff-highlight works with the --graph option' '
247 dh_test_setup_history &&
249 # topo-order so that the order of the commits is the same as with --graph
250 # trim graph elements so we can do a diff
251 # trim leading space because our trim_graph is not perfect
252 git log --branches -p --topo-order |
253 "$DIFF_HIGHLIGHT" | left_trim >graph.exp &&
254 git log --branches -p --graph |
255 "$DIFF_HIGHLIGHT" | trim_graph | left_trim >graph.act &&
256 test_cmp graph.exp graph.act
259 # Most combined diffs won't meet diff-highlight's line-number filter. So we
260 # create one here where one side drops a line and the other modifies it. That
261 # should result in a diff like:
263 # - modified content
264 # ++resolved content
266 # which naively looks like one side added "+resolved".
267 test_expect_success 'diff-highlight ignores combined diffs' '
268 echo "content" >file &&
269 git add file &&
270 git commit -m base &&
272 >file &&
273 git commit -am master &&
275 git checkout -b other HEAD^ &&
276 echo "modified content" >file &&
277 git commit -am other &&
279 test_must_fail git merge master &&
280 echo "resolved content" >file &&
281 git commit -am resolved &&
283 cat >expect <<-\EOF &&
284 --- a/file
285 +++ b/file
286 @@@ -1,1 -1,0 +1,1 @@@
287 - modified content
288 ++resolved content
291 git show -c | "$DIFF_HIGHLIGHT" >actual.raw &&
292 sed -n "/^---/,\$p" <actual.raw >actual &&
293 test_cmp expect actual
296 test_done