1#!/bin/sh
2#
3# Copyright (c) 2006 Junio C Hamano
4#
56
test_description='Binary diff and apply
7'
89
. ./test-lib.sh
1011
cat >expect.binary-numstat <<\EOF
121 1 a
13- - b
141 1 c
15- - d
16EOF
1718
test_expect_success 'prepare repository' \
19'echo AIT >a && echo BIT >b && echo CIT >c && echo DIT >d &&
20git update-index --add a b c d &&
21echo git >a &&
22cat "$TEST_DIRECTORY"/test-binary-1.png >b &&
23echo git >c &&
24cat b b >d'
2526
cat > expected <<\EOF
27a | 2 +-
28b | Bin
29c | 2 +-
30d | Bin
314 files changed, 2 insertions(+), 2 deletions(-)
32EOF
33test_expect_success '"apply --stat" output for binary file change' '
34git diff >diff &&
35git apply --stat --summary <diff >current &&
36test_i18ncmp expected current
37'
3839
test_expect_success 'diff --shortstat output for binary file change' '
40echo " 4 files changed, 2 insertions(+), 2 deletions(-)" >expected &&
41git diff --shortstat >current &&
42test_i18ncmp expected current
43'
4445
test_expect_success 'diff --shortstat output for binary file change only' '
46echo " 1 file changed, 0 insertions(+), 0 deletions(-)" >expected &&
47git diff --shortstat -- b >current &&
48test_i18ncmp expected current
49'
5051
test_expect_success 'apply --numstat notices binary file change' '
52git diff >diff &&
53git apply --numstat <diff >current &&
54test_cmp expect.binary-numstat current
55'
5657
test_expect_success 'apply --numstat understands diff --binary format' '
58git diff --binary >diff &&
59git apply --numstat <diff >current &&
60test_cmp expect.binary-numstat current
61'
6263
# apply needs to be able to skip the binary material correctly
64# in order to report the line number of a corrupt patch.
65test_expect_success 'apply detecting corrupt patch correctly' \
66'git diff | sed -e 's/-CIT/xCIT/' >broken &&
67if git apply --stat --summary broken 2>detected
68then
69echo unhappy - should have detected an error
70(exit 1)
71else
72echo happy
73fi &&
74detected=`cat detected` &&
75detected=`expr "$detected" : "fatal.*at line \\([0-9]*\\)\$"` &&
76detected=`sed -ne "${detected}p" broken` &&
77test "$detected" = xCIT'
7879
test_expect_success 'apply detecting corrupt patch correctly' \
80'git diff --binary | sed -e 's/-CIT/xCIT/' >broken &&
81if git apply --stat --summary broken 2>detected
82then
83echo unhappy - should have detected an error
84(exit 1)
85else
86echo happy
87fi &&
88detected=`cat detected` &&
89detected=`expr "$detected" : "fatal.*at line \\([0-9]*\\)\$"` &&
90detected=`sed -ne "${detected}p" broken` &&
91test "$detected" = xCIT'
9293
test_expect_success 'initial commit' 'git commit -a -m initial'
9495
# Try removal (b), modification (d), and creation (e).
96test_expect_success 'diff-index with --binary' \
97'echo AIT >a && mv b e && echo CIT >c && cat e >d &&
98git update-index --add --remove a b c d e &&
99tree0=`git write-tree` &&
100git diff --cached --binary >current &&
101git apply --stat --summary current'
102103
test_expect_success 'apply binary patch' \
104'git reset --hard &&
105git apply --binary --index <current &&
106tree1=`git write-tree` &&
107test "$tree1" = "$tree0"'
108109
test_expect_success 'diff --no-index with binary creation' '
110echo Q | q_to_nul >binary &&
111(: hide error code from diff, which just indicates differences
112git diff --binary --no-index /dev/null binary >current ||
113true
114) &&
115rm binary &&
116git apply --binary <current &&
117echo Q >expected &&
118nul_to_q <binary >actual &&
119test_cmp expected actual
120'
121122
cat >expect <<EOF
123binfile | Bin 0 -> 1026 bytes
124textfile | 10000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125EOF
126127
test_expect_success 'diff --stat with binary files and big change count' '
128echo X | dd of=binfile bs=1k seek=1 &&
129git add binfile &&
130i=0 &&
131while test $i -lt 10000; do
132echo $i &&
133i=$(($i + 1))
134done >textfile &&
135git add textfile &&
136git diff --cached --stat binfile textfile >output &&
137grep " | " output >actual &&
138test_cmp expect actual
139'
140141
test_done