Merge branch 'ar/submodule-add-config'
[git/debian.git] / t / t4012-diff-binary.sh
blob33ff588ebca03807da91c4a786df76c03fb9bff7
1 #!/bin/sh
3 # Copyright (c) 2006 Junio C Hamano
6 test_description='Binary diff and apply
9 . ./test-lib.sh
11 cat >expect.binary-numstat <<\EOF
12 1 1 a
13 - - b
14 1 1 c
15 - - d
16 EOF
18 test_expect_success 'prepare repository' '
19 echo AIT >a && echo BIT >b && echo CIT >c && echo DIT >d &&
20 git update-index --add a b c d &&
21 echo git >a &&
22 cat "$TEST_DIRECTORY"/test-binary-1.png >b &&
23 echo git >c &&
24 cat b b >d
27 cat > expected <<\EOF
28 a | 2 +-
29 b | Bin
30 c | 2 +-
31 d | Bin
32 4 files changed, 2 insertions(+), 2 deletions(-)
33 EOF
34 test_expect_success 'apply --stat output for binary file change' '
35 git diff >diff &&
36 git apply --stat --summary <diff >current &&
37 test_cmp expected current
40 test_expect_success 'diff --shortstat output for binary file change' '
41 tail -n 1 expected >expect &&
42 git diff --shortstat >current &&
43 test_cmp expect current
46 test_expect_success 'diff --shortstat output for binary file change only' '
47 echo " 1 file changed, 0 insertions(+), 0 deletions(-)" >expected &&
48 git diff --shortstat -- b >current &&
49 test_cmp expected current
52 test_expect_success 'apply --numstat notices binary file change' '
53 git diff >diff &&
54 git apply --numstat <diff >current &&
55 test_cmp expect.binary-numstat current
58 test_expect_success 'apply --numstat understands diff --binary format' '
59 git diff --binary >diff &&
60 git apply --numstat <diff >current &&
61 test_cmp expect.binary-numstat current
64 # apply needs to be able to skip the binary material correctly
65 # in order to report the line number of a corrupt patch.
66 test_expect_success 'apply detecting corrupt patch correctly' '
67 git diff >output &&
68 sed -e "s/-CIT/xCIT/" <output >broken &&
69 test_must_fail git apply --stat --summary broken 2>detected &&
70 detected=$(cat detected) &&
71 detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
72 detected=$(sed -ne "${detected}p" broken) &&
73 test "$detected" = xCIT
76 test_expect_success 'apply detecting corrupt patch correctly' '
77 git diff --binary | sed -e "s/-CIT/xCIT/" >broken &&
78 test_must_fail git apply --stat --summary broken 2>detected &&
79 detected=$(cat detected) &&
80 detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
81 detected=$(sed -ne "${detected}p" broken) &&
82 test "$detected" = xCIT
85 test_expect_success 'initial commit' 'git commit -a -m initial'
87 # Try removal (b), modification (d), and creation (e).
88 test_expect_success 'diff-index with --binary' '
89 echo AIT >a && mv b e && echo CIT >c && cat e >d &&
90 git update-index --add --remove a b c d e &&
91 tree0=$(git write-tree) &&
92 git diff --cached --binary >current &&
93 git apply --stat --summary current
96 test_expect_success 'apply binary patch' '
97 git reset --hard &&
98 git apply --binary --index <current &&
99 tree1=$(git write-tree) &&
100 test "$tree1" = "$tree0"
103 test_expect_success 'diff --no-index with binary creation' '
104 echo Q | q_to_nul >binary &&
105 # hide error code from diff, which just indicates differences
106 test_might_fail git diff --binary --no-index /dev/null binary >current &&
107 rm binary &&
108 git apply --binary <current &&
109 echo Q >expected &&
110 nul_to_q <binary >actual &&
111 test_cmp expected actual
114 cat >expect <<EOF
115 binfile | Bin 0 -> 1026 bytes
116 textfile | 10000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
119 test_expect_success 'diff --stat with binary files and big change count' '
120 printf "\01\00%1024d" 1 >binfile &&
121 git add binfile &&
122 i=0 &&
123 while test $i -lt 10000; do
124 echo $i &&
125 i=$(($i + 1))
126 done >textfile &&
127 git add textfile &&
128 git diff --cached --stat binfile textfile >output &&
129 grep " | " output >actual &&
130 test_cmp expect actual
133 test_done