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